feat: add Menu/Submenu::remove_at (#93)

This commit is contained in:
Amr Bashir 2023-08-08 17:53:15 +03:00 committed by GitHub
parent 8c2019eba0
commit 043026c30d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 29 additions and 2 deletions

5
.changes/remove_at.md Normal file
View file

@ -0,0 +1,5 @@
---
"muda": "patch"
---
Add `Menu/Submenu::remove_at` to remove an item at specified index.

View file

@ -132,6 +132,18 @@ impl Submenu {
self.inner.borrow_mut().remove(item)
}
/// Remove the menu item at the specified position from this submenu.
pub fn remove_at(&self, position: usize) -> Option<MenuItemKind> {
let mut items = self.items();
if items.len() > position {
let item = items.remove(position);
let _ = self.remove(item.as_ref());
Some(item)
} else {
None
}
}
/// Returns a list of menu items that has been added to this submenu.
pub fn items(&self) -> Vec<MenuItemKind> {
self.inner.borrow().items()
@ -159,8 +171,6 @@ impl Submenu {
self.inner.borrow_mut().set_enabled(enabled)
}
// TODO: in a minor release, rename the following two functions to be `set_as_*`
/// Set this submenu as the Window menu for the application on macOS.
///
/// This will cause macOS to automatically add window-switching items and

View file

@ -144,6 +144,18 @@ impl Menu {
self.inner.borrow_mut().remove(item)
}
/// Remove the menu item at the specified position from this menu.
pub fn remove_at(&self, position: usize) -> Option<MenuItemKind> {
let mut items = self.items();
if items.len() > position {
let item = items.remove(position);
let _ = self.remove(item.as_ref());
Some(item)
} else {
None
}
}
/// Returns a list of menu items that has been added to this menu.
pub fn items(&self) -> Vec<MenuItemKind> {
self.inner.borrow().items()