Dispatch Event refactoring. Appears to be workingCo-Authored-By: nathan@zed.dev

K Simmons created

Change summary

crates/gpui/src/elements/list.rs              |   4 
crates/gpui/src/presenter.rs                  | 143 +++++----
crates/gpui/src/presenter/event_context.rs    | 100 ++++++
crates/gpui/src/presenter/event_dispatcher.rs | 308 +++++++++++++++++++++
crates/gpui/src/scene/mouse_region.rs         |   2 
crates/gpui/src/scene/mouse_region_event.rs   |   6 
crates/workspace/src/sidebar.rs               |   2 
7 files changed, 494 insertions(+), 71 deletions(-)

Detailed changes

crates/gpui/src/elements/list.rs 🔗

@@ -698,7 +698,7 @@ mod tests {
             40.,
             vec2f(0., -54.),
             true,
-            &mut presenter.build_event_context(cx),
+            &mut presenter.build_event_context(&mut Default::default(), cx),
         );
         let (_, logical_scroll_top) = list.layout(
             constraint,
@@ -807,7 +807,7 @@ mod tests {
                         height,
                         delta,
                         true,
-                        &mut presenter.build_event_context(cx),
+                        &mut presenter.build_event_context(&mut Default::default(), cx),
                     );
                 }
                 30..=34 => {

crates/gpui/src/presenter.rs 🔗

@@ -79,6 +79,7 @@ impl Presenter {
             self.rendered_views.remove(view_id);
         }
         for view_id in &invalidation.updated {
+            dbg!(view_id);
             self.rendered_views.insert(
                 *view_id,
                 cx.render_view(RenderParams {
@@ -151,6 +152,7 @@ impl Presenter {
 
             if cx.window_is_active(self.window_id) {
                 if let Some(event) = self.last_mouse_moved_event.clone() {
+                    println!("Redispatching mouse moved");
                     self.dispatch_event(event, true, cx);
                 }
             }
@@ -236,15 +238,15 @@ impl Presenter {
         if let Some(root_view_id) = cx.root_view_id(self.window_id) {
             let mut events_to_send = Vec::new();
 
-            //1. Allocate the correct set of GPUI events generated from the platform events
-            // -> These are usually small: [Mouse Down] or [Mouse up, Click] or [Mouse Moved, Mouse Dragged?]
-            // -> Also moves around mouse related state
+            // 1. Allocate the correct set of GPUI events generated from the platform events
+            //  -> These are usually small: [Mouse Down] or [Mouse up, Click] or [Mouse Moved, Mouse Dragged?]
+            //  -> Also moves around mouse related state
             match &event {
                 Event::MouseDown(e) => {
-                    //Click events are weird because they can be fired after a drag event.
-                    //MDN says that browsers handle this by starting from 'the most
-                    //specific ancestor element that contained both [positions]'
-                    //So we need to store the overlapping regions on mouse down.
+                    // Click events are weird because they can be fired after a drag event.
+                    // MDN says that browsers handle this by starting from 'the most
+                    // specific ancestor element that contained both [positions]'
+                    // So we need to store the overlapping regions on mouse down.
                     self.clicked_regions = self
                         .mouse_regions
                         .iter()
@@ -267,9 +269,9 @@ impl Presenter {
                     }));
                 }
                 Event::MouseUp(e) => {
-                    //NOTE: The order of event pushes is important! MouseUp events MUST be fired
-                    //before click events, and so the UpRegionEvent events need to be pushed before
-                    //ClickRegionEvents
+                    // NOTE: The order of event pushes is important! MouseUp events MUST be fired
+                    // before click events, and so the UpRegionEvent events need to be pushed before
+                    // ClickRegionEvents
                     events_to_send.push(MouseRegionEvent::Up(UpRegionEvent {
                         region: Default::default(),
                         platform_event: e.clone(),
@@ -330,74 +332,87 @@ impl Presenter {
 
             let mut invalidated_views: HashSet<usize> = Default::default();
             let mut any_event_handled = false;
-            //2. Process the raw mouse events into region events
+            // 2. Process the raw mouse events into region events
             for mut region_event in events_to_send {
                 let mut valid_regions = Vec::new();
 
-                //GPUI elements are arranged by depth but sibling elements can register overlapping
-                //mouse regions. As such, hover events are only fired on overlapping elements which
-                //are at the same depth as the deepest element which overlaps with the mouse.
-                if let MouseRegionEvent::Hover(_) = region_event {
-                    let mut top_most_depth = None;
-                    let mouse_position = self.mouse_position.clone();
-                    for (region, depth) in self.mouse_regions.iter().rev() {
-                        let contains_mouse = region.bounds.contains_point(mouse_position);
-
-                        if contains_mouse && top_most_depth.is_none() {
-                            top_most_depth = Some(depth);
-                        }
+                // GPUI elements are arranged by depth but sibling elements can register overlapping
+                // mouse regions. As such, hover events are only fired on overlapping elements which
+                // are at the same depth as the deepest element which overlaps with the mouse.
 
-                        if let Some(region_id) = region.id() {
-                            //This unwrap relies on short circuiting boolean expressions
-                            //The right side of the && is only executed when contains_mouse
-                            //is true, and we know above that when contains_mouse is true
-                            //top_most_depth is set
-                            if contains_mouse && depth == top_most_depth.unwrap() {
-                                //Ensure that hover entrance events aren't sent twice
-                                if self.hovered_region_ids.insert(region_id) {
-                                    valid_regions.push(region.clone());
-                                }
-                            } else {
-                                //Ensure that hover exit events aren't sent twice
-                                if self.hovered_region_ids.remove(&region_id) {
-                                    valid_regions.push(region.clone());
+                match &region_event {
+                    MouseRegionEvent::Hover(_) => {
+                        let mut top_most_depth = None;
+                        let mouse_position = self.mouse_position.clone();
+                        for (region, depth) in self.mouse_regions.iter().rev() {
+                            let contains_mouse = region.bounds.contains_point(mouse_position);
+
+                            if contains_mouse && top_most_depth.is_none() {
+                                top_most_depth = Some(depth);
+                            }
+
+                            if let Some(region_id) = region.id() {
+                                // This unwrap relies on short circuiting boolean expressions
+                                // The right side of the && is only executed when contains_mouse
+                                // is true, and we know above that when contains_mouse is true
+                                // top_most_depth is set
+                                if contains_mouse && depth == top_most_depth.unwrap() {
+                                    //Ensure that hover entrance events aren't sent twice
+                                    if self.hovered_region_ids.insert(region_id) {
+                                        valid_regions.push(region.clone());
+                                    }
+                                } else {
+                                    // Ensure that hover exit events aren't sent twice
+                                    if self.hovered_region_ids.remove(&region_id) {
+                                        valid_regions.push(region.clone());
+                                    }
                                 }
                             }
                         }
                     }
-                } else if let MouseRegionEvent::Click(e) = &region_event {
-                    //Clear presenter state
-                    let clicked_regions = std::mem::replace(&mut self.clicked_regions, Vec::new());
-                    self.clicked_button = None;
-
-                    //Find regions which still overlap with the mouse since the last MouseDown happened
-                    for clicked_region in clicked_regions.into_iter().rev() {
-                        if clicked_region.bounds.contains_point(e.position) {
-                            valid_regions.push(clicked_region);
+                    MouseRegionEvent::Click(e) => {
+                        // Clear presenter state
+                        let clicked_regions =
+                            std::mem::replace(&mut self.clicked_regions, Vec::new());
+                        self.clicked_button = None;
+
+                        // Find regions which still overlap with the mouse since the last MouseDown happened
+                        for clicked_region in clicked_regions.into_iter().rev() {
+                            if clicked_region.bounds.contains_point(e.position) {
+                                valid_regions.push(clicked_region);
+                            }
+                        }
+                    }
+                    MouseRegionEvent::Drag(_) => {
+                        for clicked_region in self.clicked_regions.iter().rev() {
+                            valid_regions.push(clicked_region.clone());
                         }
                     }
-                } else if region_event.is_local() {
-                    for (mouse_region, _) in self.mouse_regions.iter().rev() {
-                        //Contains
-                        if mouse_region.bounds.contains_point(self.mouse_position) {
-                            valid_regions.push(mouse_region.clone());
+
+                    MouseRegionEvent::UpOut(_) | MouseRegionEvent::DownOut(_) => {
+                        for (mouse_region, _) in self.mouse_regions.iter().rev() {
+                            // NOT contains
+                            if !mouse_region.bounds.contains_point(self.mouse_position) {
+                                valid_regions.push(mouse_region.clone());
+                            }
                         }
                     }
-                } else {
-                    for (mouse_region, _) in self.mouse_regions.iter().rev() {
-                        //NOT contains
-                        if !mouse_region.bounds.contains_point(self.mouse_position) {
-                            valid_regions.push(mouse_region.clone());
+                    _ => {
+                        for (mouse_region, _) in self.mouse_regions.iter().rev() {
+                            // Contains
+                            if mouse_region.bounds.contains_point(self.mouse_position) {
+                                valid_regions.push(mouse_region.clone());
+                            }
                         }
                     }
                 }
 
                 //3. Fire region events
                 let hovered_region_ids = self.hovered_region_ids.clone();
-                let mut event_cx = self.build_event_context(&mut invalidated_views, cx);
                 for valid_region in valid_regions.into_iter() {
                     region_event.set_region(valid_region.bounds);
                     if let MouseRegionEvent::Hover(e) = &mut region_event {
+                        println!("Hover event selected");
                         e.started = valid_region
                             .id()
                             .map(|region_id| hovered_region_ids.contains(&region_id))
@@ -405,12 +420,11 @@ impl Presenter {
                     }
 
                     if let Some(callback) = valid_region.handlers.get(&region_event.handler_key()) {
-                        if !event_reused {
-                            invalidated_views.insert(valid_region.view_id);
-                        }
+                        dbg!(valid_region.view_id);
+                        invalidated_views.insert(valid_region.view_id);
 
+                        let mut event_cx = self.build_event_context(&mut invalidated_views, cx);
                         event_cx.handled = true;
-                        let local = region_event.is_local();
                         event_cx.with_current_view(valid_region.view_id, {
                             let region_event = region_event.clone();
                             |cx| {
@@ -418,18 +432,17 @@ impl Presenter {
                             }
                         });
 
+                        any_event_handled = any_event_handled || event_cx.handled;
                         // For bubbling events, if the event was handled, don't continue dispatching
                         // This only makes sense for local events.
-                        if event_cx.handled && local {
+                        if event_cx.handled && region_event.is_capturable() {
                             break;
                         }
                     }
                 }
-
-                any_event_handled = any_event_handled && event_cx.handled;
             }
 
-            if !any_event_handled {
+            if !any_event_handled && !event_reused {
                 let mut event_cx = self.build_event_context(&mut invalidated_views, cx);
                 any_event_handled = event_cx.dispatch_event(root_view_id, &event);
             }

crates/gpui/src/presenter/event_context.rs 🔗

@@ -0,0 +1,100 @@
+use std::ops::{Deref, DerefMut};
+
+use collections::{HashMap, HashSet};
+
+use crate::{Action, ElementBox, Event, FontCache, MutableAppContext, TextLayoutCache};
+
+pub struct EventContext<'a> {
+    rendered_views: &'a mut HashMap<usize, ElementBox>,
+    pub font_cache: &'a FontCache,
+    pub text_layout_cache: &'a TextLayoutCache,
+    pub app: &'a mut MutableAppContext,
+    pub window_id: usize,
+    pub notify_count: usize,
+    view_stack: Vec<usize>,
+    pub(crate) handled: bool,
+    pub(crate) invalidated_views: HashSet<usize>,
+}
+
+impl<'a> EventContext<'a> {
+    pub(crate) fn dispatch_event(&mut self, view_id: usize, event: &Event) -> bool {
+        if let Some(mut element) = self.rendered_views.remove(&view_id) {
+            let result =
+                self.with_current_view(view_id, |this| element.dispatch_event(event, this));
+            self.rendered_views.insert(view_id, element);
+            result
+        } else {
+            false
+        }
+    }
+
+    pub(crate) fn with_current_view<F, T>(&mut self, view_id: usize, f: F) -> T
+    where
+        F: FnOnce(&mut Self) -> T,
+    {
+        self.view_stack.push(view_id);
+        let result = f(self);
+        self.view_stack.pop();
+        result
+    }
+
+    pub fn window_id(&self) -> usize {
+        self.window_id
+    }
+
+    pub fn view_id(&self) -> Option<usize> {
+        self.view_stack.last().copied()
+    }
+
+    pub fn is_parent_view_focused(&self) -> bool {
+        if let Some(parent_view_id) = self.view_stack.last() {
+            self.app.focused_view_id(self.window_id) == Some(*parent_view_id)
+        } else {
+            false
+        }
+    }
+
+    pub fn focus_parent_view(&mut self) {
+        if let Some(parent_view_id) = self.view_stack.last() {
+            self.app.focus(self.window_id, Some(*parent_view_id))
+        }
+    }
+
+    pub fn dispatch_any_action(&mut self, action: Box<dyn Action>) {
+        self.app
+            .dispatch_any_action_at(self.window_id, *self.view_stack.last().unwrap(), action)
+    }
+
+    pub fn dispatch_action<A: Action>(&mut self, action: A) {
+        self.dispatch_any_action(Box::new(action));
+    }
+
+    pub fn notify(&mut self) {
+        self.notify_count += 1;
+        if let Some(view_id) = self.view_stack.last() {
+            self.invalidated_views.insert(*view_id);
+        }
+    }
+
+    pub fn notify_count(&self) -> usize {
+        self.notify_count
+    }
+
+    pub fn propogate_event(&mut self) {
+        self.handled = false;
+    }
+}
+
+impl<'a> Deref for EventContext<'a> {
+    type Target = MutableAppContext;
+
+    fn deref(&self) -> &Self::Target {
+        self.app
+    }
+}
+
+impl<'a> DerefMut for EventContext<'a> {
+    fn deref_mut(&mut self) -> &mut Self::Target {
+        self.app
+    }
+}

crates/gpui/src/presenter/event_dispatcher.rs 🔗

@@ -0,0 +1,308 @@
+use std::sync::Arc;
+
+use collections::{HashMap, HashSet};
+use pathfinder_geometry::vector::Vector2F;
+
+use crate::{
+    scene::{
+        ClickRegionEvent, DownOutRegionEvent, DownRegionEvent, DragRegionEvent, HoverRegionEvent,
+        MouseRegionEvent, MoveRegionEvent, UpOutRegionEvent, UpRegionEvent,
+    },
+    CursorRegion, CursorStyle, ElementBox, Event, EventContext, FontCache, MouseButton,
+    MouseMovedEvent, MouseRegion, MouseRegionId, MutableAppContext, Scene, TextLayoutCache,
+};
+
+pub struct EventDispatcher {
+    window_id: usize,
+    font_cache: Arc<FontCache>,
+
+    last_mouse_moved_event: Option<Event>,
+    cursor_regions: Vec<CursorRegion>,
+    mouse_regions: Vec<(MouseRegion, usize)>,
+    clicked_regions: Vec<MouseRegion>,
+    clicked_button: Option<MouseButton>,
+    mouse_position: Vector2F,
+    hovered_region_ids: HashSet<MouseRegionId>,
+}
+
+impl EventDispatcher {
+    pub fn new(window_id: usize, font_cache: Arc<FontCache>) -> Self {
+        Self {
+            window_id,
+            font_cache,
+
+            last_mouse_moved_event: Default::default(),
+            cursor_regions: Default::default(),
+            mouse_regions: Default::default(),
+            clicked_regions: Default::default(),
+            clicked_button: Default::default(),
+            mouse_position: Default::default(),
+            hovered_region_ids: Default::default(),
+        }
+    }
+
+    pub fn clicked_region_ids(&self) -> Option<(Vec<MouseRegionId>, MouseButton)> {
+        self.clicked_button.map(|button| {
+            (
+                self.clicked_regions
+                    .iter()
+                    .filter_map(MouseRegion::id)
+                    .collect(),
+                button,
+            )
+        })
+    }
+
+    pub fn hovered_region_ids(&self) -> HashSet<MouseRegionId> {
+        self.hovered_region_ids.clone()
+    }
+
+    pub fn update_mouse_regions(&mut self, scene: &Scene) {
+        self.cursor_regions = scene.cursor_regions();
+        self.mouse_regions = scene.mouse_regions();
+    }
+
+    pub fn redispatch_mouse_moved_event<'a>(&'a mut self, cx: &mut EventContext<'a>) {
+        if let Some(event) = self.last_mouse_moved_event.clone() {
+            self.dispatch_event(event, true, cx);
+        }
+    }
+
+    pub fn dispatch_event<'a>(
+        &'a mut self,
+        event: Event,
+        event_reused: bool,
+        cx: &mut EventContext<'a>,
+    ) -> bool {
+        let root_view_id = cx.root_view_id(self.window_id);
+        if root_view_id.is_none() {
+            return false;
+        }
+
+        let root_view_id = root_view_id.unwrap();
+        //1. Allocate the correct set of GPUI events generated from the platform events
+        // -> These are usually small: [Mouse Down] or [Mouse up, Click] or [Mouse Moved, Mouse Dragged?]
+        // -> Also moves around mouse related state
+        let events_to_send = self.select_region_events(&event, cx, event_reused);
+
+        // For a given platform event, potentially multiple mouse region events can be created. For a given
+        // region event, dispatch continues until a mouse region callback fails to propogate (handled is set to true)
+        // If no region handles any of the produced platform events, we fallback to the old dispatch event style.
+        let mut invalidated_views: HashSet<usize> = Default::default();
+        let mut any_event_handled = false;
+        for mut region_event in events_to_send {
+            //2. Find mouse regions relevant to each region_event. For example, if the event is click, select
+            // the clicked_regions that overlap with the mouse position
+            let valid_regions = self.select_relevant_mouse_regions(&region_event);
+            let hovered_region_ids = self.hovered_region_ids.clone();
+
+            //3. Dispatch region event ot each valid mouse region
+            for valid_region in valid_regions.into_iter() {
+                region_event.set_region(valid_region.bounds);
+                if let MouseRegionEvent::Hover(e) = &mut region_event {
+                    e.started = valid_region
+                        .id()
+                        .map(|region_id| hovered_region_ids.contains(&region_id))
+                        .unwrap_or(false)
+                }
+
+                if let Some(callback) = valid_region.handlers.get(&region_event.handler_key()) {
+                    if !event_reused {
+                        invalidated_views.insert(valid_region.view_id);
+                    }
+
+                    cx.handled = true;
+                    cx.with_current_view(valid_region.view_id, {
+                        let region_event = region_event.clone();
+                        |cx| {
+                            callback(region_event, cx);
+                        }
+                    });
+
+                    // For bubbling events, if the event was handled, don't continue dispatching
+                    // This only makes sense for local events.
+                    if cx.handled && region_event.is_local() {
+                        break;
+                    }
+                }
+            }
+
+            // Keep track if any platform event was handled
+            any_event_handled = any_event_handled && cx.handled;
+        }
+
+        if !any_event_handled {
+            // No platform event was handled, so fall back to old mouse event dispatch style
+            any_event_handled = cx.dispatch_event(root_view_id, &event);
+        }
+
+        // Notify any views which have been validated from event callbacks
+        for view_id in invalidated_views {
+            cx.notify_view(self.window_id, view_id);
+        }
+
+        any_event_handled
+    }
+
+    fn select_region_events(
+        &mut self,
+        event: &Event,
+        cx: &mut MutableAppContext,
+        event_reused: bool,
+    ) -> Vec<MouseRegionEvent> {
+        let mut events_to_send = Vec::new();
+        match event {
+            Event::MouseDown(e) => {
+                //Click events are weird because they can be fired after a drag event.
+                //MDN says that browsers handle this by starting from 'the most
+                //specific ancestor element that contained both [positions]'
+                //So we need to store the overlapping regions on mouse down.
+                self.clicked_regions = self
+                    .mouse_regions
+                    .iter()
+                    .filter_map(|(region, _)| {
+                        region
+                            .bounds
+                            .contains_point(e.position)
+                            .then(|| region.clone())
+                    })
+                    .collect();
+                self.clicked_button = Some(e.button);
+
+                events_to_send.push(MouseRegionEvent::Down(DownRegionEvent {
+                    region: Default::default(),
+                    platform_event: e.clone(),
+                }));
+                events_to_send.push(MouseRegionEvent::DownOut(DownOutRegionEvent {
+                    region: Default::default(),
+                    platform_event: e.clone(),
+                }));
+            }
+            Event::MouseUp(e) => {
+                //NOTE: The order of event pushes is important! MouseUp events MUST be fired
+                //before click events, and so the UpRegionEvent events need to be pushed before
+                //ClickRegionEvents
+                events_to_send.push(MouseRegionEvent::Up(UpRegionEvent {
+                    region: Default::default(),
+                    platform_event: e.clone(),
+                }));
+                events_to_send.push(MouseRegionEvent::UpOut(UpOutRegionEvent {
+                    region: Default::default(),
+                    platform_event: e.clone(),
+                }));
+                events_to_send.push(MouseRegionEvent::Click(ClickRegionEvent {
+                    region: Default::default(),
+                    platform_event: e.clone(),
+                }));
+            }
+            Event::MouseMoved(
+                e @ MouseMovedEvent {
+                    position,
+                    pressed_button,
+                    ..
+                },
+            ) => {
+                let mut style_to_assign = CursorStyle::Arrow;
+                for region in self.cursor_regions.iter().rev() {
+                    if region.bounds.contains_point(*position) {
+                        style_to_assign = region.style;
+                        break;
+                    }
+                }
+                cx.platform().set_cursor_style(style_to_assign);
+
+                if !event_reused {
+                    if pressed_button.is_some() {
+                        events_to_send.push(MouseRegionEvent::Drag(DragRegionEvent {
+                            region: Default::default(),
+                            prev_mouse_position: self.mouse_position,
+                            platform_event: e.clone(),
+                        }));
+                    }
+                    events_to_send.push(MouseRegionEvent::Move(MoveRegionEvent {
+                        region: Default::default(),
+                        platform_event: e.clone(),
+                    }));
+                }
+
+                events_to_send.push(MouseRegionEvent::Hover(HoverRegionEvent {
+                    region: Default::default(),
+                    platform_event: e.clone(),
+                    started: false,
+                }));
+
+                self.last_mouse_moved_event = Some(event.clone());
+            }
+            _ => {}
+        }
+        if let Some(position) = event.position() {
+            self.mouse_position = position;
+        }
+        events_to_send
+    }
+
+    fn select_relevant_mouse_regions(
+        &mut self,
+        region_event: &MouseRegionEvent,
+    ) -> Vec<MouseRegion> {
+        let mut valid_regions = Vec::new();
+        //GPUI elements are arranged by depth but sibling elements can register overlapping
+        //mouse regions. As such, hover events are only fired on overlapping elements which
+        //are at the same depth as the deepest element which overlaps with the mouse.
+        if let MouseRegionEvent::Hover(_) = *region_event {
+            let mut top_most_depth = None;
+            let mouse_position = self.mouse_position.clone();
+            for (region, depth) in self.mouse_regions.iter().rev() {
+                let contains_mouse = region.bounds.contains_point(mouse_position);
+
+                if contains_mouse && top_most_depth.is_none() {
+                    top_most_depth = Some(depth);
+                }
+
+                if let Some(region_id) = region.id() {
+                    //This unwrap relies on short circuiting boolean expressions
+                    //The right side of the && is only executed when contains_mouse
+                    //is true, and we know above that when contains_mouse is true
+                    //top_most_depth is set
+                    if contains_mouse && depth == top_most_depth.unwrap() {
+                        //Ensure that hover entrance events aren't sent twice
+                        if self.hovered_region_ids.insert(region_id) {
+                            valid_regions.push(region.clone());
+                        }
+                    } else {
+                        //Ensure that hover exit events aren't sent twice
+                        if self.hovered_region_ids.remove(&region_id) {
+                            valid_regions.push(region.clone());
+                        }
+                    }
+                }
+            }
+        } else if let MouseRegionEvent::Click(e) = region_event {
+            //Clear stored clicked_regions
+            let clicked_regions = std::mem::replace(&mut self.clicked_regions, Vec::new());
+            self.clicked_button = None;
+
+            //Find regions which still overlap with the mouse since the last MouseDown happened
+            for clicked_region in clicked_regions.into_iter().rev() {
+                if clicked_region.bounds.contains_point(e.position) {
+                    valid_regions.push(clicked_region);
+                }
+            }
+        } else if region_event.is_local() {
+            for (mouse_region, _) in self.mouse_regions.iter().rev() {
+                //Contains
+                if mouse_region.bounds.contains_point(self.mouse_position) {
+                    valid_regions.push(mouse_region.clone());
+                }
+            }
+        } else {
+            for (mouse_region, _) in self.mouse_regions.iter().rev() {
+                //NOT contains
+                if !mouse_region.bounds.contains_point(self.mouse_position) {
+                    valid_regions.push(mouse_region.clone());
+                }
+            }
+        }
+        valid_regions
+    }
+}

crates/gpui/src/scene/mouse_region.rs 🔗

@@ -113,7 +113,7 @@ impl MouseRegion {
     }
 }
 
-#[derive(Copy, Clone, Eq, PartialEq, Hash)]
+#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
 pub struct MouseRegionId {
     pub view_id: usize,
     pub discriminant: (TypeId, usize),

crates/gpui/src/scene/mouse_region_event.rs 🔗

@@ -163,10 +163,12 @@ impl MouseRegionEvent {
         }
     }
 
-    pub fn is_local(&self) -> bool {
+    /// When true, mouse event handlers must call cx.propagate_event() to bubble
+    /// the event to handlers they are painted on top of.
+    pub fn is_capturable(&self) -> bool {
         match self {
             MouseRegionEvent::Move(_) => true,
-            MouseRegionEvent::Drag(_) => true,
+            MouseRegionEvent::Drag(_) => false,
             MouseRegionEvent::Hover(_) => true,
             MouseRegionEvent::Down(_) => true,
             MouseRegionEvent::Up(_) => true,

crates/workspace/src/sidebar.rs 🔗

@@ -190,7 +190,7 @@ impl Sidebar {
         .with_cursor_style(CursorStyle::ResizeLeftRight)
         .on_down(MouseButton::Left, |_, _| {}) // This prevents the mouse down event from being propagated elsewhere
         .on_drag(MouseButton::Left, move |e, cx| {
-            let delta = e.prev_mouse_position.x() - e.position.x();
+            let delta = e.position.x() - e.prev_mouse_position.x();
             let prev_width = *actual_width.borrow();
             *custom_width.borrow_mut() = 0f32
                 .max(match side {