From 82d9d41bdb7c692bed9d1cf3c3faedd2253d7f85 Mon Sep 17 00:00:00 2001 From: karim Date: Sat, 16 May 2026 01:29:49 +0200 Subject: [PATCH] =?UTF-8?q?System-Tray:=20Quick-Access-Men=C3=BC,=20Hide-o?= =?UTF-8?q?n-Close,=20Cmd+Q=20beendet?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- src-tauri/Cargo.toml | 2 +- src-tauri/src/lib.rs | 115 +++++++++++++++++++++++++++++++++++++------ src/App.jsx | 16 ++++++ 3 files changed, 117 insertions(+), 16 deletions(-) diff --git a/src-tauri/Cargo.toml b/src-tauri/Cargo.toml index 6823087..ae4887b 100755 --- a/src-tauri/Cargo.toml +++ b/src-tauri/Cargo.toml @@ -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" diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs index 4c432de..a186454 100755 --- a/src-tauri/src/lib.rs +++ b/src-tauri/src/lib.rs @@ -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); + } + }); } diff --git a/src/App.jsx b/src/App.jsx index 01586bd..ccc1c99 100755 --- a/src/App.jsx +++ b/src/App.jsx @@ -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 => {