LenovoLegionToolkit

Форк
0
154 строки · 4.7 Кб
1
using System;
2
using System.Threading.Tasks;
3
using System.Windows;
4
using System.Windows.Controls;
5
using System.Windows.Input;
6
using LenovoLegionToolkit.WPF.Resources;
7
using Wpf.Ui.Common;
8
using MessageBox = Wpf.Ui.Controls.MessageBox;
9
using TextBox = Wpf.Ui.Controls.TextBox;
10

11
namespace LenovoLegionToolkit.WPF.Utils;
12

13
public static class MessageBoxHelper
14
{
15
    public static Task<bool> ShowAsync(DependencyObject dependencyObject,
16
        string title,
17
        string message,
18
        string? leftButton = null,
19
        string? rightButton = null
20
    )
21
    {
22
        var window = Window.GetWindow(dependencyObject)
23
                     ?? Application.Current.MainWindow
24
                     ?? throw new InvalidOperationException("Cannot show message without window.");
25
        return ShowAsync(window, title, message, leftButton, rightButton);
26
    }
27

28
    public static Task<bool> ShowAsync(Window window,
29
        string title,
30
        string message,
31
        string? primaryButton = null,
32
        string? secondaryButton = null)
33
    {
34
        var tcs = new TaskCompletionSource<bool>();
35

36
        var messageBox = new MessageBox
37
        {
38
            Owner = window,
39
            Title = title,
40
            Content = new TextBlock
41
            {
42
                Text = message,
43
                TextWrapping = TextWrapping.Wrap,
44
            },
45
            ButtonLeftName = primaryButton ?? Resource.Yes,
46
            ButtonRightName = secondaryButton ?? Resource.No,
47
            ShowInTaskbar = false,
48
            Topmost = false,
49
            ResizeMode = ResizeMode.NoResize,
50
        };
51
        messageBox.ButtonLeftClick += (_, _) =>
52
        {
53
            tcs.SetResult(true);
54
            messageBox.Close();
55
        };
56
        messageBox.ButtonRightClick += (_, _) =>
57
        {
58
            tcs.SetResult(false);
59
            messageBox.Close();
60
        };
61
        messageBox.Closing += (_, _) =>
62
        {
63
            tcs.TrySetResult(false);
64
        };
65
        messageBox.Show();
66

67
        return tcs.Task;
68
    }
69

70
    public static Task<string?> ShowInputAsync(
71
        DependencyObject dependencyObject,
72
        string title,
73
        string? placeholder = null,
74
        string? text = null,
75
        string? primaryButton = null,
76
        string? secondaryButton = null,
77
        bool allowEmpty = false
78
    )
79
    {
80
        var window = Window.GetWindow(dependencyObject)
81
                     ?? Application.Current.MainWindow
82
                     ?? throw new InvalidOperationException("Cannot show message without window.");
83
        return ShowInputAsync(window, title, placeholder, text, primaryButton, secondaryButton, allowEmpty);
84
    }
85

86
    public static Task<string?> ShowInputAsync(
87
        Window window,
88
        string title,
89
        string? placeholder = null,
90
        string? text = null,
91
        string? primaryButton = null,
92
        string? secondaryButton = null,
93
        bool allowEmpty = false
94
    )
95
    {
96
        var tcs = new TaskCompletionSource<string?>();
97

98
        var textBox = new TextBox
99
        {
100
            MaxLines = 1,
101
            MaxLength = 50,
102
            PlaceholderText = placeholder,
103
            TextWrapping = TextWrapping.Wrap
104
        };
105
        var messageBox = new MessageBox
106
        {
107
            Owner = window,
108
            Title = title,
109
            Content = textBox,
110
            ButtonLeftAppearance = ControlAppearance.Transparent,
111
            ButtonLeftName = primaryButton ?? Resource.OK,
112
            ButtonRightName = secondaryButton ?? Resource.Cancel,
113
            ShowInTaskbar = false,
114
            Topmost = false,
115
            MinHeight = 160,
116
            MaxHeight = 160,
117
            ResizeMode = ResizeMode.NoResize,
118
        };
119

120
        textBox.TextChanged += (_, _) =>
121
        {
122
            var isEmpty = !allowEmpty && string.IsNullOrWhiteSpace(textBox.Text);
123
            messageBox.ButtonLeftAppearance = isEmpty ? ControlAppearance.Transparent : ControlAppearance.Primary;
124
        };
125
        messageBox.ButtonLeftClick += (_, _) =>
126
        {
127
            // ReSharper disable once ConditionalAccessQualifierIsNonNullableAccordingToAPIContract
128
            var content = textBox.Text?.Trim();
129
            var newText = string.IsNullOrWhiteSpace(content) ? null : content;
130
            if (!allowEmpty && newText is null)
131
                return;
132
            tcs.SetResult(newText);
133
            messageBox.Close();
134
        };
135
        messageBox.ButtonRightClick += (_, _) =>
136
        {
137
            tcs.SetResult(null);
138
            messageBox.Close();
139
        };
140
        messageBox.Closing += (_, _) =>
141
        {
142
            tcs.TrySetResult(null);
143
        };
144
        messageBox.Show();
145

146
        textBox.Text = text;
147
        textBox.SelectionStart = text?.Length ?? 0;
148
        textBox.SelectionLength = 0;
149

150
        FocusManager.SetFocusedElement(window, textBox);
151

152
        return tcs.Task;
153
    }
154
}
155

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.