Skip to content

Commit

Permalink
Update IMessageBox Api for Confirmation, see #4042
Browse files Browse the repository at this point in the history
  • Loading branch information
Vogel612 committed May 26, 2018
1 parent 960fb85 commit c45bba8
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 15 deletions.
6 changes: 3 additions & 3 deletions Rubberduck.Core/UI/CodeExplorer/Commands/RemoveCommand.cs
Expand Up @@ -57,14 +57,14 @@ protected override bool EvaluateCanExecute(object parameter)
protected override void OnExecute(object parameter)
{
var message = string.Format(CodeExplorerUI.ExportBeforeRemove_Prompt, ((CodeExplorerComponentViewModel)parameter).Name);
var result = _messageBox.Confirm(message, CodeExplorerUI.ExportBeforeRemove_Caption, true);
var result = _messageBox.Confirm(message, CodeExplorerUI.ExportBeforeRemove_Caption, ConfirmationOutcome.Yes);

if (!result.HasValue)
if (result == ConfirmationOutcome.Cancel)
{
return;
}

if (result.Value && !ExportFile((CodeExplorerComponentViewModel)parameter))
if (result == ConfirmationOutcome.Yes && !ExportFile((CodeExplorerComponentViewModel)parameter))
{
return;
}
Expand Down
73 changes: 65 additions & 8 deletions Rubberduck.Interaction/IMessageBox.cs
Expand Up @@ -5,13 +5,57 @@ namespace Rubberduck.Interaction
{
public interface IMessageBox
{
/// <summary>
/// Show a message to the user. Will only return after the user has acknowledged the message.
/// </summary>
/// <param name="text">The message to show to the user</param>
void Message(string text);
/// <summary>
/// Notify the user of an error. Will only return after the user has acknowledged the error.
/// </summary>
/// <param name="text">The Error text to show the user</param>
/// <param name="caption">The caption of the dialog window</param>
void NotifyError(string text, string caption);
/// <summary>
/// Notify the user of a warning. Will only return after the user has acknowledged the warning.
/// </summary>
/// <param name="text">The Warning text to show the user</param>
/// <param name="caption">The caption of the dialog window</param>
void NotifyWarn(string text, string caption);
/// <summary>
/// Ask the user a question. Neither user selection must have any non-reversible consequences.
/// Will only return on user-input.
/// </summary>
/// <param name="text">The Question to ask the user</param>
/// <param name="caption">The caption of the dialog window</param>
/// <returns>true, if the user selects "Yes", false if the user selects "No"</returns>
bool Question(string text, string caption);
[Obsolete] // TODO absorb into confirmation with preselected result
bool ConfirmYesNo(string text, string caption);
bool ConfirmYesNo(string text, string caption, bool suggestion);
bool? Confirm(string text, string caption, bool? suggestion);
/// <summary>
/// Ask the user for a simple confirmation. If the user selects an option, non-reversible consequences are acceptable.
/// Will only return on user-input.
/// </summary>
/// <param name="text">The question to ask the user</param>
/// <param name="caption">The caption of the dialog window</param>
/// <param name="suggestion">The pre-selected result for the user, defaults to <b>Yes</b></param>
/// <returns>true, if the user selects "Yes", false if the user selects "No"</returns>
bool ConfirmYesNo(string text, string caption, bool suggestion = true);
/// <summary>
/// Ask the user for a confirmation. If the user selects an option that is not "Cancel",
/// non-reversible consequences are acceptable.
/// Will only return on user-input.
/// </summary>
/// <param name="text">The question to ask the user</param>
/// <param name="caption">The caption of the dialog window</param>
/// <param name="suggestion">The pre-selected result for the user, defaults to <b>Cancel</b></param>
/// <returns>Yes, No or Cancel respectively, according to the user's input</returns>
ConfirmationOutcome Confirm(string text, string caption, ConfirmationOutcome suggestion = ConfirmationOutcome.Cancel);
}

public enum ConfirmationOutcome
{
Yes, No, Cancel
}

public class MessageBox : IMessageBox
Expand Down Expand Up @@ -46,22 +90,35 @@ public bool ConfirmYesNo(string text, string caption, bool suggestion)
return Forms.MessageBox.Show(text, caption, Forms.MessageBoxButtons.YesNo, Forms.MessageBoxIcon.Exclamation, suggestion ? Forms.MessageBoxDefaultButton.Button1 : Forms.MessageBoxDefaultButton.Button2) == Forms.DialogResult.Yes;
}

public bool? Confirm(string text, string caption, bool? suggestion)
public ConfirmationOutcome Confirm(string text, string caption, ConfirmationOutcome suggestion = ConfirmationOutcome.Yes)
{
var suggestionButton = suggestion.HasValue ? (suggestion.Value ? Forms.MessageBoxDefaultButton.Button1 : Forms.MessageBoxDefaultButton.Button2) : Forms.MessageBoxDefaultButton.Button3;
Forms.MessageBoxDefaultButton suggestionButton;
switch (suggestion)
{
// default required to shut the compiler up about "unassigned variable"
default:
case ConfirmationOutcome.Yes:
suggestionButton = Forms.MessageBoxDefaultButton.Button1;
break;
case ConfirmationOutcome.No:
suggestionButton = Forms.MessageBoxDefaultButton.Button2;
break;
case ConfirmationOutcome.Cancel:
suggestionButton = Forms.MessageBoxDefaultButton.Button3;
break;
}
var result = Forms.MessageBox.Show(text, caption, Forms.MessageBoxButtons.YesNoCancel, Forms.MessageBoxIcon.Exclamation, suggestionButton);

switch (result)
{
case Forms.DialogResult.Cancel:
return null;
return ConfirmationOutcome.Cancel;
case Forms.DialogResult.Yes:
return true;
return ConfirmationOutcome.Yes;
case Forms.DialogResult.No:
return false;
return ConfirmationOutcome.No;
default:
return suggestion;

}
}
}
Expand Down
6 changes: 3 additions & 3 deletions RubberduckTests/CodeExplorer/CodeExplorerTests.cs
Expand Up @@ -773,7 +773,7 @@ public void RemoveCommand_RemovesModuleWhenPromptOk()
saveFileDialog.Setup(o => o.ShowDialog()).Returns(DialogResult.OK);

var messageBox = new Mock<IMessageBox>();
messageBox.Setup(m => m.Confirm(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<bool?>())).Returns(true);
messageBox.Setup(m => m.Confirm(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<ConfirmationOutcome>())).Returns(ConfirmationOutcome.Yes);

var projectRepository = new ProjectsRepository(vbe.Object);
using (var state = new RubberduckParserState(vbe.Object, projectRepository, new DeclarationFinderFactory(), vbeEvents.Object))
Expand Down Expand Up @@ -859,7 +859,7 @@ public void RemoveCommand_GivenMsgBoxNO_RemovesModuleNoExport()
saveFileDialog.Setup(o => o.ShowDialog()).Returns(DialogResult.OK);

var messageBox = new Mock<IMessageBox>();
messageBox.Setup(m => m.Confirm(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<bool?>())).Returns(false);
messageBox.Setup(m => m.Confirm(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<ConfirmationOutcome>())).Returns(ConfirmationOutcome.No);

var projectRepository = new ProjectsRepository(vbe.Object);
using (var state = new RubberduckParserState(vbe.Object, projectRepository, new DeclarationFinderFactory(), vbeEvents.Object))
Expand Down Expand Up @@ -902,7 +902,7 @@ public void RemoveModule_Cancel()
saveFileDialog.Setup(o => o.OverwritePrompt);

var messageBox = new Mock<IMessageBox>();
messageBox.Setup(m => m.Confirm(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<bool>())).Returns((bool?)null);
messageBox.Setup(m => m.Confirm(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<ConfirmationOutcome>())).Returns(ConfirmationOutcome.Cancel);

var projectRepository = new ProjectsRepository(vbe.Object);
using (var state = new RubberduckParserState(vbe.Object, projectRepository, new DeclarationFinderFactory(), vbeEvents.Object))
Expand Down
2 changes: 1 addition & 1 deletion RubberduckTests/Refactoring/ReorderParametersTests.cs
Expand Up @@ -1438,7 +1438,7 @@ public void ReorderParametersRefactoring_ParamsSwapped_RejectPrompt()
var qualifiedSelection = new QualifiedSelection(new QualifiedModuleName(project.Object.VBComponents[0]), selection);

var messageBox = new Mock<IMessageBox>();
messageBox.Setup(m => m.Confirm(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<bool?>())).Returns(false);
messageBox.Setup(m => m.Confirm(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<ConfirmationOutcome>())).Returns(ConfirmationOutcome.No);

//Specify Params to remove
var model = new ReorderParametersModel(state, qualifiedSelection, messageBox.Object);
Expand Down

0 comments on commit c45bba8

Please sign in to comment.