-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[bug] ExitRequested
not fired on macOS
#9198
Comments
For those who are looking for a way to reliably run async functions on exit in a Tauri app, this is how I managed to do it: First make an exit lock, this is responsible for blocking your Tauri app from exiting, I made a use once_cell::sync::Lazy;
pub static EXIT_LOCKED: Lazy<std::sync::Mutex<bool>> = Lazy::new(|| {
std::sync::Mutex::new(true)
}); Then, make an fn on_exit(app: &AppHandle, need_exiting: bool) {
futures::executor::block_on(async move {
tauri::async_runtime::spawn(async move {
// Execute your cleanup actions here
stop_all_downloads().await;
*EXIT_LOCKED.lock().unwrap() = false;
})
});
let win = app.get_window("main").unwrap();
win.hide().unwrap();
loop {
if !*EXIT_LOCKED.lock().unwrap() {
break;
}
};
win.close().unwrap();
if need_exiting {
app.exit(0);
}
} Intercept window events to run cleanup actions when a window is closed, this implementation is for single-window apps. Make sure your storing your let app = tauri::Builder::default()
.invoke_handler(tauri::generate_handler![
// Your handlers
])
.on_window_event(|event| match event {
WindowEvent::CloseRequested { api, .. } => {
api.prevent_close();
on_exit(&event.window().app_handle(), true);
},
_ => {}
})
.build(tauri::generate_context!())
.expect("error while running tauri application"); Intercept events from the app lifecycle events( app.run(move |app, event| match event {
RunEvent::Exit => {
// Since RunEvent:Exit already executes exit, we'll pass false to on_exit so it won't exit twice
on_exit(app, false);
},
_ => {}
}); This is a janky workaround but it works reliably enough, if there's a better way, please share. Hopefully, Tauri will handle pre-exit events reliably in v2 🤞 |
How exactly are you exiting the app? Closing Windows via the red button for example should trigger ExitRequested once all windows are closed (even though that's not really how macos apps should behave). right click -> Quit on the dock item may be the same as #3084 if it's not working. |
Using Command+Q or Quit from menu bar |
Is this still currently the best way to do this? |
Describe the bug
nothing gets run when exit is requested
Reproduction
No response
Expected behavior
No response
Full
tauri info
outputThe text was updated successfully, but these errors were encountered: