-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKickSettingsWindow.xaml.cs
More file actions
236 lines (210 loc) · 6.94 KB
/
Copy pathKickSettingsWindow.xaml.cs
File metadata and controls
236 lines (210 loc) · 6.94 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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Controls;
using VioletTooth.Localization;
using VioletTooth.Services;
using TextBox = System.Windows.Controls.TextBox;
using ComboBox = System.Windows.Controls.ComboBox;
namespace VioletTooth;
public partial class KickSettingsWindow : Window
{
private readonly KickSettingsStore _store;
private readonly ObservableCollection<KickRule> _nameRules = [];
private bool _loadingSelection;
public KickSettingsWindow(KickSettingsStore store)
{
InitializeComponent();
_store = store;
RuleList.ItemsSource = _nameRules;
ApplyLanguage();
LoadFromStore();
}
private void ApplyLanguage()
{
Title = Loc.T("KickWindowTitle");
TitleText.Text = Loc.T("KickRulesTitle");
SubtitleText.Text = Loc.T("KickRulesHint");
GlobalTitle.Text = Loc.T("GlobalRule");
GlobalEnabled.Content = Loc.T("EnableGlobalKick");
IdleOverLabel.Text = Loc.T("IdleOver");
ThenKickHint.Text = Loc.T("ThenKickHint");
GlobalBootKick.Content = Loc.T("KickOnStartup");
NameRulesTitle.Text = Loc.T("NameRules");
AddRuleButton.Content = Loc.T("AddNameRule");
DeleteRuleButton.Content = Loc.T("DeleteSelected");
RuleEnabled.Content = Loc.T("EnableRule");
NameContainsLabel.Text = Loc.T("NameContains");
IdleTimeoutLabel.Text = Loc.T("IdleTimeout");
RuleBootKick.Content = Loc.T("KickOnStartupShort");
ApplyRuleButton.Content = Loc.T("ApplySelectedRule");
HintText.Text = Loc.T("NameMatchHint");
CancelButton.Content = Loc.T("Cancel");
SaveButton.Content = Loc.T("Save");
FillUnitCombo(GlobalIdleUnit);
FillUnitCombo(RuleIdleUnit);
RuleList.Items.Refresh();
}
private static void FillUnitCombo(ComboBox combo)
{
var selected = combo.SelectedIndex;
combo.Items.Clear();
combo.Items.Add(Loc.T("UnitMinute"));
combo.Items.Add(Loc.T("UnitHour"));
combo.Items.Add(Loc.T("UnitDay"));
combo.SelectedIndex = selected < 0 ? 1 : selected;
}
private void LoadFromStore()
{
var settings = _store.GetSnapshot();
GlobalEnabled.IsChecked = settings.Global.Enabled;
SetIdleFields(settings.Global.IdleMinutes, GlobalIdleValue, GlobalIdleUnit);
GlobalBootKick.IsChecked = settings.Global.KickOnNextBoot;
_nameRules.Clear();
foreach (var rule in settings.NameRules)
{
_nameRules.Add(Clone(rule));
}
}
private void AddRule_Click(object sender, RoutedEventArgs e)
{
var rule = new KickRule
{
Enabled = true,
NameContains = Loc.T("DefaultNameKeyword"),
IdleMinutes = 60 * 24,
KickOnNextBoot = false
};
_nameRules.Add(rule);
RuleList.SelectedItem = rule;
}
private void DeleteRule_Click(object sender, RoutedEventArgs e)
{
if (RuleList.SelectedItem is KickRule rule)
{
_nameRules.Remove(rule);
ClearEditor();
}
}
private void RuleList_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (RuleList.SelectedItem is not KickRule rule)
{
ClearEditor();
return;
}
_loadingSelection = true;
RuleEditor.IsEnabled = true;
RuleEnabled.IsChecked = rule.Enabled;
RuleNameContains.Text = rule.NameContains ?? "";
SetIdleFields(rule.IdleMinutes, RuleIdleValue, RuleIdleUnit);
RuleBootKick.IsChecked = rule.KickOnNextBoot;
_loadingSelection = false;
}
private void ApplyRule_Click(object sender, RoutedEventArgs e)
{
if (_loadingSelection || RuleList.SelectedItem is not KickRule rule)
{
return;
}
if (string.IsNullOrWhiteSpace(RuleNameContains.Text))
{
System.Windows.MessageBox.Show(Loc.T("NameRequired"), Loc.T("KickWindowTitle"));
return;
}
rule.Enabled = RuleEnabled.IsChecked == true;
rule.NameContains = RuleNameContains.Text.Trim();
rule.IdleMinutes = ReadIdleMinutes(RuleIdleValue, RuleIdleUnit);
rule.KickOnNextBoot = RuleBootKick.IsChecked == true;
var index = _nameRules.IndexOf(rule);
if (index >= 0)
{
_nameRules[index] = rule;
RuleList.SelectedItem = rule;
RuleList.Items.Refresh();
}
}
private void Save_Click(object sender, RoutedEventArgs e)
{
if (RuleList.SelectedItem is KickRule)
{
ApplyRule_Click(sender, e);
}
var settings = new KickSettings
{
Global = new KickRule
{
Id = "global",
Enabled = GlobalEnabled.IsChecked == true,
NameContains = null,
IdleMinutes = ReadIdleMinutes(GlobalIdleValue, GlobalIdleUnit),
KickOnNextBoot = GlobalBootKick.IsChecked == true
},
NameRules = _nameRules
.Where(r => !string.IsNullOrWhiteSpace(r.NameContains))
.Select(Clone)
.ToList()
};
_store.Save(settings);
DialogResult = true;
Close();
}
private void Cancel_Click(object sender, RoutedEventArgs e)
{
DialogResult = false;
Close();
}
private void ClearEditor()
{
RuleEditor.IsEnabled = false;
RuleEnabled.IsChecked = false;
RuleNameContains.Text = "";
RuleIdleValue.Text = "";
RuleIdleUnit.SelectedIndex = 1;
RuleBootKick.IsChecked = false;
}
private static void SetIdleFields(int? minutes, TextBox valueBox, ComboBox unitBox)
{
if (minutes is null or <= 0)
{
valueBox.Text = "";
unitBox.SelectedIndex = 1;
return;
}
if (minutes % (60 * 24) == 0)
{
valueBox.Text = (minutes / (60 * 24)).ToString();
unitBox.SelectedIndex = 2;
}
else if (minutes % 60 == 0)
{
valueBox.Text = (minutes / 60).ToString();
unitBox.SelectedIndex = 1;
}
else
{
valueBox.Text = minutes.ToString();
unitBox.SelectedIndex = 0;
}
}
private static int? ReadIdleMinutes(TextBox valueBox, ComboBox unitBox)
{
if (!int.TryParse(valueBox.Text.Trim(), out var value) || value <= 0)
{
return null;
}
return unitBox.SelectedIndex switch
{
0 => value,
2 => value * 60 * 24,
_ => value * 60
};
}
private static KickRule Clone(KickRule rule) => new()
{
Id = rule.Id,
Enabled = rule.Enabled,
NameContains = rule.NameContains,
IdleMinutes = rule.IdleMinutes,
KickOnNextBoot = rule.KickOnNextBoot
};
}