use tauri::{ App, AppHandle, Manager, async_runtime as rt, }; use tauri::menu::{ MenuBuilder, MenuEvent, MenuItem, MenuItemBuilder, PredefinedMenuItem, }; use crate::app; use crate::state::AppState; pub struct MenuItems { pub status: MenuItem, pub show_hide: MenuItem, } impl MenuItems { pub fn after_show(&self) -> tauri::Result<()> { self.show_hide.set_text("Hide") } pub fn after_hide(&self) -> tauri::Result<()> { self.show_hide.set_text("Show") } pub fn after_lock(&self) -> tauri::Result<()> { if cfg!(debug_assertions) { self.status.set_text("Creddy (dev): Locked") } else { self.status.set_text("Creddy: Locked") } } pub fn after_unlock(&self) -> tauri::Result<()> { if cfg!(debug_assertions) { self.status.set_text("Creddy (dev): Unlocked") } else { self.status.set_text("Creddy: Unlocked") } } } pub fn setup(app: &App) -> tauri::Result<()> { let status_text = if cfg!(debug_assertions) { "Creddy (dev): Locked" } else { "Creddy: Locked" }; let status = MenuItemBuilder::with_id("status", status_text) .enabled(false) .build(app)?; let sep = PredefinedMenuItem::separator(app)?; let show_hide = MenuItemBuilder::with_id("show_hide", "Show").build(app)?; let exit = MenuItemBuilder::with_id("exit", "Exit").build(app)?; let menu = MenuBuilder::new(app) .items(&[&status, &sep, &show_hide, &exit]); let tray = app.tray_by_id("main").unwrap(); tray.set_menu(Some(menu.build()?))?; tray.on_menu_event(handle_event); // stash these so we can find them later to change the text app.manage(MenuItems { status, show_hide }); Ok(()) } fn handle_event(app_handle: &AppHandle, event: MenuEvent) { match event.id.0.as_str() { "exit" => app_handle.exit(0), "show_hide" => { let _ = app::toggle_main_window(app_handle); let new_handle = app_handle.clone(); rt::spawn(async move { let state = new_handle.state::(); state.signal_activity().await; }); }, _ => (), } }