Skip to content

Commit

Permalink
feat: add into_id methods to all menu item types (#112)
Browse files Browse the repository at this point in the history
* feat: add `into_id` methods to all menu item types

* fix: remove `into_id` from `IsMenuItem` and add `MenuItemKind::into_id` method
  • Loading branch information
rhysd authored Sep 1, 2023
1 parent bce7540 commit 02e537e
Show file tree
Hide file tree
Showing 8 changed files with 97 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changes/item-into-id.md
Original file line number Diff line number Diff line change
@@ -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.
12 changes: 11 additions & 1 deletion src/items/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down Expand Up @@ -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()
}
}
}
12 changes: 11 additions & 1 deletion src/items/icon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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()
}
}
}
27 changes: 26 additions & 1 deletion src/items/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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());
}
}
12 changes: 11 additions & 1 deletion src/items/normal.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{cell::RefCell, rc::Rc};
use std::{cell::RefCell, mem, rc::Rc};

use crate::{accelerator::Accelerator, IsMenuItem, MenuId, MenuItemKind};

Expand Down Expand Up @@ -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()
}
}
}
12 changes: 11 additions & 1 deletion src/items/predefined.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down Expand Up @@ -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]
Expand Down
12 changes: 11 additions & 1 deletion src/items/submenu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down Expand Up @@ -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 {
Expand Down
11 changes: 11 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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`]
Expand Down

0 comments on commit 02e537e

Please sign in to comment.