From ffb218d666294b4677615d5ed96876dc04adff8e Mon Sep 17 00:00:00 2001 From: Antony Male Date: Sat, 12 Nov 2016 11:35:58 +0000 Subject: [PATCH] Add default FlowDirection and TextAlignment to MessageBoxViweModel This removes the MessageBoxOptions input to MessageBoxViewModel, and replaces it with explicit FlowDirection and TextAlignment inputs. I think this is fair enough - we weren't using most of the members of MessageBoxOptions, so allowing them to be provided was misleading. Fixes #12 --- Stylet/MessageBoxViewModel.cs | 57 +++++++++++++++++--------- Stylet/WindowManager.cs | 44 +++++++++++--------- StyletUnitTests/MessageBoxTests.cs | 58 ++++++++++----------------- StyletUnitTests/WindowManagerTests.cs | 4 +- 4 files changed, 85 insertions(+), 78 deletions(-) diff --git a/Stylet/MessageBoxViewModel.cs b/Stylet/MessageBoxViewModel.cs index 7674ae4..de2c942 100644 --- a/Stylet/MessageBoxViewModel.cs +++ b/Stylet/MessageBoxViewModel.cs @@ -15,14 +15,15 @@ namespace Stylet /// /// Setup the MessageBoxViewModel with the information it needs /// - /// A System.String that specifies the text to display. - /// A System.String that specifies the title bar caption to display. - /// A System.Windows.MessageBoxButton value that specifies which button or buttons to display. - /// A System.Windows.MessageBoxImage value that specifies the icon to display. - /// A System.Windows.MessageBoxResult value that specifies the default result of the message box. - /// A System.Windows.MessageBoxResult value that specifies the cancel result of the message box - /// A System.Windows.MessageBoxOptions value object that specifies the options. + /// A that specifies the text to display. + /// A that specifies the title bar caption to display. + /// A value that specifies which button or buttons to display. + /// A value that specifies the icon to display. + /// A value that specifies the default result of the message box. + /// A value that specifies the cancel result of the message box /// A dictionary specifying the button labels, if desirable + /// The to use, overrides the + /// The to use, overrides the void Setup( string messageBoxText, string caption = null, @@ -30,8 +31,9 @@ namespace Stylet MessageBoxImage icon = MessageBoxImage.None, MessageBoxResult defaultResult = MessageBoxResult.None, MessageBoxResult cancelResult = MessageBoxResult.None, - MessageBoxOptions options = MessageBoxOptions.None, - IDictionary buttonLabels = null); + IDictionary buttonLabels = null, + FlowDirection? flowDirection = null, + TextAlignment? textAlignment = null); /// /// Gets the button clicked by the user, after they've clicked it @@ -64,6 +66,16 @@ namespace Stylet /// public static IDictionary SoundMapping { get; set; } + /// + /// Gets or sets the default to use + /// + public static FlowDirection DefaultFlowDirection { get; set; } + + /// + /// Gets or sets the default to use + /// + public static TextAlignment DefaultTextAlignment { get; set; } + static MessageBoxViewModel() { ButtonLabels = new Dictionary() @@ -100,19 +112,23 @@ namespace Stylet { MessageBoxImage.Exclamation, SystemSounds.Exclamation }, { MessageBoxImage.Information, SystemSounds.Asterisk }, }; + + DefaultFlowDirection = FlowDirection.LeftToRight; + DefaultTextAlignment = TextAlignment.Left; } /// /// Setup the MessageBoxViewModel with the information it needs /// - /// A System.String that specifies the text to display. - /// A System.String that specifies the title bar caption to display. - /// A System.Windows.MessageBoxButton value that specifies which button or buttons to display. - /// A System.Windows.MessageBoxImage value that specifies the icon to display. - /// A System.Windows.MessageBoxResult value that specifies the default result of the message box. - /// A System.Windows.MessageBoxResult value that specifies the cancel result of the message box - /// A System.Windows.MessageBoxOptions value object that specifies the options. + /// A that specifies the text to display. + /// A that specifies the title bar caption to display. + /// A value that specifies which button or buttons to display. + /// A value that specifies the icon to display. + /// A value that specifies the default result of the message box. + /// A value that specifies the cancel result of the message box /// A dictionary specifying the button labels, if desirable + /// The to use, overrides the + /// The to use, overrides the public void Setup( string messageBoxText, string caption = null, @@ -120,8 +136,9 @@ namespace Stylet MessageBoxImage icon = MessageBoxImage.None, MessageBoxResult defaultResult = MessageBoxResult.None, MessageBoxResult cancelResult = MessageBoxResult.None, - MessageBoxOptions options = MessageBoxOptions.None, - IDictionary buttonLabels = null) + IDictionary buttonLabels = null, + FlowDirection? flowDirection = null, + TextAlignment? textAlignment = null) { this.Text = messageBoxText; this.DisplayName = caption; @@ -158,8 +175,8 @@ namespace Stylet throw new ArgumentException("CancelButton set to a button which doesn't appear in Buttons"); } - this.FlowDirection = options.HasFlag(MessageBoxOptions.RtlReading) ? FlowDirection.RightToLeft : FlowDirection.LeftToRight; - this.TextAlignment = (options.HasFlag(MessageBoxOptions.RightAlign) == options.HasFlag(MessageBoxOptions.RtlReading)) ? TextAlignment.Left : TextAlignment.Right; + this.FlowDirection = flowDirection ?? DefaultFlowDirection; + this.TextAlignment = textAlignment ?? DefaultTextAlignment; } /// diff --git a/Stylet/WindowManager.cs b/Stylet/WindowManager.cs index 4a2f401..1438678 100644 --- a/Stylet/WindowManager.cs +++ b/Stylet/WindowManager.cs @@ -28,22 +28,24 @@ namespace Stylet /// /// Display a MessageBox /// - /// A System.String that specifies the text to display. - /// A System.String that specifies the title bar caption to display. - /// A System.Windows.MessageBoxButton value that specifies which button or buttons to display. - /// A System.Windows.MessageBoxImage value that specifies the icon to display. - /// A System.Windows.MessageBoxResult value that specifies the default result of the message box. - /// A System.Windows.MessageBoxResult value that specifies the cancel result of the message box - /// A System.Windows.MessageBoxOptions value object that specifies the options. + /// A that specifies the text to display. + /// A that specifies the title bar caption to display. + /// A value that specifies which button or buttons to display. + /// A value that specifies the icon to display. + /// A value that specifies the default result of the message box. + /// A value that specifies the cancel result of the message box /// A dictionary specifying the button labels, if desirable + /// The to use, overrides the + /// The to use, overrides the /// The result chosen by the user - MessageBoxResult ShowMessageBox(string messageBoxText, string caption = null, + MessageBoxResult ShowMessageBox(string messageBoxText, string caption = "", MessageBoxButton buttons = MessageBoxButton.OK, MessageBoxImage icon = MessageBoxImage.None, MessageBoxResult defaultResult = MessageBoxResult.None, MessageBoxResult cancelResult = MessageBoxResult.None, - MessageBoxOptions options = MessageBoxOptions.None, - IDictionary buttonLabels = null); + IDictionary buttonLabels = null, + FlowDirection? flowDirection = null, + TextAlignment? textAlignment = null); } /// @@ -103,25 +105,27 @@ namespace Stylet /// /// Display a MessageBox /// - /// A System.String that specifies the text to display. - /// A System.String that specifies the title bar caption to display. - /// A System.Windows.MessageBoxButton value that specifies which button or buttons to display. - /// A System.Windows.MessageBoxImage value that specifies the icon to display. - /// A System.Windows.MessageBoxResult value that specifies the default result of the message box. - /// A System.Windows.MessageBoxResult value that specifies the cancel result of the message box - /// A System.Windows.MessageBoxOptions value object that specifies the options. + /// A that specifies the text to display. + /// A that specifies the title bar caption to display. + /// A value that specifies which button or buttons to display. + /// A value that specifies the icon to display. + /// A value that specifies the default result of the message box. + /// A value that specifies the cancel result of the message box /// A dictionary specifying the button labels, if desirable + /// The to use, overrides the + /// The to use, overrides the /// The result chosen by the user public MessageBoxResult ShowMessageBox(string messageBoxText, string caption = "", MessageBoxButton buttons = MessageBoxButton.OK, MessageBoxImage icon = MessageBoxImage.None, MessageBoxResult defaultResult = MessageBoxResult.None, MessageBoxResult cancelResult = MessageBoxResult.None, - MessageBoxOptions options = MessageBoxOptions.None, - IDictionary buttonLabels = null) + IDictionary buttonLabels = null, + FlowDirection? flowDirection = null, + TextAlignment? textAlignment = null) { var vm = this.messageBoxViewModelFactory(); - vm.Setup(messageBoxText, caption, buttons, icon, defaultResult, cancelResult, options, buttonLabels); + vm.Setup(messageBoxText, caption, buttons, icon, defaultResult, cancelResult, buttonLabels, flowDirection, textAlignment); // Don't go through the IoC container to get the View. This means we can simplify it... var messageBoxView = new MessageBoxView(); messageBoxView.InitializeComponent(); diff --git a/StyletUnitTests/MessageBoxTests.cs b/StyletUnitTests/MessageBoxTests.cs index 1519c27..75f2579 100644 --- a/StyletUnitTests/MessageBoxTests.cs +++ b/StyletUnitTests/MessageBoxTests.cs @@ -31,31 +31,31 @@ namespace StyletUnitTests [Test] public void SetsTextCorrectly() { - this.vm.Setup("this is the text", null, MessageBoxButton.OK, System.Windows.MessageBoxImage.None, MessageBoxResult.None, MessageBoxResult.None, MessageBoxOptions.None, null); + this.vm.Setup("this is the text", null, MessageBoxButton.OK, System.Windows.MessageBoxImage.None, MessageBoxResult.None, MessageBoxResult.None, null, null, null); Assert.AreEqual("this is the text", this.vm.Text); } [Test] public void DeterminesTextIsMultilineCorrectly() { - this.vm.Setup("this is the text", null, MessageBoxButton.OK, System.Windows.MessageBoxImage.None, MessageBoxResult.None, MessageBoxResult.None, MessageBoxOptions.None, null); + this.vm.Setup("this is the text", null, MessageBoxButton.OK, System.Windows.MessageBoxImage.None, MessageBoxResult.None, MessageBoxResult.None, null, null, null); Assert.IsFalse(this.vm.TextIsMultiline); - this.vm.Setup("hello\nworld", null, MessageBoxButton.OK, System.Windows.MessageBoxImage.None, MessageBoxResult.None, MessageBoxResult.None, MessageBoxOptions.None, null); + this.vm.Setup("hello\nworld", null, MessageBoxButton.OK, System.Windows.MessageBoxImage.None, MessageBoxResult.None, MessageBoxResult.None, null, null, null); Assert.IsTrue(this.vm.TextIsMultiline); } [Test] public void SetsTitleCorrectly() { - this.vm.Setup(null, "this is the title", MessageBoxButton.OK, System.Windows.MessageBoxImage.None, MessageBoxResult.None, MessageBoxResult.None, MessageBoxOptions.None, null); + this.vm.Setup(null, "this is the title", MessageBoxButton.OK, System.Windows.MessageBoxImage.None, MessageBoxResult.None, MessageBoxResult.None, null, null, null); Assert.AreEqual("this is the title", this.vm.DisplayName); } [Test] public void DisplaysRequestedButtons() { - this.vm.Setup(null, null, MessageBoxButton.OKCancel, System.Windows.MessageBoxImage.None, MessageBoxResult.None, MessageBoxResult.None, MessageBoxOptions.None, null); + this.vm.Setup(null, null, MessageBoxButton.OKCancel, System.Windows.MessageBoxImage.None, MessageBoxResult.None, MessageBoxResult.None, null, null, null); var buttons = vm.ButtonList.ToList(); Assert.AreEqual(2, buttons.Count); Assert.AreEqual("OK", buttons[0].Label); @@ -67,47 +67,47 @@ namespace StyletUnitTests [Test] public void SetsDefaultButtonToTheRequestedButton() { - this.vm.Setup(null, null, MessageBoxButton.OKCancel, System.Windows.MessageBoxImage.None, MessageBoxResult.Cancel, MessageBoxResult.None, MessageBoxOptions.None, null); + this.vm.Setup(null, null, MessageBoxButton.OKCancel, System.Windows.MessageBoxImage.None, MessageBoxResult.Cancel, MessageBoxResult.None, null, null, null); Assert.AreEqual(this.vm.ButtonList.ElementAt(1), this.vm.DefaultButton); } [Test] public void SetsDefaultToLeftmostButtonIfDefaultRequested() { - this.vm.Setup(null, null, MessageBoxButton.YesNoCancel, System.Windows.MessageBoxImage.None, MessageBoxResult.None, MessageBoxResult.None, MessageBoxOptions.None, null); + this.vm.Setup(null, null, MessageBoxButton.YesNoCancel, System.Windows.MessageBoxImage.None, MessageBoxResult.None, MessageBoxResult.None, null, null, null); Assert.AreEqual(this.vm.ButtonList.ElementAt(0), this.vm.DefaultButton); } [Test] public void ThrowsIfTheRequestedDefaultButtonIsNotDisplayed() { - Assert.Throws(() => this.vm.Setup(null, null, MessageBoxButton.OKCancel, MessageBoxImage.None, MessageBoxResult.Yes, MessageBoxResult.None, MessageBoxOptions.None, null)); + Assert.Throws(() => this.vm.Setup(null, null, MessageBoxButton.OKCancel, MessageBoxImage.None, MessageBoxResult.Yes, MessageBoxResult.None, null, null, null)); } [Test] public void SetsCancelButtonToTheRequestedButton() { - this.vm.Setup(null, null, MessageBoxButton.YesNoCancel, MessageBoxImage.None, MessageBoxResult.None, MessageBoxResult.No, MessageBoxOptions.None, null); + this.vm.Setup(null, null, MessageBoxButton.YesNoCancel, MessageBoxImage.None, MessageBoxResult.None, MessageBoxResult.No, null, null, null); Assert.AreEqual(this.vm.ButtonList.ElementAt(1), this.vm.CancelButton); } [Test] public void SetsCancelToRighmostButtonIfDefaultRequested() { - this.vm.Setup(null, null, MessageBoxButton.OKCancel, MessageBoxImage.None, MessageBoxResult.None, MessageBoxResult.None, MessageBoxOptions.None, null); + this.vm.Setup(null, null, MessageBoxButton.OKCancel, MessageBoxImage.None, MessageBoxResult.None, MessageBoxResult.None, null, null, null); Assert.AreEqual(this.vm.ButtonList.ElementAt(1), this.vm.CancelButton); } [Test] public void ThrowsIfRequestedCancelButtonIsNotDisplayed() { - Assert.Throws(() => this.vm.Setup(null, null, MessageBoxButton.OK, MessageBoxImage.None, MessageBoxResult.None, MessageBoxResult.No, MessageBoxOptions.None, null)); + Assert.Throws(() => this.vm.Setup(null, null, MessageBoxButton.OK, MessageBoxImage.None, MessageBoxResult.None, MessageBoxResult.No, null, null, null)); } [Test] public void SetsIconCorrectly() { - this.vm.Setup(null, null, MessageBoxButton.OK, MessageBoxImage.Exclamation, MessageBoxResult.None, MessageBoxResult.None, MessageBoxOptions.None, null); + this.vm.Setup(null, null, MessageBoxButton.OK, MessageBoxImage.Exclamation, MessageBoxResult.None, MessageBoxResult.None, null, null, null); Assert.AreEqual(SystemIcons.Exclamation, this.vm.ImageIcon); } @@ -116,7 +116,7 @@ namespace StyletUnitTests { var parent = new Mock(); this.vm.Parent = parent.Object; - this.vm.Setup(null, null, MessageBoxButton.OK, MessageBoxImage.None, MessageBoxResult.None, MessageBoxResult.None, MessageBoxOptions.None, null); + this.vm.Setup(null, null, MessageBoxButton.OK, MessageBoxImage.None, MessageBoxResult.None, MessageBoxResult.None, null, null, null); this.vm.ButtonClicked(MessageBoxResult.No); @@ -129,17 +129,17 @@ namespace StyletUnitTests { var vm = new MyMessageBoxViewModel(); // Can't test it actually playing the sound - vm.Setup(null, null, MessageBoxButton.OK, MessageBoxImage.None, MessageBoxResult.None, MessageBoxResult.None, MessageBoxOptions.None, null); + vm.Setup(null, null, MessageBoxButton.OK, MessageBoxImage.None, MessageBoxResult.None, MessageBoxResult.None, null, null, null); Assert.DoesNotThrow(() => vm.OnViewLoaded()); } [Test] public void ButtonTextOverridesWork() { - this.vm.Setup(null, null, MessageBoxButton.OKCancel, MessageBoxImage.None, MessageBoxResult.None, MessageBoxResult.None, MessageBoxOptions.None, new Dictionary() + this.vm.Setup(null, null, MessageBoxButton.OKCancel, MessageBoxImage.None, MessageBoxResult.None, MessageBoxResult.None, new Dictionary() { { MessageBoxResult.Cancel, "YAY!" }, - }); + }, null, null); Assert.AreEqual("OK", this.vm.ButtonList.ElementAt(0).Label); Assert.AreEqual("YAY!", this.vm.ButtonList.ElementAt(1).Label); } @@ -147,43 +147,29 @@ namespace StyletUnitTests [Test] public void FlowsLeftToRightByDefault() { - this.vm.Setup(null, null, MessageBoxButton.OK, MessageBoxImage.None, MessageBoxResult.None, MessageBoxResult.None, MessageBoxOptions.None, null); + this.vm.Setup(null, null, MessageBoxButton.OK, MessageBoxImage.None, MessageBoxResult.None, MessageBoxResult.None, null, null, null); Assert.AreEqual(FlowDirection.LeftToRight, this.vm.FlowDirection); } [Test] public void FlowsRightToLeftIfRequested() { - this.vm.Setup(null, null, MessageBoxButton.OK, MessageBoxImage.None, MessageBoxResult.None, MessageBoxResult.None, MessageBoxOptions.RtlReading, null); + this.vm.Setup(null, null, MessageBoxButton.OK, MessageBoxImage.None, MessageBoxResult.None, MessageBoxResult.None, null, FlowDirection.RightToLeft, null); Assert.AreEqual(FlowDirection.RightToLeft, this.vm.FlowDirection); } [Test] - public void AlignsLeftIfLeftToRightByDefault() + public void AlignsLeftByDefault() { - this.vm.Setup(null, null, MessageBoxButton.OK, MessageBoxImage.None, MessageBoxResult.None, MessageBoxResult.None, MessageBoxOptions.None, null); + this.vm.Setup(null, null, MessageBoxButton.OK, MessageBoxImage.None, MessageBoxResult.None, MessageBoxResult.None, null, null, null); Assert.AreEqual(TextAlignment.Left, this.vm.TextAlignment); } [Test] - public void AlignsRightIfLeftToRightAndRequested() + public void AlignsRightIfRequested() { - this.vm.Setup(null, null, MessageBoxButton.OK, MessageBoxImage.None, MessageBoxResult.None, MessageBoxResult.None, MessageBoxOptions.RightAlign, null); + this.vm.Setup(null, null, MessageBoxButton.OK, MessageBoxImage.None, MessageBoxResult.None, MessageBoxResult.None, null, null, TextAlignment.Right); Assert.AreEqual(TextAlignment.Right, this.vm.TextAlignment); } - - [Test] - public void AlignsRightIfRightToLeftByDefault() - { - this.vm.Setup(null, null, MessageBoxButton.OK, MessageBoxImage.None, MessageBoxResult.None, MessageBoxResult.None, MessageBoxOptions.RtlReading, null); - Assert.AreEqual(TextAlignment.Right, this.vm.TextAlignment); - } - - [Test] - public void AlignsLeftIfRightToLeftAndRequested() - { - this.vm.Setup(null, null, MessageBoxButton.OK, MessageBoxImage.None, MessageBoxResult.None, MessageBoxResult.None, MessageBoxOptions.RightAlign | MessageBoxOptions.RtlReading, null); - Assert.AreEqual(TextAlignment.Left, this.vm.TextAlignment); - } } } diff --git a/StyletUnitTests/WindowManagerTests.cs b/StyletUnitTests/WindowManagerTests.cs index 988a8dd..df07c36 100644 --- a/StyletUnitTests/WindowManagerTests.cs +++ b/StyletUnitTests/WindowManagerTests.cs @@ -325,10 +325,10 @@ namespace StyletUnitTests { var wm = new WindowManagerWithoutCreateWindow(this.viewManager.Object, () => this.messageBoxViewModel.Object, this.config.Object); - try { wm.ShowMessageBox("text", "title", MessageBoxButton.OKCancel, MessageBoxImage.Error, MessageBoxResult.OK, MessageBoxResult.Cancel, MessageBoxOptions.RtlReading); } + try { wm.ShowMessageBox("text", "title", MessageBoxButton.OKCancel, MessageBoxImage.Error, MessageBoxResult.OK, MessageBoxResult.Cancel, null, FlowDirection.RightToLeft, TextAlignment.Right); } catch (TestException) { } - this.messageBoxViewModel.Verify(x => x.Setup("text", "title", MessageBoxButton.OKCancel, MessageBoxImage.Error, MessageBoxResult.OK, MessageBoxResult.Cancel, MessageBoxOptions.RtlReading, null)); + this.messageBoxViewModel.Verify(x => x.Setup("text", "title", MessageBoxButton.OKCancel, MessageBoxImage.Error, MessageBoxResult.OK, MessageBoxResult.Cancel, null, FlowDirection.RightToLeft, TextAlignment.Right)); } [Test]