From 676e6ea2e0073f296f3e4d5cd1f3d8e6663152d4 Mon Sep 17 00:00:00 2001 From: Jason Tsai Date: Fri, 25 Oct 2024 13:34:11 +0800 Subject: [PATCH] feat(context-menu): returns dismiss result on Windows --- src/items/submenu.rs | 2 +- src/lib.rs | 8 +++++++- src/menu.rs | 2 +- src/platform_impl/windows/mod.rs | 20 +++++++++++++++++--- 4 files changed, 26 insertions(+), 6 deletions(-) diff --git a/src/items/submenu.rs b/src/items/submenu.rs index b676f30..134d4ad 100644 --- a/src/items/submenu.rs +++ b/src/items/submenu.rs @@ -217,7 +217,7 @@ impl ContextMenu for Submenu { } #[cfg(target_os = "windows")] - unsafe fn show_context_menu_for_hwnd(&self, hwnd: isize, position: Option) { + unsafe fn show_context_menu_for_hwnd(&self, hwnd: isize, position: Option) -> bool { self.inner .borrow_mut() .show_context_menu_for_hwnd(hwnd, position) diff --git a/src/lib.rs b/src/lib.rs index 78f9481..a339c2d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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); + unsafe fn show_context_menu_for_hwnd( + &self, + hwnd: isize, + position: Option, + ) -> bool; /// Attach the menu subclass handler to the given hwnd /// so you can recieve events from that window using [MenuEvent::receiver] diff --git a/src/menu.rs b/src/menu.rs index bdeb5e9..8613148 100644 --- a/src/menu.rs +++ b/src/menu.rs @@ -375,7 +375,7 @@ impl ContextMenu for Menu { } #[cfg(target_os = "windows")] - unsafe fn show_context_menu_for_hwnd(&self, hwnd: isize, position: Option) { + unsafe fn show_context_menu_for_hwnd(&self, hwnd: isize, position: Option) -> bool { self.inner .borrow_mut() .show_context_menu_for_hwnd(hwnd, position) diff --git a/src/platform_impl/windows/mod.rs b/src/platform_impl/windows/mod.rs index e8af74d..a8ed79e 100644 --- a/src/platform_impl/windows/mod.rs +++ b/src/platform_impl/windows/mod.rs @@ -415,11 +415,19 @@ impl Menu { .unwrap_or(false) } - pub unsafe fn show_context_menu_for_hwnd(&mut self, hwnd: isize, position: Option) { + pub unsafe fn show_context_menu_for_hwnd( + &mut self, + hwnd: isize, + position: Option, + ) -> 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<()> { @@ -921,13 +929,19 @@ impl MenuChild { .collect() } - pub unsafe fn show_context_menu_for_hwnd(&mut self, hwnd: isize, position: Option) { + pub unsafe fn show_context_menu_for_hwnd( + &mut self, + hwnd: isize, + position: Option, + ) -> 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) {