diff --git a/core/tauri-runtime-wry/src/lib.rs b/core/tauri-runtime-wry/src/lib.rs index 4a61ae427..f9975a261 100644 --- a/core/tauri-runtime-wry/src/lib.rs +++ b/core/tauri-runtime-wry/src/lib.rs @@ -2636,7 +2636,7 @@ fn create_webview( fn create_rpc_handler( context: Context, label: String, - menu_ids: HashMap, + menu_ids: Arc>>, handler: WebviewRpcHandler, ) -> Box Option + 'static> { Box::new(move |window, request| { @@ -2659,7 +2659,7 @@ fn create_rpc_handler( fn create_file_drop_handler( context: Context, label: String, - menu_ids: HashMap, + menu_ids: Arc>>, handler: FileDropHandler, ) -> Box bool + 'static> { Box::new(move |window, event| { diff --git a/core/tauri-runtime/src/window.rs b/core/tauri-runtime/src/window.rs index e091c8279..c0fa4ee89 100644 --- a/core/tauri-runtime/src/window.rs +++ b/core/tauri-runtime/src/window.rs @@ -16,6 +16,7 @@ use tauri_utils::config::WindowConfig; use std::{ collections::HashMap, hash::{Hash, Hasher}, + sync::{Arc, Mutex}, }; type UriSchemeProtocol = @@ -97,7 +98,7 @@ pub struct PendingWindow { pub url: String, /// Maps runtime id to a string menu id. - pub menu_ids: HashMap, + pub menu_ids: Arc>>, } impl PendingWindow { @@ -119,7 +120,7 @@ impl PendingWindow { rpc_handler: None, file_drop_handler: None, url: "tauri://localhost".to_string(), - menu_ids, + menu_ids: Arc::new(Mutex::new(menu_ids)), } } @@ -142,14 +143,14 @@ impl PendingWindow { rpc_handler: None, file_drop_handler: None, url: "tauri://localhost".to_string(), - menu_ids, + menu_ids: Arc::new(Mutex::new(menu_ids)), } } pub fn set_menu(mut self, menu: Menu) -> Self { let mut menu_ids = HashMap::new(); get_menu_ids(&mut menu_ids, &menu); - self.menu_ids = menu_ids; + *self.menu_ids.lock().unwrap() = menu_ids; self.window_builder = self.window_builder.menu(menu); self } @@ -179,7 +180,7 @@ pub struct DetachedWindow { pub dispatcher: R::Dispatcher, /// Maps runtime id to a string menu id. - pub menu_ids: HashMap, + pub menu_ids: Arc>>, } impl Clone for DetachedWindow { diff --git a/core/tauri/src/window.rs b/core/tauri/src/window.rs index 3a7e23421..2f4cccfb4 100644 --- a/core/tauri/src/window.rs +++ b/core/tauri/src/window.rs @@ -342,7 +342,12 @@ impl Window { let menu_ids = self.window.menu_ids.clone(); self.window.dispatcher.on_menu_event(move |event| { f(MenuEvent { - menu_item_id: menu_ids.get(&event.menu_item_id).unwrap().clone(), + menu_item_id: menu_ids + .lock() + .unwrap() + .get(&event.menu_item_id) + .unwrap() + .clone(), }) }) } diff --git a/core/tauri/src/window/menu.rs b/core/tauri/src/window/menu.rs index f0281199c..b9b346aaa 100644 --- a/core/tauri/src/window/menu.rs +++ b/core/tauri/src/window/menu.rs @@ -9,7 +9,10 @@ use crate::runtime::{ use tauri_macros::default_runtime; -use std::collections::HashMap; +use std::{ + collections::HashMap, + sync::{Arc, Mutex}, +}; /// The window menu event. #[derive(Debug, Clone)] @@ -28,7 +31,7 @@ impl MenuEvent { #[default_runtime(crate::Wry, wry)] #[derive(Debug)] pub struct MenuHandle { - pub(crate) ids: HashMap, + pub(crate) ids: Arc>>, pub(crate) dispatcher: R::Dispatcher, } @@ -61,7 +64,7 @@ impl Clone for MenuItemHandle { impl MenuHandle { /// Gets a handle to the menu item that has the specified `id`. pub fn get_item(&self, id: MenuIdRef<'_>) -> MenuItemHandle { - for (raw, item_id) in self.ids.iter() { + for (raw, item_id) in self.ids.lock().unwrap().iter() { if item_id == id { return MenuItemHandle { id: *raw,