From 8273865fa39e0a7e83cb3ec7ee6d3f35ef198da6 Mon Sep 17 00:00:00 2001 From: Antonio Scandurra Date: Wed, 20 Dec 2023 14:00:12 +0100 Subject: [PATCH] Introduce `InteractiveElement::capture_any_mouse_{down,up}` --- crates/gpui2/src/elements/div.rs | 40 ++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/crates/gpui2/src/elements/div.rs b/crates/gpui2/src/elements/div.rs index fa1eb9e5b63454d0a3fa370913e094f30c3ad68c..908b302dfc1cd28f6ff873eff215d583d1b489f4 100644 --- a/crates/gpui2/src/elements/div.rs +++ b/crates/gpui2/src/elements/div.rs @@ -62,6 +62,18 @@ impl Interactivity { })); } + pub fn capture_any_mouse_down( + &mut self, + listener: impl Fn(&MouseDownEvent, &mut WindowContext) + 'static, + ) { + self.mouse_down_listeners + .push(Box::new(move |event, bounds, phase, cx| { + if phase == DispatchPhase::Capture && bounds.visibly_contains(&event.position, cx) { + (listener)(event, cx) + } + })); + } + pub fn on_any_mouse_down( &mut self, listener: impl Fn(&MouseDownEvent, &mut WindowContext) + 'static, @@ -90,6 +102,18 @@ impl Interactivity { })); } + pub fn capture_any_mouse_up( + &mut self, + listener: impl Fn(&MouseUpEvent, &mut WindowContext) + 'static, + ) { + self.mouse_up_listeners + .push(Box::new(move |event, bounds, phase, cx| { + if phase == DispatchPhase::Capture && bounds.visibly_contains(&event.position, cx) { + (listener)(event, cx) + } + })); + } + pub fn on_any_mouse_up( &mut self, listener: impl Fn(&MouseUpEvent, &mut WindowContext) + 'static, @@ -384,6 +408,14 @@ pub trait InteractiveElement: Sized { self } + fn capture_any_mouse_down( + mut self, + listener: impl Fn(&MouseDownEvent, &mut WindowContext) + 'static, + ) -> Self { + self.interactivity().capture_any_mouse_down(listener); + self + } + fn on_any_mouse_down( mut self, listener: impl Fn(&MouseDownEvent, &mut WindowContext) + 'static, @@ -401,6 +433,14 @@ pub trait InteractiveElement: Sized { self } + fn capture_any_mouse_up( + mut self, + listener: impl Fn(&MouseUpEvent, &mut WindowContext) + 'static, + ) -> Self { + self.interactivity().capture_any_mouse_up(listener); + self + } + fn on_mouse_down_out( mut self, listener: impl Fn(&MouseDownEvent, &mut WindowContext) + 'static,