Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions .changes/store-tauri-menu-items.md
Original file line number Diff line number Diff line change
@@ -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.
51 changes: 30 additions & 21 deletions crates/tauri/src/menu/menu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,9 @@ impl<R: Runtime> Menu<R> {
pub fn append(&self, item: &dyn IsMenuItem<R>) -> 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)
}
Expand Down Expand Up @@ -294,7 +296,9 @@ impl<R: Runtime> Menu<R> {
pub fn prepend(&self, item: &dyn IsMenuItem<R>) -> 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)
}
Expand All @@ -319,9 +323,13 @@ impl<R: Runtime> Menu<R> {
/// [`Submenu`]: super::Submenu
pub fn insert(&self, item: &dyn IsMenuItem<R>, 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)
}

Expand All @@ -344,18 +352,23 @@ impl<R: Runtime> Menu<R> {
pub fn remove(&self, item: &dyn IsMenuItem<R>) -> 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)
}

/// Remove the menu item at the specified position from this menu and returns it.
pub fn remove_at(&self, position: usize) -> crate::Result<Option<MenuItemKind<R>>> {
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))
})
}

Expand All @@ -366,22 +379,18 @@ impl<R: Runtime> Menu<R> {
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<Vec<MenuItemKind<R>>> {
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::<Vec<_>>()
})
Ok(self.0.items.lock().unwrap().clone())
}

/// Set this menu as the application menu.
Expand Down
35 changes: 12 additions & 23 deletions crates/tauri/src/menu/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -66,6 +66,7 @@ macro_rules! gen_wrappers {
$(
$(#[$attr:meta])*
$type:ident($inner:ident$(, $kind:ident)?)
$({ $($(#[$field_attr:meta])* $field:ident: $field_type:ty),* })?
),*
) => {
$(
Expand All @@ -74,13 +75,15 @@ 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<R>,
$($( $(#[$field_attr])* $field: $field_type,)*)?
}

impl<R: $crate::Runtime> $inner<R> {
fn new(app_handle: $crate::AppHandle<R>, menu: ::muda::$type) -> Self {
Self {
inner: ManuallyDrop::new(menu),
app_handle,
$($($field: Default::default(),)*)?
}
}
}
Expand Down Expand Up @@ -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<Vec<MenuItemKind<R>>>
},
/// 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<Vec<MenuItemKind<R>>>
},
/// 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`]
Expand Down Expand Up @@ -574,26 +583,6 @@ impl<R: Runtime> MenuItemKind<R> {
}
}

pub(crate) fn from_muda(app_handle: AppHandle<R>, 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<R>> {
match self {
Expand Down
45 changes: 26 additions & 19 deletions crates/tauri/src/menu/submenu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,9 @@ impl<R: Runtime> Submenu<R> {
pub fn append(&self, item: &dyn IsMenuItem<R>) -> 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)
}
Expand All @@ -277,7 +279,9 @@ impl<R: Runtime> Submenu<R> {
pub fn prepend(&self, item: &dyn IsMenuItem<R>) -> 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)
}
Expand All @@ -293,7 +297,9 @@ impl<R: Runtime> Submenu<R> {
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)
}
Expand All @@ -311,18 +317,23 @@ impl<R: Runtime> Submenu<R> {
pub fn remove(&self, item: &dyn IsMenuItem<R>) -> 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)
}

/// Remove the menu item at the specified position from this submenu and returns it.
pub fn remove_at(&self, position: usize) -> crate::Result<Option<MenuItemKind<R>>> {
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))
})
}

Expand All @@ -333,22 +344,18 @@ impl<R: Runtime> Submenu<R> {
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<Vec<MenuItemKind<R>>> {
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::<Vec<_>>()
})
Ok(self.0.items.lock().unwrap().clone())
}

/// Get the text for this submenu.
Expand Down
Loading