LenovoLegionToolkit
154 строки · 4.7 Кб
1using System;
2using System.Threading.Tasks;
3using System.Windows;
4using System.Windows.Controls;
5using System.Windows.Input;
6using LenovoLegionToolkit.WPF.Resources;
7using Wpf.Ui.Common;
8using MessageBox = Wpf.Ui.Controls.MessageBox;
9using TextBox = Wpf.Ui.Controls.TextBox;
10
11namespace LenovoLegionToolkit.WPF.Utils;
12
13public static class MessageBoxHelper
14{
15public static Task<bool> ShowAsync(DependencyObject dependencyObject,
16string title,
17string message,
18string? leftButton = null,
19string? rightButton = null
20)
21{
22var window = Window.GetWindow(dependencyObject)
23?? Application.Current.MainWindow
24?? throw new InvalidOperationException("Cannot show message without window.");
25return ShowAsync(window, title, message, leftButton, rightButton);
26}
27
28public static Task<bool> ShowAsync(Window window,
29string title,
30string message,
31string? primaryButton = null,
32string? secondaryButton = null)
33{
34var tcs = new TaskCompletionSource<bool>();
35
36var messageBox = new MessageBox
37{
38Owner = window,
39Title = title,
40Content = new TextBlock
41{
42Text = message,
43TextWrapping = TextWrapping.Wrap,
44},
45ButtonLeftName = primaryButton ?? Resource.Yes,
46ButtonRightName = secondaryButton ?? Resource.No,
47ShowInTaskbar = false,
48Topmost = false,
49ResizeMode = ResizeMode.NoResize,
50};
51messageBox.ButtonLeftClick += (_, _) =>
52{
53tcs.SetResult(true);
54messageBox.Close();
55};
56messageBox.ButtonRightClick += (_, _) =>
57{
58tcs.SetResult(false);
59messageBox.Close();
60};
61messageBox.Closing += (_, _) =>
62{
63tcs.TrySetResult(false);
64};
65messageBox.Show();
66
67return tcs.Task;
68}
69
70public static Task<string?> ShowInputAsync(
71DependencyObject dependencyObject,
72string title,
73string? placeholder = null,
74string? text = null,
75string? primaryButton = null,
76string? secondaryButton = null,
77bool allowEmpty = false
78)
79{
80var window = Window.GetWindow(dependencyObject)
81?? Application.Current.MainWindow
82?? throw new InvalidOperationException("Cannot show message without window.");
83return ShowInputAsync(window, title, placeholder, text, primaryButton, secondaryButton, allowEmpty);
84}
85
86public static Task<string?> ShowInputAsync(
87Window window,
88string title,
89string? placeholder = null,
90string? text = null,
91string? primaryButton = null,
92string? secondaryButton = null,
93bool allowEmpty = false
94)
95{
96var tcs = new TaskCompletionSource<string?>();
97
98var textBox = new TextBox
99{
100MaxLines = 1,
101MaxLength = 50,
102PlaceholderText = placeholder,
103TextWrapping = TextWrapping.Wrap
104};
105var messageBox = new MessageBox
106{
107Owner = window,
108Title = title,
109Content = textBox,
110ButtonLeftAppearance = ControlAppearance.Transparent,
111ButtonLeftName = primaryButton ?? Resource.OK,
112ButtonRightName = secondaryButton ?? Resource.Cancel,
113ShowInTaskbar = false,
114Topmost = false,
115MinHeight = 160,
116MaxHeight = 160,
117ResizeMode = ResizeMode.NoResize,
118};
119
120textBox.TextChanged += (_, _) =>
121{
122var isEmpty = !allowEmpty && string.IsNullOrWhiteSpace(textBox.Text);
123messageBox.ButtonLeftAppearance = isEmpty ? ControlAppearance.Transparent : ControlAppearance.Primary;
124};
125messageBox.ButtonLeftClick += (_, _) =>
126{
127// ReSharper disable once ConditionalAccessQualifierIsNonNullableAccordingToAPIContract
128var content = textBox.Text?.Trim();
129var newText = string.IsNullOrWhiteSpace(content) ? null : content;
130if (!allowEmpty && newText is null)
131return;
132tcs.SetResult(newText);
133messageBox.Close();
134};
135messageBox.ButtonRightClick += (_, _) =>
136{
137tcs.SetResult(null);
138messageBox.Close();
139};
140messageBox.Closing += (_, _) =>
141{
142tcs.TrySetResult(null);
143};
144messageBox.Show();
145
146textBox.Text = text;
147textBox.SelectionStart = text?.Length ?? 0;
148textBox.SelectionLength = 0;
149
150FocusManager.SetFocusedElement(window, textBox);
151
152return tcs.Task;
153}
154}
155