rust_macios/appkit/
ns_menu.rs1use 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 unsafe pub struct NSMenu;
16}
17
18impl NSMenu {
19 pub fn new() -> Self {
21 unsafe { Self::from_id(msg_send![class!(NSMenu), new]) }
22 }
23}
24
25#[interface_impl(NSObject)]
26impl NSMenu {
27 #[method]
32 pub fn menu_bar_visible() -> bool {
33 unsafe { to_bool(msg_send![Self::m_class(), isMenuBarVisible]) }
34 }
35
36 #[method]
38 pub fn set_menu_bar_visible(visible: bool) {
39 unsafe { msg_send![Self::m_class(), setMenuBarVisible: visible] }
40 }
41
42 #[property]
44 pub fn menu_bar_height(&self) -> CGFloat {
45 unsafe { msg_send![self.m_self(), menuBarHeight] }
46 }
47
48 #[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 #[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 #[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 #[method]
92 pub fn add_item(&mut self, new_item: NSMenuItem) {
93 unsafe { msg_send![self.m_self(), addItem: new_item] }
94 }
95
96 #[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 #[method]
116 pub fn remove_item(&mut self, item: NSMenuItem) {
117 unsafe { msg_send![self.m_self(), removeItem: item] }
118 }
119
120 #[method]
122 pub fn remove_item_at_index(&mut self, index: Int) {
123 unsafe { msg_send![self.m_self(), removeItemAtIndex: index] }
124 }
125
126 #[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}