forked from Devolutions/UniGetUI
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCheckboxButtonCard.cs
More file actions
105 lines (94 loc) · 3.34 KB
/
Copy pathCheckboxButtonCard.cs
File metadata and controls
105 lines (94 loc) · 3.34 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
using Avalonia;
using Avalonia.Automation;
using Avalonia.Controls;
using Avalonia.Interactivity;
using Avalonia.Layout;
using Avalonia.Media;
using UniGetUI.Avalonia.Infrastructure;
using UniGetUI.Core.Tools;
using CoreSettings = global::UniGetUI.Core.SettingsEngine.Settings;
namespace UniGetUI.Avalonia.Views.Controls.Settings;
public sealed partial class CheckboxButtonCard : SettingsCard
{
public ToggleSwitch _checkbox;
public TextBlock _textblock;
public Button Button;
private bool IS_INVERTED;
private CoreSettings.K setting_name = CoreSettings.K.Unset;
public CoreSettings.K SettingName
{
set
{
setting_name = value;
IS_INVERTED = CoreSettings.ResolveKey(value).StartsWith("Disable");
_checkbox.IsChecked = CoreSettings.Get(setting_name) ^ IS_INVERTED ^ ForceInversion;
_textblock.Opacity = (_checkbox.IsChecked ?? false) ? 1 : 0.7;
Button.IsEnabled = (_checkbox.IsChecked ?? false) || _buttonAlwaysOn;
}
}
public bool ForceInversion { get; set; }
public bool Checked => _checkbox.IsChecked ?? false;
public event EventHandler<EventArgs>? StateChanged;
public new event EventHandler<RoutedEventArgs>? Click;
public string CheckboxText
{
set
{
_textblock.Text = value;
ApplyAutomationMetadata(_checkbox, value);
ApplyAutomationMetadata(Button, value);
}
}
public string ButtonText
{
set
{
Button.Content = value;
ApplyAutomationMetadata(Button, _textblock.Text, value);
}
}
private bool _buttonAlwaysOn;
public bool ButtonAlwaysOn
{
set
{
_buttonAlwaysOn = value;
Button.IsEnabled = (_checkbox.IsChecked ?? false) || _buttonAlwaysOn;
}
}
public CheckboxButtonCard()
{
Button = new Button { Margin = new Thickness(0, 8, 0, 0) };
_checkbox = new ToggleSwitch
{
Margin = new Thickness(0, 0, 8, 0),
OnContent = new TextBlock { Text = CoreTools.Translate("Enabled") },
OffContent = new TextBlock { Text = CoreTools.Translate("Disabled") },
};
_textblock = new TextBlock
{
Margin = new Thickness(2, 0, 0, 0),
VerticalAlignment = VerticalAlignment.Center,
TextWrapping = TextWrapping.Wrap,
FontWeight = FontWeight.Medium,
};
IS_INVERTED = false;
AutomationProperties.SetAccessibilityView(Button, AccessibilityView.Control);
Content = _checkbox;
Header = _textblock;
Description = Button;
_checkbox.IsCheckedChanged += (_, _) =>
{
CoreSettings.Set(setting_name, (_checkbox.IsChecked ?? false) ^ IS_INVERTED ^ ForceInversion);
StateChanged?.Invoke(this, EventArgs.Empty);
Button.IsEnabled = (_checkbox.IsChecked ?? false) ? true : _buttonAlwaysOn;
_textblock.Opacity = (_checkbox.IsChecked ?? false) ? 1 : 0.7;
if (_textblock.Text is not null)
{
AccessibilityAnnouncementService.AnnounceToggle(_textblock.Text, _checkbox.IsChecked ?? false);
}
};
Button.Click += (s, e) => Click?.Invoke(s, e);
ApplyAutomationMetadata(_checkbox, _textblock.Text);
}
}