From 7fbea39566b2076304212e04f5a4eb830c546c25 Mon Sep 17 00:00:00 2001 From: Piotr Osiewicz <24362066+osiewicz@users.noreply.github.com> Date: Mon, 19 Aug 2024 18:48:57 +0200 Subject: [PATCH] ui: Dismiss popovers when clicking on trigger button (#16476) Release Notes: - Clicking on an already-deployed popover menu trigger now hides the popover menu. --- crates/ui/src/components/popover_menu.rs | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/crates/ui/src/components/popover_menu.rs b/crates/ui/src/components/popover_menu.rs index 49d801b4f0c741cf069d7c11354060c525d5e7d9..8707bee0a7a7bbe4fb45c0ff4a35400de1cf67aa 100644 --- a/crates/ui/src/components/popover_menu.rs +++ b/crates/ui/src/components/popover_menu.rs @@ -208,14 +208,15 @@ impl Default for PopoverMenuElementState { } } -pub struct PopoverMenuFrameState { +pub struct PopoverMenuFrameState { child_layout_id: Option, child_element: Option, menu_element: Option, + menu_handle: Rc>>>, } impl Element for PopoverMenu { - type RequestLayoutState = PopoverMenuFrameState; + type RequestLayoutState = PopoverMenuFrameState; type PrepaintState = Option; fn id(&self) -> Option { @@ -280,6 +281,7 @@ impl Element for PopoverMenu { child_element, child_layout_id, menu_element, + menu_handle: element_state.menu.clone(), }, ), element_state, @@ -333,11 +335,14 @@ impl Element for PopoverMenu { menu.paint(cx); if let Some(child_hitbox) = *child_hitbox { + let menu_handle = request_layout.menu_handle.clone(); // Mouse-downing outside the menu dismisses it, so we don't // want a click on the toggle to re-open it. cx.on_mouse_event(move |_: &MouseDownEvent, phase, cx| { if phase == DispatchPhase::Bubble && child_hitbox.is_hovered(cx) { - cx.stop_propagation() + menu_handle.borrow_mut().take(); + cx.stop_propagation(); + cx.refresh(); } }) }