Skip to content

Commit

Permalink
feat(context-menu): returns dismissed result on macOS
Browse files Browse the repository at this point in the history
  • Loading branch information
pewsheen committed Oct 25, 2024
1 parent cea276a commit 4a06d78
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/items/submenu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ impl ContextMenu for Submenu {
&self,
view: *const std::ffi::c_void,
position: Option<Position>,
) {
) -> bool {
self.inner
.borrow_mut()
.show_context_menu_for_nsview(view, position)
Expand Down
4 changes: 3 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,8 @@ pub trait ContextMenu {
///
/// - `position` is relative to the window top-left corner, if `None`, the cursor position is used.
///
/// Returns `true` if menu tracking ended because an item was selected, and `false` if menu tracking was cancelled for any reason.
///
/// # Safety
///
/// The view must be a pointer to a valid `NSView`.
Expand All @@ -362,7 +364,7 @@ pub trait ContextMenu {
&self,
view: *const std::ffi::c_void,
position: Option<dpi::Position>,
);
) -> bool;

/// Get the underlying NSMenu reserved for context menus.
///
Expand Down
2 changes: 1 addition & 1 deletion src/menu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,7 @@ impl ContextMenu for Menu {
&self,
view: *const std::ffi::c_void,
position: Option<Position>,
) {
) -> bool {
self.inner
.borrow_mut()
.show_context_menu_for_nsview(view, position)
Expand Down
12 changes: 8 additions & 4 deletions src/platform_impl/macos/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ impl Menu {
&self,
view: *const c_void,
position: Option<Position>,
) {
) -> bool {
// SAFETY: Upheld by caller
show_context_menu(&self.ns_menu.1, view, position)
}
Expand Down Expand Up @@ -660,7 +660,7 @@ impl MenuChild {
&self,
view: *const c_void,
position: Option<Position>,
) {
) -> bool {
show_context_menu(&self.ns_menu.as_ref().unwrap().1, view, position)
}

Expand Down Expand Up @@ -1100,7 +1100,11 @@ fn menuitem_set_native_icon(menuitem: &NSMenuItem, icon: Option<NativeIcon>) {
}
}

unsafe fn show_context_menu(ns_menu: &NSMenu, view: *const c_void, position: Option<Position>) {
unsafe fn show_context_menu(
ns_menu: &NSMenu,
view: *const c_void,
position: Option<Position>,
) -> bool {
// SAFETY: Caller verifies that the view is valid.
let view: &NSView = unsafe { &*view.cast() };

Expand All @@ -1120,7 +1124,7 @@ unsafe fn show_context_menu(ns_menu: &NSMenu, view: *const c_void, position: Opt
(location, None)
};

let _ = unsafe { ns_menu.popUpMenuPositioningItem_atLocation_inView(None, location, in_view) };
unsafe { ns_menu.popUpMenuPositioningItem_atLocation_inView(None, location, in_view) }
}

impl NativeIcon {
Expand Down

0 comments on commit 4a06d78

Please sign in to comment.