From 37153b826f8b381ed814e1032f085f9a2551b21d Mon Sep 17 00:00:00 2001 From: amrbashir Date: Thu, 24 Nov 2022 18:07:11 +0200 Subject: [PATCH] feat: expose macOS NSMenu --- src/lib.rs | 3 +++ src/menu.rs | 5 +++++ src/platform_impl/macos/mod.rs | 17 +++++++++++++++++ src/submenu.rs | 5 +++++ 4 files changed, 30 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index 0a13730..cc9b108 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -209,6 +209,9 @@ pub trait ContextMenu { /// (the click which triggered the menu to be shown). #[cfg(target_os = "macos")] fn show_context_menu_for_nsview(&self, view: cocoa::base::id, x: f64, y: f64); + + #[cfg(target_os = "macos")] + fn ns_menu(&self) -> *mut std::ffi::c_void; } /// Describes a menu event emitted when a menu item is activated diff --git a/src/menu.rs b/src/menu.rs index c6d2b11..d13f666 100644 --- a/src/menu.rs +++ b/src/menu.rs @@ -46,6 +46,11 @@ impl ContextMenu for Menu { fn show_context_menu_for_nsview(&self, view: cocoa::base::id, x: f64, y: f64) { self.0.show_context_menu_for_nsview(view, x, y) } + + #[cfg(target_os = "macos")] + fn ns_menu(&self) -> *mut std::ffi::c_void { + self.0.ns_menu() + } } impl Menu { diff --git a/src/platform_impl/macos/mod.rs b/src/platform_impl/macos/mod.rs index 463de4a..21fbd22 100644 --- a/src/platform_impl/macos/mod.rs +++ b/src/platform_impl/macos/mod.rs @@ -242,6 +242,10 @@ impl Menu { msg_send![self.ns_menu, popUpMenuPositioningItem: nil atLocation: location inView: view] } } + + pub fn ns_menu(&self) -> *mut std::ffi::c_void { + self.ns_menu as _ + } } #[derive(Clone)] @@ -446,6 +450,19 @@ impl Submenu { unsafe { msg_send![NSApp(), setHelpMenu: ns_menus[0]] } } } + + pub fn ns_menu(&self) -> *mut std::ffi::c_void { + *self + .0 + .borrow() + .ns_menus + .values() + .collect::>() + .get(0) + .unwrap() + .get(0) + .unwrap() as _ + } } #[derive(Clone, Debug)] diff --git a/src/submenu.rs b/src/submenu.rs index 9fa0696..de97c26 100644 --- a/src/submenu.rs +++ b/src/submenu.rs @@ -54,6 +54,11 @@ impl ContextMenu for Submenu { fn show_context_menu_for_nsview(&self, view: cocoa::base::id, x: f64, y: f64) { self.0.show_context_menu_for_nsview(view, x, y) } + + #[cfg(target_os = "macos")] + fn ns_menu(&self) -> *mut std::ffi::c_void { + self.0.ns_menu() + } } impl Submenu {