Skip to content

Commit

Permalink
feat(context-menu): returns dismiss result on Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
pewsheen committed Oct 25, 2024
1 parent 4a06d78 commit 676e6ea
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/items/submenu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ impl ContextMenu for Submenu {
}

#[cfg(target_os = "windows")]
unsafe fn show_context_menu_for_hwnd(&self, hwnd: isize, position: Option<Position>) {
unsafe fn show_context_menu_for_hwnd(&self, hwnd: isize, position: Option<Position>) -> bool {
self.inner
.borrow_mut()
.show_context_menu_for_hwnd(hwnd, position)
Expand Down
8 changes: 7 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -311,11 +311,17 @@ 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 `hwnd` must be a valid window HWND.
#[cfg(target_os = "windows")]
unsafe fn show_context_menu_for_hwnd(&self, hwnd: isize, position: Option<dpi::Position>);
unsafe fn show_context_menu_for_hwnd(
&self,
hwnd: isize,
position: Option<dpi::Position>,
) -> bool;

/// Attach the menu subclass handler to the given hwnd
/// so you can recieve events from that window using [MenuEvent::receiver]
Expand Down
2 changes: 1 addition & 1 deletion src/menu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ impl ContextMenu for Menu {
}

#[cfg(target_os = "windows")]
unsafe fn show_context_menu_for_hwnd(&self, hwnd: isize, position: Option<Position>) {
unsafe fn show_context_menu_for_hwnd(&self, hwnd: isize, position: Option<Position>) -> bool {
self.inner
.borrow_mut()
.show_context_menu_for_hwnd(hwnd, position)
Expand Down
20 changes: 17 additions & 3 deletions src/platform_impl/windows/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -415,11 +415,19 @@ impl Menu {
.unwrap_or(false)
}

pub unsafe fn show_context_menu_for_hwnd(&mut self, hwnd: isize, position: Option<Position>) {
pub unsafe fn show_context_menu_for_hwnd(
&mut self,
hwnd: isize,
position: Option<Position>,
) -> bool {
let rc = show_context_menu(hwnd as _, self.hpopupmenu, position);
if let Some(item) = rc.and_then(|rc| self.find_by_id(rc)) {
menu_selected(hwnd as _, &mut item.borrow_mut());
unsafe {
menu_selected(hwnd as _, &mut item.borrow_mut());
}
return true;
}
false
}

pub unsafe fn set_theme_for_hwnd(&self, hwnd: isize, theme: MenuTheme) -> crate::Result<()> {
Expand Down Expand Up @@ -921,13 +929,19 @@ impl MenuChild {
.collect()
}

pub unsafe fn show_context_menu_for_hwnd(&mut self, hwnd: isize, position: Option<Position>) {
pub unsafe fn show_context_menu_for_hwnd(
&mut self,
hwnd: isize,
position: Option<Position>,
) -> bool {
let rc = show_context_menu(hwnd as _, self.hpopupmenu, position);
if let Some(item) = rc.and_then(|rc| self.find_by_id(rc)) {
unsafe {
menu_selected(hwnd as _, &mut item.borrow_mut());
}
return true;
}
false
}

pub unsafe fn attach_menu_subclass_for_hwnd(&self, hwnd: isize) {
Expand Down

0 comments on commit 676e6ea

Please sign in to comment.