diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index 26036e6..e4fb0c8 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -146,11 +146,7 @@ fn setup(app: &mut App) -> Result<(), Box> { let app_clone = app.clone(); app.listen_global("window-hidden", move |_| { - app_clone - .tray_handle() - .get_item("showhide") - .set_title("Show") - .ok(); + tray::set_tray_showhide_text(&app_clone, "Hide"); }) }); @@ -202,9 +198,10 @@ fn main() { .expect("error while running tauri application"); #[allow(clippy::single_match)] - app.run(|_app_handle, event| match event { + app.run(|app_handle, event| match event { tauri::RunEvent::ExitRequested { api, .. } => { api.prevent_exit(); + tray::set_tray_showhide_text(app_handle, "Show"); } _ => {} }); diff --git a/src-tauri/src/tray.rs b/src-tauri/src/tray.rs index 27f9fbd..9624c30 100644 --- a/src-tauri/src/tray.rs +++ b/src-tauri/src/tray.rs @@ -25,8 +25,8 @@ use tokio::sync::oneshot; use crate::ListenerHandle; pub fn create_tray() -> SystemTray { - let hide = CustomMenuItem::new("showhide".to_string(), "Hide"); - let quit = CustomMenuItem::new("quit".to_string(), "Quit"); + let hide = CustomMenuItem::new("showhide", "Hide"); + let quit = CustomMenuItem::new("quit", "Quit"); let tray_menu = SystemTrayMenu::new() .add_item(hide) .add_native_item(SystemTrayMenuItem::Separator) @@ -35,6 +35,13 @@ pub fn create_tray() -> SystemTray { SystemTray::new().with_menu(tray_menu) } +pub fn set_tray_showhide_text(app: &AppHandle, text: &str) { + app.tray_handle() + .get_item("showhide") + .set_title(text) + .ok(); +} + pub fn on_tray_event(app: &AppHandle, event: SystemTrayEvent) { let main_window = app.get_window("main"); match event { @@ -63,16 +70,10 @@ pub fn toggle_main_window(app: AppHandle, window: Option) { window.unminimize().ok(); window.set_focus().ok(); window.emit("window-shown", "").ok(); - app.tray_handle() - .get_item("showhide") - .set_title("Hide") - .ok(); + set_tray_showhide_text(&app, "Hide"); return; } - app.tray_handle() - .get_item("showhide") - .set_title("Show") - .ok(); + set_tray_showhide_text(&app, "Show"); async_runtime::spawn(async move { close_main(window).await; }); @@ -82,10 +83,7 @@ pub fn toggle_main_window(app: AppHandle, window: Option) { WindowBuilder::new(&app, "main", tauri::WindowUrl::App("index.html".into())) .build() .unwrap(); - app.tray_handle() - .get_item("showhide") - .set_title("Hide") - .ok(); + set_tray_showhide_text(&app, "Hide"); window.set_title("Transmission GUI").ok(); window.set_focus().ok(); }