Skip to content

fix: skip autostart toggle when state already matches so settings always save - #1

Merged
poneding merged 1 commit into
poneding:masterfrom
Aqiang6:fix/settings-not-saved-when-autostart-absent
Sep 30, 2026
Merged

poneding merged 1 commit into
poneding:masterfrom
Aqiang6:fix/settings-not-saved-when-autostart-absent

Conversation

@Aqiang6

@Aqiang6 Aqiang6 commented Sep 23, 2026

Copy link
Copy Markdown
Contributor

Problem

On Windows, settings changes (language, hotkeys, theme, …) are silently lost on every restart when "launch at login" is disabled and no autostart entry exists in HKCU\...\Run.

auto-launch's disable() returns an error when the registry value it is asked to delete does not exist. Since set_settings applies the login-startup toggle before persisting, that error aborts set_settings before settings.json is ever written — and the frontend swallows the error, so the failure is invisible:

pub fn set_settings(app: AppHandle, settings: Settings) -> Result<(), String> {
    let autolaunch = app.autolaunch();
    apply_launch_at_login(&*autolaunch, settings.launch_at_login)?;  // <- Err aborts here
    settings_store::save(&settings).map_err(|e| e.to_string())?;    // <- never reached
    ...
}

Reproduction: fresh install (no Run entry) with launchAtLogin: false (the default) → change language to zh-CN → restart → language reverts to en, and AppData\Roaming\flashot\settings.json was never created.

Fix

Query the current autostart state first and only touch the registry when it differs from the requested state:

  • add is_enabled to the LaunchAtLogin trait (delegating to AutoLaunchManager::is_enabled)
  • skip the toggle in apply_launch_at_login when is_enabled()? == launch_at_login
  • extend FakeLaunchAtLogin with an enabled state and add a regression test covering the exact failing path (apply_launch_at_login_skips_disable_when_autostart_already_off), plus state-transition assertions on the existing enable/disable tests

Testing

  • cargo test apply_launch_at_login — 3 passed (including the new regression test)
  • full suite on Windows (MSVC, x86_64-pc-windows-msvc): 226 passed, 0 failed
  • manually verified on Windows 11: fresh state → change language to zh-CN and remap hotkeys → settings.json written immediately → persists across restarts

中文说明

问题: 未开启开机自启且注册表 Run 项不存在时,auto-launch 的 disable() 会因"值不存在"报错,导致 set_settings 在写入 settings.json 之前就中断,前端又静默吞掉错误——表现为"改语言/快捷键当次生效、重启后还原"。

修复: 先查询自启状态,状态与请求一致时直接跳过注册表操作,并补充回归测试。

…ays 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.
@poneding
poneding merged commit dc69351 into poneding:master Sep 30, 2026
1 check passed
@poneding

Copy link
Copy Markdown
Owner

thanks for your contributions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants