-
Notifications
You must be signed in to change notification settings - Fork 9
/
MessageBoxDialogFactory.cs
126 lines (112 loc) · 4.44 KB
/
MessageBoxDialogFactory.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
using HanumanInstitute.MvvmDialogs.FrameworkDialogs;
// ReSharper disable CheckNamespace
namespace HanumanInstitute.MvvmDialogs.Avalonia.MessageBox;
/// <summary>
/// Default framework dialog factory that will create instances of standard Windows dialogs.
/// </summary>
public class MessageBoxDialogFactory : DialogFactoryBase
{
private readonly IMessageBoxApi _api;
/// <summary>
/// Gets or sets the message box type to show.
/// </summary>
public MessageBoxMode Mode { get; set; }
/// <summary>
/// Initializes a new instance of a FrameworkDialog.
/// </summary>
/// <param name="chain">If the dialog is not handled by this class, calls this other handler next.</param>
public MessageBoxDialogFactory(IDialogFactory? chain = null)
: this(chain, null)
{
}
/// <summary>
/// Initializes a new instance of a FrameworkDialog.
/// </summary>
/// <param name="chain">If the dialog is not handled by this class, calls this other handler next.</param>
/// <param name="api">An interface exposing Avalonia messagebox dialogs API.</param>
internal MessageBoxDialogFactory(IDialogFactory? chain, IMessageBoxApi? api)
: base(chain)
{
_api = api ?? new MessageBoxApi();
}
/// <inheritdoc />
public override async Task<object?> ShowDialogAsync<TSettings>(IView? owner, TSettings settings) =>
settings switch
{
MessageBoxSettings s => await ShowMessageBoxDialogAsync(owner, s).ConfigureAwait(true),
_ => await base.ShowDialogAsync(owner, settings).ConfigureAwait(true)
};
private async Task<bool?> ShowMessageBoxDialogAsync(IView? owner, MessageBoxSettings settings)
{
var apiSettings = new MessageBoxApiSettings()
{
Title = settings.Title,
Text = settings.Content,
Buttons = SyncButton(settings.Button),
Icon = SyncIcon(settings.Icon),
EnterDefaultButton = SyncDefaultEnter(settings.Button, settings.DefaultValue),
EscDefaultButton = SyncDefaultEsc(settings.Button)
};
var ownerRef = owner.GetRef();
if (ownerRef is Window ownerWin)
{
var result = await _api.ShowMessageBoxAsync(ownerWin, apiSettings, Mode);
return result switch
{
ButtonResult.Yes => true,
ButtonResult.Ok => true,
ButtonResult.No => false,
ButtonResult.Cancel => null,
_ => null
};
}
else
{
throw new InvalidCastException("Owner must be of type Window.");
}
}
// Convert platform-agnostic types into Win32 types.
private static ButtonEnum SyncButton(MessageBoxButton value) =>
(value) switch
{
MessageBoxButton.Ok => ButtonEnum.Ok,
MessageBoxButton.YesNo => ButtonEnum.YesNo,
MessageBoxButton.OkCancel => ButtonEnum.OkCancel,
MessageBoxButton.YesNoCancel => ButtonEnum.YesNoCancel,
_ => ButtonEnum.Ok
};
private static Icon SyncIcon(MessageBoxImage value) =>
(value) switch
{
MessageBoxImage.None => Icon.None,
MessageBoxImage.Error => Icon.Error,
MessageBoxImage.Exclamation => Icon.Warning,
MessageBoxImage.Information => Icon.Info,
MessageBoxImage.Stop => Icon.Stop,
MessageBoxImage.Warning => Icon.Warning,
_ => Icon.None
};
private static ClickEnum SyncDefaultEnter(MessageBoxButton buttons, bool? value) =>
buttons switch
{
MessageBoxButton.Ok => ClickEnum.Ok,
MessageBoxButton.OkCancel => value == true ? ClickEnum.Ok : ClickEnum.Cancel,
MessageBoxButton.YesNo => value == true ? ClickEnum.Yes : ClickEnum.No,
MessageBoxButton.YesNoCancel => value switch
{
true => ClickEnum.Yes,
false => ClickEnum.No,
null => ClickEnum.Cancel
},
_ => ClickEnum.Default
};
private static ClickEnum SyncDefaultEsc(MessageBoxButton buttons) =>
buttons switch
{
MessageBoxButton.Ok => ClickEnum.Ok,
MessageBoxButton.OkCancel => ClickEnum.Cancel,
MessageBoxButton.YesNo => ClickEnum.No,
MessageBoxButton.YesNoCancel => ClickEnum.Cancel,
_ => ClickEnum.Default
};
}