fix(core): menu ids map not updated after set_menu call (#2963)

This commit is contained in:
Lucas Fernandes Nogueira
2021-11-25 13:27:12 -03:00
committed by GitHub
parent c52b4b7da4
commit 411618f0de
4 changed files with 20 additions and 11 deletions

View File

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

View File

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

View File

@@ -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(),
})
})
}

View File

@@ -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<R: Runtime> {
pub(crate) ids: HashMap<MenuHash, MenuId>,
pub(crate) ids: Arc<Mutex<HashMap<MenuHash, MenuId>>>,
pub(crate) dispatcher: R::Dispatcher,
}
@@ -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,