Toggle sidebar items on mouse down instead of on click

Antonio Scandurra created

Change summary

gpui/src/elements/mouse_event_handler.rs | 11 +++++++++++
zed/src/workspace/sidebar.rs             |  2 +-
2 files changed, 12 insertions(+), 1 deletion(-)

Detailed changes

gpui/src/elements/mouse_event_handler.rs 🔗

@@ -12,6 +12,7 @@ pub struct MouseEventHandler {
     state: ElementStateHandle<MouseState>,
     child: ElementBox,
     cursor_style: Option<CursorStyle>,
+    mouse_down_handler: Option<Box<dyn FnMut(&mut EventContext)>>,
     click_handler: Option<Box<dyn FnMut(&mut EventContext)>>,
     drag_handler: Option<Box<dyn FnMut(Vector2F, &mut EventContext)>>,
 }
@@ -38,6 +39,7 @@ impl MouseEventHandler {
             state: state_handle,
             child,
             cursor_style: None,
+            mouse_down_handler: None,
             click_handler: None,
             drag_handler: None,
         }
@@ -48,6 +50,11 @@ impl MouseEventHandler {
         self
     }
 
+    pub fn on_mouse_down(mut self, handler: impl FnMut(&mut EventContext) + 'static) -> Self {
+        self.mouse_down_handler = Some(Box::new(handler));
+        self
+    }
+
     pub fn on_click(mut self, handler: impl FnMut(&mut EventContext) + 'static) -> Self {
         self.click_handler = Some(Box::new(handler));
         self
@@ -89,6 +96,7 @@ impl Element for MouseEventHandler {
         cx: &mut EventContext,
     ) -> bool {
         let cursor_style = self.cursor_style;
+        let mouse_down_handler = self.mouse_down_handler.as_mut();
         let click_handler = self.click_handler.as_mut();
         let drag_handler = self.drag_handler.as_mut();
 
@@ -124,6 +132,9 @@ impl Element for MouseEventHandler {
                     state.clicked = true;
                     state.prev_drag_position = Some(*position);
                     cx.notify();
+                    if let Some(handler) = mouse_down_handler {
+                        handler(cx);
+                    }
                     true
                 } else {
                     handled_in_child

zed/src/workspace/sidebar.rs 🔗

@@ -91,7 +91,7 @@ impl Sidebar {
                         .boxed()
                     })
                     .with_cursor_style(CursorStyle::PointingHand)
-                    .on_click(move |cx| {
+                    .on_mouse_down(move |cx| {
                         cx.dispatch_action(ToggleSidebarItem(ToggleArg { side, item_index }))
                     })
                     .boxed()