Skip to content

Commit 8a7edbf

Browse files
authored
Merge pull request #6005 from menloresearch/fix/save_my_life
Add RunEvent::Exit event to tauri to handle macos context menu exit
2 parents 59a17d4 + e11b4c9 commit 8a7edbf

File tree

1 file changed

+38
-4
lines changed

1 file changed

+38
-4
lines changed

src-tauri/src/lib.rs

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@ use core::{
88
};
99
use reqwest::Client;
1010
use std::{collections::HashMap, sync::Arc};
11-
use tauri::{Emitter, Manager};
12-
11+
use tauri::{Emitter, Manager, RunEvent};
1312
use tokio::sync::Mutex;
1413

1514
#[cfg_attr(mobile, tauri::mobile_entry_point)]
@@ -22,7 +21,8 @@ pub fn run() {
2221
// when defining deep link schemes at runtime, you must also check `argv` here
2322
}));
2423
}
25-
builder
24+
25+
let app = builder
2626
.plugin(tauri_plugin_os::init())
2727
.plugin(tauri_plugin_deep_link::init())
2828
.plugin(tauri_plugin_dialog::init())
@@ -143,12 +143,46 @@ pub fn run() {
143143
cleanup_processes(state).await;
144144
});
145145
}
146+
146147
let client = Client::new();
147148
let url = "http://127.0.0.1:39291/processManager/destroy";
148149
let _ = client.delete(url).send();
149150
}
150151
_ => {}
151152
})
152-
.run(tauri::generate_context!())
153+
.build(tauri::generate_context!())
153154
.expect("error while running tauri application");
155+
156+
// Handle app lifecycle events
157+
app.run(|app, event| match event {
158+
RunEvent::Exit => {
159+
// This is called when the app is actually exiting (e.g., macOS dock quit)
160+
// We can't prevent this, so run cleanup quickly
161+
let app_handle = app.clone();
162+
tokio::task::block_in_place(|| {
163+
tauri::async_runtime::block_on(async {
164+
let state = app_handle.state::<AppState>();
165+
166+
// Hide window immediately
167+
if let Some(window) = app_handle.get_webview_window("main") {
168+
let _ = window.hide();
169+
let _ = window.emit("kill-mcp-servers", ());
170+
}
171+
172+
// Quick cleanup with shorter timeout
173+
cleanup_processes(state).await;
174+
175+
// Stop HTTP server with shorter timeout
176+
let client = Client::new();
177+
let url = "http://127.0.0.1:39291/processManager/destroy";
178+
let _ = tokio::time::timeout(
179+
tokio::time::Duration::from_secs(2),
180+
client.delete(url).send(),
181+
)
182+
.await;
183+
});
184+
});
185+
}
186+
_ => {}
187+
});
154188
}

0 commit comments

Comments
 (0)