System-Tray: Quick-Access-Menü, Hide-on-Close, Cmd+Q beendet

Tray-Icon in der macOS-Menüleiste mit Linksklick zum Fokussieren und
Rechtsklick-Menü: Rapport öffnen, Dashboard, Zeiterfassung, Projekte,
Buchhaltung, Einstellungen, Beenden. Menü-Klicks senden ein
rapport:navigate-Event ans Frontend.

Der rote Schliessen-Button versteckt nur — die App läuft im Hintergrund
weiter. Cmd+Q (RunEvent::ExitRequested) und der Tray-Quit-Eintrag
setzen ein is_quitting-Flag und beenden die App wirklich.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
2026-05-16 01:29:49 +02:00
parent a5997cb52b
commit 22eb0f3e48
3 changed files with 117 additions and 16 deletions
+1 -1
View File
@@ -21,7 +21,7 @@ tauri-build = { version = "2.5.6", features = [] }
serde_json = "1.0"
serde = { version = "1.0", features = ["derive"] }
log = "0.4"
tauri = { version = "2.10.3", features = [] }
tauri = { version = "2.10.3", features = ["tray-icon"] }
tauri-plugin-log = "2"
tauri-plugin-updater = "2"
tauri-plugin-process = "2"
+100 -15
View File
@@ -1,18 +1,103 @@
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;
use tauri::{
menu::{Menu, MenuItem, PredefinedMenuItem},
tray::{MouseButton, MouseButtonState, TrayIconBuilder, TrayIconEvent},
Emitter, Manager,
};
fn show_main_window(app: &tauri::AppHandle) {
if let Some(window) = app.get_webview_window("main") {
let _ = window.show();
let _ = window.unminimize();
let _ = window.set_focus();
}
}
#[cfg_attr(mobile, tauri::mobile_entry_point)]
pub fn run() {
tauri::Builder::default()
.plugin(tauri_plugin_updater::Builder::new().build())
.plugin(tauri_plugin_process::init())
.setup(|app| {
if cfg!(debug_assertions) {
app.handle().plugin(
tauri_plugin_log::Builder::default()
.level(log::LevelFilter::Info)
.build(),
)?;
}
Ok(())
})
.run(tauri::generate_context!())
.expect("error while running tauri application");
let is_quitting = Arc::new(AtomicBool::new(false));
tauri::Builder::default()
.plugin(tauri_plugin_updater::Builder::new().build())
.plugin(tauri_plugin_process::init())
.on_window_event({
let is_quitting = is_quitting.clone();
move |window, event| {
if let tauri::WindowEvent::CloseRequested { api, .. } = event {
if !is_quitting.load(Ordering::SeqCst) {
api.prevent_close();
let _ = window.hide();
}
}
}
})
.setup({
let is_quitting = is_quitting.clone();
move |app| {
if cfg!(debug_assertions) {
app.handle().plugin(
tauri_plugin_log::Builder::default()
.level(log::LevelFilter::Info)
.build(),
)?;
}
let show = MenuItem::with_id(app, "show", "Rapport öffnen", true, None::<&str>)?;
let dashboard = MenuItem::with_id(app, "nav:dashboard", "Dashboard", true, None::<&str>)?;
let time = MenuItem::with_id(app, "nav:time", "Zeiterfassung", true, None::<&str>)?;
let projects = MenuItem::with_id(app, "nav:projects", "Projekte", true, None::<&str>)?;
let accounting = MenuItem::with_id(app, "nav:buchhaltung", "Buchhaltung", true, None::<&str>)?;
let settings = MenuItem::with_id(app, "nav:settings", "Einstellungen", true, None::<&str>)?;
let sep1 = PredefinedMenuItem::separator(app)?;
let sep2 = PredefinedMenuItem::separator(app)?;
let quit = MenuItem::with_id(app, "quit", "Beenden", true, Some("Cmd+Q"))?;
let menu = Menu::with_items(
app,
&[&show, &sep1, &dashboard, &time, &projects, &accounting, &settings, &sep2, &quit],
)?;
let is_quitting_menu = is_quitting.clone();
let _tray = TrayIconBuilder::with_id("main")
.icon(app.default_window_icon().unwrap().clone())
.icon_as_template(true)
.menu(&menu)
.show_menu_on_left_click(false)
.on_menu_event(move |app, event| match event.id.as_ref() {
"show" => show_main_window(app),
"quit" => {
is_quitting_menu.store(true, Ordering::SeqCst);
app.exit(0);
}
id if id.starts_with("nav:") => {
show_main_window(app);
let view = &id["nav:".len()..];
let _ = app.emit("rapport:navigate", view.to_string());
}
_ => {}
})
.on_tray_icon_event(|tray, event| {
if let TrayIconEvent::Click {
button: MouseButton::Left,
button_state: MouseButtonState::Up,
..
} = event
{
show_main_window(tray.app_handle());
}
})
.build(app)?;
Ok(())
}
})
.build(tauri::generate_context!())
.expect("error while building tauri application")
.run(move |_app, event| {
if let tauri::RunEvent::ExitRequested { .. } = event {
is_quitting.store(true, Ordering::SeqCst);
}
});
}
+16
View File
@@ -277,6 +277,22 @@ export default function App() {
return () => window.removeEventListener("openProtokoll", handler);
}, []);
// Tray-Menü: „Zeiterfassung", „Projekte" usw. springen zur passenden View
useEffect(() => {
if (!window.__TAURI_INTERNALS__) return;
let unlisten = null;
import("@tauri-apps/api/event").then(({ listen }) => {
listen("rapport:navigate", (event) => {
const target = event.payload;
if (typeof target === "string") {
navigate(target);
setSelectedProjectId(null);
}
}).then((fn) => { unlisten = fn; });
});
return () => { if (unlisten) unlisten(); };
}, []);
// Auto-expand parent when navigating to a child
useEffect(() => {
NAV_ITEMS.forEach(item => {