aboutsummaryrefslogtreecommitdiff
path: root/manager/MessageBox.axaml.cs
blob: 3a6868b80b960d5a8cbb28c8cefad27b52e2ec61 (plain) (blame)
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
using System;
using System.Threading.Tasks;
using Avalonia;
using Avalonia.Controls;

namespace manager;

public enum MessageBoxResult
{
    Ok,
    Yes,
    No,
    Cancel,
}

public partial class MessageBox : Window
{
    public static readonly StyledProperty<string> TextProperty =
        AvaloniaProperty.Register<MessageBox, string>(nameof(Text));

    public string Text
    {
        get => GetValue(TextProperty);
        set => SetValue(TextProperty, value);
    }

    public MessageBox()
    {
        InitializeComponent();
    }

    protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs change)
    {
        base.OnPropertyChanged(change);
        if (change.Property == TextProperty)
            MessageText.Text = change.NewValue as string ?? "";
    }

    public void AddButtons(params (string text, MessageBoxResult result)[] buttons)
    {
        foreach (var (text, result) in buttons)
        {
            var btn = new Button { Content = text, Width = 80 };
            btn.Click += (_, _) => Close(result);
            ButtonPanel.Children.Add(btn);
        }
    }

    public static Task<MessageBoxResult> Show(Window owner, string text, string title,
        params (string text, MessageBoxResult result)[] buttons)
    {
        var msgBox = new MessageBox
        {
            Title = title,
            Text = text,
        };
        msgBox.AddButtons(buttons);
        return msgBox.ShowDialog<MessageBoxResult>(owner);
    }
}