Move back to lots of separate arguments for ShowMessageBox - consistent with MessageBox.Show

This commit is contained in:
Antony Male 2014-12-02 16:13:33 +00:00
parent f47db1cbec
commit 509d428dc8
5 changed files with 97 additions and 109 deletions

View File

@ -7,55 +7,6 @@ using System.Windows;
namespace Stylet
{
/// <summary>
/// Optional configuration for a MessageBox
/// </summary>
public class MessageBoxConfig
{
/// <summary>
/// Text to display in the body of the MessageBox
/// </summary>
public MessageBoxButton Buttons { get; set; }
/// <summary>
/// Icon to display to the left of the text. This also determines the sound played when the MessageBox is shown
/// </summary>
public MessageBoxImage Icon { get; set; }
/// <summary>
/// Button pressed when the user presses Enter. Defaults to the leftmost button
/// </summary>
public MessageBoxResult DefaultButton { get; set; }
/// <summary>
/// Button pressed when the user preses Esc or clicks the red X on the titlebar. Defaults to the rightmost button
/// </summary>
public MessageBoxResult CancelButton { get; set; }
/// <summary>
/// Additional options
/// </summary>
public MessageBoxOptions Options { get; set; }
/// <summary>
/// You may override the text for individual buttons on a case-by-case basis
/// </summary>
public IDictionary<MessageBoxResult, string> ButtonLabels { get; set; }
/// <summary>
/// Create a new instance with default configuration
/// </summary>
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;
}
}
/// <summary>
/// Interface for a MessageBoxViewModel. MessageBoxWindowManagerExtensions.ShowMessageBox will use the configured implementation of this
/// </summary>
@ -64,10 +15,21 @@ namespace Stylet
/// <summary>
/// Setup the MessageBoxViewModel with the information it needs
/// </summary>
/// <param name="text">Text to display in the body of the MessageBox</param>
/// <param name="title">Title to display in the titlebar of the MessageBox</param>
/// <param name="config">Other configuration. Optional</param>
void Setup(string text, string title, MessageBoxConfig config = null);
/// <param name="messageBoxText">A System.String that specifies the text to display.</param>
/// <param name="caption">A System.String that specifies the title bar caption to display.</param>
/// <param name="buttons">A System.Windows.MessageBoxButton value that specifies which button or buttons to display.</param>
/// <param name="icon">A System.Windows.MessageBoxImage value that specifies the icon to display.</param>
/// <param name="defaultResult">A System.Windows.MessageBoxResult value that specifies the default result of the message box.</param>
/// <param name="cancelResult">A System.Windows.MessageBoxResult value that specifies the cancel result of the message box</param>
/// <param name="options">A System.Windows.MessageBoxOptions value object that specifies the options.</param>
/// <param name="buttonLabels">A dictionary specifying the button labels, if desirable</param>
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<MessageBoxResult, string> buttonLabels = null);
/// <summary>
/// After the user has clicked a button, holds which button was clicked
@ -141,50 +103,59 @@ namespace Stylet
/// <summary>
/// Setup the MessageBoxViewModel with the information it needs
/// </summary>
/// <param name="text">Text to display in the body of the MessageBox</param>
/// <param name="title">Title to display in the titlebar of the MessageBox</param>
/// <param name="config">Other configuration. Optional</param>
public void Setup(string text, string title, MessageBoxConfig config = null)
/// <param name="messageBoxText">A System.String that specifies the text to display.</param>
/// <param name="caption">A System.String that specifies the title bar caption to display.</param>
/// <param name="buttons">A System.Windows.MessageBoxButton value that specifies which button or buttons to display.</param>
/// <param name="icon">A System.Windows.MessageBoxImage value that specifies the icon to display.</param>
/// <param name="defaultResult">A System.Windows.MessageBoxResult value that specifies the default result of the message box.</param>
/// <param name="cancelResult">A System.Windows.MessageBoxResult value that specifies the cancel result of the message box</param>
/// <param name="options">A System.Windows.MessageBoxOptions value object that specifies the options.</param>
/// <param name="buttonLabels">A dictionary specifying the button labels, if desirable</param>
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<MessageBoxResult, string> 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<LabelledValue<MessageBoxResult>>();
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<MessageBoxResult>(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;
}
/// <summary>

View File

@ -27,12 +27,23 @@ namespace Stylet
bool? ShowDialog(object viewModel);
/// <summary>
/// Setup the MessageBoxViewModel with the information it needs
/// Display a MessageBox
/// </summary>
/// <param name="text">Text to display in the body of the MessageBox</param>
/// <param name="title">Title to display in the titlebar of the MessageBox</param>
/// <param name="config">Other configuration. Optional</param>
MessageBoxResult ShowMessageBox(string text, string title = "", MessageBoxConfig config = null);
/// <param name="messageBoxText">A System.String that specifies the text to display.</param>
/// <param name="caption">A System.String that specifies the title bar caption to display.</param>
/// <param name="buttons">A System.Windows.MessageBoxButton value that specifies which button or buttons to display.</param>
/// <param name="icon">A System.Windows.MessageBoxImage value that specifies the icon to display.</param>
/// <param name="defaultResult">A System.Windows.MessageBoxResult value that specifies the default result of the message box.</param>
/// <param name="cancelResult">A System.Windows.MessageBoxResult value that specifies the cancel result of the message box</param>
/// <param name="options">A System.Windows.MessageBoxOptions value object that specifies the options.</param>
/// <param name="buttonLabels">A dictionary specifying the button labels, if desirable</param>
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<MessageBoxResult, string> buttonLabels = null);
}
/// <summary>
@ -75,15 +86,26 @@ namespace Stylet
}
/// <summary>
/// Setup the MessageBoxViewModel with the information it needs
/// Display a MessageBox
/// </summary>
/// <param name="text">Text to display in the body of the MessageBox</param>
/// <param name="title">Title to display in the titlebar of the MessageBox</param>
/// <param name="config">Other configuration. Optional</param>
public MessageBoxResult ShowMessageBox(string text, string title = "", MessageBoxConfig config = null)
/// <param name="messageBoxText">A System.String that specifies the text to display.</param>
/// <param name="caption">A System.String that specifies the title bar caption to display.</param>
/// <param name="buttons">A System.Windows.MessageBoxButton value that specifies which button or buttons to display.</param>
/// <param name="icon">A System.Windows.MessageBoxImage value that specifies the icon to display.</param>
/// <param name="defaultResult">A System.Windows.MessageBoxResult value that specifies the default result of the message box.</param>
/// <param name="cancelResult">A System.Windows.MessageBoxResult value that specifies the cancel result of the message box</param>
/// <param name="options">A System.Windows.MessageBoxOptions value object that specifies the options.</param>
/// <param name="buttonLabels">A dictionary specifying the button labels, if desirable</param>
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<MessageBoxResult, string> 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;
}

View File

@ -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");
}

View File

@ -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<ArgumentException>(() => this.vm.Setup(null, null, new MessageBoxConfig() { Buttons = MessageBoxButton.OKCancel, DefaultButton = MessageBoxResult.Yes }));
Assert.Throws<ArgumentException>(() => 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<ArgumentException>(() => this.vm.Setup(null, null, new MessageBoxConfig() { CancelButton = MessageBoxResult.No }));
Assert.Throws<ArgumentException>(() => 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<IChildDelegate>();
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<MessageBoxResult, string>()
this.vm.Setup(null, null, MessageBoxButton.OKCancel, MessageBoxImage.None, MessageBoxResult.None, MessageBoxResult.None, MessageBoxOptions.None, new Dictionary<MessageBoxResult, string>()
{
{ 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);
}
}

View File

@ -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));
}
}
}