diff --git a/packages/desktop/src-tauri/Cargo.toml b/packages/desktop/src-tauri/Cargo.toml index b7c238f064f..9ca5880c029 100644 --- a/packages/desktop/src-tauri/Cargo.toml +++ b/packages/desktop/src-tauri/Cargo.toml @@ -29,6 +29,7 @@ tauri-plugin-window-state = "2" tauri-plugin-clipboard-manager = "2" tauri-plugin-http = "2" tauri-plugin-notification = "2" +tauri-plugin-single-instance = "2" serde = { version = "1", features = ["derive"] } serde_json = "1" diff --git a/packages/desktop/src-tauri/src/lib.rs b/packages/desktop/src-tauri/src/lib.rs index 46c0ab256db..cbca27a0a49 100644 --- a/packages/desktop/src-tauri/src/lib.rs +++ b/packages/desktop/src-tauri/src/lib.rs @@ -189,6 +189,16 @@ pub fn run() { let updater_enabled = option_env!("TAURI_SIGNING_PRIVATE_KEY").is_some(); let mut builder = tauri::Builder::default() + .plugin(tauri_plugin_single_instance::init(|app, _args, _cwd| { + // When a second instance is launched, focus the existing window instead + if let Some(window) = app.get_webview_window("main") { + let _ = window.set_focus(); + #[cfg(target_os = "windows")] + { + let _ = window.unminimize(); + } + } + })) .plugin(tauri_plugin_os::init()) .plugin(tauri_plugin_window_state::Builder::new().build()) .plugin(tauri_plugin_store::Builder::new().build()) @@ -222,6 +232,10 @@ pub fn run() { let timestamp = Instant::now(); loop { if timestamp.elapsed() > Duration::from_secs(7) { + // Kill the sidecar before showing dialog to prevent orphaned processes + let _ = child.kill(); + println!("Killed sidecar due to startup timeout"); + let res = app.dialog() .message("Failed to spawn OpenCode Server. Copy logs using the button below and send them to the team for assistance.") .title("Startup Failed")