Fix editor mouse event dispatch stealing clicks (#3679)

Mikayla Maki created

[[PR Description]]

Release Notes:

-

Change summary

crates/editor2/src/editor.rs  |   2 
crates/editor2/src/element.rs | 106 +++++++++++++++++++-----------------
2 files changed, 57 insertions(+), 51 deletions(-)

Detailed changes

crates/editor2/src/editor.rs 🔗

@@ -9758,7 +9758,7 @@ pub fn diagnostic_block_renderer(diagnostic: Diagnostic, is_valid: bool) -> Rend
                     .px_1p5()
                     .child(HighlightedLabel::new(line.clone(), highlights.clone()))
                     .child(
-                        div().border().border_color(gpui::red()).child(
+                        div().z_index(1).child(
                             IconButton::new(copy_id.clone(), Icon::Copy)
                                 .icon_color(Color::Muted)
                                 .size(ButtonSize::Compact)

crates/editor2/src/element.rs 🔗

@@ -2447,13 +2447,13 @@ impl EditorElement {
             let interactive_bounds = interactive_bounds.clone();
 
             move |event: &ScrollWheelEvent, phase, cx| {
-                if phase != DispatchPhase::Bubble {
-                    return;
+                if phase == DispatchPhase::Bubble
+                    && interactive_bounds.visibly_contains(&event.position, cx)
+                {
+                    editor.update(cx, |editor, cx| {
+                        Self::scroll(editor, event, &position_map, &interactive_bounds, cx)
+                    });
                 }
-
-                editor.update(cx, |editor, cx| {
-                    Self::scroll(editor, event, &position_map, &interactive_bounds, cx)
-                });
             }
         });
 
@@ -2461,29 +2461,30 @@ impl EditorElement {
             let position_map = layout.position_map.clone();
             let editor = self.editor.clone();
             let stacking_order = cx.stacking_order().clone();
+            let interactive_bounds = interactive_bounds.clone();
 
             move |event: &MouseDownEvent, phase, cx| {
-                if phase != DispatchPhase::Bubble {
-                    return;
+                if phase == DispatchPhase::Bubble
+                    && interactive_bounds.visibly_contains(&event.position, cx)
+                {
+                    match event.button {
+                        MouseButton::Left => editor.update(cx, |editor, cx| {
+                            Self::mouse_left_down(
+                                editor,
+                                event,
+                                &position_map,
+                                text_bounds,
+                                gutter_bounds,
+                                &stacking_order,
+                                cx,
+                            );
+                        }),
+                        MouseButton::Right => editor.update(cx, |editor, cx| {
+                            Self::mouse_right_down(editor, event, &position_map, text_bounds, cx);
+                        }),
+                        _ => {}
+                    };
                 }
-
-                match event.button {
-                    MouseButton::Left => editor.update(cx, |editor, cx| {
-                        Self::mouse_left_down(
-                            editor,
-                            event,
-                            &position_map,
-                            text_bounds,
-                            gutter_bounds,
-                            &stacking_order,
-                            cx,
-                        );
-                    }),
-                    MouseButton::Right => editor.update(cx, |editor, cx| {
-                        Self::mouse_right_down(editor, event, &position_map, text_bounds, cx);
-                    }),
-                    _ => {}
-                };
             }
         });
 
@@ -2491,18 +2492,23 @@ impl EditorElement {
             let position_map = layout.position_map.clone();
             let editor = self.editor.clone();
             let stacking_order = cx.stacking_order().clone();
+            let interactive_bounds = interactive_bounds.clone();
 
             move |event: &MouseUpEvent, phase, cx| {
-                editor.update(cx, |editor, cx| {
-                    Self::mouse_up(
-                        editor,
-                        event,
-                        &position_map,
-                        text_bounds,
-                        &stacking_order,
-                        cx,
-                    )
-                });
+                if phase == DispatchPhase::Bubble
+                    && interactive_bounds.visibly_contains(&event.position, cx)
+                {
+                    editor.update(cx, |editor, cx| {
+                        Self::mouse_up(
+                            editor,
+                            event,
+                            &position_map,
+                            text_bounds,
+                            &stacking_order,
+                            cx,
+                        )
+                    });
+                }
             }
         });
         cx.on_mouse_event({
@@ -2511,21 +2517,21 @@ impl EditorElement {
             let stacking_order = cx.stacking_order().clone();
 
             move |event: &MouseMoveEvent, phase, cx| {
-                if phase != DispatchPhase::Bubble {
-                    return;
+                if phase == DispatchPhase::Bubble
+                    && interactive_bounds.visibly_contains(&event.position, cx)
+                {
+                    editor.update(cx, |editor, cx| {
+                        Self::mouse_moved(
+                            editor,
+                            event,
+                            &position_map,
+                            text_bounds,
+                            gutter_bounds,
+                            &stacking_order,
+                            cx,
+                        )
+                    });
                 }
-
-                editor.update(cx, |editor, cx| {
-                    Self::mouse_moved(
-                        editor,
-                        event,
-                        &position_map,
-                        text_bounds,
-                        gutter_bounds,
-                        &stacking_order,
-                        cx,
-                    )
-                });
             }
         });
     }