瀏覽代碼

fix(core): update tray menu ids on `set_menu`, closes #3608 (#3611)

Lucas Fernandes Nogueira 3 年之前
父節點
當前提交
da8824318a
共有 3 個文件被更改,包括 19 次插入6 次删除
  1. 5 0
      .changes/fix-tray-menu-ids-update.md
  2. 3 2
      core/tauri/src/app.rs
  3. 11 4
      core/tauri/src/app/tray.rs

+ 5 - 0
.changes/fix-tray-menu-ids-update.md

@@ -0,0 +1,5 @@
+---
+"tauri": patch
+---
+
+Update tray menu id map when `SystemTrayHandle::set_menu` is called.

+ 3 - 2
core/tauri/src/app.rs

@@ -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 {
@@ -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 {

+ 11 - 4
core/tauri/src/app/tray.rs

@@ -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 {
@@ -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,
 }
 
@@ -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,
@@ -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.