From 5e8fec4f10add72d1fd9e58f61e9993f4dd3fe82 Mon Sep 17 00:00:00 2001 From: Aqiang6 <2360638493@qq.com> Date: Wed, 23 Sep 2026 17:50:52 +0800 Subject: [PATCH] fix: skip autostart toggle when state already matches so settings always save auto-launch's disable() errors when the HKCU Run entry is already absent, which aborted set_settings before settings.json was written; every save with launchAtLogin=false and no autostart entry therefore failed silently, losing language and hotkey changes across restarts. Query the current autostart state first and only toggle the registry entry when it differs from the requested state. --- src-tauri/src/commands.rs | 41 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) diff --git a/src-tauri/src/commands.rs b/src-tauri/src/commands.rs index fefc6ba..ab3514d 100644 --- a/src-tauri/src/commands.rs +++ b/src-tauri/src/commands.rs @@ -578,11 +578,16 @@ pub fn choose_default_save_dir(current_dir: Option) -> Result Result; fn enable(&self) -> Result<(), String>; fn disable(&self) -> Result<(), String>; } impl LaunchAtLogin for tauri_plugin_autostart::AutoLaunchManager { + fn is_enabled(&self) -> Result { + tauri_plugin_autostart::AutoLaunchManager::is_enabled(self).map_err(|e| e.to_string()) + } + fn enable(&self) -> Result<(), String> { tauri_plugin_autostart::AutoLaunchManager::enable(self).map_err(|e| e.to_string()) } @@ -596,6 +601,12 @@ fn apply_launch_at_login( manager: &impl LaunchAtLogin, launch_at_login: bool, ) -> Result<(), String> { + // auto-launch errors when disabling an entry that is already absent, which + // would abort set_settings before the settings file is written; skip the + // registry toggle entirely when the current state already matches. + if manager.is_enabled()? == launch_at_login { + return Ok(()); + } if launch_at_login { manager.enable() } else { @@ -2903,16 +2914,32 @@ mod tests { #[derive(Default)] struct FakeLaunchAtLogin { + enabled: std::cell::Cell, calls: std::cell::RefCell>, } + impl FakeLaunchAtLogin { + fn with_autostart_enabled() -> Self { + Self { + enabled: std::cell::Cell::new(true), + calls: std::cell::RefCell::new(Vec::new()), + } + } + } + impl LaunchAtLogin for FakeLaunchAtLogin { + fn is_enabled(&self) -> Result { + Ok(self.enabled.get()) + } + fn enable(&self) -> Result<(), String> { + self.enabled.set(true); self.calls.borrow_mut().push("enable"); Ok(()) } fn disable(&self) -> Result<(), String> { + self.enabled.set(false); self.calls.borrow_mut().push("disable"); Ok(()) } @@ -2929,13 +2956,25 @@ mod tests { #[test] fn apply_launch_at_login_disables_login_startup_when_requested() { - let manager = FakeLaunchAtLogin::default(); + let manager = FakeLaunchAtLogin::with_autostart_enabled(); apply_launch_at_login(&manager, false).unwrap(); assert_eq!(*manager.calls.borrow(), ["disable"]); } + #[test] + fn apply_launch_at_login_skips_disable_when_autostart_already_off() { + let manager = FakeLaunchAtLogin::default(); + + apply_launch_at_login(&manager, false).unwrap(); + + assert!( + manager.calls.borrow().is_empty(), + "disabling an already-absent autostart entry must not be attempted" + ); + } + #[test] fn set_settings_applies_launch_at_login_before_saving_settings() { let source = include_str!("commands.rs").replace("\r\n", "\n");