|
| 1 | +use tauri_plugin_autostart::ManagerExt as _; |
| 2 | + |
| 3 | +use crate::models::InstanceStore; |
| 4 | +use crate::modules; |
| 5 | +use crate::modules::config::{self, UserConfig}; |
| 6 | +use crate::modules::websocket; |
| 7 | + |
| 8 | +fn get_app_auto_launch_enabled(app: &tauri::AppHandle) -> Result<bool, String> { |
| 9 | + app.autolaunch() |
| 10 | + .is_enabled() |
| 11 | + .map_err(|err| format!("读取应用自启动状态失败: {}", err)) |
| 12 | +} |
| 13 | + |
| 14 | +fn apply_app_auto_launch_enabled(app: &tauri::AppHandle, enabled: bool) -> Result<(), String> { |
| 15 | + if enabled { |
| 16 | + app.autolaunch() |
| 17 | + .enable() |
| 18 | + .map_err(|err| format!("启用应用自启动失败: {}", err)) |
| 19 | + } else { |
| 20 | + app.autolaunch() |
| 21 | + .disable() |
| 22 | + .map_err(|err| format!("停用应用自启动失败: {}", err)) |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +fn load_instance_store_by_platform(platform: &str) -> Result<InstanceStore, String> { |
| 27 | + match platform { |
| 28 | + "antigravity" => modules::instance::load_instance_store(), |
| 29 | + "codex" => modules::codex_instance::load_instance_store(), |
| 30 | + "github-copilot" => modules::github_copilot_instance::load_instance_store(), |
| 31 | + "windsurf" => modules::windsurf_instance::load_instance_store(), |
| 32 | + "kiro" => modules::kiro_instance::load_instance_store(), |
| 33 | + "cursor" => modules::cursor_instance::load_instance_store(), |
| 34 | + "gemini" => modules::gemini_instance::load_instance_store(), |
| 35 | + "codebuddy" => modules::codebuddy_instance::load_instance_store(), |
| 36 | + "codebuddy_cn" => modules::codebuddy_cn_instance::load_instance_store(), |
| 37 | + "qoder" => modules::qoder_instance::load_instance_store(), |
| 38 | + "trae" => modules::trae_instance::load_instance_store(), |
| 39 | + "workbuddy" => modules::workbuddy_instance::load_instance_store(), |
| 40 | + _ => Err("不支持的实例平台".to_string()), |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +fn save_instance_store_by_platform(platform: &str, store: &InstanceStore) -> Result<(), String> { |
| 45 | + match platform { |
| 46 | + "antigravity" => modules::instance::save_instance_store(store), |
| 47 | + "codex" => modules::codex_instance::save_instance_store(store), |
| 48 | + "github-copilot" => modules::github_copilot_instance::save_instance_store(store), |
| 49 | + "windsurf" => modules::windsurf_instance::save_instance_store(store), |
| 50 | + "kiro" => modules::kiro_instance::save_instance_store(store), |
| 51 | + "cursor" => modules::cursor_instance::save_instance_store(store), |
| 52 | + "gemini" => modules::gemini_instance::save_instance_store(store), |
| 53 | + "codebuddy" => modules::codebuddy_instance::save_instance_store(store), |
| 54 | + "codebuddy_cn" => modules::codebuddy_cn_instance::save_instance_store(store), |
| 55 | + "qoder" => modules::qoder_instance::save_instance_store(store), |
| 56 | + "trae" => modules::trae_instance::save_instance_store(store), |
| 57 | + "workbuddy" => modules::workbuddy_instance::save_instance_store(store), |
| 58 | + _ => Err("不支持的实例平台".to_string()), |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +fn sanitize_instance_store(store: &InstanceStore) -> InstanceStore { |
| 63 | + let mut next = store.clone(); |
| 64 | + next.default_settings.last_pid = None; |
| 65 | + for instance in &mut next.instances { |
| 66 | + instance.last_pid = None; |
| 67 | + instance.last_launched_at = None; |
| 68 | + } |
| 69 | + next |
| 70 | +} |
| 71 | + |
| 72 | +#[tauri::command] |
| 73 | +pub fn data_transfer_get_user_config() -> Result<UserConfig, String> { |
| 74 | + Ok(config::get_user_config()) |
| 75 | +} |
| 76 | + |
| 77 | +#[tauri::command] |
| 78 | +pub fn data_transfer_apply_user_config( |
| 79 | + app: tauri::AppHandle, |
| 80 | + config: UserConfig, |
| 81 | +) -> Result<bool, String> { |
| 82 | + let current = config::get_user_config(); |
| 83 | + let current_app_auto_launch_enabled = |
| 84 | + get_app_auto_launch_enabled(&app).unwrap_or(current.app_auto_launch_enabled); |
| 85 | + |
| 86 | + let needs_restart = current.ws_port != config.ws_port |
| 87 | + || current.ws_enabled != config.ws_enabled |
| 88 | + || current.report_enabled != config.report_enabled |
| 89 | + || current.report_port != config.report_port |
| 90 | + || current.report_token != config.report_token; |
| 91 | + let language_changed = current.language != config.language; |
| 92 | + let app_auto_launch_changed = current_app_auto_launch_enabled != config.app_auto_launch_enabled; |
| 93 | + |
| 94 | + #[cfg(target_os = "macos")] |
| 95 | + let hide_dock_icon_changed = current.hide_dock_icon != config.hide_dock_icon; |
| 96 | + |
| 97 | + config::save_user_config(&config)?; |
| 98 | + |
| 99 | + if app_auto_launch_changed { |
| 100 | + apply_app_auto_launch_enabled(&app, config.app_auto_launch_enabled)?; |
| 101 | + } |
| 102 | + |
| 103 | + if let Err(err) = modules::floating_card_window::apply_floating_card_always_on_top(&app) { |
| 104 | + modules::logger::log_warn(&format!("[DataTransfer] 应用悬浮卡片置顶状态失败: {}", err)); |
| 105 | + } |
| 106 | + |
| 107 | + #[cfg(target_os = "macos")] |
| 108 | + if hide_dock_icon_changed { |
| 109 | + crate::apply_macos_activation_policy(&app); |
| 110 | + } |
| 111 | + |
| 112 | + if language_changed { |
| 113 | + let normalized_language = config.language.clone(); |
| 114 | + websocket::broadcast_language_changed(&normalized_language, "desktop"); |
| 115 | + modules::sync_settings::write_sync_setting("language", &normalized_language); |
| 116 | + if let Err(err) = modules::tray::update_tray_menu(&app) { |
| 117 | + modules::logger::log_warn(&format!("[DataTransfer] 语言变更后刷新托盘失败: {}", err)); |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + Ok(needs_restart) |
| 122 | +} |
| 123 | + |
| 124 | +#[tauri::command] |
| 125 | +pub fn data_transfer_get_instance_store(platform: String) -> Result<InstanceStore, String> { |
| 126 | + load_instance_store_by_platform(platform.trim()) |
| 127 | +} |
| 128 | + |
| 129 | +#[tauri::command] |
| 130 | +pub fn data_transfer_replace_instance_store( |
| 131 | + platform: String, |
| 132 | + store: InstanceStore, |
| 133 | +) -> Result<(), String> { |
| 134 | + let sanitized = sanitize_instance_store(&store); |
| 135 | + save_instance_store_by_platform(platform.trim(), &sanitized) |
| 136 | +} |
0 commit comments