mirror of
https://github.com/farion1231/cc-switch.git
synced 2026-03-22 15:08:22 +08:00
Implement auto-launch functionality with proper state synchronization and error handling across Windows, macOS, and Linux platforms. Key changes: - Add auto_launch module using auto-launch crate 0.5 - Define typed errors (AutoLaunchPathError, AutoLaunchEnableError, etc.) - Sync system state with settings.json on app startup - Only call system API when auto-launch state actually changes - Add UI toggle in Window Settings panel - Add i18n support for auto-launch settings (en/zh) Implementation details: - Settings file (settings.json) is the single source of truth - On startup, system state is synced to match settings.json - Error handling uses Rust type system with proper error propagation - Frontend optimized to avoid unnecessary system API calls Platform support: - Windows: HKEY_CURRENT_USER registry modification - macOS: AppleScript-based launch item (configurable to Launch Agent) - Linux: XDG autostart desktop file
40 lines
1.2 KiB
Rust
40 lines
1.2 KiB
Rust
use crate::error::AppError;
|
|
use auto_launch::AutoLaunch;
|
|
|
|
/// 初始化 AutoLaunch 实例
|
|
fn get_auto_launch() -> Result<AutoLaunch, AppError> {
|
|
let app_name = "CC Switch";
|
|
let app_path = std::env::current_exe().map_err(AppError::AutoLaunchPathError)?;
|
|
|
|
let auto_launch = AutoLaunch::new(app_name, &app_path.to_string_lossy(), false, &[] as &[&str]);
|
|
Ok(auto_launch)
|
|
}
|
|
|
|
/// 启用开机自启
|
|
pub fn enable_auto_launch() -> Result<(), AppError> {
|
|
let auto_launch = get_auto_launch()?;
|
|
auto_launch
|
|
.enable()
|
|
.map_err(|e| AppError::AutoLaunchEnableError(e.to_string()))?;
|
|
log::info!("Auto-launch enabled");
|
|
Ok(())
|
|
}
|
|
|
|
/// 禁用开机自启
|
|
pub fn disable_auto_launch() -> Result<(), AppError> {
|
|
let auto_launch = get_auto_launch()?;
|
|
auto_launch
|
|
.disable()
|
|
.map_err(|e| AppError::AutoLaunchDisableError(e.to_string()))?;
|
|
log::info!("Auto-launch disabled");
|
|
Ok(())
|
|
}
|
|
|
|
/// 检查是否已启用开机自启
|
|
pub fn is_auto_launch_enabled() -> Result<bool, AppError> {
|
|
let auto_launch = get_auto_launch()?;
|
|
auto_launch
|
|
.is_enabled()
|
|
.map_err(|e| AppError::AutoLaunchCheckError(e.to_string()))
|
|
}
|