feat: 设置区加开机自启动 CheckBox(默认勾选)

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 一次保证一致。
This commit is contained in:
2026-09-07 00:50:44 +08:00
parent 17480f683e
commit bd1c68d8e4
3 changed files with 47 additions and 0 deletions
+6
View File
@@ -79,10 +79,16 @@ public partial class App : Application
_apiService, _apiService,
_appCache); _appCache);
// 开机启动:config 是 source of truth,启动时双向同步 Run 项
// (如果用户在 UI 里禁用过,启动时也会确保从 Run 项里删掉)
if (config.Startup.Enabled) if (config.Startup.Enabled)
{ {
Utils.StartupManager.EnableCurrentUserStartup(_logger); Utils.StartupManager.EnableCurrentUserStartup(_logger);
} }
else
{
Utils.StartupManager.DisableCurrentUserStartup(_logger);
}
// 后台异步拉一次应用列表,不阻塞启动 // 后台异步拉一次应用列表,不阻塞启动
_ = _appCache.RefreshAsync(); _ = _appCache.RefreshAsync();
+11
View File
@@ -111,6 +111,7 @@
<Grid.RowDefinitions> <Grid.RowDefinitions>
<RowDefinition Height="Auto"/> <RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/> <RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions> </Grid.RowDefinitions>
<TextBlock Grid.Row="0" Grid.Column="0" Text="Gotify URL" FontSize="11" <TextBlock Grid.Row="0" Grid.Column="0" Text="Gotify URL" FontSize="11"
Foreground="{DynamicResource SecondaryText}" /> Foreground="{DynamicResource SecondaryText}" />
@@ -126,6 +127,16 @@
Background="{DynamicResource CardBackground}" Background="{DynamicResource CardBackground}"
Foreground="{DynamicResource PrimaryText}" Foreground="{DynamicResource PrimaryText}"
BorderBrush="{DynamicResource BorderColor}" /> BorderBrush="{DynamicResource BorderColor}" />
<!-- 开机自启动(跨两列) -->
<CheckBox Grid.Row="2" Grid.Column="0" Grid.ColumnSpan="2"
x:Name="StartupCheckBox"
Content="开机自动启动(写入 HKCU\Software\Microsoft\Windows\CurrentVersion\Run,只对当前用户生效)"
Margin="0,12,0,0"
VerticalContentAlignment="Center"
Click="StartupCheckBox_Click"
Foreground="{DynamicResource PrimaryText}"
ToolTip="勾选后登录 Windows 时自动启动本程序到托盘;取消勾选会从 Run 项里删除" />
</Grid> </Grid>
</Border> </Border>
+30
View File
@@ -63,6 +63,7 @@ public partial class MainWindow : Window
UrlTextBox.Text = config.Gotify.Url; UrlTextBox.Text = config.Gotify.Url;
TokenBox.Password = config.Gotify.ClientToken; TokenBox.Password = config.Gotify.ClientToken;
StartupCheckBox.IsChecked = config.Startup.Enabled;
UpdateAppCount(); UpdateAppCount();
SetStatus("等待连接", StatusKind.Pending); SetStatus("等待连接", StatusKind.Pending);
UpdateHistoryCount(); UpdateHistoryCount();
@@ -377,6 +378,35 @@ public partial class MainWindow : Window
ThemeToggleButton.Content = ThemeService.Current == ThemeService.Dark ? "☾ 深色" : "☼ 浅色"; 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) private async void SaveButton_Click(object sender, RoutedEventArgs e)
{ {
if (_config is null || _configService is null) return; if (_config is null || _configService is null) return;