Skip to content

Commit 56de051

Browse files
committed
Change tray menu item to "Show" when window is closed
Issue #93
1 parent bdca523 commit 56de051

2 files changed

Lines changed: 15 additions & 20 deletions

File tree

src-tauri/src/main.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -146,11 +146,7 @@ fn setup(app: &mut App) -> Result<(), Box<dyn std::error::Error>> {
146146

147147
let app_clone = app.clone();
148148
app.listen_global("window-hidden", move |_| {
149-
app_clone
150-
.tray_handle()
151-
.get_item("showhide")
152-
.set_title("Show")
153-
.ok();
149+
tray::set_tray_showhide_text(&app_clone, "Hide");
154150
})
155151
});
156152

@@ -202,9 +198,10 @@ fn main() {
202198
.expect("error while running tauri application");
203199

204200
#[allow(clippy::single_match)]
205-
app.run(|_app_handle, event| match event {
201+
app.run(|app_handle, event| match event {
206202
tauri::RunEvent::ExitRequested { api, .. } => {
207203
api.prevent_exit();
204+
tray::set_tray_showhide_text(app_handle, "Show");
208205
}
209206
_ => {}
210207
});

src-tauri/src/tray.rs

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ use tokio::sync::oneshot;
2525
use crate::ListenerHandle;
2626

2727
pub fn create_tray() -> SystemTray {
28-
let hide = CustomMenuItem::new("showhide".to_string(), "Hide");
29-
let quit = CustomMenuItem::new("quit".to_string(), "Quit");
28+
let hide = CustomMenuItem::new("showhide", "Hide");
29+
let quit = CustomMenuItem::new("quit", "Quit");
3030
let tray_menu = SystemTrayMenu::new()
3131
.add_item(hide)
3232
.add_native_item(SystemTrayMenuItem::Separator)
@@ -35,6 +35,13 @@ pub fn create_tray() -> SystemTray {
3535
SystemTray::new().with_menu(tray_menu)
3636
}
3737

38+
pub fn set_tray_showhide_text(app: &AppHandle, text: &str) {
39+
app.tray_handle()
40+
.get_item("showhide")
41+
.set_title(text)
42+
.ok();
43+
}
44+
3845
pub fn on_tray_event(app: &AppHandle, event: SystemTrayEvent) {
3946
let main_window = app.get_window("main");
4047
match event {
@@ -63,16 +70,10 @@ pub fn toggle_main_window(app: AppHandle, window: Option<Window>) {
6370
window.unminimize().ok();
6471
window.set_focus().ok();
6572
window.emit("window-shown", "").ok();
66-
app.tray_handle()
67-
.get_item("showhide")
68-
.set_title("Hide")
69-
.ok();
73+
set_tray_showhide_text(&app, "Hide");
7074
return;
7175
}
72-
app.tray_handle()
73-
.get_item("showhide")
74-
.set_title("Show")
75-
.ok();
76+
set_tray_showhide_text(&app, "Show");
7677
async_runtime::spawn(async move {
7778
close_main(window).await;
7879
});
@@ -82,10 +83,7 @@ pub fn toggle_main_window(app: AppHandle, window: Option<Window>) {
8283
WindowBuilder::new(&app, "main", tauri::WindowUrl::App("index.html".into()))
8384
.build()
8485
.unwrap();
85-
app.tray_handle()
86-
.get_item("showhide")
87-
.set_title("Hide")
88-
.ok();
86+
set_tray_showhide_text(&app, "Hide");
8987
window.set_title("Transmission GUI").ok();
9088
window.set_focus().ok();
9189
}

0 commit comments

Comments
 (0)