Repository navigation
Expand file tree
/
Copy pathDialogHelper.cs
More file actions
143 lines (120 loc) · 6.76 KB
/
Copy pathDialogHelper.cs
File metadata and controls
143 lines (120 loc) · 6.76 KB
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
using MahApps.Metro.Controls;
using MahApps.Metro.SimpleChildWindow;
using NETworkManager.Localization.Resources;
using NETworkManager.Settings;
using NETworkManager.Utilities;
using NETworkManager.ViewModels;
using NETworkManager.Views;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
namespace NETworkManager
{
/// <summary>
/// Helper class for showing dialog messages.
/// </summary>
public static class DialogHelper
{
/// <summary>
/// Occurs before a message or confirmation dialog is shown, e.g. so a dialog that stays open (the run command
/// with Ctrl+Enter) can close itself first instead of the dialogs being shown on top of each other.
/// </summary>
public static event EventHandler DialogOpening;
/// <summary>
/// Displays a modal message dialog with an OK button, allowing the user to acknowledge the message before
/// continuing.
/// </summary>
/// <remarks>The dialog is shown as a child window of the specified parent. The method is
/// asynchronous and returns when the dialog is dismissed by the user.</remarks>
/// <param name="parentWindow">The parent window that will host the message dialog. Cannot be null.</param>
/// <param name="title">The title text displayed in the message dialog window. Cannot be null.</param>
/// <param name="message">The main message content shown in the dialog. Cannot be null.</param>
/// <param name="icon">The icon to display in the dialog, indicating the message type. Defaults to Info if not specified.</param>
/// <param name="confirmButtonText">The text to display on the confirm button. If null, a default value is used.</param>
/// <returns>A task that completes when the user closes the message dialog.</returns>
public static Task ShowMessageAsync(Window parentWindow, string title, string message, ChildWindowIcon icon = ChildWindowIcon.Info, string confirmButtonText = null)
{
confirmButtonText ??= Strings.OK;
DialogOpening?.Invoke(null, EventArgs.Empty);
var childWindow = new MessageChildWindow();
var childWindowViewModel = new MessageViewModel(_ =>
{
childWindow.IsOpen = false;
ConfigurationManager.Current.IsChildWindowOpen = false;
}, message, icon, confirmButtonText);
childWindow.Title = title;
childWindow.DataContext = childWindowViewModel;
ConfigurationManager.Current.IsChildWindowOpen = true;
return parentWindow.ShowChildWindowAsync(childWindow);
}
/// <summary>
/// Displays an asynchronous modal dialog with OK and Cancel buttons, allowing the user to confirm or cancel an
/// action.
/// </summary>
/// <remarks>The dialog is shown as a child window of the specified parent. The method does not
/// return until the user closes the dialog. Custom button text and icon can be provided to tailor the dialog to
/// specific scenarios.</remarks>
/// <param name="parentWindow">The parent window that hosts the child dialog. Cannot be null.</param>
/// <param name="title">The title text displayed in the dialog window.</param>
/// <param name="message">The message content shown to the user in the dialog.</param>
/// <param name="icon">The icon displayed in the dialog to indicate the message type. Defaults to Info.</param>
/// <param name="confirmButtonText">The text label for the confirm button. If null, a default value is used.</param>
/// <param name="cancelButtonText">The text label for the cancel button. If null, a default value is used.</param>
/// <returns>A task that represents the asynchronous operation. The task result is <see langword="true"/> if the user
/// clicks OK; otherwise, <see langword="false"/>.</returns>
public static async Task<bool> ShowConfirmationMessageAsync(Window parentWindow, string title, string message, ChildWindowIcon icon = ChildWindowIcon.Info, string confirmButtonText = null, string cancelButtonText = null)
{
confirmButtonText ??= Strings.OK;
cancelButtonText ??= Strings.Cancel;
var result = false;
DialogOpening?.Invoke(null, EventArgs.Empty);
var childWindow = new MessageConfirmationChildWindow();
var childWindowViewModel = new MessageConfirmationViewModel(_ =>
{
childWindow.IsOpen = false;
ConfigurationManager.Current.IsChildWindowOpen = false;
result = true;
},
_ =>
{
childWindow.IsOpen = false;
ConfigurationManager.Current.IsChildWindowOpen = false;
},
message, icon, confirmButtonText, cancelButtonText);
childWindow.Title = title;
childWindow.DataContext = childWindowViewModel;
ConfigurationManager.Current.IsChildWindowOpen = true;
await parentWindow.ShowChildWindowAsync(childWindow);
return result;
}
/// <summary>
/// Gets whether a dialog (MahApps dialog or child window) is open in the main window, or the main window doesn't
/// exist yet - commands that would open another dialog, or run (e.g. refresh) behind it, can't be executed.
/// </summary>
/// <returns>True if a dialog is open or there is no main window.</returns>
public static bool IsAnyDialogOpen()
{
return Application.Current.MainWindow is not MetroWindow mainWindow ||
mainWindow.IsAnyDialogOpen ||
ConfigurationManager.Current.IsChildWindowOpen;
}
/// <summary>
/// Formats the given items as a bulleted list (one item per line, prefixed with "- ") to show them
/// in a dialog message, e.g. the entries that will be deleted. Only the first <paramref name="maxItems"/>
/// items are listed, the remaining items are summarized as "(+X more)".
/// </summary>
/// <param name="items">The items to list.</param>
/// <param name="maxItems">The maximum number of items to list.</param>
/// <returns>The formatted list.</returns>
public static string FormatItemList(IEnumerable<string> items, int maxItems = 10)
{
var itemList = items.ToList();
var lines = itemList.Take(maxItems).Select(item => $"- {item}").ToList();
if (itemList.Count > maxItems)
lines.Add(string.Format(Strings.PlusXMore, itemList.Count - maxItems));
return string.Join(Environment.NewLine, lines);
}
}
}