//! Tray icon with "Öffnen", "Aufnahme manuell starten" and "Beenden". use std::thread; use tauri::menu::{Menu, MenuItem, PredefinedMenuItem}; use tauri::tray::{MouseButton, MouseButtonState, TrayIconBuilder, TrayIconEvent}; use tauri::{AppHandle, Manager}; use crate::commands; use crate::state::{AppState, RecordingState}; const OPEN: &str = "open"; const RECORD: &str = "record"; const QUIT: &str = "quit"; pub fn create(app: &AppHandle) -> tauri::Result<()> { let menu = Menu::with_items( app, &[ &MenuItem::with_id(app, OPEN, "Öffnen", true, None::<&str>)?, &MenuItem::with_id(app, RECORD, "Aufnahme manuell starten", true, None::<&str>)?, &PredefinedMenuItem::separator(app)?, &MenuItem::with_id(app, QUIT, "Beenden", true, None::<&str>)?, ], )?; let mut builder = TrayIconBuilder::with_id("main") .tooltip("Gesprächsrekorder") .menu(&menu) .show_menu_on_left_click(false) .on_menu_event(|app, event| match event.id().as_ref() { OPEN => { commands::show_main(app); commands::show_recorder_if_recording(app); } RECORD => { if matches!( app.state::().recording_state(), RecordingState::Recording { .. } ) { commands::focus_recorder(app); return; } // Opening the audio devices can take a moment; keep the // event loop free. let app = app.clone(); thread::spawn(move || { if let Err(e) = commands::start_manual(&app) { log::error!("manual recording from tray failed: {e}"); } }); } QUIT => quit(app), _ => {} }) .on_tray_icon_event(|tray, event| { if let TrayIconEvent::Click { button: MouseButton::Left, button_state: MouseButtonState::Up, .. } = event { commands::show_main(tray.app_handle()); } }); if let Some(icon) = app.default_window_icon() { builder = builder.icon(icon.clone()); } builder.build(app)?; Ok(()) } /// Finalizes a running recording, then exits. fn quit(app: &AppHandle) { let app = app.clone(); thread::spawn(move || { if let Some(Err(e)) = app .state::() .stop_recording() .map(|ended| ended.result) { log::error!("stopping the recording on quit failed: {e}"); } app.exit(0); }); }