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:
+100
-15
@@ -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);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user