Fix tray quit blocked by unconditional prevent_exit()

app_handle.exit(0) fires ExitRequested which the run callback was
unconditionally preventing. Use an AtomicBool flag so the callback
only prevents exit for hide-to-tray, not for explicit quit requests.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
Ole-Morten Duesund 2026-02-16 15:29:46 +01:00
commit 12c10a1ea0

View file

@ -1,3 +1,5 @@
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;
use tauri::{
image::Image,
menu::{Menu, MenuItem},
@ -92,6 +94,9 @@ fn is_allowed_navigation(url: &Url) -> bool {
}
pub fn run() {
let quit_flag = Arc::new(AtomicBool::new(false));
let quit_flag_for_menu = quit_flag.clone();
let app = tauri::Builder::default()
.plugin(tauri_plugin_notification::init())
.plugin(tauri_plugin_opener::init())
@ -172,6 +177,7 @@ pub fn run() {
}
}
"quit" => {
quit_flag_for_menu.store(true, Ordering::SeqCst);
app_handle.exit(0);
}
_ => {}
@ -199,10 +205,12 @@ pub fn run() {
.build(tauri::generate_context!())
.expect("error building tauri application");
// -- Keep app alive when all windows are hidden --
app.run(|_app, event| {
// -- Keep app alive when all windows are hidden (but allow explicit quit) --
app.run(move |_app, event| {
if let RunEvent::ExitRequested { api, .. } = event {
api.prevent_exit();
if !quit_flag.load(Ordering::SeqCst) {
api.prevent_exit();
}
}
});
}