add checkmark for menu items

This commit is contained in:
Alex Janka 2023-11-28 16:20:28 +11:00
parent 4fd93e3fae
commit 65b32e79c0

View file

@ -36,7 +36,7 @@ fn make_menu_item<S: AsRef<str>>(
title: S,
key: Option<&str>,
action: Option<Sel>,
modifiers: Option<&[EventModifierFlag]>
modifiers: Option<&[EventModifierFlag]>,
) -> Id<Object, Owned> {
unsafe {
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.
let key = NSString::new(match key {
Some(s) => s,
None => ""
None => "",
});
// 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,
action: sel!(fireBlockAction:),
keyEquivalent: &*key,
]
],
};
if let Some(modifiers) = modifiers {
@ -151,7 +151,7 @@ pub enum MenuItem {
/// Represents a Separator. It's useful nonetheless for
/// separating out pieces of the `NSMenu` structure.
Separator
Separator,
}
impl MenuItem {
@ -186,7 +186,7 @@ impl MenuItem {
"Hide Others",
Some("h"),
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),
@ -203,7 +203,7 @@ impl MenuItem {
"Enter Full Screen",
Some("f"),
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),
@ -213,13 +213,13 @@ impl MenuItem {
"Toggle Sidebar",
Some("s"),
Some(sel!(toggleSidebar:)),
Some(&[EventModifierFlag::Command, EventModifierFlag::Option])
Some(&[EventModifierFlag::Command, EventModifierFlag::Option]),
),
Self::Separator => {
let cls = class!(NSMenuItem);
msg_send_id![cls, separatorItem]
}
},
}
}
@ -287,6 +287,19 @@ impl MenuItem {
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