Skip to content

Commit

Permalink
Change tray menu item to "Show" when window is closed
Browse files Browse the repository at this point in the history
Issue #93
  • Loading branch information
qu1ck committed Oct 19, 2023
1 parent bdca523 commit 56de051
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 20 deletions.
9 changes: 3 additions & 6 deletions src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,11 +146,7 @@ fn setup(app: &mut App) -> Result<(), Box<dyn std::error::Error>> {

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");
})
});

Expand Down Expand Up @@ -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");
}
_ => {}
});
Expand Down
26 changes: 12 additions & 14 deletions src-tauri/src/tray.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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 {
Expand Down Expand Up @@ -63,16 +70,10 @@ pub fn toggle_main_window(app: AppHandle, window: Option<Window>) {
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;
});
Expand All @@ -82,10 +83,7 @@ pub fn toggle_main_window(app: AppHandle, window: Option<Window>) {
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();
}
Expand Down

0 comments on commit 56de051

Please sign in to comment.