forked from chocolatey/ChocolateyGUI
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request chocolatey#851 from punker76/feature/IDialogService
Introduce DialogService
- Loading branch information
Showing
13 changed files
with
190 additions
and
134 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 88 additions & 0 deletions
88
Source/ChocolateyGui.Common.Windows/Services/DialogService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
// -------------------------------------------------------------------------------------------------------------------- | ||
// <copyright company="Chocolatey" file="DialogService.cs"> | ||
// Copyright 2017 - Present Chocolatey Software, LLC | ||
// Copyright 2014 - 2017 Rob Reynolds, the maintainers of Chocolatey, and RealDimensions Software, LLC | ||
// </copyright> | ||
// -------------------------------------------------------------------------------------------------------------------- | ||
|
||
using System.Threading.Tasks; | ||
using System.Windows; | ||
using ChocolateyGui.Common.Properties; | ||
using ChocolateyGui.Common.Windows.Views; | ||
using MahApps.Metro.Controls.Dialogs; | ||
using Microsoft.VisualStudio.Threading; | ||
|
||
namespace ChocolateyGui.Common.Windows.Services | ||
{ | ||
public class DialogService : IDialogService | ||
{ | ||
private readonly AsyncSemaphore _lock; | ||
|
||
public DialogService() | ||
{ | ||
_lock = new AsyncSemaphore(1); | ||
} | ||
|
||
public ShellView ShellView { get; set; } | ||
|
||
/// <inheritdoc /> | ||
public async Task<MessageDialogResult> ShowMessageAsync(string title, string message) | ||
{ | ||
using (await _lock.EnterAsync()) | ||
{ | ||
if (ShellView != null) | ||
{ | ||
var dialogSettings = new MetroDialogSettings | ||
{ | ||
AffirmativeButtonText = Resources.ChocolateyDialog_OK | ||
}; | ||
|
||
return await ShellView.ShowMessageAsync(title, message, MessageDialogStyle.Affirmative, dialogSettings); | ||
} | ||
|
||
return MessageBox.Show(message, title) == MessageBoxResult.OK | ||
? MessageDialogResult.Affirmative | ||
: MessageDialogResult.Negative; | ||
} | ||
} | ||
|
||
/// <inheritdoc /> | ||
public async Task<MessageDialogResult> ShowConfirmationMessageAsync(string title, string message) | ||
{ | ||
using (await _lock.EnterAsync()) | ||
{ | ||
if (ShellView != null) | ||
{ | ||
var dialogSettings = new MetroDialogSettings | ||
{ | ||
AffirmativeButtonText = Resources.Dialog_Yes, | ||
NegativeButtonText = Resources.Dialog_No | ||
}; | ||
|
||
return await ShellView.ShowMessageAsync(title, message, MessageDialogStyle.AffirmativeAndNegative, dialogSettings); | ||
} | ||
|
||
return MessageBox.Show(message, title, MessageBoxButton.YesNo) == MessageBoxResult.Yes | ||
? MessageDialogResult.Affirmative | ||
: MessageDialogResult.Negative; | ||
} | ||
} | ||
|
||
/// <inheritdoc /> | ||
public async Task<LoginDialogData> ShowLoginAsync(string title, string message, LoginDialogSettings settings = null) | ||
{ | ||
using (await _lock.EnterAsync()) | ||
{ | ||
if (ShellView != null) | ||
{ | ||
return await ShellView.ShowLoginAsync( | ||
Resources.SettingsViewModel_SetSourceUsernameAndPasswordTitle, | ||
Resources.SettingsViewModel_SetSourceUsernameAndPasswordMessage, | ||
settings); | ||
} | ||
|
||
return null; | ||
} | ||
} | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
Source/ChocolateyGui.Common.Windows/Services/IDialogService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
// -------------------------------------------------------------------------------------------------------------------- | ||
// <copyright company="Chocolatey" file="IDialogService.cs"> | ||
// Copyright 2017 - Present Chocolatey Software, LLC | ||
// Copyright 2014 - 2017 Rob Reynolds, the maintainers of Chocolatey, and RealDimensions Software, LLC | ||
// </copyright> | ||
// -------------------------------------------------------------------------------------------------------------------- | ||
|
||
using System.Threading.Tasks; | ||
using ChocolateyGui.Common.Windows.Views; | ||
using MahApps.Metro.Controls.Dialogs; | ||
|
||
namespace ChocolateyGui.Common.Windows.Services | ||
{ | ||
public interface IDialogService | ||
{ | ||
ShellView ShellView { get; set; } | ||
|
||
/// <summary> | ||
/// Creates a Message dialog with an OK button inside of the ShellView. | ||
/// </summary> | ||
/// <param name="title">The title of the Dialog.</param> | ||
/// <param name="message">The message contained within the Dialog.</param> | ||
/// <returns>A task promising the result of which button was pressed.</returns> | ||
Task<MessageDialogResult> ShowMessageAsync(string title, string message); | ||
|
||
/// <summary> | ||
/// Creates a Message dialog with Yes/No buttons inside of the ShellView. | ||
/// </summary> | ||
/// <param name="title">The title of the Dialog.</param> | ||
/// <param name="message">The message contained within the Dialog.</param> | ||
/// <returns>A task promising the result of which button was pressed.</returns> | ||
Task<MessageDialogResult> ShowConfirmationMessageAsync(string title, string message); | ||
|
||
/// <summary> | ||
/// Creates a Login dialog inside of the ShellView. | ||
/// </summary> | ||
/// <param name="title">The title of the Dialog.</param> | ||
/// <param name="message">The message contained within the Dialog.</param> | ||
/// <param name="settings">Optional settings that override the global dialog settings.</param> | ||
/// <returns>The text that was entered or null (Nothing in Visual Basic) if the user cancelled the operation.</returns> | ||
Task<LoginDialogData> ShowLoginAsync(string title, string message, LoginDialogSettings settings = null); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.