Skip to content

Commit

Permalink
feat(core/tauri): add try_get_item for SystemTray and WindowMenu, c…
Browse files Browse the repository at this point in the history
…loses #5491 (#6408)

* feat: try_get_item() for window menu

Add a method in the MenuHandle struct, that will return an Optional MenuItemHandle

feat: try_get_item() for systemtray

Add a method in the SystemTrayHandle struct, that will return an Optional SystemTrayMenuItemHandle

docs: features documented in ./changes/minor.md

fix: suggested changes

fix CI

* Update .changes/tray_get_item.md

---------
  • Loading branch information
mrjackwills authored May 12, 2023
1 parent 2c1fd57 commit 441f964
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changes/tray_get_item.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'tauri': minor
---

Add `MenuHandle::try_get_item` and `SystemTrayHandle::try_get_item` which returns a `Option` instead of panicking.
13 changes: 13 additions & 0 deletions core/tauri/src/app/tray.rs
Original file line number Diff line number Diff line change
Expand Up @@ -610,6 +610,19 @@ impl<R: Runtime> SystemTrayHandle<R> {
panic!("item id not found")
}

/// Attempts to get a handle to the menu item that has the specified `id`, return an error if `id` is not found.
pub fn try_get_item(&self, id: MenuIdRef<'_>) -> Option<SystemTrayMenuItemHandle<R>> {
self
.ids
.lock()
.unwrap()
.iter()
.find(|i| i.1 == id)
.map(|i| SystemTrayMenuItemHandle {
id: *i.0,
tray_handler: self.inner.clone(),
})
}
/// Updates the tray icon.
pub fn set_icon(&self, icon: Icon) -> crate::Result<()> {
self.inner.set_icon(icon.try_into()?).map_err(Into::into)
Expand Down
14 changes: 14 additions & 0 deletions core/tauri/src/window/menu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,20 @@ impl<R: Runtime> MenuHandle<R> {
panic!("item id not found")
}

/// Attempts to get a handle to the menu item that has the specified `id`, return an error if `id` is not found.
pub fn try_get_item(&self, id: MenuIdRef<'_>) -> Option<MenuItemHandle<R>> {
self
.ids
.lock()
.unwrap()
.iter()
.find(|i| i.1 == id)
.map(|i| MenuItemHandle {
id: *i.0,
dispatcher: self.dispatcher.clone(),
})
}

/// Shows the menu.
pub fn show(&self) -> crate::Result<()> {
self.dispatcher.show_menu().map_err(Into::into)
Expand Down

0 comments on commit 441f964

Please sign in to comment.