|
| 1 | +using MahApps.Metro.SimpleChildWindow; |
| 2 | +using NETworkManager.Localization.Resources; |
| 3 | +using NETworkManager.Settings; |
| 4 | +using NETworkManager.Utilities; |
| 5 | +using NETworkManager.ViewModels; |
| 6 | +using NETworkManager.Views; |
| 7 | +using System.Threading.Tasks; |
| 8 | +using System.Windows; |
| 9 | + |
| 10 | +namespace NETworkManager |
| 11 | +{ |
| 12 | + /// <summary> |
| 13 | + /// Helper class for showing dialog messages. |
| 14 | + /// </summary> |
| 15 | + public static class DialogHelper |
| 16 | + { |
| 17 | + /// <summary> |
| 18 | + /// Displays a modal message dialog with an OK button, allowing the user to acknowledge the message before |
| 19 | + /// continuing. |
| 20 | + /// </summary> |
| 21 | + /// <remarks>The dialog is shown as a child window of the specified parent. The method is |
| 22 | + /// asynchronous and returns when the dialog is dismissed by the user.</remarks> |
| 23 | + /// <param name="parentWindow">The parent window that will host the message dialog. Cannot be null.</param> |
| 24 | + /// <param name="title">The title text displayed in the message dialog window. Cannot be null.</param> |
| 25 | + /// <param name="message">The main message content shown in the dialog. Cannot be null.</param> |
| 26 | + /// <param name="icon">The icon to display in the dialog, indicating the message type. Defaults to Info if not specified.</param> |
| 27 | + /// <param name="buttonOKText">The text to display on the OK button. If null, a default value is used.</param> |
| 28 | + /// <returns>A task that completes when the user closes the message dialog.</returns> |
| 29 | + public static Task ShowOKMessageAsync(Window parentWindow, string title, string message, ChildWindowIcon icon = ChildWindowIcon.Info, string buttonOKText = null) |
| 30 | + { |
| 31 | + buttonOKText ??= Strings.OK; |
| 32 | + |
| 33 | + var childWindow = new OKMessageChildWindow(); |
| 34 | + |
| 35 | + var childWindowViewModel = new OKMessageViewModel(_ => |
| 36 | + { |
| 37 | + childWindow.IsOpen = false; |
| 38 | + ConfigurationManager.Current.IsChildWindowOpen = false; |
| 39 | + }, message, icon, buttonOKText); |
| 40 | + |
| 41 | + childWindow.Title = title; |
| 42 | + |
| 43 | + childWindow.DataContext = childWindowViewModel; |
| 44 | + |
| 45 | + ConfigurationManager.Current.IsChildWindowOpen = true; |
| 46 | + |
| 47 | + return parentWindow.ShowChildWindowAsync(childWindow); |
| 48 | + } |
| 49 | + |
| 50 | + /// <summary> |
| 51 | + /// Displays an asynchronous modal dialog with OK and Cancel buttons, allowing the user to confirm or cancel an |
| 52 | + /// action. |
| 53 | + /// </summary> |
| 54 | + /// <remarks>The dialog is shown as a child window of the specified parent. The method does not |
| 55 | + /// return until the user closes the dialog. Custom button text and icon can be provided to tailor the dialog to |
| 56 | + /// specific scenarios.</remarks> |
| 57 | + /// <param name="parentWindow">The parent window that hosts the child dialog. Cannot be null.</param> |
| 58 | + /// <param name="title">The title text displayed in the dialog window.</param> |
| 59 | + /// <param name="message">The message content shown to the user in the dialog.</param> |
| 60 | + /// <param name="icon">The icon displayed in the dialog to indicate the message type. Defaults to Info.</param> |
| 61 | + /// <param name="buttonOKText">The text label for the OK button. If null, a default value is used.</param> |
| 62 | + /// <param name="buttonCancelText">The text label for the Cancel button. If null, a default value is used.</param> |
| 63 | + /// <returns>A task that represents the asynchronous operation. The task result is <see langword="true"/> if the user |
| 64 | + /// clicks OK; otherwise, <see langword="false"/>.</returns> |
| 65 | + public static async Task<bool> ShowOKCancelMessageAsync(Window parentWindow, string title, string message, ChildWindowIcon icon = ChildWindowIcon.Info, string buttonOKText = null, string buttonCancelText = null) |
| 66 | + { |
| 67 | + buttonOKText ??= Strings.OK; |
| 68 | + buttonCancelText ??= Strings.Cancel; |
| 69 | + |
| 70 | + var result = false; |
| 71 | + |
| 72 | + var childWindow = new OKCancelMessageChildWindow(); |
| 73 | + |
| 74 | + var childWindowViewModel = new OKCancelMessageViewModel(_ => |
| 75 | + { |
| 76 | + childWindow.IsOpen = false; |
| 77 | + ConfigurationManager.Current.IsChildWindowOpen = false; |
| 78 | + |
| 79 | + result = true; |
| 80 | + }, |
| 81 | + _ => |
| 82 | + { |
| 83 | + childWindow.IsOpen = false; |
| 84 | + ConfigurationManager.Current.IsChildWindowOpen = false; |
| 85 | + }, |
| 86 | + message, icon, buttonOKText, buttonCancelText); |
| 87 | + |
| 88 | + childWindow.Title = title; |
| 89 | + childWindow.DataContext = childWindowViewModel; |
| 90 | + |
| 91 | + ConfigurationManager.Current.IsChildWindowOpen = true; |
| 92 | + |
| 93 | + await parentWindow.ShowChildWindowAsync(childWindow); |
| 94 | + |
| 95 | + return result; |
| 96 | + } |
| 97 | + } |
| 98 | +} |
0 commit comments