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:
Linda_pp 2023-09-01 20:59:42 +09:00 committed by GitHub
parent bce7540bba
commit 02e537ea06
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 97 additions and 6 deletions

5
.changes/item-into-id.md Normal file
View 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.

View file

@ -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()
}
}
}

View file

@ -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()
}
}
}

View file

@ -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());
}
}

View file

@ -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()
}
}
}

View file

@ -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]

View file

@ -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 {

View file

@ -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`]