rust_macios/appkit/
ns_menu.rs

1use objc::{class, msg_send, runtime::Sel, sel, sel_impl};
2
3use crate::{
4    core_graphics::CGFloat,
5    foundation::{Int, NSString},
6    object,
7    objective_c_runtime::traits::{FromId, PNSObject},
8    utils::to_bool,
9};
10
11use super::{interface_impl, NSMenuItem};
12
13object! {
14    /// An object that manages an app’s menus.
15    unsafe pub struct NSMenu;
16}
17
18impl NSMenu {
19    /// Returns a new `NSMenu` instance.
20    pub fn new() -> Self {
21        unsafe { Self::from_id(msg_send![class!(NSMenu), new]) }
22    }
23}
24
25#[interface_impl(NSObject)]
26impl NSMenu {
27    /* Managing the Menu Bar
28     */
29
30    /// Returns a Boolean value that indicates whether the menu bar is visible.
31    #[method]
32    pub fn menu_bar_visible() -> bool {
33        unsafe { to_bool(msg_send![Self::m_class(), isMenuBarVisible]) }
34    }
35
36    /// Sets whether the menu bar is visible and selectable by the user.
37    #[method]
38    pub fn set_menu_bar_visible(visible: bool) {
39        unsafe { msg_send![Self::m_class(), setMenuBarVisible: visible] }
40    }
41
42    /// The menu bar height for the main menu in pixels.
43    #[property]
44    pub fn menu_bar_height(&self) -> CGFloat {
45        unsafe { msg_send![self.m_self(), menuBarHeight] }
46    }
47
48    /* Creating an NSMenu Object
49     */
50
51    /// Initializes and returns a menu having the specified title and with
52    /// autoenabling of menu items turned on.
53    #[method]
54    pub fn init_with_title(title: NSString) -> Self
55    where
56        Self: Sized + FromId,
57    {
58        unsafe { Self::from_id(msg_send![Self::m_class(), initWithTitle: title]) }
59    }
60
61    /* Adding and Removing Menu Items
62     */
63
64    /// Inserts a menu item into the menu at a specific location.
65    #[method]
66    pub fn insert_item_at_index(&mut self, new_item: NSMenuItem, index: Int) {
67        unsafe { msg_send![self.m_self(), insertItem: new_item atIndex: index] }
68    }
69
70    /// Creates and adds a menu item at a specified location in the menu.
71    #[method]
72    pub fn insert_item_with_title_action_key_equivalent_at_index(
73        &mut self,
74        string: NSString,
75        sel: Sel,
76        char_code: NSString,
77        index: Int,
78    ) -> NSMenuItem {
79        unsafe {
80            NSMenuItem::from_id(msg_send![
81                self.m_self(),
82                insertItemWithTitle: string
83                action: sel
84                keyEquivalent: char_code
85                atIndex: index
86            ])
87        }
88    }
89
90    /// Adds a menu item to the end of the menu.
91    #[method]
92    pub fn add_item(&mut self, new_item: NSMenuItem) {
93        unsafe { msg_send![self.m_self(), addItem: new_item] }
94    }
95
96    /// Creates a new menu item and adds it to the end of the menu.
97    #[method]
98    pub fn add_item_with_title_action_key_equivalent(
99        &mut self,
100        title: NSString,
101        sel: Sel,
102        char_code: NSString,
103    ) -> NSMenuItem {
104        unsafe {
105            NSMenuItem::from_id(msg_send![
106                self.m_self(),
107                addItemWithTitle: title
108                action: sel
109                keyEquivalent: char_code
110            ])
111        }
112    }
113
114    /// Removes a menu item from the menu.
115    #[method]
116    pub fn remove_item(&mut self, item: NSMenuItem) {
117        unsafe { msg_send![self.m_self(), removeItem: item] }
118    }
119
120    /// Removes the menu item at a specified location in the menu.
121    #[method]
122    pub fn remove_item_at_index(&mut self, index: Int) {
123        unsafe { msg_send![self.m_self(), removeItemAtIndex: index] }
124    }
125
126    /// Removes all the menu items in the menu.
127    #[method]
128    pub fn remove_all_items(&mut self) {
129        unsafe { msg_send![self.m_self(), removeAllItems] }
130    }
131}
132
133impl Default for NSMenu {
134    fn default() -> Self {
135        Self::new()
136    }
137}