Skip to content

Commit

Permalink
fix(core): menu ids map not updated after set_menu call (#2963)
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasfernog authored Nov 25, 2021
1 parent c52b4b7 commit 411618f
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 11 deletions.
4 changes: 2 additions & 2 deletions core/tauri-runtime-wry/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2636,7 +2636,7 @@ fn create_webview(
fn create_rpc_handler(
context: Context,
label: String,
menu_ids: HashMap<MenuHash, MenuId>,
menu_ids: Arc<Mutex<HashMap<MenuHash, MenuId>>>,
handler: WebviewRpcHandler<Wry>,
) -> Box<dyn Fn(&Window, WryRpcRequest) -> Option<RpcResponse> + 'static> {
Box::new(move |window, request| {
Expand All @@ -2659,7 +2659,7 @@ fn create_rpc_handler(
fn create_file_drop_handler(
context: Context,
label: String,
menu_ids: HashMap<MenuHash, MenuId>,
menu_ids: Arc<Mutex<HashMap<MenuHash, MenuId>>>,
handler: FileDropHandler<Wry>,
) -> Box<dyn Fn(&Window, WryFileDropEvent) -> bool + 'static> {
Box::new(move |window, event| {
Expand Down
11 changes: 6 additions & 5 deletions core/tauri-runtime/src/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use tauri_utils::config::WindowConfig;
use std::{
collections::HashMap,
hash::{Hash, Hasher},
sync::{Arc, Mutex},
};

type UriSchemeProtocol =
Expand Down Expand Up @@ -97,7 +98,7 @@ pub struct PendingWindow<R: Runtime> {
pub url: String,

/// Maps runtime id to a string menu id.
pub menu_ids: HashMap<MenuHash, MenuId>,
pub menu_ids: Arc<Mutex<HashMap<MenuHash, MenuId>>>,
}

impl<R: Runtime> PendingWindow<R> {
Expand All @@ -119,7 +120,7 @@ impl<R: Runtime> PendingWindow<R> {
rpc_handler: None,
file_drop_handler: None,
url: "tauri://localhost".to_string(),
menu_ids,
menu_ids: Arc::new(Mutex::new(menu_ids)),
}
}

Expand All @@ -142,14 +143,14 @@ impl<R: Runtime> PendingWindow<R> {
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
}
Expand Down Expand Up @@ -179,7 +180,7 @@ pub struct DetachedWindow<R: Runtime> {
pub dispatcher: R::Dispatcher,

/// Maps runtime id to a string menu id.
pub menu_ids: HashMap<MenuHash, MenuId>,
pub menu_ids: Arc<Mutex<HashMap<MenuHash, MenuId>>>,
}

impl<R: Runtime> Clone for DetachedWindow<R> {
Expand Down
7 changes: 6 additions & 1 deletion core/tauri/src/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,12 @@ impl<R: Runtime> Window<R> {
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(),
})
})
}
Expand Down
9 changes: 6 additions & 3 deletions core/tauri/src/window/menu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand All @@ -28,7 +31,7 @@ impl MenuEvent {
#[default_runtime(crate::Wry, wry)]
#[derive(Debug)]
pub struct MenuHandle<R: Runtime> {
pub(crate) ids: HashMap<MenuHash, MenuId>,
pub(crate) ids: Arc<Mutex<HashMap<MenuHash, MenuId>>>,
pub(crate) dispatcher: R::Dispatcher,
}

Expand Down Expand Up @@ -61,7 +64,7 @@ impl<R: Runtime> Clone for MenuItemHandle<R> {
impl<R: Runtime> MenuHandle<R> {
/// Gets a handle to the menu item that has the specified `id`.
pub fn get_item(&self, id: MenuIdRef<'_>) -> MenuItemHandle<R> {
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,
Expand Down

0 comments on commit 411618f

Please sign in to comment.