-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathSettingsWindow.axaml.cs
More file actions
234 lines (208 loc) · 10.4 KB
/
SettingsWindow.axaml.cs
File metadata and controls
234 lines (208 loc) · 10.4 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
using Avalonia.Controls;
using Avalonia.Interactivity;
using System;
using System.Management;
namespace ShowWrite
{
public partial class SettingsWindow : Window
{
private StackPanel? _generalPage;
private StackPanel? _penPage;
private StackPanel? _cameraPage;
private StackPanel? _aboutPage;
private NumericUpDown? _denominatorInput;
private NumericUpDown? _ratioMinInput;
private NumericUpDown? _ratioMaxInput;
private NumericUpDown? _speedThresholdFastInput;
private NumericUpDown? _speedThresholdSlowInput;
private NumericUpDown? _ratioChangeCoefficientInput;
private CheckBox? _enablePalmEraserInput;
private NumericUpDown? _palmEraserThresholdInput;
private TextBlock? _motherboardSerialText;
private TextBlock? _uuidText;
private ComboBox? _themeComboBox;
public SettingsWindow()
{
InitializeComponent();
_generalPage = this.FindControl<StackPanel>("GeneralPage");
_penPage = this.FindControl<StackPanel>("PenPage");
_cameraPage = this.FindControl<StackPanel>("CameraPage");
_aboutPage = this.FindControl<StackPanel>("AboutPage");
_denominatorInput = this.FindControl<NumericUpDown>("DenominatorInput");
_ratioMinInput = this.FindControl<NumericUpDown>("RatioMinInput");
_ratioMaxInput = this.FindControl<NumericUpDown>("RatioMaxInput");
_speedThresholdFastInput = this.FindControl<NumericUpDown>("SpeedThresholdFastInput");
_speedThresholdSlowInput = this.FindControl<NumericUpDown>("SpeedThresholdSlowInput");
_ratioChangeCoefficientInput = this.FindControl<NumericUpDown>("RatioChangeCoefficientInput");
_enablePalmEraserInput = this.FindControl<CheckBox>("EnablePalmEraserInput");
_palmEraserThresholdInput = this.FindControl<NumericUpDown>("PalmEraserThresholdInput");
_motherboardSerialText = this.FindControl<TextBlock>("MotherboardSerialText");
_uuidText = this.FindControl<TextBlock>("UuidText");
_themeComboBox = this.FindControl<ComboBox>("ThemeComboBox");
LoadPenSettings();
LoadThemeSettings();
LoadSystemInfo();
}
private void NavListBox_SelectionChanged(object? sender, SelectionChangedEventArgs e)
{
if (NavListBox == null) return;
var selectedItem = NavListBox.SelectedItem as ListBoxItem;
if (selectedItem == null) return;
var tag = selectedItem.Tag?.ToString();
if (_generalPage != null) _generalPage.IsVisible = tag == "general";
if (_penPage != null) _penPage.IsVisible = tag == "pen";
if (_cameraPage != null) _cameraPage.IsVisible = tag == "camera";
if (_aboutPage != null) _aboutPage.IsVisible = tag == "about";
}
private void LoadPenSettings()
{
var config = Config.Load();
var settings = config.PenSettings ?? new PenSettings();
if (_denominatorInput != null) _denominatorInput.Value = settings.Denominator;
if (_ratioMinInput != null) _ratioMinInput.Value = (decimal)settings.RatioMin;
if (_ratioMaxInput != null) _ratioMaxInput.Value = (decimal)settings.RatioMax;
if (_speedThresholdFastInput != null) _speedThresholdFastInput.Value = (decimal)settings.SpeedThresholdFast;
if (_speedThresholdSlowInput != null) _speedThresholdSlowInput.Value = (decimal)settings.SpeedThresholdSlow;
if (_ratioChangeCoefficientInput != null) _ratioChangeCoefficientInput.Value = (decimal)settings.RatioChangeCoefficient;
if (_enablePalmEraserInput != null) _enablePalmEraserInput.IsChecked = settings.EnablePalmEraser;
if (_palmEraserThresholdInput != null) _palmEraserThresholdInput.Value = (decimal)settings.PalmEraserThreshold;
}
private void LoadThemeSettings()
{
var config = Config.Load();
var theme = config.Theme ?? "Dark";
if (_themeComboBox != null)
{
for (int i = 0; i < _themeComboBox.ItemCount; i++)
{
var item = _themeComboBox.Items[i] as ComboBoxItem;
if (item?.Tag?.ToString() == theme)
{
_themeComboBox.SelectedIndex = i;
break;
}
}
}
}
private void SavePenSettings_Click(object? sender, RoutedEventArgs e)
{
var config = Config.Load();
config.PenSettings ??= new PenSettings();
if (_denominatorInput != null) config.PenSettings.Denominator = (int)_denominatorInput.Value;
if (_ratioMinInput != null) config.PenSettings.RatioMin = (float)_ratioMinInput.Value;
if (_ratioMaxInput != null) config.PenSettings.RatioMax = (float)_ratioMaxInput.Value;
if (_speedThresholdFastInput != null) config.PenSettings.SpeedThresholdFast = (float)_speedThresholdFastInput.Value;
if (_speedThresholdSlowInput != null) config.PenSettings.SpeedThresholdSlow = (float)_speedThresholdSlowInput.Value;
if (_ratioChangeCoefficientInput != null) config.PenSettings.RatioChangeCoefficient = (float)_ratioChangeCoefficientInput.Value;
if (_enablePalmEraserInput != null) config.PenSettings.EnablePalmEraser = _enablePalmEraserInput.IsChecked ?? true;
if (_palmEraserThresholdInput != null) config.PenSettings.PalmEraserThreshold = (double)_palmEraserThresholdInput.Value;
if (_themeComboBox != null)
{
var selectedItem = _themeComboBox.SelectedItem as ComboBoxItem;
if (selectedItem != null)
{
config.Theme = selectedItem.Tag?.ToString() ?? "Dark";
ThemeType themeType = config.Theme switch
{
"Light" => ThemeType.Light,
"LightMinimal" => ThemeType.LightMinimal,
"NoBackground" => ThemeType.NoBackground,
_ => ThemeType.Dark
};
ThemeManager.SetTheme(themeType);
if (Avalonia.Application.Current != null)
{
ThemeManager.ApplyTheme(Avalonia.Application.Current, themeType);
}
}
}
config.Save();
Close();
}
private void ResetPenSettings_Click(object? sender, RoutedEventArgs e)
{
var defaultSettings = new PenSettings();
if (_denominatorInput != null) _denominatorInput.Value = defaultSettings.Denominator;
if (_ratioMinInput != null) _ratioMinInput.Value = (decimal)defaultSettings.RatioMin;
if (_ratioMaxInput != null) _ratioMaxInput.Value = (decimal)defaultSettings.RatioMax;
if (_speedThresholdFastInput != null) _speedThresholdFastInput.Value = (decimal)defaultSettings.SpeedThresholdFast;
if (_speedThresholdSlowInput != null) _speedThresholdSlowInput.Value = (decimal)defaultSettings.SpeedThresholdSlow;
if (_ratioChangeCoefficientInput != null) _ratioChangeCoefficientInput.Value = (decimal)defaultSettings.RatioChangeCoefficient;
if (_enablePalmEraserInput != null) _enablePalmEraserInput.IsChecked = defaultSettings.EnablePalmEraser;
if (_palmEraserThresholdInput != null) _palmEraserThresholdInput.Value = (decimal)defaultSettings.PalmEraserThreshold;
}
private void LoadSystemInfo()
{
try
{
var searcher = new ManagementObjectSearcher("SELECT SerialNumber FROM Win32_BaseBoard");
foreach (ManagementObject obj in searcher.Get())
{
var serial = obj["SerialNumber"]?.ToString();
if (!string.IsNullOrWhiteSpace(serial))
{
if (_motherboardSerialText != null)
_motherboardSerialText.Text = serial;
break;
}
}
if (_motherboardSerialText != null && _motherboardSerialText.Text == "正在获取...")
{
_motherboardSerialText.Text = "未获取到";
}
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine($"[SettingsWindow] 获取主板序列号失败: {ex.Message}");
if (_motherboardSerialText != null)
_motherboardSerialText.Text = "获取失败";
}
var uuid = LicenseManager.Instance.CurrentUuid;
if (_uuidText != null)
{
if (!string.IsNullOrEmpty(uuid))
{
_uuidText.Text = uuid;
}
else
{
var motherboardSerial = LicenseManager.Instance.MotherboardSerial;
if (!string.IsNullOrEmpty(motherboardSerial))
{
_uuidText.Text = "正在向服务器注册...";
_ = TryGetUuidAsync();
}
else
{
_uuidText.Text = "未注册 (无法获取主板序列号)";
}
}
}
}
private async System.Threading.Tasks.Task TryGetUuidAsync()
{
try
{
var uuid = await LicenseManager.Instance.GetOrCreateLicenseAsync();
if (_uuidText != null)
{
await Avalonia.Threading.Dispatcher.UIThread.InvokeAsync(() =>
{
_uuidText.Text = uuid;
});
}
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine($"[SettingsWindow] 获取UUID失败: {ex.Message}");
if (_uuidText != null)
{
await Avalonia.Threading.Dispatcher.UIThread.InvokeAsync(() =>
{
_uuidText.Text = $"获取失败: {ex.Message}";
});
}
}
}
}
}