diff --git a/.changes/remove_at.md b/.changes/remove_at.md new file mode 100644 index 0000000..d4484ba --- /dev/null +++ b/.changes/remove_at.md @@ -0,0 +1,5 @@ +--- +"muda": "patch" +--- + +Add `Menu/Submenu::remove_at` to remove an item at specified index. diff --git a/src/items/submenu.rs b/src/items/submenu.rs index 3f288fd..eb997fa 100644 --- a/src/items/submenu.rs +++ b/src/items/submenu.rs @@ -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 { + 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 { 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 diff --git a/src/menu.rs b/src/menu.rs index 0b20dea..bf84e1b 100644 --- a/src/menu.rs +++ b/src/menu.rs @@ -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 { + 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 { self.inner.borrow().items()