From bd1c68d8e499a0d26f1a9f9049bcd2bcc3ab9f57 Mon Sep 17 00:00:00 2001 From: kennethcheng Date: Mon, 7 Sep 2026 00:50:44 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E8=AE=BE=E7=BD=AE=E5=8C=BA=E5=8A=A0?= =?UTF-8?q?=E5=BC=80=E6=9C=BA=E8=87=AA=E5=90=AF=E5=8A=A8=20CheckBox(?= =?UTF-8?q?=E9=BB=98=E8=AE=A4=E5=8B=BE=E9=80=89)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit StartupManager 早就实现了,但之前: 1. 没 UI 入口 —— 用户得去注册表手动改 2. App.OnStartup 里只 EnableCurrentUserStartup,不 Disable —— 用户禁用了下次启动会被强制写回,改不掉 现在: MainWindow.xaml 设置区(URL/Token 下方)加 CheckBox,跨两列: - 标签:"开机自动启动(写入 HKCU\Software\Microsoft\Windows\CurrentVersion\Run,只对当前用户生效)" - Tooltip 说明 - 初始状态从 config.Startup.Enabled 读 MainWindow.xaml.cs StartupCheckBox_Click: - 勾选变化 → 改 _config.Startup.Enabled → 调 EnableCurrentUserStartup 或 DisableCurrentUserStartup → 异步 SaveAsync - 状态条反馈 "已开启/关闭开机启动" - 异常时状态条变 ErrorDot 红色 App.xaml.cs OnStartup 双向同步: - config.Startup.Enabled=true → Enable - config.Startup.Enabled=false → Disable (之前是单向只 Enable,导致用户在 UI 禁用了重启又被写回) config 是 source of truth,UI 改了立刻落盘 + 同步注册表,下次启动再 sync 一次保证一致。 --- App.xaml.cs | 6 ++++++ MainWindow.xaml | 11 +++++++++++ MainWindow.xaml.cs | 30 ++++++++++++++++++++++++++++++ 3 files changed, 47 insertions(+) diff --git a/App.xaml.cs b/App.xaml.cs index f05e20f..4907409 100644 --- a/App.xaml.cs +++ b/App.xaml.cs @@ -79,10 +79,16 @@ public partial class App : Application _apiService, _appCache); + // 开机启动:config 是 source of truth,启动时双向同步 Run 项 + // (如果用户在 UI 里禁用过,启动时也会确保从 Run 项里删掉) if (config.Startup.Enabled) { Utils.StartupManager.EnableCurrentUserStartup(_logger); } + else + { + Utils.StartupManager.DisableCurrentUserStartup(_logger); + } // 后台异步拉一次应用列表,不阻塞启动 _ = _appCache.RefreshAsync(); diff --git a/MainWindow.xaml b/MainWindow.xaml index 04e779a..381f478 100644 --- a/MainWindow.xaml +++ b/MainWindow.xaml @@ -111,6 +111,7 @@ + @@ -126,6 +127,16 @@ Background="{DynamicResource CardBackground}" Foreground="{DynamicResource PrimaryText}" BorderBrush="{DynamicResource BorderColor}" /> + + + diff --git a/MainWindow.xaml.cs b/MainWindow.xaml.cs index 6eb9ee1..fc62f27 100644 --- a/MainWindow.xaml.cs +++ b/MainWindow.xaml.cs @@ -63,6 +63,7 @@ public partial class MainWindow : Window UrlTextBox.Text = config.Gotify.Url; TokenBox.Password = config.Gotify.ClientToken; + StartupCheckBox.IsChecked = config.Startup.Enabled; UpdateAppCount(); SetStatus("等待连接", StatusKind.Pending); UpdateHistoryCount(); @@ -377,6 +378,35 @@ public partial class MainWindow : Window ThemeToggleButton.Content = ThemeService.Current == ThemeService.Dark ? "☾ 深色" : "☼ 浅色"; } + private async void StartupCheckBox_Click(object sender, RoutedEventArgs e) + { + if (_config is null || _configService is null) return; + + var enabled = StartupCheckBox.IsChecked == true; + _config.Startup.Enabled = enabled; + + try + { + if (enabled) + { + Utils.StartupManager.EnableCurrentUserStartup(_logger!); + SetStatus("已开启开机启动", StatusKind.Connected); + } + else + { + Utils.StartupManager.DisableCurrentUserStartup(_logger!); + SetStatus("已关闭开机启动", StatusKind.Connected); + } + await _configService.SaveAsync(_config); + _logger?.Info($"Startup toggled. enabled={enabled}"); + } + catch (Exception ex) + { + _logger?.Error("Toggle startup failed.", ex); + SetStatus($"切换开机启动失败:{ex.Message}", StatusKind.Error); + } + } + private async void SaveButton_Click(object sender, RoutedEventArgs e) { if (_config is null || _configService is null) return;