Skip to content

Commit da88243

Browse files
authored
fix(core): update tray menu ids on set_menu, closes #3608 (#3611)
1 parent f7acb06 commit da88243

File tree

3 files changed

+19
-6
lines changed

3 files changed

+19
-6
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"tauri": patch
3+
---
4+
5+
Update tray menu id map when `SystemTrayHandle::set_menu` is called.

core/tauri/src/app.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1241,9 +1241,10 @@ impl<R: Runtime> Builder<R> {
12411241
.expect("failed to run tray");
12421242

12431243
let tray_handle = tray::SystemTrayHandle {
1244-
ids: Arc::new(ids.clone()),
1244+
ids: Arc::new(std::sync::Mutex::new(ids)),
12451245
inner: tray_handler,
12461246
};
1247+
let ids = tray_handle.ids.clone();
12471248
app.tray_handle.replace(tray_handle.clone());
12481249
app.handle.tray_handle.replace(tray_handle);
12491250
for listener in self.system_tray_event_listeners {
@@ -1258,7 +1259,7 @@ impl<R: Runtime> Builder<R> {
12581259
let app_handle = app_handle.clone();
12591260
let event = match event {
12601261
RuntimeSystemTrayEvent::MenuItemClick(id) => tray::SystemTrayEvent::MenuItemClick {
1261-
id: ids.get(id).unwrap().clone(),
1262+
id: ids.lock().unwrap().get(id).unwrap().clone(),
12621263
},
12631264
RuntimeSystemTrayEvent::LeftClick { position, size } => {
12641265
tray::SystemTrayEvent::LeftClick {

core/tauri/src/app/tray.rs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@ pub use crate::runtime::{
1212

1313
use tauri_macros::default_runtime;
1414

15-
use std::{collections::HashMap, sync::Arc};
15+
use std::{
16+
collections::HashMap,
17+
sync::{Arc, Mutex},
18+
};
1619

1720
pub(crate) fn get_menu_ids(map: &mut HashMap<MenuHash, MenuId>, menu: &SystemTrayMenu) {
1821
for item in &menu.items {
@@ -80,7 +83,7 @@ pub enum SystemTrayEvent {
8083
#[default_runtime(crate::Wry, wry)]
8184
#[derive(Debug)]
8285
pub struct SystemTrayHandle<R: Runtime> {
83-
pub(crate) ids: Arc<HashMap<MenuHash, MenuId>>,
86+
pub(crate) ids: Arc<Mutex<HashMap<MenuHash, MenuId>>>,
8487
pub(crate) inner: R::TrayHandler,
8588
}
8689

@@ -113,7 +116,7 @@ impl<R: Runtime> Clone for SystemTrayMenuItemHandle<R> {
113116
impl<R: Runtime> SystemTrayHandle<R> {
114117
/// Gets a handle to the menu item that has the specified `id`.
115118
pub fn get_item(&self, id: MenuIdRef<'_>) -> SystemTrayMenuItemHandle<R> {
116-
for (raw, item_id) in self.ids.iter() {
119+
for (raw, item_id) in self.ids.lock().unwrap().iter() {
117120
if item_id == id {
118121
return SystemTrayMenuItemHandle {
119122
id: *raw,
@@ -131,7 +134,11 @@ impl<R: Runtime> SystemTrayHandle<R> {
131134

132135
/// Updates the tray menu.
133136
pub fn set_menu(&self, menu: SystemTrayMenu) -> crate::Result<()> {
134-
self.inner.set_menu(menu).map_err(Into::into)
137+
let mut ids = HashMap::new();
138+
get_menu_ids(&mut ids, &menu);
139+
self.inner.set_menu(menu)?;
140+
*self.ids.lock().unwrap() = ids;
141+
Ok(())
135142
}
136143

137144
/// Support [macOS tray icon template](https://developer.apple.com/documentation/appkit/nsimage/1520017-template?language=objc) to adjust automatically based on taskbar color.

0 commit comments

Comments
 (0)