diff --git a/Stylet/MessageBoxViewModel.cs b/Stylet/MessageBoxViewModel.cs index 411dc65..dfdd88e 100644 --- a/Stylet/MessageBoxViewModel.cs +++ b/Stylet/MessageBoxViewModel.cs @@ -7,55 +7,6 @@ using System.Windows; namespace Stylet { - /// - /// Optional configuration for a MessageBox - /// - public class MessageBoxConfig - { - /// - /// Text to display in the body of the MessageBox - /// - public MessageBoxButton Buttons { get; set; } - - /// - /// Icon to display to the left of the text. This also determines the sound played when the MessageBox is shown - /// - public MessageBoxImage Icon { get; set; } - - /// - /// Button pressed when the user presses Enter. Defaults to the leftmost button - /// - public MessageBoxResult DefaultButton { get; set; } - - /// - /// Button pressed when the user preses Esc or clicks the red X on the titlebar. Defaults to the rightmost button - /// - public MessageBoxResult CancelButton { get; set; } - - /// - /// Additional options - /// - public MessageBoxOptions Options { get; set; } - - /// - /// You may override the text for individual buttons on a case-by-case basis - /// - public IDictionary ButtonLabels { get; set; } - - /// - /// Create a new instance with default configuration - /// - public MessageBoxConfig() - { - this.Buttons = MessageBoxButton.OK; - this.Icon = MessageBoxImage.None; - this.DefaultButton = MessageBoxResult.None; - this.CancelButton = MessageBoxResult.None; - this.Options = MessageBoxOptions.None; - this.ButtonLabels = null; - } - } - /// /// Interface for a MessageBoxViewModel. MessageBoxWindowManagerExtensions.ShowMessageBox will use the configured implementation of this /// @@ -64,10 +15,21 @@ namespace Stylet /// /// Setup the MessageBoxViewModel with the information it needs /// - /// Text to display in the body of the MessageBox - /// Title to display in the titlebar of the MessageBox - /// Other configuration. Optional - void Setup(string text, string title, MessageBoxConfig config = null); + /// 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 dictionary specifying the button labels, if desirable + void Setup(string messageBoxText, string caption = null, + MessageBoxButton buttons = MessageBoxButton.OK, + MessageBoxImage icon = MessageBoxImage.None, + MessageBoxResult defaultResult = MessageBoxResult.None, + MessageBoxResult cancelResult = MessageBoxResult.None, + MessageBoxOptions options = MessageBoxOptions.None, + IDictionary buttonLabels = null); /// /// After the user has clicked a button, holds which button was clicked @@ -141,50 +103,59 @@ namespace Stylet /// /// Setup the MessageBoxViewModel with the information it needs /// - /// Text to display in the body of the MessageBox - /// Title to display in the titlebar of the MessageBox - /// Other configuration. Optional - public void Setup(string text, string title, MessageBoxConfig config = null) + /// 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 dictionary specifying the button labels, if desirable + public void Setup(string messageBoxText, string caption = null, + MessageBoxButton buttons = MessageBoxButton.OK, + MessageBoxImage icon = MessageBoxImage.None, + MessageBoxResult defaultResult = MessageBoxResult.None, + MessageBoxResult cancelResult = MessageBoxResult.None, + MessageBoxOptions options = MessageBoxOptions.None, + IDictionary buttonLabels = null) { - config = config ?? new MessageBoxConfig(); - - this.Text = text; - this.DisplayName = title; - this.Icon = config.Icon; + this.Text = messageBoxText; + this.DisplayName = caption; + this.Icon = icon; var buttonList = new List>(); this.ButtonList = buttonList; - foreach (var val in ButtonToResults[config.Buttons]) + foreach (var val in ButtonToResults[buttons]) { string label; - if (config.ButtonLabels == null || !config.ButtonLabels.TryGetValue(val, out label)) + if (buttonLabels == null || !buttonLabels.TryGetValue(val, out label)) label = ButtonLabels[val]; var lbv = new LabelledValue(label, val); buttonList.Add(lbv); - if (val == config.DefaultButton) + if (val == defaultResult) this.DefaultButton = lbv; - else if (val == config.CancelButton) + else if (val == cancelResult) this.CancelButton = lbv; } // If they didn't specify a button which we showed, then pick a default, if we can if (this.DefaultButton == null) { - if (config.DefaultButton == MessageBoxResult.None && this.ButtonList.Any()) + if (defaultResult == MessageBoxResult.None && this.ButtonList.Any()) this.DefaultButton = buttonList[0]; else throw new ArgumentException("DefaultButton set to a button which doesn't appear in Buttons"); } if (this.CancelButton == null) { - if (config.CancelButton == MessageBoxResult.None && this.ButtonList.Any()) + if (cancelResult == MessageBoxResult.None && this.ButtonList.Any()) this.CancelButton = buttonList.Last(); else throw new ArgumentException("CancelButton set to a button which doesn't appear in Buttons"); } - this.FlowDirection = config.Options.HasFlag(MessageBoxOptions.RtlReading) ? FlowDirection.RightToLeft : FlowDirection.LeftToRight; - this.TextAlignment = (config.Options.HasFlag(MessageBoxOptions.RightAlign) == config.Options.HasFlag(MessageBoxOptions.RtlReading)) ? TextAlignment.Left : TextAlignment.Right; + this.FlowDirection = options.HasFlag(MessageBoxOptions.RtlReading) ? FlowDirection.RightToLeft : FlowDirection.LeftToRight; + this.TextAlignment = (options.HasFlag(MessageBoxOptions.RightAlign) == options.HasFlag(MessageBoxOptions.RtlReading)) ? TextAlignment.Left : TextAlignment.Right; } /// diff --git a/Stylet/WindowManager.cs b/Stylet/WindowManager.cs index 29702dc..961fb12 100644 --- a/Stylet/WindowManager.cs +++ b/Stylet/WindowManager.cs @@ -27,12 +27,23 @@ namespace Stylet bool? ShowDialog(object viewModel); /// - /// Setup the MessageBoxViewModel with the information it needs + /// Display a MessageBox /// - /// Text to display in the body of the MessageBox - /// Title to display in the titlebar of the MessageBox - /// Other configuration. Optional - MessageBoxResult ShowMessageBox(string text, string title = "", MessageBoxConfig config = null); + /// 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 dictionary specifying the button labels, if desirable + MessageBoxResult ShowMessageBox(string messageBoxText, string caption = null, + MessageBoxButton buttons = MessageBoxButton.OK, + MessageBoxImage icon = MessageBoxImage.None, + MessageBoxResult defaultResult = MessageBoxResult.None, + MessageBoxResult cancelResult = MessageBoxResult.None, + MessageBoxOptions options = MessageBoxOptions.None, + IDictionary buttonLabels = null); } /// @@ -75,15 +86,26 @@ namespace Stylet } /// - /// Setup the MessageBoxViewModel with the information it needs + /// Display a MessageBox /// - /// Text to display in the body of the MessageBox - /// Title to display in the titlebar of the MessageBox - /// Other configuration. Optional - public MessageBoxResult ShowMessageBox(string text, string title = "", MessageBoxConfig config = null) + /// 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 dictionary specifying the button labels, if desirable + 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) { var vm = this.messageBoxViewModelFactory(); - vm.Setup(text, title, config); + vm.Setup(messageBoxText, caption, buttons, icon, defaultResult, cancelResult, options, buttonLabels); this.ShowDialog(vm); return vm.ClickedButton; } diff --git a/StyletIntegrationTests/ShellViewModel.cs b/StyletIntegrationTests/ShellViewModel.cs index a60790b..cb37d2b 100644 --- a/StyletIntegrationTests/ShellViewModel.cs +++ b/StyletIntegrationTests/ShellViewModel.cs @@ -60,7 +60,7 @@ namespace StyletIntegrationTests await Task.Delay(100); if (log.SequenceEqual(new[] { "One", "Two", "Four", "Three" })) - this.windowManager.ShowMessageBox("Success", config: new MessageBoxConfig() { Icon = MessageBoxImage.Information }); + this.windowManager.ShowMessageBox("Success", icon: MessageBoxImage.Information); else this.windowManager.ShowMessageBox("Failure"); } diff --git a/StyletUnitTests/MessageBoxTests.cs b/StyletUnitTests/MessageBoxTests.cs index d256759..be97269 100644 --- a/StyletUnitTests/MessageBoxTests.cs +++ b/StyletUnitTests/MessageBoxTests.cs @@ -33,21 +33,21 @@ namespace StyletUnitTests [Test] public void SetsTextCorrectly() { - this.vm.Setup("this is the text", null); + this.vm.Setup("this is the text", null, MessageBoxButton.OK, System.Windows.MessageBoxImage.None, MessageBoxResult.None, MessageBoxResult.None, MessageBoxOptions.None, null); Assert.AreEqual("this is the text", this.vm.Text); } [Test] public void SetsTitleCorrectly() { - this.vm.Setup(null, "this is the title"); + this.vm.Setup(null, "this is the title", MessageBoxButton.OK, System.Windows.MessageBoxImage.None, MessageBoxResult.None, MessageBoxResult.None, MessageBoxOptions.None, null); Assert.AreEqual("this is the title", this.vm.DisplayName); } [Test] public void DisplaysRequestedButtons() { - this.vm.Setup(null, null, new MessageBoxConfig() { Buttons = MessageBoxButton.OKCancel }); + this.vm.Setup(null, null, MessageBoxButton.OKCancel, System.Windows.MessageBoxImage.None, MessageBoxResult.None, MessageBoxResult.None, MessageBoxOptions.None, null); var buttons = vm.ButtonList.ToList(); Assert.AreEqual(2, buttons.Count); Assert.AreEqual("OK", buttons[0].Label); @@ -59,47 +59,47 @@ namespace StyletUnitTests [Test] public void SetsDefaultButtonToTheRequestedButton() { - this.vm.Setup(null, null, new MessageBoxConfig() { Buttons = MessageBoxButton.OKCancel, DefaultButton = MessageBoxResult.Cancel }); + this.vm.Setup(null, null, MessageBoxButton.OKCancel, System.Windows.MessageBoxImage.None, MessageBoxResult.Cancel, MessageBoxResult.None, MessageBoxOptions.None, null); Assert.AreEqual(this.vm.ButtonList.ElementAt(1), this.vm.DefaultButton); } [Test] public void SetsDefaultToLeftmostButtonIfDefaultRequested() { - this.vm.Setup(null, null, new MessageBoxConfig() { Buttons = MessageBoxButton.YesNoCancel }); + this.vm.Setup(null, null, MessageBoxButton.YesNoCancel, System.Windows.MessageBoxImage.None, MessageBoxResult.None, MessageBoxResult.None, MessageBoxOptions.None, null); Assert.AreEqual(this.vm.ButtonList.ElementAt(0), this.vm.DefaultButton); } [Test] public void ThrowsIfTheRequestedDefaultButtonIsNotDisplayed() { - Assert.Throws(() => this.vm.Setup(null, null, new MessageBoxConfig() { Buttons = MessageBoxButton.OKCancel, DefaultButton = MessageBoxResult.Yes })); + Assert.Throws(() => this.vm.Setup(null, null, MessageBoxButton.OKCancel, MessageBoxImage.None, MessageBoxResult.Yes, MessageBoxResult.None, MessageBoxOptions.None, null)); } [Test] public void SetsCancelButtonToTheRequestedButton() { - this.vm.Setup(null, null, new MessageBoxConfig() { Buttons = MessageBoxButton.YesNoCancel, CancelButton = MessageBoxResult.No }); + this.vm.Setup(null, null, MessageBoxButton.YesNoCancel, MessageBoxImage.None, MessageBoxResult.None, MessageBoxResult.No, MessageBoxOptions.None, null); Assert.AreEqual(this.vm.ButtonList.ElementAt(1), this.vm.CancelButton); } [Test] public void SetsCancelToRighmostButtonIfDefaultRequested() { - this.vm.Setup(null, null, new MessageBoxConfig() { Buttons = MessageBoxButton.OKCancel }); + this.vm.Setup(null, null, MessageBoxButton.OKCancel, MessageBoxImage.None, MessageBoxResult.None, MessageBoxResult.None, MessageBoxOptions.None, null); Assert.AreEqual(this.vm.ButtonList.ElementAt(1), this.vm.CancelButton); } [Test] public void ThrowsIfRequestedCancelButtonIsNotDisplayed() { - Assert.Throws(() => this.vm.Setup(null, null, new MessageBoxConfig() { CancelButton = MessageBoxResult.No })); + Assert.Throws(() => this.vm.Setup(null, null, MessageBoxButton.OK, MessageBoxImage.None, MessageBoxResult.None, MessageBoxResult.No, MessageBoxOptions.None, null)); } [Test] public void SetsIconCorrectly() { - this.vm.Setup(null, null, new MessageBoxConfig() { Icon = MessageBoxImage.Exclamation }); + this.vm.Setup(null, null, MessageBoxButton.OK, MessageBoxImage.Exclamation, MessageBoxResult.None, MessageBoxResult.None, MessageBoxOptions.None, null); Assert.AreEqual(SystemIcons.Exclamation, this.vm.ImageIcon); } @@ -108,7 +108,7 @@ namespace StyletUnitTests { var parent = new Mock(); this.vm.Parent = parent.Object; - this.vm.Setup(null, null); + this.vm.Setup(null, null, MessageBoxButton.OK, MessageBoxImage.None, MessageBoxResult.None, MessageBoxResult.None, MessageBoxOptions.None, null); this.vm.ButtonClicked(MessageBoxResult.No); @@ -121,21 +121,17 @@ namespace StyletUnitTests { var vm = new MyMessageBoxViewModel(); // Can't test it actually playing the sound - vm.Setup(null, null); + vm.Setup(null, null, MessageBoxButton.OK, MessageBoxImage.None, MessageBoxResult.None, MessageBoxResult.None, MessageBoxOptions.None, null); Assert.DoesNotThrow(() => vm.OnViewLoaded()); } [Test] public void ButtonTextOverridesWork() { - this.vm.Setup(null, null, new MessageBoxConfig() - { - Buttons = MessageBoxButton.OKCancel, - ButtonLabels = new Dictionary() + this.vm.Setup(null, null, MessageBoxButton.OKCancel, MessageBoxImage.None, MessageBoxResult.None, MessageBoxResult.None, MessageBoxOptions.None, new Dictionary() { { MessageBoxResult.Cancel, "YAY!" }, - } - }); + }); Assert.AreEqual("OK", this.vm.ButtonList.ElementAt(0).Label); Assert.AreEqual("YAY!", this.vm.ButtonList.ElementAt(1).Label); } @@ -143,42 +139,42 @@ namespace StyletUnitTests [Test] public void FlowsLeftToRightByDefault() { - this.vm.Setup(null, null); + this.vm.Setup(null, null, MessageBoxButton.OK, MessageBoxImage.None, MessageBoxResult.None, MessageBoxResult.None, MessageBoxOptions.None, null); Assert.AreEqual(FlowDirection.LeftToRight, this.vm.FlowDirection); } [Test] public void FlowsRightToLeftIfRequested() { - this.vm.Setup(null, null, new MessageBoxConfig() { Options = MessageBoxOptions.RtlReading }); + this.vm.Setup(null, null, MessageBoxButton.OK, MessageBoxImage.None, MessageBoxResult.None, MessageBoxResult.None, MessageBoxOptions.RtlReading, null); Assert.AreEqual(FlowDirection.RightToLeft, this.vm.FlowDirection); } [Test] public void AlignsLeftIfLeftToRightByDefault() { - this.vm.Setup(null, null); + this.vm.Setup(null, null, MessageBoxButton.OK, MessageBoxImage.None, MessageBoxResult.None, MessageBoxResult.None, MessageBoxOptions.None, null); Assert.AreEqual(TextAlignment.Left, this.vm.TextAlignment); } [Test] public void AlignsRightIfLeftToRightAndRequested() { - this.vm.Setup(null, null, new MessageBoxConfig() { Options = MessageBoxOptions.RightAlign }); + this.vm.Setup(null, null, MessageBoxButton.OK, MessageBoxImage.None, MessageBoxResult.None, MessageBoxResult.None, MessageBoxOptions.RightAlign, null); Assert.AreEqual(TextAlignment.Right, this.vm.TextAlignment); } [Test] public void AlignsRightIfRightToLeftByDefault() { - this.vm.Setup(null, null, new MessageBoxConfig() { Options = MessageBoxOptions.RtlReading }); + 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, new MessageBoxConfig() { Options = MessageBoxOptions.RightAlign | MessageBoxOptions.RtlReading }); + 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 a692e78..238e6df 100644 --- a/StyletUnitTests/WindowManagerTests.cs +++ b/StyletUnitTests/WindowManagerTests.cs @@ -279,13 +279,12 @@ namespace StyletUnitTests [Test] public void ShowMessageBoxShowsMessageBox() { - var config = new MessageBoxConfig(); var wm = new WindowManagerWithoutCreateWindow(this.viewManager.Object, () => this.messageBoxViewModel.Object); - try { wm.ShowMessageBox("text", "title", config); } + try { wm.ShowMessageBox("text", "title", MessageBoxButton.OKCancel, MessageBoxImage.Error, MessageBoxResult.OK, MessageBoxResult.Cancel, MessageBoxOptions.RtlReading); } catch (TestException) { } - this.messageBoxViewModel.Verify(x => x.Setup("text", "title", config)); + this.messageBoxViewModel.Verify(x => x.Setup("text", "title", MessageBoxButton.OKCancel, MessageBoxImage.Error, MessageBoxResult.OK, MessageBoxResult.Cancel, MessageBoxOptions.RtlReading, null)); } } }