mirror of
https://github.com/italicsjenga/muda.git
synced 2024-12-23 12:01:31 +11:00
feat: add into_id
methods to all menu item types (#112)
* feat: add `into_id` methods to all menu item types * fix: remove `into_id` from `IsMenuItem` and add `MenuItemKind::into_id` method
This commit is contained in:
parent
bce7540bba
commit
02e537ea06
5
.changes/item-into-id.md
Normal file
5
.changes/item-into-id.md
Normal file
|
@ -0,0 +1,5 @@
|
|||
---
|
||||
"muda": "patch"
|
||||
---
|
||||
|
||||
Added `into_id` method to `MenuItem`, `CheckMenuItem`, `PredefinedMenuItem`, `Submenu`, and `MenuItemKind`. It moves the menu item into its menu ID.
|
|
@ -2,7 +2,7 @@
|
|||
// SPDX-License-Identifier: Apache-2.inner
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
use std::{cell::RefCell, rc::Rc};
|
||||
use std::{cell::RefCell, mem, rc::Rc};
|
||||
|
||||
use crate::{accelerator::Accelerator, IsMenuItem, MenuId, MenuItemKind};
|
||||
|
||||
|
@ -117,4 +117,14 @@ impl CheckMenuItem {
|
|||
pub fn set_checked(&self, checked: bool) {
|
||||
self.inner.borrow_mut().set_checked(checked)
|
||||
}
|
||||
|
||||
/// Convert this menu item into its menu ID.
|
||||
pub fn into_id(mut self) -> MenuId {
|
||||
// Note: `Rc::into_inner` is available from Rust 1.70
|
||||
if let Some(id) = Rc::get_mut(&mut self.id) {
|
||||
mem::take(id)
|
||||
} else {
|
||||
self.id().clone()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
// SPDX-License-Identifier: Apache-2.inner
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
use std::{cell::RefCell, rc::Rc};
|
||||
use std::{cell::RefCell, mem, rc::Rc};
|
||||
|
||||
use crate::{
|
||||
accelerator::Accelerator,
|
||||
|
@ -180,4 +180,14 @@ impl IconMenuItem {
|
|||
#[cfg(target_os = "macos")]
|
||||
self.inner.borrow_mut().set_native_icon(_icon)
|
||||
}
|
||||
|
||||
/// Convert this menu item into its menu ID.
|
||||
pub fn into_id(mut self) -> MenuId {
|
||||
// Note: `Rc::into_inner` is available from Rust 1.70
|
||||
if let Some(id) = Rc::get_mut(&mut self.id) {
|
||||
mem::take(id)
|
||||
} else {
|
||||
self.id().clone()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -16,7 +16,7 @@ pub use submenu::*;
|
|||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use crate::{CheckMenuItem, IconMenuItem, MenuId, MenuItem, Submenu};
|
||||
use crate::{CheckMenuItem, IconMenuItem, MenuId, MenuItem, PredefinedMenuItem, Submenu};
|
||||
|
||||
#[test]
|
||||
fn it_returns_same_id() {
|
||||
|
@ -32,4 +32,29 @@ mod test {
|
|||
IconMenuItem::with_id(id.clone(), "", true, None, None).id()
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_convert_from_id_and_into_id() {
|
||||
let id = "TEST ID";
|
||||
let expected = MenuId(id.to_string());
|
||||
|
||||
let item = CheckMenuItem::with_id(id, "test", true, true, None);
|
||||
assert_eq!(item.id(), &expected);
|
||||
assert_eq!(item.into_id(), expected);
|
||||
|
||||
let item = IconMenuItem::with_id(id, "test", true, None, None);
|
||||
assert_eq!(item.id(), &expected);
|
||||
assert_eq!(item.into_id(), expected);
|
||||
|
||||
let item = MenuItem::with_id(id, "test", true, None);
|
||||
assert_eq!(item.id(), &expected);
|
||||
assert_eq!(item.into_id(), expected);
|
||||
|
||||
let item = Submenu::with_id(id, "test", true);
|
||||
assert_eq!(item.id(), &expected);
|
||||
assert_eq!(item.into_id(), expected);
|
||||
|
||||
let item = PredefinedMenuItem::separator();
|
||||
assert_eq!(item.id().clone(), item.into_id());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
use std::{cell::RefCell, rc::Rc};
|
||||
use std::{cell::RefCell, mem, rc::Rc};
|
||||
|
||||
use crate::{accelerator::Accelerator, IsMenuItem, MenuId, MenuItemKind};
|
||||
|
||||
|
@ -88,4 +88,14 @@ impl MenuItem {
|
|||
pub fn set_accelerator(&self, acccelerator: Option<Accelerator>) -> crate::Result<()> {
|
||||
self.inner.borrow_mut().set_accelerator(acccelerator)
|
||||
}
|
||||
|
||||
/// Convert this menu item into its menu ID.
|
||||
pub fn into_id(mut self) -> MenuId {
|
||||
// Note: `Rc::into_inner` is available from Rust 1.70
|
||||
if let Some(id) = Rc::get_mut(&mut self.id) {
|
||||
mem::take(id)
|
||||
} else {
|
||||
self.id().clone()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
// SPDX-License-Identifier: Apache-2.inner
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
use std::{cell::RefCell, rc::Rc};
|
||||
use std::{cell::RefCell, mem, rc::Rc};
|
||||
|
||||
use crate::{
|
||||
accelerator::{Accelerator, CMD_OR_CTRL},
|
||||
|
@ -181,6 +181,16 @@ impl PredefinedMenuItem {
|
|||
pub fn set_text<S: AsRef<str>>(&self, text: S) {
|
||||
self.inner.borrow_mut().set_text(text.as_ref())
|
||||
}
|
||||
|
||||
/// Convert this menu item into its menu ID.
|
||||
pub fn into_id(mut self) -> MenuId {
|
||||
// Note: `Rc::into_inner` is available from Rust 1.70
|
||||
if let Some(id) = Rc::get_mut(&mut self.id) {
|
||||
mem::take(id)
|
||||
} else {
|
||||
self.id().clone()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
// SPDX-License-Identifier: Apache-2.inner
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
use std::{cell::RefCell, rc::Rc};
|
||||
use std::{cell::RefCell, mem, rc::Rc};
|
||||
|
||||
use crate::{util::AddOp, ContextMenu, IsMenuItem, MenuId, MenuItemKind, Position};
|
||||
|
||||
|
@ -190,6 +190,16 @@ impl Submenu {
|
|||
pub fn set_as_help_menu_for_nsapp(&self) {
|
||||
self.inner.borrow_mut().set_as_help_menu_for_nsapp()
|
||||
}
|
||||
|
||||
/// Convert this submenu into its menu ID.
|
||||
pub fn into_id(mut self) -> MenuId {
|
||||
// Note: `Rc::into_inner` is available from Rust 1.70
|
||||
if let Some(id) = Rc::get_mut(&mut self.id) {
|
||||
mem::take(id)
|
||||
} else {
|
||||
self.id().clone()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl ContextMenu for Submenu {
|
||||
|
|
11
src/lib.rs
11
src/lib.rs
|
@ -256,6 +256,17 @@ impl MenuItemKind {
|
|||
_ => panic!("Not an IconMenuItem"),
|
||||
}
|
||||
}
|
||||
|
||||
/// Convert this item into its menu ID.
|
||||
pub fn into_id(self) -> MenuId {
|
||||
match self {
|
||||
MenuItemKind::MenuItem(i) => i.into_id(),
|
||||
MenuItemKind::Submenu(i) => i.into_id(),
|
||||
MenuItemKind::Predefined(i) => i.into_id(),
|
||||
MenuItemKind::Check(i) => i.into_id(),
|
||||
MenuItemKind::Icon(i) => i.into_id(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// A trait that defines a generic item in a menu, which may be one of [`MenuItemKind`]
|
||||
|
|
Loading…
Reference in a new issue