mirror of
https://github.com/tauri-apps/tauri.git
synced 2026-04-03 10:11:15 +02:00
fix(core): menu ids map not updated after set_menu call (#2963)
This commit is contained in:
committed by
GitHub
parent
c52b4b7da4
commit
411618f0de
@@ -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| {
|
||||
|
||||
@@ -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> {
|
||||
|
||||
@@ -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(),
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user