Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion src-tauri/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ serial_test = "3"
cocoa = "0.26"
objc = "0.2"

[target.'cfg(target_os = "windows")'.dependencies]
winreg = "0.52"

[features]
default = ["custom-protocol"]
custom-protocol = ["tauri/custom-protocol"]
2 changes: 2 additions & 0 deletions src-tauri/src/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ pub mod onboarding;
pub mod profile_commands; // Profile 管理命令(v2.0)
pub mod proxy_commands;
pub mod session_commands;
pub mod startup_commands; // 开机自启动管理命令
pub mod stats_commands;
pub mod tool_commands;
pub mod tool_management;
Expand All @@ -22,6 +23,7 @@ pub use onboarding::*;
pub use profile_commands::*; // Profile 管理命令(v2.0)
pub use proxy_commands::*;
pub use session_commands::*;
pub use startup_commands::*; // 开机自启动管理命令
pub use stats_commands::*;
pub use tool_commands::*;
pub use tool_management::*;
Expand Down
1 change: 1 addition & 0 deletions src-tauri/src/commands/onboarding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ fn create_minimal_config() -> GlobalConfig {
external_watch_enabled: true,
external_poll_interval_ms: 5000,
single_instance_enabled: true,
startup_enabled: false,
}
}

Expand Down
99 changes: 99 additions & 0 deletions src-tauri/src/commands/startup_commands.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
// filepath: src-tauri/src/commands/startup_commands.rs

//! 开机自启动管理命令
//!
//! 提供前端调用的开机自启动配置管理接口

use duckcoding::utils::auto_startup::{
disable_auto_startup, enable_auto_startup, is_auto_startup_enabled,
};
use duckcoding::utils::config::{read_global_config, write_global_config};

/// 获取开机自启动配置
///
/// 返回当前配置状态,并自动同步系统实际状态
#[tauri::command]
pub async fn get_startup_config() -> Result<bool, String> {
// 读取配置文件中的状态
let config_opt = read_global_config().map_err(|e| e.to_string())?;

// 检查系统实际状态
let system_enabled = is_auto_startup_enabled().map_err(|e| e.to_string())?;

// 如果配置不存在,返回系统状态
let Some(mut config) = config_opt else {
return Ok(system_enabled);
};

// 如果配置与系统状态不一致,以系统状态为准并更新配置
if config.startup_enabled != system_enabled {
config.startup_enabled = system_enabled;
write_global_config(&config).map_err(|e| e.to_string())?;
}

Ok(config.startup_enabled)
}

/// 更新开机自启动配置
///
/// # 参数
/// - `enabled`: true 表示启用自启动,false 表示禁用
///
/// # 返回
/// - 成功返回 Ok(())
/// - 失败返回 Err(错误信息)
#[tauri::command]
pub async fn update_startup_config(enabled: bool) -> Result<(), String> {
// 根据参数调用系统API
if enabled {
enable_auto_startup().map_err(|e| e.to_string())?;
} else {
disable_auto_startup().map_err(|e| e.to_string())?;
}

// 更新配置文件
let config_opt = read_global_config().map_err(|e| e.to_string())?;

// 如果配置不存在,只更新系统设置不保存到配置文件
let Some(mut config) = config_opt else {
return Ok(());
};

config.startup_enabled = enabled;
write_global_config(&config).map_err(|e| e.to_string())?;

Ok(())
}

#[cfg(test)]
mod tests {
use super::*;

#[tokio::test]
async fn test_get_startup_config() {
// 测试读取配置(不进行实际系统操作)
let result = get_startup_config().await;
// 应该能正常读取,无论启用与否
assert!(result.is_ok());
}

#[tokio::test]
#[ignore] // 需要手动测试,避免污染系统
async fn test_update_startup_config() {
// 测试启用
let result = update_startup_config(true).await;
assert!(result.is_ok());

// 检查状态
let enabled = get_startup_config().await.unwrap();
assert!(enabled);

// 测试禁用
let result = update_startup_config(false).await;
assert!(result.is_ok());

// 再次检查状态
let enabled = get_startup_config().await.unwrap();
assert!(!enabled);
}
}
2 changes: 2 additions & 0 deletions src-tauri/src/core/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ mod tests {
external_watch_enabled: true,
external_poll_interval_ms: 5000,
single_instance_enabled: true,
startup_enabled: false,
};

let url = build_proxy_url(&config).unwrap();
Expand Down Expand Up @@ -141,6 +142,7 @@ mod tests {
external_watch_enabled: true,
external_poll_interval_ms: 5000,
single_instance_enabled: true,
startup_enabled: false,
};

let url = build_proxy_url(&config).unwrap();
Expand Down
3 changes: 3 additions & 0 deletions src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,9 @@ fn main() {
// 单实例模式配置命令
get_single_instance_config,
update_single_instance_config,
// 开机自启动管理命令
get_startup_config,
update_startup_config,
// Profile 管理命令(v2.0)
pm_list_all_profiles,
pm_list_tool_profiles,
Expand Down
3 changes: 3 additions & 0 deletions src-tauri/src/models/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,9 @@ pub struct GlobalConfig {
/// 单实例模式开关(默认启用,仅生产环境生效)
#[serde(default = "default_single_instance_enabled")]
pub single_instance_enabled: bool,
/// 开机自启动开关(默认关闭)
#[serde(default)]
pub startup_enabled: bool,
}

fn default_proxy_configs() -> HashMap<String, ToolProxyConfig> {
Expand Down
1 change: 1 addition & 0 deletions src-tauri/src/services/migration_manager/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ impl MigrationManager {
external_watch_enabled: true,
external_poll_interval_ms: 5000,
single_instance_enabled: true,
startup_enabled: false,
});

config.version = Some(new_version.to_string());
Expand Down
3 changes: 3 additions & 0 deletions src-tauri/src/services/proxy/proxy_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@ mod tests {
external_watch_enabled: true,
external_poll_interval_ms: 5000,
single_instance_enabled: true,
startup_enabled: false,
};

let url = ProxyService::build_proxy_url(&config);
Expand Down Expand Up @@ -257,6 +258,7 @@ mod tests {
external_watch_enabled: true,
external_poll_interval_ms: 5000,
single_instance_enabled: true,
startup_enabled: false,
};

let url = ProxyService::build_proxy_url(&config);
Expand Down Expand Up @@ -288,6 +290,7 @@ mod tests {
external_watch_enabled: true,
external_poll_interval_ms: 5000,
single_instance_enabled: true,
startup_enabled: false,
};

let url = ProxyService::build_proxy_url(&config);
Expand Down
Loading