Skip to content

Commit

Permalink
fix(core): update tray menu ids on set_menu, closes #3608 (#3611)
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasfernog committed Mar 4, 2022
1 parent f7acb06 commit da88243
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changes/fix-tray-menu-ids-update.md
@@ -0,0 +1,5 @@
---
"tauri": patch
---

Update tray menu id map when `SystemTrayHandle::set_menu` is called.
5 changes: 3 additions & 2 deletions core/tauri/src/app.rs
Expand Up @@ -1241,9 +1241,10 @@ impl<R: Runtime> Builder<R> {
.expect("failed to run tray");

let tray_handle = tray::SystemTrayHandle {
ids: Arc::new(ids.clone()),
ids: Arc::new(std::sync::Mutex::new(ids)),
inner: tray_handler,
};
let ids = tray_handle.ids.clone();
app.tray_handle.replace(tray_handle.clone());
app.handle.tray_handle.replace(tray_handle);
for listener in self.system_tray_event_listeners {
Expand All @@ -1258,7 +1259,7 @@ impl<R: Runtime> Builder<R> {
let app_handle = app_handle.clone();
let event = match event {
RuntimeSystemTrayEvent::MenuItemClick(id) => tray::SystemTrayEvent::MenuItemClick {
id: ids.get(id).unwrap().clone(),
id: ids.lock().unwrap().get(id).unwrap().clone(),
},
RuntimeSystemTrayEvent::LeftClick { position, size } => {
tray::SystemTrayEvent::LeftClick {
Expand Down
15 changes: 11 additions & 4 deletions core/tauri/src/app/tray.rs
Expand Up @@ -12,7 +12,10 @@ pub use crate::runtime::{

use tauri_macros::default_runtime;

use std::{collections::HashMap, sync::Arc};
use std::{
collections::HashMap,
sync::{Arc, Mutex},
};

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

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

/// Updates the tray menu.
pub fn set_menu(&self, menu: SystemTrayMenu) -> crate::Result<()> {
self.inner.set_menu(menu).map_err(Into::into)
let mut ids = HashMap::new();
get_menu_ids(&mut ids, &menu);
self.inner.set_menu(menu)?;
*self.ids.lock().unwrap() = ids;
Ok(())
}

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

0 comments on commit da88243

Please sign in to comment.