forked from Devolutions/UniGetUI
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCheckboxCard.cs
More file actions
179 lines (160 loc) · 5.96 KB
/
CheckboxCard.cs
File metadata and controls
179 lines (160 loc) · 5.96 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
using CommunityToolkit.WinUI.Controls;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Media;
using UniGetUI.Core.SettingsEngine;
using UniGetUI.Core.Tools;
// To learn more about WinUI, the WinUI project structure,
// and more about our project templates, see: http://aka.ms/winui-project-info.
namespace UniGetUI.Interface.Widgets
{
public partial class CheckboxCard : SettingsCard
{
public ToggleSwitch _checkbox;
public TextBlock _textblock;
public TextBlock _warningBlock;
protected bool IS_INVERTED;
private Settings.K setting_name = Settings.K.Unset;
public Settings.K SettingName
{
set
{
setting_name = value;
IS_INVERTED = Settings.ResolveKey(value).StartsWith("Disable");
_checkbox.IsOn = Settings.Get(setting_name) ^ IS_INVERTED ^ ForceInversion;
_textblock.Opacity = _checkbox.IsOn ? 1 : 0.7;
}
}
public bool ForceInversion { get; set; }
public bool Checked
{
get => _checkbox.IsOn;
}
public virtual event EventHandler<EventArgs>? StateChanged;
public string Text
{
set
{
_textblock.Text = CoreTools.Translate(value);
Microsoft.UI.Xaml.Automation.AutomationProperties.SetName(this, _textblock.Text);
Microsoft.UI.Xaml.Automation.AutomationProperties.SetLocalizedControlType(this, "grouping");
Microsoft.UI.Xaml.Automation.AutomationProperties.SetName(_checkbox, _textblock.Text);
}
}
public string WarningText
{
set
{
_warningBlock.Text = CoreTools.Translate(value);
_warningBlock.Visibility = value.Any() ? Visibility.Visible : Visibility.Collapsed;
}
}
public Brush WarningForeground
{
set => _warningBlock.Foreground = value;
}
public double WarningOpacity
{
set => _warningBlock.Opacity = value;
}
public CheckboxCard()
{
_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()
{
VerticalAlignment = VerticalAlignment.Center,
Margin = new Thickness(0, 0, 0, 0),
TextWrapping = TextWrapping.Wrap,
};
_warningBlock = new TextBlock()
{
VerticalAlignment = VerticalAlignment.Center,
Margin = new Thickness(0, 0, 0, 0),
TextWrapping = TextWrapping.Wrap,
FontSize = 12,
Opacity = 0.7,
Visibility = Visibility.Collapsed,
};
IS_INVERTED = false;
Content = _checkbox;
Header = new StackPanel()
{
Spacing = 4,
Orientation = Orientation.Vertical,
Children = { _textblock, _warningBlock },
};
_checkbox.HorizontalAlignment = HorizontalAlignment.Stretch;
_checkbox.Toggled += _checkbox_Toggled;
Loaded += (s, e) =>
{
Microsoft.UI.Xaml.Automation.AutomationProperties.SetName(this, _textblock.Text);
Microsoft.UI.Xaml.Automation.AutomationProperties.SetLocalizedControlType(this, "grouping");
};
}
protected virtual void _checkbox_Toggled(object sender, RoutedEventArgs e)
{
Settings.Set(setting_name, _checkbox.IsOn ^ IS_INVERTED ^ ForceInversion);
StateChanged?.Invoke(this, EventArgs.Empty);
_textblock.Opacity = _checkbox.IsOn ? 1 : 0.7;
}
}
public partial class CheckboxCard_Dict : CheckboxCard
{
public override event EventHandler<EventArgs>? StateChanged;
private Settings.K _dictName = Settings.K.Unset;
private bool _disableStateChangedEvent;
private string _keyName = "";
public string KeyName
{
set
{
_keyName = value;
if (_dictName != Settings.K.Unset && _keyName.Any())
{
_disableStateChangedEvent = true;
_checkbox.IsOn =
Settings.GetDictionaryItem<string, bool>(_dictName, _keyName)
^ IS_INVERTED
^ ForceInversion;
_textblock.Opacity = _checkbox.IsOn ? 1 : 0.7;
_disableStateChangedEvent = false;
}
}
}
public Settings.K DictionaryName
{
set
{
_dictName = value;
IS_INVERTED = Settings.ResolveKey(value).StartsWith("Disable");
if (_dictName != Settings.K.Unset && _keyName.Any())
{
_checkbox.IsOn =
Settings.GetDictionaryItem<string, bool>(_dictName, _keyName)
^ IS_INVERTED
^ ForceInversion;
_textblock.Opacity = _checkbox.IsOn ? 1 : 0.7;
}
}
}
public CheckboxCard_Dict()
: base() { }
protected override void _checkbox_Toggled(object sender, RoutedEventArgs e)
{
if (_disableStateChangedEvent)
return;
Settings.SetDictionaryItem(
_dictName,
_keyName,
_checkbox.IsOn ^ IS_INVERTED ^ ForceInversion
);
StateChanged?.Invoke(this, EventArgs.Empty);
_textblock.Opacity = _checkbox.IsOn ? 1 : 0.7;
}
}
}