add checkmark for menu items

This commit is contained in:
Alex Janka 2023-11-28 16:20:28 +11:00
parent 5db4785825
commit 6952cc2429

View file

@ -36,7 +36,7 @@ fn make_menu_item<S: AsRef<str>>(
title: S, title: S,
key: Option<&str>, key: Option<&str>,
action: Option<Sel>, action: Option<Sel>,
modifiers: Option<&[EventModifierFlag]> modifiers: Option<&[EventModifierFlag]>,
) -> Id<Object, Owned> { ) -> Id<Object, Owned> {
unsafe { unsafe {
let title = NSString::new(title.as_ref()); let title = NSString::new(title.as_ref());
@ -44,7 +44,7 @@ fn make_menu_item<S: AsRef<str>>(
// Note that AppKit requires a blank string if nil, not nil. // Note that AppKit requires a blank string if nil, not nil.
let key = NSString::new(match key { let key = NSString::new(match key {
Some(s) => s, Some(s) => s,
None => "" None => "",
}); });
// Stock menu items that use selectors targeted at system pieces are just standard // Stock menu items that use selectors targeted at system pieces are just standard
@ -63,7 +63,7 @@ fn make_menu_item<S: AsRef<str>>(
initWithTitle: &*title, initWithTitle: &*title,
action: sel!(fireBlockAction:), action: sel!(fireBlockAction:),
keyEquivalent: &*key, keyEquivalent: &*key,
] ],
}; };
if let Some(modifiers) = modifiers { if let Some(modifiers) = modifiers {
@ -151,7 +151,7 @@ pub enum MenuItem {
/// Represents a Separator. It's useful nonetheless for /// Represents a Separator. It's useful nonetheless for
/// separating out pieces of the `NSMenu` structure. /// separating out pieces of the `NSMenu` structure.
Separator Separator,
} }
impl MenuItem { impl MenuItem {
@ -186,7 +186,7 @@ impl MenuItem {
"Hide Others", "Hide Others",
Some("h"), Some("h"),
Some(sel!(hide:)), Some(sel!(hide:)),
Some(&[EventModifierFlag::Command, EventModifierFlag::Option]) Some(&[EventModifierFlag::Command, EventModifierFlag::Option]),
), ),
Self::ShowAll => make_menu_item("Show All", None, Some(sel!(unhideAllApplications:)), None), Self::ShowAll => make_menu_item("Show All", None, Some(sel!(unhideAllApplications:)), None),
@ -203,7 +203,7 @@ impl MenuItem {
"Enter Full Screen", "Enter Full Screen",
Some("f"), Some("f"),
Some(sel!(toggleFullScreen:)), Some(sel!(toggleFullScreen:)),
Some(&[EventModifierFlag::Command, EventModifierFlag::Control]) Some(&[EventModifierFlag::Command, EventModifierFlag::Control]),
), ),
Self::Minimize => make_menu_item("Minimize", Some("m"), Some(sel!(performMiniaturize:)), None), Self::Minimize => make_menu_item("Minimize", Some("m"), Some(sel!(performMiniaturize:)), None),
@ -213,13 +213,13 @@ impl MenuItem {
"Toggle Sidebar", "Toggle Sidebar",
Some("s"), Some("s"),
Some(sel!(toggleSidebar:)), Some(sel!(toggleSidebar:)),
Some(&[EventModifierFlag::Command, EventModifierFlag::Option]) Some(&[EventModifierFlag::Command, EventModifierFlag::Option]),
), ),
Self::Separator => { Self::Separator => {
let cls = class!(NSMenuItem); let cls = class!(NSMenuItem);
msg_send_id![cls, separatorItem] msg_send_id![cls, separatorItem]
} },
} }
} }
@ -287,6 +287,19 @@ impl MenuItem {
self self
} }
pub fn checkmark(self, enabled: bool) -> Self {
if let MenuItem::Custom(objc) = self {
unsafe {
let enabled: NSUInteger = if enabled { 1 } else { 0 };
let _: () = msg_send![&*objc, setState: enabled];
}
return MenuItem::Custom(objc);
}
self
}
} }
/// On the Objective-C side, we need to ensure our handler is dropped when this subclass /// On the Objective-C side, we need to ensure our handler is dropped when this subclass