Skip to content

Commit 476bd35

Browse files
committed
refine tray icon
1 parent 2be6660 commit 476bd35

File tree

1 file changed

+30
-24
lines changed

1 file changed

+30
-24
lines changed

src/main.rs

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -520,11 +520,20 @@ async fn main() -> Result<(), BoxError> {
520520
let icon = util::get_embedded_main_icon()?;
521521
win.set_icon(Some(icon));
522522

523-
let tray_icon_closur = || -> Result<(tray_icon::TrayIcon, tray_icon::menu::MenuItem, tray_icon::menu::MenuItem), BoxError> {
523+
const STR_SHOW: &str = "Show main window";
524+
const STR_QUIT: &str = "Quit";
525+
526+
static TRAY_ICON_MENU_ITEM_IDS: std::sync::LazyLock<Arc<Mutex<std::collections::HashMap<&str, tray_icon::menu::MenuId>>>> =
527+
std::sync::LazyLock::new(|| Arc::new(Mutex::new(std::collections::HashMap::new())));
528+
529+
let tray_icon_closure = || -> Result<tray_icon::TrayIcon, BoxError> {
524530
let icon = util::load_icon_from_bytes(util::MAIN_ICON_BYTES)?;
525531

526-
let show_item = tray_icon::menu::MenuItem::new("Show main window", true, None);
527-
let quit_item = tray_icon::menu::MenuItem::new("Quit", true, None);
532+
let show_item = tray_icon::menu::MenuItem::new(STR_SHOW, true, None);
533+
let quit_item = tray_icon::menu::MenuItem::new(STR_QUIT, true, None);
534+
535+
TRAY_ICON_MENU_ITEM_IDS.lock().unwrap().insert(STR_SHOW, show_item.id().clone());
536+
TRAY_ICON_MENU_ITEM_IDS.lock().unwrap().insert(STR_QUIT, quit_item.id().clone());
528537

529538
let tray_menu = tray_icon::menu::Menu::with_items(&[&show_item, &quit_item])?;
530539

@@ -533,7 +542,7 @@ async fn main() -> Result<(), BoxError> {
533542
.with_tooltip("OverTLS GUI - Click to show/hide window")
534543
.with_icon(icon)
535544
.build()?;
536-
Ok((tray_icon, show_item, quit_item))
545+
Ok(tray_icon)
537546
};
538547

539548
#[cfg(target_os = "linux")]
@@ -542,14 +551,10 @@ async fn main() -> Result<(), BoxError> {
542551
#[cfg(target_os = "linux")]
543552
std::thread::spawn(move || {
544553
gtk::init()?;
545-
let (_tray_icon, show_item, quit_item) = tray_icon_closur()?;
554+
let _holder = tray_icon_closure()?;
546555
loop {
547556
while let Ok(event) = tray_icon::menu::MenuEvent::receiver().try_recv() {
548-
if event.id == show_item.id() {
549-
tray_icon_tx.send("show").unwrap();
550-
} else if event.id == quit_item.id() {
551-
tray_icon_tx.send("quit").unwrap();
552-
}
557+
tray_icon_tx.send(event).unwrap();
553558
}
554559
gtk::main_iteration();
555560
}
@@ -560,7 +565,7 @@ async fn main() -> Result<(), BoxError> {
560565
});
561566

562567
#[cfg(not(target_os = "linux"))]
563-
let (_tray_icon, show_item, quit_item) = tray_icon_closur()?;
568+
let _holder = tray_icon_closure()?;
564569

565570
win.show();
566571

@@ -620,25 +625,26 @@ async fn main() -> Result<(), BoxError> {
620625
});
621626

622627
while ::fltk::app::wait() {
623-
// Handle tray menu events
624-
#[cfg(not(target_os = "linux"))]
625-
while let Ok(event) = tray_icon::menu::MenuEvent::receiver().try_recv() {
626-
if event.id == show_item.id() {
628+
fn handle_menu_event(event: &tray_icon::menu::MenuEvent, win: &mut Window) {
629+
log::debug!("Tray event received: {event:?}");
630+
let show_id = TRAY_ICON_MENU_ITEM_IDS.lock().unwrap().get(&STR_SHOW).cloned().unwrap();
631+
let quit_id = TRAY_ICON_MENU_ITEM_IDS.lock().unwrap().get(&STR_QUIT).cloned().unwrap();
632+
if show_id == event.id() {
627633
win.show();
628-
} else if event.id == quit_item.id() {
634+
} else if quit_id == event.id() {
629635
::fltk::app::quit();
630-
break;
631636
}
632637
}
633638

639+
// Handle tray menu events
640+
#[cfg(not(target_os = "linux"))]
641+
while let Ok(event) = tray_icon::menu::MenuEvent::receiver().try_recv() {
642+
handle_menu_event(&event, &mut win);
643+
}
644+
634645
#[cfg(target_os = "linux")]
635-
while let Ok(cmd) = tray_icon_rx.try_recv() {
636-
log::debug!("Tray command received: {cmd}");
637-
match cmd {
638-
"show" => win.show(),
639-
"quit" => ::fltk::app::quit(),
640-
_ => (),
641-
}
646+
while let Ok(event) = tray_icon_rx.try_recv() {
647+
handle_menu_event(&event, &mut win);
642648
}
643649

644650
// Deal with settings dialog results

0 commit comments

Comments
 (0)