-
Notifications
You must be signed in to change notification settings - Fork 1
/
DialogBase.cs
411 lines (372 loc) · 13.7 KB
/
DialogBase.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
using MaterialDesignThemes.Wpf;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Forms;
using System.Windows.Media;
using wpf_material_dialogs.Buttons;
using wpf_material_dialogs.Enums;
using wpf_material_dialogs.Interfaces;
using HorizontalAlignment = System.Windows.HorizontalAlignment;
namespace wpf_material_dialogs.Dialogs
{
/// <inheritdoc />
/// <summary>
/// Class DialogBase.
/// Implements the <see cref="T:wpf_material_dialogs.Interfaces.IDialog" />
/// </summary>
/// <seealso cref="T:wpf_material_dialogs.Interfaces.IDialog" />
public abstract class DialogBase : IDialog
{
#region Events
/// <inheritdoc />
/// <summary>
/// Occurs when [property changed].
/// </summary>
public event PropertyChangedEventHandler PropertyChanged;
#endregion
#region Fields
private readonly object buttonLock = new();
private readonly List<Type> buttonTypes = new();
private HorizontalAlignment buttonAlignment = HorizontalAlignment.Right;
private IButtons buttons = new OkButtons();
private DialogButton dialogButtons = DialogButton.OkButtons;
private FontWeight fontWeight = FontWeights.Normal;
private double height = double.NaN;
private Brush iconBrush;
private PackIconKind iconKind;
private bool showIcon;
private string text;
private double textFontSize = 14;
private string title;
private double titleFontSize = 16;
private FontWeight titleFontWeight = FontWeights.Bold;
private double width = double.NaN;
private bool showButtons = true;
public bool ShowButtons
{
get => showButtons;
set
{
showButtons = value;
NotifyOfPropertyChanged();
NotifyOfPropertyChanged(nameof(SelectedButtons));
}
}
#endregion
#region Properties
/// <summary>
/// Gets the selected buttons based on the <see cref="DialogButtons" /> selection.
/// </summary>
/// <value><see cref="IButtons" />.</value>
public IButtons SelectedButtons => ShowButtons ? DialogButtons == DialogButton.Custom
? Buttons
: GetButtons(DialogButtons)
: null;
public FontWeight TitleFontWeight
{
get => titleFontWeight;
set
{
titleFontWeight = value;
NotifyOfPropertyChanged();
}
}
public FontWeight FontWeight
{
get => fontWeight;
set
{
fontWeight = value;
NotifyOfPropertyChanged();
}
}
public double Width
{
get => width;
set
{
width = value;
NotifyOfPropertyChanged();
}
}
public double Height
{
get => height;
set
{
height = value;
NotifyOfPropertyChanged();
}
}
/// <inheritdoc />
/// <summary>
/// Gets or sets the button alignment.
/// </summary>
/// <value><see cref="T:System.Windows.HorizontalAlignment" />.</value>
public HorizontalAlignment ButtonAlignment
{
get => buttonAlignment;
set
{
buttonAlignment = value;
NotifyOfPropertyChanged();
}
}
/// <inheritdoc />
/// <summary>
/// Gets or sets the custom buttons to be used. <see cref="P:wpf_material_dialogs.Dialogs.DialogBase.DialogButtons" /> must set to custom for this property to
/// apply.
/// </summary>
/// <value><see cref="T:wpf_material_dialogs.Interfaces.IButtons" />.</value>
public IButtons Buttons
{
get => buttons;
set
{
buttons = value;
NotifyOfPropertyChanged();
NotifyOfPropertyChanged(nameof(SelectedButtons));
}
}
/// <inheritdoc />
/// <summary>
/// Gets or sets the buttons to use in the dialog.
/// </summary>
/// <value><see cref="T:wpf_material_dialogs.Enums.DialogButton" />.</value>
public DialogButton DialogButtons
{
get => dialogButtons;
set
{
dialogButtons = value;
NotifyOfPropertyChanged();
NotifyOfPropertyChanged(nameof(SelectedButtons));
}
}
/// <inheritdoc />
/// <summary>
/// Gets or sets the icon brush.
/// </summary>
/// <value>The icon brush.</value>
public Brush IconBrush
{
get => iconBrush;
set
{
iconBrush = value;
NotifyOfPropertyChanged();
}
}
/// <inheritdoc />
/// <summary>
/// Gets or sets the kind of the icon.
/// </summary>
/// <value>The kind of the icon.</value>
public PackIconKind IconKind
{
get => iconKind;
set
{
iconKind = value;
NotifyOfPropertyChanged();
}
}
/// <inheritdoc />
/// <summary>
/// Gets or sets a value indicating whether to show the icon.
/// </summary>
/// <value><c>true</c> if [show icon]; otherwise, <c>false</c>.</value>
public bool ShowIcon
{
get => showIcon;
set
{
showIcon = value;
NotifyOfPropertyChanged();
}
}
/// <inheritdoc />
/// <summary>
/// Gets or sets the dialog primary text.
/// </summary>
/// <value>The primary text of the dialog.</value>
public string Text
{
get => text;
set
{
text = value;
NotifyOfPropertyChanged();
}
}
/// <inheritdoc />
/// <summary>
/// Gets or sets the size of the text font.
/// </summary>
/// <value>The size of the text font.</value>
public double TextFontSize
{
get => textFontSize;
set
{
textFontSize = value;
NotifyOfPropertyChanged();
}
}
/// <inheritdoc />
/// <summary>
/// Gets or sets the dialog title.
/// </summary>
/// <value>The title of the dialog.</value>
public string Title
{
get => title;
set
{
title = value;
NotifyOfPropertyChanged();
}
}
/// <inheritdoc />
/// <summary>
/// Gets or sets the size of the title font.
/// </summary>
/// <value>The size of the title font.</value>
public double TitleFontSize
{
get => titleFontSize;
set
{
titleFontSize = value;
NotifyOfPropertyChanged();
}
}
#endregion
/// <summary>
/// Shows the destroy alert dialog.
/// </summary>
/// <param name="text">The text.</param>
/// <param name="buttons">The buttons.</param>
/// <returns><c>true</c> if <see cref="DialogResult.Yes" />, <c>false</c> otherwise.</returns>
public static async Task<DialogResult> ShowAlertDialog(string text, DialogButton buttons) => await new AlertDialog
{
Title = "Alert",
IconBrush = Brushes.Red,
Text = text,
DialogButtons = buttons,
}.ShowDialog();
/// <summary>
/// Shows the dialog with a few customizable options.
/// </summary>
/// <param name="dialogText">The dialog text.</param>
/// <param name="buttonType">The footer dialog buttons.</param>
/// <param name="titleText">The dialog title.</param>
/// <param name="dialogHosId">The dialog identifier.</param>
/// <returns><see cref="DialogResult" />.</returns>
public async Task<DialogResult> ShowDialog(string dialogText, DialogButton buttonType, string titleText,
object dialogHosId = null)
{
Title = titleText;
Text = dialogText;
DialogButtons = buttonType;
return await ShowDialog(dialogHosId);
}
/// <summary>
/// Shows the dialog with a few customizable options.
/// </summary>
/// <param name="dialogText">The dialog text.</param>
/// <param name="buttonType">The footer dialog buttons.</param>
/// <param name="packIconKind">Kind of the icon.</param>
/// <param name="packIconBrush">The icon brush.</param>
/// <param name="titleText">The dialog title.</param>
/// <param name="dialogHosId">The dialog identifier.</param>
/// <returns><see cref="DialogResult" />.</returns>
public async Task<DialogResult> ShowDialog(string dialogText, DialogButton buttonType, PackIconKind packIconKind,
Brush packIconBrush, string titleText, object dialogHosId = null)
{
IconKind = packIconKind;
ShowIcon = true;
IconBrush = packIconBrush;
return await ShowDialog(dialogText, buttonType, titleText, dialogHosId);
}
public static async Task<DialogResult> ShowDialogContent(object content, object dialogHosId = null,
DialogOpenedEventHandler openedAction = null,
DialogClosingEventHandler closingAction = null)
=> content == null
? throw new ArgumentNullException(nameof(content))
: await DialogHost.Show(content, dialogHosId, openedAction, closingAction) as DialogResult? ?? DialogResult.None;
#region IDialog
/// <inheritdoc />
/// <summary>
/// Gets the buttons.
/// </summary>
/// <param name="button">The button enum to convert to an IButton.</param>
/// <returns>IButtons.</returns>
/// <exception cref="T:System.ArgumentOutOfRangeException">button</exception>
/// <exception cref="T:System.ArgumentOutOfRangeException">button</exception>
/// <exception cref="T:System.ArgumentOutOfRangeException">button</exception>
public IButtons GetButtons(DialogButton button)
{
// ReSharper disable once PossibleNullReferenceException
lock (buttonLock)
{
var assembly = Assembly.GetExecutingAssembly();
if (!buttonTypes?.Any() ?? false)
{
buttonTypes.AddRange(assembly
.GetTypes()
.Where(mytype => mytype.GetInterfaces().Contains(typeof(IButtons))));
}
var selectedButtonType =
buttonTypes?.First(mytype => button.ToString().Equals(mytype?.Name, StringComparison.CurrentCultureIgnoreCase));
return selectedButtonType?.FullName != null
? assembly.CreateInstance(selectedButtonType.FullName) as IButtons
: throw new ArgumentOutOfRangeException(nameof(button));
}
}
/// <inheritdoc />
/// <summary>
/// Notifies the of property changed.
/// </summary>
/// <param name="propertyName">Name of the property.</param>
public void NotifyOfPropertyChanged([CallerMemberName] string propertyName = "") =>
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
/// <inheritdoc />
/// <summary>
/// Shows the dialog with given configuration.
/// </summary>
/// <param name="dialogHosId">The dialog hos identifier.</param>
/// <param name="openedAction">The opened action.</param>
/// <param name="closingAction">The closing action.</param>
/// <returns>
/// <see cref="T:System.Tuple`2" /> containing <see cref="T:System.Windows.Forms.DialogResult" /> and
/// <see cref="T:System.Boolean" />.
/// </returns>
public async Task<DialogResult> ShowDialog(object dialogHosId = null,
DialogOpenedEventHandler openedAction = null,
DialogClosingEventHandler closingAction = null) =>
await DialogHost.Show(this, dialogHosId, openedAction, closingAction) as DialogResult? ??
DialogResult.None;
/// <inheritdoc />
/// <summary>
/// Shows the dialog with given configuration.
/// </summary>
/// <param name="dialogHosId">The dialog hos identifier.</param>
/// <param name="openedAction">The opened action.</param>
/// <param name="closingAction">The closing action.</param>
/// <returns>
/// <see cref="T:System.Tuple`2" /> containing <see cref="T:System.Windows.Forms.DialogResult" /> and
/// <see cref="T:System.Boolean" />.
/// </returns>
public async Task<object> ShowDialogObject(object dialogHosId = null,
DialogOpenedEventHandler openedAction = null,
DialogClosingEventHandler closingAction = null) => await DialogHost.Show(this, dialogHosId, openedAction, closingAction);
#endregion
}
}