diff --git a/.changes/store-tauri-menu-items.md b/.changes/store-tauri-menu-items.md new file mode 100644 index 000000000000..0960963665db --- /dev/null +++ b/.changes/store-tauri-menu-items.md @@ -0,0 +1,16 @@ +--- +'tauri': 'patch:bug' +--- + +Fix `Menu` creation no longer takes `MenuItemOptions` with `action` directly + +```ts +const menu = await Menu.new({ + items: [{ + text: "Hello", action: () => { alert('World') } + }] +}) +await menu.popup() +``` + +The `Hello` item should now trigger the `alert('World')` action again. diff --git a/crates/tauri/src/menu/menu.rs b/crates/tauri/src/menu/menu.rs index ba99e47f501a..f362fce8c962 100644 --- a/crates/tauri/src/menu/menu.rs +++ b/crates/tauri/src/menu/menu.rs @@ -264,7 +264,9 @@ impl Menu { pub fn append(&self, item: &dyn IsMenuItem) -> crate::Result<()> { let kind = item.kind(); run_item_main_thread!(self, |self_: Self| { - (*self_.0).as_ref().append(kind.inner().inner_muda()) + (*self_.0).as_ref().append(kind.inner().inner_muda())?; + self_.0.items.lock().unwrap().push(kind); + Ok::<_, muda::Error>(()) })? .map_err(Into::into) } @@ -294,7 +296,9 @@ impl Menu { pub fn prepend(&self, item: &dyn IsMenuItem) -> crate::Result<()> { let kind = item.kind(); run_item_main_thread!(self, |self_: Self| { - (*self_.0).as_ref().prepend(kind.inner().inner_muda()) + (*self_.0).as_ref().prepend(kind.inner().inner_muda())?; + self_.0.items.lock().unwrap().insert(0, kind); + Ok::<_, muda::Error>(()) })? .map_err(Into::into) } @@ -319,9 +323,13 @@ impl Menu { /// [`Submenu`]: super::Submenu pub fn insert(&self, item: &dyn IsMenuItem, position: usize) -> crate::Result<()> { let kind = item.kind(); - run_item_main_thread!(self, |self_: Self| (*self_.0) - .as_ref() - .insert(kind.inner().inner_muda(), position))? + run_item_main_thread!(self, |self_: Self| { + (*self_.0) + .as_ref() + .insert(kind.inner().inner_muda(), position)?; + self_.0.items.lock().unwrap().insert(position, kind); + Ok::<_, muda::Error>(()) + })? .map_err(Into::into) } @@ -344,7 +352,14 @@ impl Menu { pub fn remove(&self, item: &dyn IsMenuItem) -> crate::Result<()> { let kind = item.kind(); run_item_main_thread!(self, |self_: Self| { - (*self_.0).as_ref().remove(kind.inner().inner_muda()) + (*self_.0).as_ref().remove(kind.inner().inner_muda())?; + self_ + .0 + .items + .lock() + .unwrap() + .retain(|i| i.id() != kind.id()); + Ok::<_, muda::Error>(()) })? .map_err(Into::into) } @@ -352,10 +367,8 @@ impl Menu { /// Remove the menu item at the specified position from this menu and returns it. pub fn remove_at(&self, position: usize) -> crate::Result>> { run_item_main_thread!(self, |self_: Self| { - (*self_.0) - .as_ref() - .remove_at(position) - .map(|i| MenuItemKind::from_muda(self_.0.app_handle.clone(), i)) + (*self_.0).as_ref().remove_at(position)?; + Some(self_.0.items.lock().unwrap().remove(position)) }) } @@ -366,22 +379,18 @@ impl Menu { MenuId: PartialEq<&'a I>, { self - .items() - .unwrap_or_default() - .into_iter() + .0 + .items + .lock() + .unwrap() + .iter() .find(|i| i.id() == &id) + .cloned() } /// Returns a list of menu items that has been added to this menu. pub fn items(&self) -> crate::Result>> { - run_item_main_thread!(self, |self_: Self| { - (*self_.0) - .as_ref() - .items() - .into_iter() - .map(|i| MenuItemKind::from_muda(self_.0.app_handle.clone(), i)) - .collect::>() - }) + Ok(self.0.items.lock().unwrap().clone()) } /// Set this menu as the application menu. diff --git a/crates/tauri/src/menu/mod.rs b/crates/tauri/src/menu/mod.rs index ab435920378b..088f08bbee34 100644 --- a/crates/tauri/src/menu/mod.rs +++ b/crates/tauri/src/menu/mod.rs @@ -13,7 +13,7 @@ mod normal; pub(crate) mod plugin; mod predefined; mod submenu; -use std::{mem::ManuallyDrop, sync::Arc}; +use std::mem::ManuallyDrop; pub use builders::*; pub use menu::{HELP_SUBMENU_ID, WINDOW_SUBMENU_ID}; @@ -66,6 +66,7 @@ macro_rules! gen_wrappers { $( $(#[$attr:meta])* $type:ident($inner:ident$(, $kind:ident)?) + $({ $($(#[$field_attr:meta])* $field:ident: $field_type:ty),* })? ),* ) => { $( @@ -74,6 +75,7 @@ macro_rules! gen_wrappers { // This [`ManuallyDrop`] is used to [`ManuallyDrop::take`] in [`Self::drop`] to drop it on main thread inner: ManuallyDrop<::muda::$type>, app_handle: $crate::AppHandle, + $($( $(#[$field_attr])* $field: $field_type,)*)? } impl $inner { @@ -81,6 +83,7 @@ macro_rules! gen_wrappers { Self { inner: ManuallyDrop::new(menu), app_handle, + $($($field: Default::default(),)*)? } } } @@ -149,11 +152,17 @@ gen_wrappers!( /// ## Platform-specific: /// /// - **macOS**: if using [`Menu`] for the global menubar, it can only contain [`Submenu`]s - Menu(MenuInner), + Menu(MenuInner) { + /// **SAFTY:** Must only be modified in sync with the inner [`muda::Menu::items`] + items: std::sync::Mutex>> + }, /// A menu item inside a [`Menu`] or [`Submenu`] and contains only text. MenuItem(MenuItemInner, MenuItem), /// A type that is a submenu inside a [`Menu`] or [`Submenu`] - Submenu(SubmenuInner, Submenu), + Submenu(SubmenuInner, Submenu) { + /// **SAFTY:** Must only be modified in sync with the inner [`muda::Submenu::items`] + items: std::sync::Mutex>> + }, /// A predefined (native) menu item which has a predefined behavior by the OS or by this crate. PredefinedMenuItem(PredefinedMenuItemInner, Predefined), /// A menu item inside a [`Menu`] or [`Submenu`] @@ -574,26 +583,6 @@ impl MenuItemKind { } } - pub(crate) fn from_muda(app_handle: AppHandle, i: muda::MenuItemKind) -> Self { - match i { - muda::MenuItemKind::MenuItem(i) => { - Self::MenuItem(MenuItem(Arc::new(MenuItemInner::new(app_handle, i)))) - } - muda::MenuItemKind::Submenu(i) => { - Self::Submenu(Submenu(Arc::new(SubmenuInner::new(app_handle, i)))) - } - muda::MenuItemKind::Predefined(i) => Self::Predefined(PredefinedMenuItem(Arc::new( - PredefinedMenuItemInner::new(app_handle, i), - ))), - muda::MenuItemKind::Check(i) => Self::Check(CheckMenuItem(Arc::new( - CheckMenuItemInner::new(app_handle, i), - ))), - muda::MenuItemKind::Icon(i) => Self::Icon(IconMenuItem(Arc::new(IconMenuItemInner::new( - app_handle, i, - )))), - } - } - /// Casts this item to a [`MenuItem`], and returns `None` if it wasn't. pub fn as_menuitem(&self) -> Option<&MenuItem> { match self { diff --git a/crates/tauri/src/menu/submenu.rs b/crates/tauri/src/menu/submenu.rs index c7b7bf8cb06e..9cdd0a711528 100644 --- a/crates/tauri/src/menu/submenu.rs +++ b/crates/tauri/src/menu/submenu.rs @@ -259,7 +259,9 @@ impl Submenu { pub fn append(&self, item: &dyn IsMenuItem) -> crate::Result<()> { let kind = item.kind(); run_item_main_thread!(self, |self_: Self| { - (*self_.0).as_ref().append(kind.inner().inner_muda()) + (*self_.0).as_ref().append(kind.inner().inner_muda())?; + self_.0.items.lock().unwrap().push(kind); + Ok::<_, muda::Error>(()) })? .map_err(Into::into) } @@ -277,7 +279,9 @@ impl Submenu { pub fn prepend(&self, item: &dyn IsMenuItem) -> crate::Result<()> { let kind = item.kind(); run_item_main_thread!(self, |self_: Self| { - (*self_.0).as_ref().prepend(kind.inner().inner_muda()) + (*self_.0).as_ref().prepend(kind.inner().inner_muda())?; + self_.0.items.lock().unwrap().insert(0, kind); + Ok::<_, muda::Error>(()) })? .map_err(Into::into) } @@ -293,7 +297,9 @@ impl Submenu { run_item_main_thread!(self, |self_: Self| { (*self_.0) .as_ref() - .insert(kind.inner().inner_muda(), position) + .insert(kind.inner().inner_muda(), position)?; + self_.0.items.lock().unwrap().insert(position, kind); + Ok::<_, muda::Error>(()) })? .map_err(Into::into) } @@ -311,7 +317,14 @@ impl Submenu { pub fn remove(&self, item: &dyn IsMenuItem) -> crate::Result<()> { let kind = item.kind(); run_item_main_thread!(self, |self_: Self| { - (*self_.0).as_ref().remove(kind.inner().inner_muda()) + (*self_.0).as_ref().remove(kind.inner().inner_muda())?; + self_ + .0 + .items + .lock() + .unwrap() + .retain(|i| i.id() != kind.id()); + Ok::<_, muda::Error>(()) })? .map_err(Into::into) } @@ -319,10 +332,8 @@ impl Submenu { /// Remove the menu item at the specified position from this submenu and returns it. pub fn remove_at(&self, position: usize) -> crate::Result>> { run_item_main_thread!(self, |self_: Self| { - (*self_.0) - .as_ref() - .remove_at(position) - .map(|i| MenuItemKind::from_muda(self_.0.app_handle.clone(), i)) + (*self_.0).as_ref().remove_at(position)?; + Some(self_.0.items.lock().unwrap().remove(position)) }) } @@ -333,22 +344,18 @@ impl Submenu { MenuId: PartialEq<&'a I>, { self - .items() - .unwrap_or_default() - .into_iter() + .0 + .items + .lock() + .unwrap() + .iter() .find(|i| i.id() == &id) + .cloned() } /// Returns a list of menu items that has been added to this submenu. pub fn items(&self) -> crate::Result>> { - run_item_main_thread!(self, |self_: Self| { - (*self_.0) - .as_ref() - .items() - .into_iter() - .map(|i| MenuItemKind::from_muda(self_.0.app_handle.clone(), i)) - .collect::>() - }) + Ok(self.0.items.lock().unwrap().clone()) } /// Get the text for this submenu.