diff --git a/gpui/src/elements/mouse_event_handler.rs b/gpui/src/elements/mouse_event_handler.rs index c9d9bb7a01..6f5f3fbc4a 100644 --- a/gpui/src/elements/mouse_event_handler.rs +++ b/gpui/src/elements/mouse_event_handler.rs @@ -12,6 +12,7 @@ pub struct MouseEventHandler { state: ElementStateHandle, child: ElementBox, cursor_style: Option, + mouse_down_handler: Option>, click_handler: Option>, drag_handler: Option>, } @@ -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 diff --git a/zed/src/workspace/sidebar.rs b/zed/src/workspace/sidebar.rs index cbaf9b7ccf..3d57c96c4f 100644 --- a/zed/src/workspace/sidebar.rs +++ b/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()