mirror of
https://github.com/italicsjenga/muda.git
synced 2025-01-10 03:41:30 +11:00
refactor: seal IsMenuItem
trait
This commit is contained in:
parent
02e537ea06
commit
264aed722d
|
@ -2,4 +2,4 @@
|
||||||
"muda": "patch"
|
"muda": "patch"
|
||||||
---
|
---
|
||||||
|
|
||||||
Added `into_id` method to `MenuItem`, `CheckMenuItem`, `PredefinedMenuItem`, `Submenu`, and `MenuItemKind`. It moves the menu item into its menu ID.
|
Added `into_id` method to `MenuItem`, `CheckMenuItem`, `PredefinedMenuItem`, `Submenu`, `MenuItemKind` and `IsMenuItem` trait. It moves the menu item into its id.
|
||||||
|
|
|
@ -4,7 +4,7 @@
|
||||||
|
|
||||||
use std::{cell::RefCell, mem, rc::Rc};
|
use std::{cell::RefCell, mem, rc::Rc};
|
||||||
|
|
||||||
use crate::{accelerator::Accelerator, IsMenuItem, MenuId, MenuItemKind};
|
use crate::{accelerator::Accelerator, sealed::IsMenuItemBase, IsMenuItem, MenuId, MenuItemKind};
|
||||||
|
|
||||||
/// A check menu item inside a [`Menu`] or [`Submenu`]
|
/// A check menu item inside a [`Menu`] or [`Submenu`]
|
||||||
/// and usually contains a text and a check mark or a similar toggle
|
/// and usually contains a text and a check mark or a similar toggle
|
||||||
|
@ -18,7 +18,8 @@ pub struct CheckMenuItem {
|
||||||
pub(crate) inner: Rc<RefCell<crate::platform_impl::MenuChild>>,
|
pub(crate) inner: Rc<RefCell<crate::platform_impl::MenuChild>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
unsafe impl IsMenuItem for CheckMenuItem {
|
impl IsMenuItemBase for CheckMenuItem {}
|
||||||
|
impl IsMenuItem for CheckMenuItem {
|
||||||
fn kind(&self) -> MenuItemKind {
|
fn kind(&self) -> MenuItemKind {
|
||||||
MenuItemKind::Check(self.clone())
|
MenuItemKind::Check(self.clone())
|
||||||
}
|
}
|
||||||
|
@ -26,6 +27,10 @@ unsafe impl IsMenuItem for CheckMenuItem {
|
||||||
fn id(&self) -> &MenuId {
|
fn id(&self) -> &MenuId {
|
||||||
self.id()
|
self.id()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn into_id(self) -> MenuId {
|
||||||
|
self.into_id()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl CheckMenuItem {
|
impl CheckMenuItem {
|
||||||
|
|
|
@ -7,6 +7,7 @@ use std::{cell::RefCell, mem, rc::Rc};
|
||||||
use crate::{
|
use crate::{
|
||||||
accelerator::Accelerator,
|
accelerator::Accelerator,
|
||||||
icon::{Icon, NativeIcon},
|
icon::{Icon, NativeIcon},
|
||||||
|
sealed::IsMenuItemBase,
|
||||||
IsMenuItem, MenuId, MenuItemKind,
|
IsMenuItem, MenuId, MenuItemKind,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -21,7 +22,8 @@ pub struct IconMenuItem {
|
||||||
pub(crate) inner: Rc<RefCell<crate::platform_impl::MenuChild>>,
|
pub(crate) inner: Rc<RefCell<crate::platform_impl::MenuChild>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
unsafe impl IsMenuItem for IconMenuItem {
|
impl IsMenuItemBase for IconMenuItem {}
|
||||||
|
impl IsMenuItem for IconMenuItem {
|
||||||
fn kind(&self) -> MenuItemKind {
|
fn kind(&self) -> MenuItemKind {
|
||||||
MenuItemKind::Icon(self.clone())
|
MenuItemKind::Icon(self.clone())
|
||||||
}
|
}
|
||||||
|
@ -29,6 +31,10 @@ unsafe impl IsMenuItem for IconMenuItem {
|
||||||
fn id(&self) -> &MenuId {
|
fn id(&self) -> &MenuId {
|
||||||
self.id()
|
self.id()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn into_id(self) -> MenuId {
|
||||||
|
self.into_id()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl IconMenuItem {
|
impl IconMenuItem {
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
use std::{cell::RefCell, mem, rc::Rc};
|
use std::{cell::RefCell, mem, rc::Rc};
|
||||||
|
|
||||||
use crate::{accelerator::Accelerator, IsMenuItem, MenuId, MenuItemKind};
|
use crate::{accelerator::Accelerator, sealed::IsMenuItemBase, IsMenuItem, MenuId, MenuItemKind};
|
||||||
|
|
||||||
/// A menu item inside a [`Menu`] or [`Submenu`] and contains only text.
|
/// A menu item inside a [`Menu`] or [`Submenu`] and contains only text.
|
||||||
///
|
///
|
||||||
|
@ -12,7 +12,8 @@ pub struct MenuItem {
|
||||||
pub(crate) inner: Rc<RefCell<crate::platform_impl::MenuChild>>,
|
pub(crate) inner: Rc<RefCell<crate::platform_impl::MenuChild>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
unsafe impl IsMenuItem for MenuItem {
|
impl IsMenuItemBase for MenuItem {}
|
||||||
|
impl IsMenuItem for MenuItem {
|
||||||
fn kind(&self) -> MenuItemKind {
|
fn kind(&self) -> MenuItemKind {
|
||||||
MenuItemKind::MenuItem(self.clone())
|
MenuItemKind::MenuItem(self.clone())
|
||||||
}
|
}
|
||||||
|
@ -20,6 +21,10 @@ unsafe impl IsMenuItem for MenuItem {
|
||||||
fn id(&self) -> &MenuId {
|
fn id(&self) -> &MenuId {
|
||||||
self.id()
|
self.id()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn into_id(self) -> MenuId {
|
||||||
|
self.into_id()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl MenuItem {
|
impl MenuItem {
|
||||||
|
|
|
@ -6,6 +6,7 @@ use std::{cell::RefCell, mem, rc::Rc};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
accelerator::{Accelerator, CMD_OR_CTRL},
|
accelerator::{Accelerator, CMD_OR_CTRL},
|
||||||
|
sealed::IsMenuItemBase,
|
||||||
AboutMetadata, IsMenuItem, MenuId, MenuItemKind,
|
AboutMetadata, IsMenuItem, MenuId, MenuItemKind,
|
||||||
};
|
};
|
||||||
use keyboard_types::{Code, Modifiers};
|
use keyboard_types::{Code, Modifiers};
|
||||||
|
@ -17,7 +18,8 @@ pub struct PredefinedMenuItem {
|
||||||
pub(crate) inner: Rc<RefCell<crate::platform_impl::MenuChild>>,
|
pub(crate) inner: Rc<RefCell<crate::platform_impl::MenuChild>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
unsafe impl IsMenuItem for PredefinedMenuItem {
|
impl IsMenuItemBase for PredefinedMenuItem {}
|
||||||
|
impl IsMenuItem for PredefinedMenuItem {
|
||||||
fn kind(&self) -> MenuItemKind {
|
fn kind(&self) -> MenuItemKind {
|
||||||
MenuItemKind::Predefined(self.clone())
|
MenuItemKind::Predefined(self.clone())
|
||||||
}
|
}
|
||||||
|
@ -25,6 +27,10 @@ unsafe impl IsMenuItem for PredefinedMenuItem {
|
||||||
fn id(&self) -> &MenuId {
|
fn id(&self) -> &MenuId {
|
||||||
self.id()
|
self.id()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn into_id(self) -> MenuId {
|
||||||
|
self.into_id()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl PredefinedMenuItem {
|
impl PredefinedMenuItem {
|
||||||
|
|
|
@ -4,7 +4,9 @@
|
||||||
|
|
||||||
use std::{cell::RefCell, mem, rc::Rc};
|
use std::{cell::RefCell, mem, rc::Rc};
|
||||||
|
|
||||||
use crate::{util::AddOp, ContextMenu, IsMenuItem, MenuId, MenuItemKind, Position};
|
use crate::{
|
||||||
|
sealed::IsMenuItemBase, util::AddOp, ContextMenu, IsMenuItem, MenuId, MenuItemKind, Position,
|
||||||
|
};
|
||||||
|
|
||||||
/// A menu that can be added to a [`Menu`] or another [`Submenu`].
|
/// A menu that can be added to a [`Menu`] or another [`Submenu`].
|
||||||
///
|
///
|
||||||
|
@ -15,7 +17,8 @@ pub struct Submenu {
|
||||||
pub(crate) inner: Rc<RefCell<crate::platform_impl::MenuChild>>,
|
pub(crate) inner: Rc<RefCell<crate::platform_impl::MenuChild>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
unsafe impl IsMenuItem for Submenu {
|
impl IsMenuItemBase for Submenu {}
|
||||||
|
impl IsMenuItem for Submenu {
|
||||||
fn kind(&self) -> MenuItemKind {
|
fn kind(&self) -> MenuItemKind {
|
||||||
MenuItemKind::Submenu(self.clone())
|
MenuItemKind::Submenu(self.clone())
|
||||||
}
|
}
|
||||||
|
@ -23,6 +26,10 @@ unsafe impl IsMenuItem for Submenu {
|
||||||
fn id(&self) -> &MenuId {
|
fn id(&self) -> &MenuId {
|
||||||
self.id()
|
self.id()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn into_id(self) -> MenuId {
|
||||||
|
self.into_id()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Submenu {
|
impl Submenu {
|
||||||
|
|
17
src/lib.rs
17
src/lib.rs
|
@ -166,7 +166,7 @@ pub enum MenuItemKind {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl MenuItemKind {
|
impl MenuItemKind {
|
||||||
/// Returns the id associated with this menu item.
|
/// Returns a unique identifier associated with this menu item.
|
||||||
pub fn id(&self) -> &MenuId {
|
pub fn id(&self) -> &MenuId {
|
||||||
match self {
|
match self {
|
||||||
MenuItemKind::MenuItem(i) => i.id(),
|
MenuItemKind::MenuItem(i) => i.id(),
|
||||||
|
@ -270,14 +270,17 @@ impl MenuItemKind {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// A trait that defines a generic item in a menu, which may be one of [`MenuItemKind`]
|
/// A trait that defines a generic item in a menu, which may be one of [`MenuItemKind`]
|
||||||
///
|
pub trait IsMenuItem: sealed::IsMenuItemBase {
|
||||||
/// # Safety
|
/// Returns a [`MenuItemKind`] associated with this item.
|
||||||
///
|
|
||||||
/// This trait is ONLY meant to be implemented internally by the crate.
|
|
||||||
pub unsafe trait IsMenuItem {
|
|
||||||
fn kind(&self) -> MenuItemKind;
|
fn kind(&self) -> MenuItemKind;
|
||||||
|
/// Returns a unique identifier associated with this menu item.
|
||||||
fn id(&self) -> &MenuId;
|
fn id(&self) -> &MenuId;
|
||||||
|
/// Convert this menu item into its menu ID.
|
||||||
|
fn into_id(self) -> MenuId;
|
||||||
|
}
|
||||||
|
|
||||||
|
mod sealed {
|
||||||
|
pub trait IsMenuItemBase {}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, PartialEq, PartialOrd, Clone, Copy)]
|
#[derive(Debug, PartialEq, PartialOrd, Clone, Copy)]
|
||||||
|
|
Loading…
Reference in a new issue