rust_macios/appkit/
ns_menu_item.rs

1use objc::{msg_send, runtime::Sel, sel, sel_impl};
2
3use crate::{
4    foundation::NSString,
5    object,
6    objective_c_runtime::{
7        id, nil,
8        traits::{FromId, PNSObject},
9    },
10    utils::to_bool,
11};
12
13use super::{interface_impl, NSMenu};
14
15object! {
16    /// A command item in an app menu.
17    unsafe pub struct NSMenuItem;
18}
19
20#[interface_impl(NSObject)]
21impl NSMenuItem {
22    /* Enabling a Menu Item
23     */
24
25    /// A Boolean value that indicates whether the menu item is enabled.
26    #[property]
27    pub fn is_enabled(&self) -> bool {
28        unsafe { to_bool(msg_send![self.m_self(), isEnabled]) }
29    }
30
31    /// Sets whether the menu item is enabled.
32    #[property]
33    pub fn set_enabled(&mut self, enabled: bool) {
34        unsafe { msg_send![self.m_self(), setEnabled: enabled] }
35    }
36
37    /* Managing Hidden Status
38     */
39
40    /// A Boolean value that indicates whether the menu item is hidden.
41    #[property]
42    pub fn is_hidden(&self) -> bool {
43        unsafe { to_bool(msg_send![self.m_self(), isHidden]) }
44    }
45
46    /// Sets whether the menu item is hidden.
47    #[property]
48    pub fn set_hidden(&mut self, hidden: bool) {
49        unsafe { msg_send![self.m_self(), setHidden: hidden] }
50    }
51
52    /// A Boolean value that indicates whether the menu item or any of its superitems is hidden.
53    #[property]
54    pub fn is_hidden_or_has_hidden_ancestor(&self) -> bool {
55        unsafe { to_bool(msg_send![self.m_self(), isHiddenOrHasHiddenAncestor]) }
56    }
57
58    /*  Managing the Target and Action */
59
60    /// The menu item's target.
61    #[property]
62    pub fn target(&self) -> id {
63        unsafe { msg_send![self.m_self(), target] }
64    }
65
66    /// Sets the menu item's target.
67    #[property]
68    pub fn set_target(&mut self, target: id) {
69        unsafe { msg_send![self.m_self(), setTarget: target] }
70    }
71
72    /// The menu item's action-method selector.
73    #[property]
74    pub fn action(&self) -> Sel {
75        unsafe { msg_send![self.m_self(), action] }
76    }
77
78    /// Sets the menu item's action-method selector.
79    #[property]
80    pub fn set_action(&mut self, action: Sel) {
81        unsafe { msg_send![self.m_self(), setAction: action] }
82    }
83
84    /* Managing the Title
85     */
86
87    /// The menu item's title.
88    #[property]
89    pub fn title(&self) -> NSString {
90        unsafe { NSString::from_id(msg_send![self.m_self(), title]) }
91    }
92
93    /// Sets the menu item's title.
94    #[property]
95    pub fn set_title(&mut self, title: NSString) {
96        unsafe { msg_send![self.m_self(), setTitle: title] }
97    }
98
99    /* Managing Submenus
100     */
101
102    /// The submenu of the menu item.
103    #[property]
104    pub fn submenu(&self) -> NSMenu {
105        unsafe { NSMenu::from_id(msg_send![self.m_self(), submenu]) }
106    }
107
108    /// Sets the submenu of the menu item.
109    #[property]
110    pub fn set_submenu(&mut self, submenu: NSMenu) {
111        unsafe { msg_send![self.m_self(), setSubmenu: submenu] }
112    }
113
114    /// A Boolean value that indicates whether the menu item has a submenu.
115    #[property]
116    pub fn has_submenu(&self) -> bool {
117        unsafe { to_bool(msg_send![self.m_self(), hasSubmenu]) }
118    }
119
120    /// The menu item whose submenu contains the receiver.
121    #[property]
122    pub fn parent_item(&self) -> Option<NSMenuItem> {
123        unsafe {
124            let id = msg_send![self.m_self(), parentItem];
125            if id == nil {
126                None
127            } else {
128                Some(NSMenuItem::from_id(id))
129            }
130        }
131    }
132}
133
134impl Default for NSMenuItem {
135    fn default() -> Self {
136        Self::m_new()
137    }
138}