Repository navigation
Expand file tree
/
Copy pathSettingsWindow.cs
More file actions
635 lines (570 loc) · 24.3 KB
/
Copy pathSettingsWindow.cs
File metadata and controls
635 lines (570 loc) · 24.3 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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
using System;
using System.Globalization;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
namespace DesktopNest
{
/// <summary>
/// On-demand settings window. API keys are written directly to Windows
/// Credential Manager through SecretStore and are never copied into AppSettings.
/// </summary>
public sealed class SettingsWindow : Window
{
private const string OpenAiProvider = "openai";
private const string DeepSeekProvider = "deepseek";
private const string OpenAiDefaultModel = "gpt-5.6-luna";
private const string DeepSeekDefaultModel = "deepseek-v4-flash";
private readonly AppSettings _settings;
private CheckBox _startupCheckBox;
private CheckBox _takeoverCheckBox;
private CheckBox _aiCheckBox;
private Slider _opacitySlider;
private TextBlock _opacityValue;
private ComboBox _providerComboBox;
private TextBox _modelTextBox;
private PasswordBox _apiKeyPasswordBox;
private TextBlock _keyStatusText;
private Button _saveKeyButton;
private Button _deleteKeyButton;
private bool _initializing;
private string _previousProvider;
private bool _openAiHasKey;
private bool _deepSeekHasKey;
public SettingsWindow(AppSettings settings)
{
if (settings == null)
{
throw new ArgumentNullException("settings");
}
_settings = settings;
_settings.EnsureDefaults();
Title = "拾光桌面设置";
Width = 540;
Height = 650;
MinWidth = 500;
MinHeight = 590;
WindowStartupLocation = WindowStartupLocation.CenterScreen;
WindowStyle = WindowStyle.ToolWindow;
ResizeMode = ResizeMode.CanResize;
ShowInTaskbar = false;
Background = CreateBrush(Color.FromRgb(19, 27, 39));
Foreground = CreateBrush(Color.FromRgb(232, 238, 247));
FontFamily = new FontFamily("Microsoft YaHei UI");
SnapsToDevicePixels = true;
UseLayoutRounding = true;
Content = BuildContent();
LoadValues();
}
public event EventHandler SettingsApplied;
private UIElement BuildContent()
{
Grid page = new Grid { Margin = new Thickness(24, 20, 24, 18) };
page.RowDefinitions.Add(new RowDefinition { Height = new GridLength(1, GridUnitType.Star) });
page.RowDefinitions.Add(new RowDefinition { Height = GridLength.Auto });
ScrollViewer scrollViewer = new ScrollViewer
{
VerticalScrollBarVisibility = ScrollBarVisibility.Auto,
HorizontalScrollBarVisibility = ScrollBarVisibility.Disabled
};
Grid.SetRow(scrollViewer, 0);
page.Children.Add(scrollViewer);
StackPanel content = new StackPanel { Margin = new Thickness(0, 0, 8, 4) };
scrollViewer.Content = content;
TextBlock heading = new TextBlock
{
Text = "桌面整理设置",
FontSize = 23,
FontWeight = FontWeights.SemiBold,
Foreground = Brushes.White,
Margin = new Thickness(0, 0, 0, 3)
};
content.Children.Add(heading);
TextBlock introduction = new TextBlock
{
Text = "保持常驻逻辑轻量;只有桌面文件发生新增、重命名或减少时才触发分类。",
FontSize = 12,
Foreground = CreateBrush(Color.FromRgb(167, 180, 198)),
TextWrapping = TextWrapping.Wrap,
Margin = new Thickness(0, 0, 0, 18)
};
content.Children.Add(introduction);
content.Children.Add(CreateSectionTitle("常规"));
_startupCheckBox = CreateCheckBox("开机自动启动拾光桌面");
content.Children.Add(_startupCheckBox);
_takeoverCheckBox = CreateCheckBox("接管 Windows 桌面图标显示");
_takeoverCheckBox.ToolTip = "关闭软件或取消此选项后可恢复系统桌面图标;不会删除桌面文件。";
content.Children.Add(_takeoverCheckBox);
Grid opacityRow = new Grid { Margin = new Thickness(0, 8, 0, 18) };
opacityRow.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(116) });
opacityRow.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) });
opacityRow.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(46) });
content.Children.Add(opacityRow);
TextBlock opacityLabel = CreateLabel("收纳盒透明度");
opacityRow.Children.Add(opacityLabel);
_opacitySlider = new Slider
{
Minimum = 0.38,
Maximum = 0.92,
SmallChange = 0.02,
LargeChange = 0.1,
Margin = new Thickness(4, 0, 10, 0),
VerticalAlignment = VerticalAlignment.Center
};
_opacitySlider.ValueChanged += delegate
{
if (_opacityValue != null)
{
_opacityValue.Text = Math.Round(_opacitySlider.Value * 100.0).ToString(CultureInfo.InvariantCulture) + "%";
}
};
Grid.SetColumn(_opacitySlider, 1);
opacityRow.Children.Add(_opacitySlider);
_opacityValue = new TextBlock
{
Foreground = Brushes.White,
FontFamily = new FontFamily("Segoe UI"),
FontSize = 12,
TextAlignment = TextAlignment.Right,
VerticalAlignment = VerticalAlignment.Center
};
Grid.SetColumn(_opacityValue, 2);
opacityRow.Children.Add(_opacityValue);
content.Children.Add(CreateSectionTitle("AI 自动归类"));
_aiCheckBox = CreateCheckBox("启用 AI 辅助识别(仅在桌面内容变化时调用)");
_aiCheckBox.Checked += delegate { UpdateAiControlsEnabled(); };
_aiCheckBox.Unchecked += delegate { UpdateAiControlsEnabled(); };
content.Children.Add(_aiCheckBox);
Grid providerRow = CreateFormRow("提供方");
_providerComboBox = new ComboBox
{
Height = 30,
VerticalContentAlignment = VerticalAlignment.Center
};
_providerComboBox.Items.Add(new ProviderOption(OpenAiProvider, "OpenAI"));
_providerComboBox.Items.Add(new ProviderOption(DeepSeekProvider, "DeepSeek"));
_providerComboBox.SelectionChanged += OnProviderSelectionChanged;
Grid.SetColumn(_providerComboBox, 1);
providerRow.Children.Add(_providerComboBox);
content.Children.Add(providerRow);
Grid modelRow = CreateFormRow("模型");
_modelTextBox = new TextBox
{
Height = 30,
Padding = new Thickness(7, 4, 7, 4),
VerticalContentAlignment = VerticalAlignment.Center
};
Grid.SetColumn(_modelTextBox, 1);
modelRow.Children.Add(_modelTextBox);
content.Children.Add(modelRow);
TextBlock modelHint = new TextBlock
{
Text = "默认使用小模型完成文件名分类;可填写账户中可用的兼容模型。",
Foreground = CreateBrush(Color.FromRgb(151, 166, 186)),
FontSize = 11,
TextWrapping = TextWrapping.Wrap,
Margin = new Thickness(116, -2, 0, 9)
};
content.Children.Add(modelHint);
Grid keyRow = CreateFormRow("API 密钥");
keyRow.ColumnDefinitions[1].Width = new GridLength(1, GridUnitType.Star);
_apiKeyPasswordBox = new PasswordBox
{
Height = 30,
Padding = new Thickness(7, 4, 7, 4),
VerticalContentAlignment = VerticalAlignment.Center,
ToolTip = "密钥不会显示或写入 settings.xml"
};
_apiKeyPasswordBox.PasswordChanged += delegate { UpdateKeyButtons(); };
Grid.SetColumn(_apiKeyPasswordBox, 1);
keyRow.Children.Add(_apiKeyPasswordBox);
content.Children.Add(keyRow);
Grid keyActions = new Grid { Margin = new Thickness(116, 1, 0, 8) };
keyActions.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) });
keyActions.ColumnDefinitions.Add(new ColumnDefinition { Width = GridLength.Auto });
keyActions.ColumnDefinitions.Add(new ColumnDefinition { Width = GridLength.Auto });
content.Children.Add(keyActions);
_keyStatusText = new TextBlock
{
Foreground = CreateBrush(Color.FromRgb(154, 210, 190)),
FontSize = 11,
VerticalAlignment = VerticalAlignment.Center,
TextTrimming = TextTrimming.CharacterEllipsis
};
keyActions.Children.Add(_keyStatusText);
_saveKeyButton = CreateActionButton("保存密钥", true);
_saveKeyButton.Margin = new Thickness(8, 0, 0, 0);
_saveKeyButton.Click += SaveKey;
Grid.SetColumn(_saveKeyButton, 1);
keyActions.Children.Add(_saveKeyButton);
_deleteKeyButton = CreateActionButton("删除", false);
_deleteKeyButton.Margin = new Thickness(7, 0, 0, 0);
_deleteKeyButton.Click += DeleteKey;
Grid.SetColumn(_deleteKeyButton, 2);
keyActions.Children.Add(_deleteKeyButton);
Border privacy = new Border
{
Background = CreateBrush(Color.FromArgb(105, 39, 54, 73)),
BorderBrush = CreateBrush(Color.FromArgb(80, 126, 150, 180)),
BorderThickness = new Thickness(1),
CornerRadius = new CornerRadius(7),
Padding = new Thickness(11, 9, 11, 9),
Margin = new Thickness(0, 7, 0, 10)
};
privacy.Child = new TextBlock
{
Text = "隐私说明:AI 分类仅上传文件名和扩展名,不读取或上传文件内容。密钥保存在 Windows 凭据管理器中;也可使用 OPENAI_API_KEY 或 DEEPSEEK_API_KEY 环境变量。",
Foreground = CreateBrush(Color.FromRgb(190, 203, 220)),
FontSize = 11,
LineHeight = 18,
TextWrapping = TextWrapping.Wrap
};
content.Children.Add(privacy);
Grid footer = new Grid { Margin = new Thickness(0, 14, 8, 0) };
footer.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) });
footer.ColumnDefinitions.Add(new ColumnDefinition { Width = GridLength.Auto });
footer.ColumnDefinitions.Add(new ColumnDefinition { Width = GridLength.Auto });
Grid.SetRow(footer, 1);
page.Children.Add(footer);
Button cancelButton = CreateFooterButton("取消", false);
cancelButton.Click += delegate { CloseWithResult(false); };
Grid.SetColumn(cancelButton, 1);
footer.Children.Add(cancelButton);
Button applyButton = CreateFooterButton("保存设置", true);
applyButton.Margin = new Thickness(9, 0, 0, 0);
applyButton.Click += ApplySettings;
Grid.SetColumn(applyButton, 2);
footer.Children.Add(applyButton);
return page;
}
private void LoadValues()
{
_openAiHasKey = TryHasKey(OpenAiProvider);
_deepSeekHasKey = TryHasKey(DeepSeekProvider);
_initializing = true;
try
{
_startupCheckBox.IsChecked = _settings.StartWithWindows;
_takeoverCheckBox.IsChecked = _settings.TakeOverDesktopIcons;
_opacitySlider.Value = Math.Max(_opacitySlider.Minimum,
Math.Min(_opacitySlider.Maximum, _settings.BoxOpacity));
_opacityValue.Text = Math.Round(_opacitySlider.Value * 100.0).ToString(CultureInfo.InvariantCulture) + "%";
_aiCheckBox.IsChecked = _settings.AiEnabled;
string provider = NormalizeProvider(_settings.AiProvider);
_providerComboBox.SelectedIndex = provider == DeepSeekProvider ? 1 : 0;
_previousProvider = provider;
_modelTextBox.Text = String.IsNullOrWhiteSpace(_settings.AiModel)
? GetDefaultModel(provider)
: _settings.AiModel.Trim();
}
finally
{
_initializing = false;
}
UpdateAiControlsEnabled();
UpdateKeyStatus();
UpdateKeyButtons();
}
private void OnProviderSelectionChanged(object sender, SelectionChangedEventArgs e)
{
string provider = GetSelectedProvider();
if (!_initializing)
{
string currentModel = _modelTextBox.Text == null ? String.Empty : _modelTextBox.Text.Trim();
if (currentModel.Length == 0 ||
String.Equals(currentModel, GetDefaultModel(_previousProvider), StringComparison.OrdinalIgnoreCase))
{
_modelTextBox.Text = GetDefaultModel(provider);
}
}
_previousProvider = provider;
if (_keyStatusText != null)
{
UpdateKeyStatus();
UpdateKeyButtons();
}
}
private void UpdateAiControlsEnabled()
{
bool enabled = _aiCheckBox != null && _aiCheckBox.IsChecked == true;
if (_providerComboBox != null)
{
_providerComboBox.IsEnabled = enabled;
_modelTextBox.IsEnabled = enabled;
_apiKeyPasswordBox.IsEnabled = enabled;
UpdateKeyButtons();
}
}
private void UpdateKeyButtons()
{
if (_saveKeyButton == null || _providerComboBox == null)
{
return;
}
bool aiEnabled = _aiCheckBox != null && _aiCheckBox.IsChecked == true;
_saveKeyButton.IsEnabled = aiEnabled &&
_apiKeyPasswordBox != null &&
!String.IsNullOrWhiteSpace(_apiKeyPasswordBox.Password);
_deleteKeyButton.IsEnabled = aiEnabled && GetCachedHasKey(GetSelectedProvider());
}
private void UpdateKeyStatus()
{
if (_keyStatusText == null || _providerComboBox == null)
{
return;
}
bool hasKey = GetCachedHasKey(GetSelectedProvider());
_keyStatusText.Text = hasKey ? "已配置密钥" : "尚未配置";
_keyStatusText.Foreground = hasKey
? CreateBrush(Color.FromRgb(133, 214, 181))
: CreateBrush(Color.FromRgb(173, 183, 198));
}
private void SaveKey(object sender, RoutedEventArgs e)
{
string key = _apiKeyPasswordBox.Password == null
? String.Empty
: _apiKeyPasswordBox.Password.Trim();
if (key.Length == 0)
{
return;
}
try
{
SecretStore.SaveKey(GetSelectedProvider(), key);
SetCachedHasKey(GetSelectedProvider(), true);
_apiKeyPasswordBox.Clear();
UpdateKeyStatus();
UpdateKeyButtons();
MessageBox.Show(this, "API 密钥已安全保存到 Windows 凭据管理器。",
"拾光桌面", MessageBoxButton.OK, MessageBoxImage.Information);
}
catch (Exception ex)
{
MessageBox.Show(this, "无法保存 API 密钥:\n" + ex.Message,
"拾光桌面", MessageBoxButton.OK, MessageBoxImage.Warning);
}
}
private void DeleteKey(object sender, RoutedEventArgs e)
{
MessageBoxResult result = MessageBox.Show(this,
"删除当前提供方保存在 Windows 凭据管理器中的密钥?",
"拾光桌面", MessageBoxButton.YesNo, MessageBoxImage.Question);
if (result != MessageBoxResult.Yes)
{
return;
}
try
{
SecretStore.DeleteKey(GetSelectedProvider());
// An environment variable takes precedence over the credential
// just deleted, so re-check rather than assuming no key exists.
SetCachedHasKey(GetSelectedProvider(), TryHasKey(GetSelectedProvider()));
_apiKeyPasswordBox.Clear();
UpdateKeyStatus();
UpdateKeyButtons();
}
catch (Exception ex)
{
MessageBox.Show(this, "无法删除 API 密钥:\n" + ex.Message,
"拾光桌面", MessageBoxButton.OK, MessageBoxImage.Warning);
}
}
private void ApplySettings(object sender, RoutedEventArgs e)
{
string provider = GetSelectedProvider();
string model = _modelTextBox.Text == null ? String.Empty : _modelTextBox.Text.Trim();
if (model.Length == 0)
{
model = GetDefaultModel(provider);
}
if (!AppSettings.IsValidModelName(model))
{
MessageBox.Show(this, "模型名称不能超过 128 个字符,也不能包含控制字符。",
"拾光桌面", MessageBoxButton.OK, MessageBoxImage.Warning);
_modelTextBox.Focus();
return;
}
string pendingKey = _apiKeyPasswordBox.Password == null
? String.Empty
: _apiKeyPasswordBox.Password.Trim();
if (pendingKey.Length > 0)
{
try
{
SecretStore.SaveKey(provider, pendingKey);
SetCachedHasKey(provider, true);
_apiKeyPasswordBox.Clear();
}
catch (Exception ex)
{
MessageBox.Show(this, "无法保存 API 密钥:\n" + ex.Message,
"拾光桌面", MessageBoxButton.OK, MessageBoxImage.Warning);
return;
}
}
_settings.StartWithWindows = _startupCheckBox.IsChecked == true;
_settings.TakeOverDesktopIcons = _takeoverCheckBox.IsChecked == true;
_settings.BoxOpacity = Math.Max(0.38, Math.Min(0.92, _opacitySlider.Value));
_settings.AiEnabled = _aiCheckBox.IsChecked == true;
_settings.AiProvider = provider;
_settings.AiModel = model;
_settings.EnsureDefaults();
EventHandler handler = SettingsApplied;
if (handler != null)
{
handler(this, EventArgs.Empty);
}
CloseWithResult(true);
}
private void CloseWithResult(bool result)
{
try
{
DialogResult = result;
}
catch (InvalidOperationException)
{
Close();
}
}
private string GetSelectedProvider()
{
ProviderOption option = _providerComboBox == null
? null
: _providerComboBox.SelectedItem as ProviderOption;
return option == null ? OpenAiProvider : option.Id;
}
private static string NormalizeProvider(string provider)
{
return String.Equals(provider, DeepSeekProvider, StringComparison.OrdinalIgnoreCase)
? DeepSeekProvider
: OpenAiProvider;
}
private static string GetDefaultModel(string provider)
{
return String.Equals(provider, DeepSeekProvider, StringComparison.OrdinalIgnoreCase)
? DeepSeekDefaultModel
: OpenAiDefaultModel;
}
private bool GetCachedHasKey(string provider)
{
return String.Equals(provider, DeepSeekProvider, StringComparison.OrdinalIgnoreCase)
? _deepSeekHasKey
: _openAiHasKey;
}
private void SetCachedHasKey(string provider, bool value)
{
if (String.Equals(provider, DeepSeekProvider, StringComparison.OrdinalIgnoreCase))
{
_deepSeekHasKey = value;
}
else
{
_openAiHasKey = value;
}
}
private static bool TryHasKey(string provider)
{
try
{
return SecretStore.HasKey(provider);
}
catch
{
return false;
}
}
private static TextBlock CreateSectionTitle(string text)
{
return new TextBlock
{
Text = text,
Foreground = CreateBrush(Color.FromRgb(115, 220, 202)),
FontSize = 12,
FontWeight = FontWeights.SemiBold,
Margin = new Thickness(0, 0, 0, 8)
};
}
private static CheckBox CreateCheckBox(string text)
{
return new CheckBox
{
Content = text,
Foreground = CreateBrush(Color.FromRgb(224, 232, 242)),
FontSize = 12,
Margin = new Thickness(0, 3, 0, 8),
VerticalContentAlignment = VerticalAlignment.Center
};
}
private static Grid CreateFormRow(string label)
{
Grid row = new Grid { Margin = new Thickness(0, 4, 0, 7) };
row.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(116) });
row.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) });
row.Children.Add(CreateLabel(label));
return row;
}
private static TextBlock CreateLabel(string text)
{
return new TextBlock
{
Text = text,
Foreground = CreateBrush(Color.FromRgb(204, 214, 228)),
FontSize = 12,
VerticalAlignment = VerticalAlignment.Center
};
}
private static Button CreateActionButton(string text, bool accent)
{
return new Button
{
Content = text,
Height = 27,
MinWidth = 58,
Padding = new Thickness(9, 2, 9, 2),
BorderThickness = new Thickness(1),
BorderBrush = accent
? CreateBrush(Color.FromRgb(80, 206, 188))
: CreateBrush(Color.FromRgb(92, 109, 132)),
Background = accent
? CreateBrush(Color.FromRgb(39, 143, 132))
: CreateBrush(Color.FromRgb(39, 50, 66)),
Foreground = Brushes.White,
FontSize = 11,
Cursor = Cursors.Hand
};
}
private static Button CreateFooterButton(string text, bool accent)
{
Button button = CreateActionButton(text, accent);
button.Height = 34;
button.MinWidth = accent ? 92 : 74;
button.FontSize = 12;
return button;
}
private static SolidColorBrush CreateBrush(Color color)
{
SolidColorBrush brush = new SolidColorBrush(color);
if (brush.CanFreeze)
{
brush.Freeze();
}
return brush;
}
private sealed class ProviderOption
{
public ProviderOption(string id, string name)
{
Id = id;
Name = name;
}
public string Id { get; private set; }
public string Name { get; private set; }
public override string ToString()
{
return Name;
}
}
}
}