diff --git a/crates/ui/src/menu/dropdown_menu.rs b/crates/ui/src/menu/dropdown_menu.rs index e3263254..308f5cb9 100644 --- a/crates/ui/src/menu/dropdown_menu.rs +++ b/crates/ui/src/menu/dropdown_menu.rs @@ -89,6 +89,7 @@ where Popover::new(SharedString::from(format!("popover:{}", self.id))) .appearance(false) + .overlay_closable(false) .trigger(self.trigger) .trigger_style(self.style) .anchor(self.anchor) diff --git a/crates/ui/src/popover.rs b/crates/ui/src/popover.rs index 2be7f6d7..0fd286d6 100644 --- a/crates/ui/src/popover.rs +++ b/crates/ui/src/popover.rs @@ -35,6 +35,7 @@ pub struct Popover { trigger_style: Option, mouse_button: MouseButton, appearance: bool, + overlay_closable: bool, on_open_change: Option>, } @@ -52,6 +53,7 @@ impl Popover { children: vec![], mouse_button: MouseButton::Left, appearance: true, + overlay_closable: true, default_open: false, open: None, on_open_change: None, @@ -121,6 +123,12 @@ impl Popover { self } + /// Set whether clicking outside the popover will dismiss it, default is `true`. + pub fn overlay_closable(mut self, closable: bool) -> Self { + self.overlay_closable = closable; + self + } + /// Set the content builder for content of the Popover. /// /// This callback will called every time on render the popover. @@ -373,24 +381,17 @@ impl RenderOnce for Popover { ) }) .children(self.children) - .when(self.appearance, |this| { - let state = state.clone(); - this.on_mouse_down_out(move |_, window, cx| { - state.update(cx, |state, cx| { - state.toggle_open(window, cx); - }); - cx.notify(parent_view_id); + .when(self.overlay_closable, |this| { + this.on_mouse_down_out({ + let state = state.clone(); + move |_, window, cx| { + state.update(cx, |state, cx| { + state.dismiss(window, cx); + }); + cx.notify(parent_view_id); + } }) }) - .on_mouse_down_out({ - let state = state.clone(); - move |_, window, cx| { - state.update(cx, |state, cx| { - state.dismiss(window, cx); - }); - cx.notify(parent_view_id); - } - }) .refine_style(&self.style), ), )