-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathSettingsWindow.xaml.cs
More file actions
794 lines (701 loc) · 34.4 KB
/
SettingsWindow.xaml.cs
File metadata and controls
794 lines (701 loc) · 34.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
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
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using Microsoft.Win32;
namespace TrayChrome
{
public partial class SettingsWindow : Window
{
private AppSettings originalSettings;
private AppSettings currentSettings;
private MainWindow? mainWindow;
private App? app;
private List<Bookmark> bookmarks = new List<Bookmark>();
private string bookmarksFilePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "bookmarks.json");
// 收藏夹更新事件
public event EventHandler? BookmarksUpdated;
// 标记设置是否已保存(用于非模态窗口)
public bool SettingsSaved { get; private set; } = false;
// 用于数据绑定的属性
public double WindowWidth { get; set; }
public double WindowHeight { get; set; }
public bool IsTopMost { get; set; }
public bool IsSuperMinimalMode { get; set; }
public bool IsAnimationEnabled { get; set; }
public bool IsDarkMode { get; set; }
public double ZoomFactor { get; set; }
public bool IsMobileUA { get; set; }
public bool IsAdBlockEnabled { get; set; }
public string AdBlockRulesText { get; set; } = string.Empty;
public string AdAllowRulesText { get; set; } = string.Empty;
public bool EnableGlobalHotKey { get; set; }
public string Hotkey { get; set; } = string.Empty;
public string SelectedIconType { get; set; } = "default";
public bool IsProxyEnabled { get; set; }
public string ProxyServer { get; set; } = string.Empty;
public bool AutoZoomOutOnStartup { get; set; }
public SettingsWindow(AppSettings settings, MainWindow? mainWindow = null, App? app = null)
{
InitializeComponent();
this.mainWindow = mainWindow;
this.app = app;
this.originalSettings = settings;
// 设置窗口所有者
if (mainWindow != null)
{
this.Owner = mainWindow;
}
// 创建当前设置的副本
currentSettings = new AppSettings
{
ZoomFactor = settings.ZoomFactor,
IsMobileUA = settings.IsMobileUA,
WindowWidth = settings.WindowWidth,
WindowHeight = settings.WindowHeight,
WindowLeft = settings.WindowLeft,
WindowTop = settings.WindowTop,
IsDarkMode = settings.IsDarkMode,
IsTopMost = settings.IsTopMost,
IsSuperMinimalMode = settings.IsSuperMinimalMode,
IsAnimationEnabled = settings.IsAnimationEnabled,
IsAdBlockEnabled = settings.IsAdBlockEnabled,
AdBlockRules = new List<string>(settings.AdBlockRules ?? new List<string>()),
AdAllowRules = new List<string>(settings.AdAllowRules ?? new List<string>()),
Hotkey = settings.Hotkey,
EnableGlobalHotKey = settings.EnableGlobalHotKey,
IsProxyEnabled = settings.IsProxyEnabled,
ProxyServer = settings.ProxyServer,
AutoZoomOutOnStartup = settings.AutoZoomOutOnStartup
};
// 加载当前设置到UI
LoadSettingsToUI();
// 设置数据绑定
SetupDataBinding();
// 加载当前图标设置
LoadIconSetting();
// 加载收藏夹
LoadBookmarks();
RefreshBookmarkList();
}
private void LoadSettingsToUI()
{
WindowWidth = currentSettings.WindowWidth;
WindowHeight = currentSettings.WindowHeight;
IsTopMost = currentSettings.IsTopMost;
IsSuperMinimalMode = currentSettings.IsSuperMinimalMode;
IsAnimationEnabled = currentSettings.IsAnimationEnabled;
IsDarkMode = currentSettings.IsDarkMode;
ZoomFactor = currentSettings.ZoomFactor;
IsMobileUA = currentSettings.IsMobileUA;
IsAdBlockEnabled = currentSettings.IsAdBlockEnabled;
AdBlockRulesText = string.Join("\r\n", currentSettings.AdBlockRules ?? new List<string>());
AdAllowRulesText = string.Join("\r\n", currentSettings.AdAllowRules ?? new List<string>());
EnableGlobalHotKey = currentSettings.EnableGlobalHotKey;
Hotkey = currentSettings.Hotkey;
IsProxyEnabled = currentSettings.IsProxyEnabled;
ProxyServer = currentSettings.ProxyServer;
AutoZoomOutOnStartup = currentSettings.AutoZoomOutOnStartup;
}
private void SetupDataBinding()
{
// 窗口大小 - 使用事件处理
WidthTextBox.Text = WindowWidth.ToString();
HeightTextBox.Text = WindowHeight.ToString();
WidthTextBox.TextChanged += (s, e) => { if (double.TryParse(WidthTextBox.Text, out double w)) WindowWidth = w; };
HeightTextBox.TextChanged += (s, e) => { if (double.TryParse(HeightTextBox.Text, out double h)) WindowHeight = h; };
// 窗口行为
TopMostCheckBox.IsChecked = IsTopMost;
TopMostCheckBox.Checked += (s, e) => IsTopMost = true;
TopMostCheckBox.Unchecked += (s, e) => IsTopMost = false;
SuperMinimalModeCheckBox.IsChecked = IsSuperMinimalMode;
SuperMinimalModeCheckBox.Checked += (s, e) => IsSuperMinimalMode = true;
SuperMinimalModeCheckBox.Unchecked += (s, e) => IsSuperMinimalMode = false;
AnimationCheckBox.IsChecked = IsAnimationEnabled;
AnimationCheckBox.Checked += (s, e) => IsAnimationEnabled = true;
AnimationCheckBox.Unchecked += (s, e) => IsAnimationEnabled = false;
// 外观
DarkModeCheckBox.IsChecked = IsDarkMode;
DarkModeCheckBox.Checked += (s, e) => IsDarkMode = true;
DarkModeCheckBox.Unchecked += (s, e) => IsDarkMode = false;
// 缩放
ZoomSlider.Value = ZoomFactor;
ZoomValueLabel.Content = $"{ZoomFactor:F1}x";
ZoomSlider.ValueChanged += (s, e) =>
{
ZoomFactor = ZoomSlider.Value;
ZoomValueLabel.Content = $"{ZoomFactor:F1}x";
};
// UA - 使用事件处理
if (IsMobileUA)
{
MobileUARadio.IsChecked = true;
DesktopUARadio.IsChecked = false;
}
else
{
MobileUARadio.IsChecked = false;
DesktopUARadio.IsChecked = true;
}
MobileUARadio.Checked += (s, e) => IsMobileUA = true;
DesktopUARadio.Checked += (s, e) => IsMobileUA = false;
// 广告拦截
AdBlockEnabledCheckBox.IsChecked = IsAdBlockEnabled;
AdBlockEnabledCheckBox.Checked += (s, e) => IsAdBlockEnabled = true;
AdBlockEnabledCheckBox.Unchecked += (s, e) => IsAdBlockEnabled = false;
AdBlockRulesTextBox.Text = AdBlockRulesText;
AdBlockRulesTextBox.TextChanged += (s, e) => AdBlockRulesText = AdBlockRulesTextBox.Text;
AdAllowRulesTextBox.Text = AdAllowRulesText;
AdAllowRulesTextBox.TextChanged += (s, e) => AdAllowRulesText = AdAllowRulesTextBox.Text;
// 快捷键
EnableHotKeyCheckBox.IsChecked = EnableGlobalHotKey;
EnableHotKeyCheckBox.Checked += (s, e) => EnableGlobalHotKey = true;
EnableHotKeyCheckBox.Unchecked += (s, e) => EnableGlobalHotKey = false;
HotKeyTextBox.Text = Hotkey;
HotKeyTextBox.TextChanged += (s, e) => Hotkey = HotKeyTextBox.Text;
// 代理
ProxyEnabledCheckBox.IsChecked = IsProxyEnabled;
ProxyEnabledCheckBox.Checked += (s, e) => IsProxyEnabled = true;
ProxyEnabledCheckBox.Unchecked += (s, e) => IsProxyEnabled = false;
ProxyServerTextBox.Text = ProxyServer;
ProxyServerTextBox.TextChanged += (s, e) => ProxyServer = ProxyServerTextBox.Text;
// 启动缩放
AutoZoomOutCheckBox.IsChecked = AutoZoomOutOnStartup;
AutoZoomOutCheckBox.Checked += (s, e) => AutoZoomOutOnStartup = true;
AutoZoomOutCheckBox.Unchecked += (s, e) => AutoZoomOutOnStartup = false;
}
private void LoadIconSetting()
{
try
{
string settingsFile = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "icon-settings.json");
if (File.Exists(settingsFile))
{
string json = File.ReadAllText(settingsFile);
var settings = System.Text.Json.JsonSerializer.Deserialize<System.Text.Json.JsonElement>(json);
if (settings.TryGetProperty("IconType", out var iconTypeElement))
{
SelectedIconType = iconTypeElement.GetString() ?? "default";
}
}
}
catch { }
// 设置单选按钮状态
switch (SelectedIconType)
{
case "default":
DefaultIconRadio.IsChecked = true;
break;
case "dingding":
DingDingIconRadio.IsChecked = true;
break;
case "feishu":
FeishuIconRadio.IsChecked = true;
break;
case "wecom":
WecomIconRadio.IsChecked = true;
break;
case "weixin":
WeixinIconRadio.IsChecked = true;
break;
default:
DefaultIconRadio.IsChecked = true;
break;
}
}
private void CustomIconButton_Click(object sender, RoutedEventArgs e)
{
try
{
var openFileDialog = new OpenFileDialog();
openFileDialog.Title = "选择图标文件";
openFileDialog.Filter = "图标文件 (*.ico)|*.ico|PNG图片 (*.png)|*.png|所有文件 (*.*)|*.*";
openFileDialog.FilterIndex = 1;
openFileDialog.CheckFileExists = true;
if (openFileDialog.ShowDialog() == true && !string.IsNullOrEmpty(openFileDialog.FileName))
{
string selectedPath = openFileDialog.FileName;
if (!File.Exists(selectedPath))
{
MessageBox.Show("选择的文件不存在!", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
return;
}
var fileInfo = new FileInfo(selectedPath);
if (fileInfo.Length > 5 * 1024 * 1024)
{
MessageBox.Show("图标文件过大,请选择小于5MB的文件!", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
return;
}
// 复制图标到应用程序目录
string customIconsDir = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Resources", "custom-icons");
Directory.CreateDirectory(customIconsDir);
string fileName = $"custom_{DateTime.Now:yyyyMMddHHmmss}{Path.GetExtension(selectedPath)}";
string targetPath = Path.Combine(customIconsDir, fileName);
File.Copy(selectedPath, targetPath, true);
SelectedIconType = "custom";
SaveIconSetting("custom", targetPath);
// 立即应用图标
if (app != null)
{
app.SetApplicationIcon(targetPath);
}
MessageBox.Show("自定义图标设置成功!", "成功", MessageBoxButton.OK, MessageBoxImage.Information);
}
}
catch (Exception ex)
{
MessageBox.Show($"选择自定义图标失败: {ex.Message}", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
private void SaveIconSetting(string iconType, string iconPath)
{
try
{
string settingsFile = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "icon-settings.json");
var settings = new
{
IconType = iconType,
IconPath = iconPath,
LastUpdated = DateTime.Now
};
string json = System.Text.Json.JsonSerializer.Serialize(settings, new System.Text.Json.JsonSerializerOptions { WriteIndented = true });
File.WriteAllText(settingsFile, json);
}
catch { }
}
private void LoadDefaultRulesButton_Click(object sender, RoutedEventArgs e)
{
try
{
var adBlocker = new AdBlocker();
adBlocker.LoadDefaultRules();
AdBlockRulesText = string.Join("\r\n", adBlocker.BlockRules);
}
catch (Exception ex)
{
MessageBox.Show($"加载默认规则失败: {ex.Message}", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
private void OkButton_Click(object sender, RoutedEventArgs e)
{
try
{
// 验证窗口大小
if (!double.TryParse(WidthTextBox.Text, out double width) || width < 200 || width > 3840)
{
MessageBox.Show("窗口宽度必须在 200-3840 之间!", "错误", MessageBoxButton.OK, MessageBoxImage.Warning);
WidthTextBox.Focus();
return;
}
if (!double.TryParse(HeightTextBox.Text, out double height) || height < 150 || height > 2160)
{
MessageBox.Show("窗口高度必须在 150-2160 之间!", "错误", MessageBoxButton.OK, MessageBoxImage.Warning);
HeightTextBox.Focus();
return;
}
// 更新设置对象
currentSettings.WindowWidth = width;
currentSettings.WindowHeight = height;
currentSettings.IsTopMost = IsTopMost;
currentSettings.IsSuperMinimalMode = IsSuperMinimalMode;
currentSettings.IsAnimationEnabled = IsAnimationEnabled;
currentSettings.IsDarkMode = IsDarkMode;
currentSettings.ZoomFactor = ZoomFactor;
currentSettings.IsMobileUA = IsMobileUA;
currentSettings.IsAdBlockEnabled = IsAdBlockEnabled;
currentSettings.AdBlockRules = AdBlockRulesText.Split(new[] { "\r\n", "\r", "\n" }, StringSplitOptions.RemoveEmptyEntries)
.Select(r => r.Trim())
.Where(r => !string.IsNullOrEmpty(r))
.ToList();
currentSettings.AdAllowRules = AdAllowRulesText.Split(new[] { "\r\n", "\r", "\n" }, StringSplitOptions.RemoveEmptyEntries)
.Select(r => r.Trim())
.Where(r => !string.IsNullOrEmpty(r))
.ToList();
currentSettings.EnableGlobalHotKey = EnableGlobalHotKey;
currentSettings.Hotkey = Hotkey;
currentSettings.IsProxyEnabled = IsProxyEnabled;
currentSettings.ProxyServer = ProxyServer;
currentSettings.AutoZoomOutOnStartup = AutoZoomOutOnStartup;
// 应用设置到主窗口
if (mainWindow != null)
{
mainWindow.ApplySettings(currentSettings);
}
// 应用图标设置
string iconType = "default";
string iconPath = "pack://application:,,,/Resources/Ampeross-Ampola-Chrome.ico";
if (DingDingIconRadio.IsChecked == true)
{
iconType = "dingding";
iconPath = "pack://application:,,,/Resources/alternative-icons/dingding.ico";
}
else if (FeishuIconRadio.IsChecked == true)
{
iconType = "feishu";
iconPath = "pack://application:,,,/Resources/alternative-icons/feishu.ico";
}
else if (WecomIconRadio.IsChecked == true)
{
iconType = "wecom";
iconPath = "pack://application:,,,/Resources/alternative-icons/wecom.ico";
}
else if (WeixinIconRadio.IsChecked == true)
{
iconType = "weixin";
iconPath = "pack://application:,,,/Resources/alternative-icons/weixin.ico";
}
if (app != null)
{
app.SetApplicationIcon(iconPath);
app.SaveIconSetting(iconType, iconPath);
}
// 保存设置到文件
SaveSettingsToFile();
// 更新原始设置对象(用于同步)
CopySettings(originalSettings, currentSettings);
// 标记设置已保存(非模态窗口不能设置DialogResult)
SettingsSaved = true;
Close();
}
catch (Exception ex)
{
MessageBox.Show($"保存设置失败: {ex.Message}", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
private void CancelButton_Click(object sender, RoutedEventArgs e)
{
// 非模态窗口不需要设置DialogResult
Close();
}
private void SaveSettingsToFile()
{
try
{
string settingsFilePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "settings.json");
string directory = Path.GetDirectoryName(settingsFilePath);
if (!string.IsNullOrEmpty(directory) && !Directory.Exists(directory))
{
Directory.CreateDirectory(directory);
}
var options = new System.Text.Json.JsonSerializerOptions
{
WriteIndented = true,
Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping
};
string json = System.Text.Json.JsonSerializer.Serialize(currentSettings, options);
File.WriteAllText(settingsFilePath, json);
}
catch (Exception ex)
{
MessageBox.Show($"保存设置文件失败: {ex.Message}", "错误", MessageBoxButton.OK, MessageBoxImage.Warning);
}
}
private void CopySettings(AppSettings target, AppSettings source)
{
target.ZoomFactor = source.ZoomFactor;
target.IsMobileUA = source.IsMobileUA;
target.WindowWidth = source.WindowWidth;
target.WindowHeight = source.WindowHeight;
target.WindowLeft = source.WindowLeft;
target.WindowTop = source.WindowTop;
target.IsDarkMode = source.IsDarkMode;
target.IsTopMost = source.IsTopMost;
target.IsSuperMinimalMode = source.IsSuperMinimalMode;
target.IsAnimationEnabled = source.IsAnimationEnabled;
target.IsAdBlockEnabled = source.IsAdBlockEnabled;
target.AdBlockRules = new List<string>(source.AdBlockRules ?? new List<string>());
target.AdAllowRules = new List<string>(source.AdAllowRules ?? new List<string>());
target.Hotkey = source.Hotkey;
target.EnableGlobalHotKey = source.EnableGlobalHotKey;
target.IsProxyEnabled = source.IsProxyEnabled;
target.ProxyServer = source.ProxyServer;
target.AutoZoomOutOnStartup = source.AutoZoomOutOnStartup;
}
// 收藏夹管理方法
private void LoadBookmarks()
{
try
{
Directory.CreateDirectory(Path.GetDirectoryName(bookmarksFilePath));
if (File.Exists(bookmarksFilePath))
{
var json = File.ReadAllText(bookmarksFilePath);
bookmarks = System.Text.Json.JsonSerializer.Deserialize<List<Bookmark>>(json) ?? new List<Bookmark>();
}
else
{
bookmarks = new List<Bookmark>();
}
}
catch (Exception ex)
{
MessageBox.Show($"加载收藏夹失败: {ex.Message}", "错误", MessageBoxButton.OK, MessageBoxImage.Warning);
bookmarks = new List<Bookmark>();
}
}
private void SaveBookmarks()
{
try
{
Directory.CreateDirectory(Path.GetDirectoryName(bookmarksFilePath));
var json = System.Text.Json.JsonSerializer.Serialize(bookmarks, new System.Text.Json.JsonSerializerOptions { WriteIndented = true });
File.WriteAllText(bookmarksFilePath, json);
// 同步到主窗口和托盘图标
if (app != null)
{
app.OnBookmarksUpdated(this, EventArgs.Empty);
}
else if (mainWindow != null)
{
mainWindow.LoadBookmarks();
BookmarksUpdated?.Invoke(this, EventArgs.Empty);
}
else
{
BookmarksUpdated?.Invoke(this, EventArgs.Empty);
}
}
catch (Exception ex)
{
MessageBox.Show($"保存收藏夹失败: {ex.Message}", "错误", MessageBoxButton.OK, MessageBoxImage.Warning);
}
}
private void RefreshBookmarkList()
{
BookmarkListBox.ItemsSource = null;
BookmarkListBox.ItemsSource = bookmarks;
BookmarkStatusText.Text = $"共 {bookmarks.Count} 个收藏";
}
private void AddBookmarkButton_Click(object sender, RoutedEventArgs e)
{
try
{
string currentUrl = mainWindow?.webView?.CoreWebView2?.Source ?? "";
string currentTitle = mainWindow?.webView?.CoreWebView2?.DocumentTitle ?? "未知页面";
if (string.IsNullOrEmpty(currentUrl))
{
// 如果没有当前URL,显示输入对话框
var dialog = new Window
{
Title = "添加收藏",
Width = 400,
Height = 200,
WindowStartupLocation = WindowStartupLocation.CenterOwner,
Owner = this,
ResizeMode = ResizeMode.NoResize
};
var grid = new Grid();
grid.RowDefinitions.Add(new RowDefinition { Height = GridLength.Auto });
grid.RowDefinitions.Add(new RowDefinition { Height = GridLength.Auto });
grid.RowDefinitions.Add(new RowDefinition { Height = GridLength.Auto });
grid.ColumnDefinitions.Add(new ColumnDefinition { Width = GridLength.Auto });
grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) });
var titleLabel = new Label { Content = "标题:", Margin = new Thickness(10, 10, 5, 5) };
Grid.SetRow(titleLabel, 0);
Grid.SetColumn(titleLabel, 0);
grid.Children.Add(titleLabel);
var titleTextBox = new TextBox { Margin = new Thickness(5, 10, 10, 5), VerticalAlignment = VerticalAlignment.Center };
Grid.SetRow(titleTextBox, 0);
Grid.SetColumn(titleTextBox, 1);
grid.Children.Add(titleTextBox);
var urlLabel = new Label { Content = "URL:", Margin = new Thickness(10, 5, 5, 5) };
Grid.SetRow(urlLabel, 1);
Grid.SetColumn(urlLabel, 0);
grid.Children.Add(urlLabel);
var urlTextBox = new TextBox { Margin = new Thickness(5, 5, 10, 5), VerticalAlignment = VerticalAlignment.Center };
Grid.SetRow(urlTextBox, 1);
Grid.SetColumn(urlTextBox, 1);
grid.Children.Add(urlTextBox);
var buttonPanel = new StackPanel { Orientation = Orientation.Horizontal, HorizontalAlignment = HorizontalAlignment.Right, Margin = new Thickness(10, 20, 10, 10) };
Grid.SetRow(buttonPanel, 2);
Grid.SetColumnSpan(buttonPanel, 2);
var okButton = new Button { Content = "确定", Width = 80, Height = 30, Margin = new Thickness(5, 0, 5, 0) };
okButton.Click += (s, args) =>
{
if (!string.IsNullOrWhiteSpace(titleTextBox.Text) && !string.IsNullOrWhiteSpace(urlTextBox.Text))
{
var newBookmark = new Bookmark
{
Title = titleTextBox.Text.Trim(),
Url = urlTextBox.Text.Trim()
};
if (!bookmarks.Any(b => b.Url.Equals(newBookmark.Url, StringComparison.OrdinalIgnoreCase)))
{
bookmarks.Add(newBookmark);
SaveBookmarks();
RefreshBookmarkList();
dialog.DialogResult = true;
dialog.Close();
}
else
{
MessageBox.Show("该URL已在收藏夹中!", "提示", MessageBoxButton.OK, MessageBoxImage.Information);
}
}
else
{
MessageBox.Show("标题和URL不能为空!", "错误", MessageBoxButton.OK, MessageBoxImage.Warning);
}
};
var cancelButton = new Button { Content = "取消", Width = 80, Height = 30, Margin = new Thickness(5, 0, 5, 0) };
cancelButton.Click += (s, args) => { dialog.DialogResult = false; dialog.Close(); };
buttonPanel.Children.Add(okButton);
buttonPanel.Children.Add(cancelButton);
grid.Children.Add(buttonPanel);
dialog.Content = grid;
dialog.ShowDialog();
}
else
{
// 使用当前页面URL
if (!bookmarks.Any(b => b.Url.Equals(currentUrl, StringComparison.OrdinalIgnoreCase)))
{
var newBookmark = new Bookmark
{
Title = currentTitle,
Url = currentUrl
};
bookmarks.Add(newBookmark);
SaveBookmarks();
RefreshBookmarkList();
MessageBox.Show($"已添加到收藏夹:{currentTitle}", "成功", MessageBoxButton.OK, MessageBoxImage.Information);
}
else
{
MessageBox.Show("该页面已在收藏夹中!", "提示", MessageBoxButton.OK, MessageBoxImage.Information);
}
}
}
catch (Exception ex)
{
MessageBox.Show($"添加收藏失败: {ex.Message}", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
private void EditBookmarkButton_Click(object sender, RoutedEventArgs e)
{
if (BookmarkListBox.SelectedItem is Bookmark bookmark)
{
EditBookmark(bookmark);
}
else
{
MessageBox.Show("请先选择一个收藏项!", "提示", MessageBoxButton.OK, MessageBoxImage.Information);
}
}
private void DeleteBookmarkButton_Click(object sender, RoutedEventArgs e)
{
if (BookmarkListBox.SelectedItem is Bookmark bookmark)
{
var result = MessageBox.Show($"确定要删除收藏夹 \"{bookmark.Title}\" 吗?",
"删除收藏夹", MessageBoxButton.YesNo, MessageBoxImage.Question);
if (result == MessageBoxResult.Yes)
{
bookmarks.Remove(bookmark);
SaveBookmarks();
RefreshBookmarkList();
}
}
else
{
MessageBox.Show("请先选择一个收藏项!", "提示", MessageBoxButton.OK, MessageBoxImage.Information);
}
}
private void RefreshBookmarkButton_Click(object sender, RoutedEventArgs e)
{
LoadBookmarks();
RefreshBookmarkList();
}
private void BookmarkListBox_MouseDoubleClick(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
if (BookmarkListBox.SelectedItem is Bookmark bookmark && mainWindow?.webView?.CoreWebView2 != null)
{
mainWindow.webView.CoreWebView2.Navigate(bookmark.Url);
if (!mainWindow.IsVisible)
{
mainWindow.Show();
mainWindow.WindowState = WindowState.Normal;
mainWindow.Activate();
}
}
}
private void EditBookmark(Bookmark bookmark)
{
var dialog = new Window
{
Title = "编辑收藏夹",
Width = 400,
Height = 200,
WindowStartupLocation = WindowStartupLocation.CenterOwner,
Owner = this,
ResizeMode = ResizeMode.NoResize
};
var grid = new Grid();
grid.RowDefinitions.Add(new RowDefinition { Height = GridLength.Auto });
grid.RowDefinitions.Add(new RowDefinition { Height = GridLength.Auto });
grid.RowDefinitions.Add(new RowDefinition { Height = GridLength.Auto });
grid.ColumnDefinitions.Add(new ColumnDefinition { Width = GridLength.Auto });
grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) });
var titleLabel = new Label { Content = "标题:", Margin = new Thickness(10, 10, 5, 5) };
Grid.SetRow(titleLabel, 0);
Grid.SetColumn(titleLabel, 0);
grid.Children.Add(titleLabel);
var titleTextBox = new TextBox { Text = bookmark.Title, Margin = new Thickness(5, 10, 10, 5), VerticalAlignment = VerticalAlignment.Center };
Grid.SetRow(titleTextBox, 0);
Grid.SetColumn(titleTextBox, 1);
grid.Children.Add(titleTextBox);
var urlLabel = new Label { Content = "URL:", Margin = new Thickness(10, 5, 5, 5) };
Grid.SetRow(urlLabel, 1);
Grid.SetColumn(urlLabel, 0);
grid.Children.Add(urlLabel);
var urlTextBox = new TextBox { Text = bookmark.Url, Margin = new Thickness(5, 5, 10, 5), VerticalAlignment = VerticalAlignment.Center };
Grid.SetRow(urlTextBox, 1);
Grid.SetColumn(urlTextBox, 1);
grid.Children.Add(urlTextBox);
var buttonPanel = new StackPanel { Orientation = Orientation.Horizontal, HorizontalAlignment = HorizontalAlignment.Right, Margin = new Thickness(10, 20, 10, 10) };
Grid.SetRow(buttonPanel, 2);
Grid.SetColumnSpan(buttonPanel, 2);
var okButton = new Button { Content = "确定", Width = 80, Height = 30, Margin = new Thickness(5, 0, 5, 0) };
okButton.Click += (s, args) =>
{
if (!string.IsNullOrWhiteSpace(titleTextBox.Text) && !string.IsNullOrWhiteSpace(urlTextBox.Text))
{
bookmark.Title = titleTextBox.Text.Trim();
bookmark.Url = urlTextBox.Text.Trim();
SaveBookmarks();
RefreshBookmarkList();
dialog.DialogResult = true;
dialog.Close();
}
else
{
MessageBox.Show("标题和URL不能为空!", "错误", MessageBoxButton.OK, MessageBoxImage.Warning);
}
};
var cancelButton = new Button { Content = "取消", Width = 80, Height = 30, Margin = new Thickness(5, 0, 5, 0) };
cancelButton.Click += (s, args) => { dialog.DialogResult = false; dialog.Close(); };
buttonPanel.Children.Add(okButton);
buttonPanel.Children.Add(cancelButton);
grid.Children.Add(buttonPanel);
dialog.Content = grid;
dialog.ShowDialog();
}
private void GitHubButton_Click(object sender, RoutedEventArgs e)
{
try
{
System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo
{
FileName = "https://github.com/cornradio/tray-chrome",
UseShellExecute = true
});
}
catch (Exception ex)
{
MessageBox.Show($"打开 GitHub 链接失败: {ex.Message}", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
}
}