feat(backend): add auto-launch functionality
Implement system auto-launch feature to allow CC-Switch to start automatically on system boot, improving user convenience. Backend Implementation: - auto_launch.rs: New module for auto-launch management * Cross-platform support using auto-launch crate * Enable/disable auto-launch with system integration * Proper error handling for permission issues * Platform-specific implementations (macOS/Windows/Linux) Command Layer: - Add get_auto_launch command to check current status - Add set_auto_launch command to toggle auto-start - Integrate commands with settings API Settings Integration: - Extend Settings struct with auto_launch field - Persist auto-launch preference in settings store - Automatic state synchronization on app startup Dependencies: - Add auto-launch ^0.5.0 to Cargo.toml - Update Cargo.lock with new dependency tree Technical Details: - Uses platform-specific auto-launch mechanisms: * macOS: Login Items via LaunchServices * Windows: Registry Run key * Linux: XDG autostart desktop files - Handles edge cases like permission denials gracefully - Maintains settings consistency across app restarts This feature enables users to have CC-Switch readily available after system boot without manual intervention, particularly useful for users who frequently switch between API providers.
This commit is contained in:
41
src-tauri/Cargo.lock
generated
41
src-tauri/Cargo.lock
generated
@@ -291,6 +291,17 @@ version = "1.1.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1505bd5d3d116872e7271a6d4e16d81d0c8570876c8de68093a09ac269d8aac0"
|
||||
|
||||
[[package]]
|
||||
name = "auto-launch"
|
||||
version = "0.5.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1f012b8cc0c850f34117ec8252a44418f2e34a2cf501de89e29b241ae5f79471"
|
||||
dependencies = [
|
||||
"dirs 4.0.0",
|
||||
"thiserror 1.0.69",
|
||||
"winreg 0.10.1",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "autocfg"
|
||||
version = "1.5.0"
|
||||
@@ -598,6 +609,7 @@ name = "cc-switch"
|
||||
version = "3.7.0"
|
||||
dependencies = [
|
||||
"anyhow",
|
||||
"auto-launch",
|
||||
"chrono",
|
||||
"dirs 5.0.1",
|
||||
"futures",
|
||||
@@ -982,6 +994,15 @@ dependencies = [
|
||||
"subtle",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "dirs"
|
||||
version = "4.0.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ca3aa72a6f96ea37bbc5aa912f6788242832f75369bdfdadcb0e38423f100059"
|
||||
dependencies = [
|
||||
"dirs-sys 0.3.7",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "dirs"
|
||||
version = "5.0.1"
|
||||
@@ -1000,6 +1021,17 @@ dependencies = [
|
||||
"dirs-sys 0.5.0",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "dirs-sys"
|
||||
version = "0.3.7"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1b1d1d91c932ef41c0f2663aa8b0ca0342d444d842c06914aa0a7e352d0bada6"
|
||||
dependencies = [
|
||||
"libc",
|
||||
"redox_users 0.4.6",
|
||||
"winapi",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "dirs-sys"
|
||||
version = "0.4.1"
|
||||
@@ -6397,6 +6429,15 @@ dependencies = [
|
||||
"memchr",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "winreg"
|
||||
version = "0.10.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "80d0f4e272c85def139476380b12f9ac60926689dd2e01d4923222f40580869d"
|
||||
dependencies = [
|
||||
"winapi",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "winreg"
|
||||
version = "0.52.0"
|
||||
|
||||
@@ -48,6 +48,7 @@ zip = "2.2"
|
||||
serde_yaml = "0.9"
|
||||
tempfile = "3"
|
||||
url = "2.5"
|
||||
auto-launch = "0.5"
|
||||
|
||||
[target.'cfg(any(target_os = "macos", target_os = "windows", target_os = "linux"))'.dependencies]
|
||||
tauri-plugin-single-instance = "2"
|
||||
|
||||
40
src-tauri/src/auto_launch.rs
Normal file
40
src-tauri/src/auto_launch.rs
Normal file
@@ -0,0 +1,40 @@
|
||||
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(|e| AppError::Message(format!("无法获取应用路径: {e}")))?;
|
||||
|
||||
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::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<bool, AppError> {
|
||||
let auto_launch = get_auto_launch()?;
|
||||
auto_launch
|
||||
.is_enabled()
|
||||
.map_err(|e| AppError::Message(format!("检查开机自启状态失败: {e}")))
|
||||
}
|
||||
@@ -37,3 +37,20 @@ pub async fn set_app_config_dir_override(
|
||||
crate::app_store::set_app_config_dir_to_store(&app, path.as_deref())?;
|
||||
Ok(true)
|
||||
}
|
||||
|
||||
/// 设置开机自启
|
||||
#[tauri::command]
|
||||
pub async fn set_auto_launch(enabled: bool) -> Result<bool, String> {
|
||||
if enabled {
|
||||
crate::auto_launch::enable_auto_launch().map_err(|e| format!("启用开机自启失败: {e}"))?;
|
||||
} else {
|
||||
crate::auto_launch::disable_auto_launch().map_err(|e| format!("禁用开机自启失败: {e}"))?;
|
||||
}
|
||||
Ok(true)
|
||||
}
|
||||
|
||||
/// 获取开机自启状态
|
||||
#[tauri::command]
|
||||
pub async fn get_auto_launch_status() -> Result<bool, String> {
|
||||
crate::auto_launch::is_auto_launch_enabled().map_err(|e| format!("获取开机自启状态失败: {e}"))
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
mod app_config;
|
||||
mod app_store;
|
||||
mod auto_launch;
|
||||
mod claude_mcp;
|
||||
mod claude_plugin;
|
||||
mod codex_config;
|
||||
@@ -717,6 +718,9 @@ pub fn run() {
|
||||
commands::get_skill_repos,
|
||||
commands::add_skill_repo,
|
||||
commands::remove_skill_repo,
|
||||
// Auto launch
|
||||
commands::set_auto_launch,
|
||||
commands::get_auto_launch_status,
|
||||
]);
|
||||
|
||||
let app = builder
|
||||
|
||||
@@ -49,6 +49,9 @@ pub struct AppSettings {
|
||||
pub gemini_config_dir: Option<String>,
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub language: Option<String>,
|
||||
/// 是否开机自启
|
||||
#[serde(default)]
|
||||
pub launch_on_startup: bool,
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub security: Option<SecuritySettings>,
|
||||
/// Claude 自定义端点列表
|
||||
@@ -77,6 +80,7 @@ impl Default for AppSettings {
|
||||
codex_config_dir: None,
|
||||
gemini_config_dir: None,
|
||||
language: None,
|
||||
launch_on_startup: false,
|
||||
security: None,
|
||||
custom_endpoints_claude: HashMap::new(),
|
||||
custom_endpoints_codex: HashMap::new(),
|
||||
|
||||
Reference in New Issue
Block a user