Introduce `InteractiveElement::capture_any_mouse_{down,up}`

Antonio Scandurra created

Change summary

crates/gpui2/src/elements/div.rs | 40 ++++++++++++++++++++++++++++++++++
1 file changed, 40 insertions(+)

Detailed changes

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,