use crate::error::AppError; use auto_launch::{AutoLaunch, AutoLaunchBuilder}; /// 初始化 AutoLaunch 实例 fn get_auto_launch() -> Result { let app_name = "CC Switch"; let app_path = std::env::current_exe().map_err(|e| AppError::Message(format!("无法获取应用路径: {e}")))?; // 使用 AutoLaunchBuilder 消除平台差异 // Windows/Linux: new() 接受 3 参数 // macOS: new() 接受 4 参数(含 hidden 参数) // Builder 模式自动处理这些差异 let auto_launch = AutoLaunchBuilder::new() .set_app_name(app_name) .set_app_path(&app_path.to_string_lossy()) .build() .map_err(|e| AppError::Message(format!("创建 AutoLaunch 失败: {e}")))?; Ok(auto_launch) } /// 启用开机自启 pub fn enable_auto_launch() -> Result<(), AppError> { let auto_launch = get_auto_launch()?; auto_launch .enable() .map_err(|e| AppError::Message(format!("启用开机自启失败: {e}")))?; log::info!("已启用开机自启"); Ok(()) } /// 禁用开机自启 pub fn disable_auto_launch() -> Result<(), AppError> { let auto_launch = get_auto_launch()?; auto_launch .disable() .map_err(|e| AppError::Message(format!("禁用开机自启失败: {e}")))?; log::info!("已禁用开机自启"); Ok(()) } /// 检查是否已启用开机自启 pub fn is_auto_launch_enabled() -> Result { let auto_launch = get_auto_launch()?; auto_launch .is_enabled() .map_err(|e| AppError::Message(format!("检查开机自启状态失败: {e}"))) }