Add init_for_nsapp

This commit is contained in:
Yu-Wei Wu 2022-05-08 14:32:31 +08:00
parent b5886fe6b1
commit 413358ed85
No known key found for this signature in database
GPG key ID: 3C5E0AA459C7E6E4
5 changed files with 34 additions and 1 deletions

View file

@ -20,6 +20,8 @@ let cut_item = file_menu.add_text_item("Cut", true);
menu.init_for_hwnd(window.hwnd() as isize); menu.init_for_hwnd(window.hwnd() as isize);
#[cfg(target_os = "linux")] #[cfg(target_os = "linux")]
menu.init_for_gtk_window(&gtk_window); menu.init_for_gtk_window(&gtk_window);
#[cfg(target_os = "macos")]
menu.init_for_nsapp();
``` ```
Then listen for the events Then listen for the events
```rs ```rs

View file

@ -38,6 +38,11 @@ fn main() {
menu_bar.init_for_gtk_window(window2.gtk_window()); menu_bar.init_for_gtk_window(window2.gtk_window());
} }
#[cfg(target_os = "macos")]
{
menu_bar.init_for_nsapp();
}
let menu_channel = menu_event_receiver(); let menu_channel = menu_event_receiver();
let mut open_item_disabled = false; let mut open_item_disabled = false;
let mut counter = 0; let mut counter = 0;

View file

@ -31,6 +31,11 @@ fn main() {
menu_bar.init_for_hwnd(_window2.hwnd() as _); menu_bar.init_for_hwnd(_window2.hwnd() as _);
} }
#[cfg(target_os = "macos")]
{
menu_bar.init_for_nsapp();
}
let menu_channel = menu_event_receiver(); let menu_channel = menu_event_receiver();
let mut open_item_disabled = false; let mut open_item_disabled = false;
let mut counter = 0; let mut counter = 0;

View file

@ -40,6 +40,8 @@
//! menu.init_for_hwnd(window.hwnd() as isize); //! menu.init_for_hwnd(window.hwnd() as isize);
//! #[cfg(target_os = "linux")] //! #[cfg(target_os = "linux")]
//! menu.init_for_gtk_window(&gtk_window); //! menu.init_for_gtk_window(&gtk_window);
//! #[cfg(target_os = "macos")]
//! menu.init_for_nsapp();
//! ``` //! ```
//! //!
//! # Processing menu events //! # Processing menu events
@ -118,6 +120,12 @@ impl Menu {
pub fn init_for_hwnd(&self, hwnd: isize) { pub fn init_for_hwnd(&self, hwnd: isize) {
self.0.init_for_hwnd(hwnd) self.0.init_for_hwnd(hwnd)
} }
/// Adds this menu to NSApp.
#[cfg(target_os = "macos")]
pub fn init_for_nsapp(&self) {
self.0.init_for_nsapp()
}
} }
#[derive(Clone)] #[derive(Clone)]

View file

@ -23,10 +23,23 @@ impl Menu {
pub fn add_submenu(&mut self, label: impl AsRef<str>, enabled: bool) -> Submenu { pub fn add_submenu(&mut self, label: impl AsRef<str>, enabled: bool) -> Submenu {
let mut sub_menu = Submenu(Menu::new()); let mut sub_menu = Submenu(Menu::new());
sub_menu.set_label(label); sub_menu.set_label(label.as_ref());
sub_menu.set_enabled(enabled); sub_menu.set_enabled(enabled);
let item = TextMenuItem::new(label, enabled, sel!(fireMenubarAction:));
unsafe {
item.ns_menu_item.setSubmenu_(sub_menu.0 .0);
self.0.addItem_(item.ns_menu_item);
}
sub_menu sub_menu
} }
pub fn init_for_nsapp(&self) {
unsafe {
NSApp().setMainMenu_(self.0);
}
}
} }
#[derive(Debug, Clone)] #[derive(Debug, Clone)]