From b2fa511acd7a57d29ec593ab671f10e13252e863 Mon Sep 17 00:00:00 2001 From: Mikayla Maki Date: Wed, 6 Jul 2022 17:52:20 -0700 Subject: [PATCH 1/3] GPUI change proposals --- crates/gpui/src/elements/event_handler.rs | 2 +- .../gpui/src/elements/mouse_event_handler.rs | 7 +++++-- crates/gpui/src/presenter.rs | 20 +++++++++++++++---- crates/gpui/src/scene.rs | 2 +- crates/workspace/src/sidebar.rs | 2 +- 5 files changed, 24 insertions(+), 9 deletions(-) diff --git a/crates/gpui/src/elements/event_handler.rs b/crates/gpui/src/elements/event_handler.rs index 7144b21dd0..1b025c3d35 100644 --- a/crates/gpui/src/elements/event_handler.rs +++ b/crates/gpui/src/elements/event_handler.rs @@ -90,7 +90,7 @@ impl Element for EventHandler { click: Some(Rc::new(|_, _, _| {})), right_mouse_down: Some(Rc::new(|_, _| {})), right_click: Some(Rc::new(|_, _, _| {})), - drag: Some(Rc::new(|_, _| {})), + drag: Some(Rc::new(|_, _, _| {})), mouse_down_out: Some(Rc::new(|_, _| {})), right_mouse_down_out: Some(Rc::new(|_, _| {})), }); diff --git a/crates/gpui/src/elements/mouse_event_handler.rs b/crates/gpui/src/elements/mouse_event_handler.rs index 8f70daf9e6..832aafaa9e 100644 --- a/crates/gpui/src/elements/mouse_event_handler.rs +++ b/crates/gpui/src/elements/mouse_event_handler.rs @@ -24,7 +24,7 @@ pub struct MouseEventHandler { right_click: Option>, mouse_down_out: Option>, right_mouse_down_out: Option>, - drag: Option>, + drag: Option>, hover: Option>, padding: Padding, } @@ -106,7 +106,10 @@ impl MouseEventHandler { self } - pub fn on_drag(mut self, handler: impl Fn(Vector2F, &mut EventContext) + 'static) -> Self { + pub fn on_drag( + mut self, + handler: impl Fn(Vector2F, Vector2F, &mut EventContext) + 'static, + ) -> Self { self.drag = Some(Rc::new(handler)); self } diff --git a/crates/gpui/src/presenter.rs b/crates/gpui/src/presenter.rs index 6285b1be99..fd3b70916a 100644 --- a/crates/gpui/src/presenter.rs +++ b/crates/gpui/src/presenter.rs @@ -306,8 +306,11 @@ impl Presenter { .as_ref() .zip(self.prev_drag_position.as_mut()) { - dragged_region = - Some((clicked_region.clone(), position - *prev_drag_position)); + dragged_region = Some(( + clicked_region.clone(), + position - *prev_drag_position, + position, + )); *prev_drag_position = position; } @@ -366,11 +369,11 @@ impl Presenter { } } - if let Some((dragged_region, delta)) = dragged_region { + if let Some((dragged_region, delta, position)) = dragged_region { handled = true; if let Some(drag_callback) = dragged_region.drag { event_cx.with_current_view(dragged_region.view_id, |event_cx| { - drag_callback(delta, event_cx); + drag_callback(delta, position, event_cx); }) } } @@ -648,6 +651,15 @@ impl<'a> PaintContext<'a> { } } + pub fn paint_layer(&mut self, clip_bounds: Option, f: F) + where + F: FnOnce(&mut Self) -> (), + { + self.scene.push_layer(clip_bounds); + f(self); + self.scene.pop_layer(); + } + pub fn current_view_id(&self) -> usize { *self.view_stack.last().unwrap() } diff --git a/crates/gpui/src/scene.rs b/crates/gpui/src/scene.rs index 1f0e2c0ecc..a413d96981 100644 --- a/crates/gpui/src/scene.rs +++ b/crates/gpui/src/scene.rs @@ -54,7 +54,7 @@ pub struct MouseRegion { pub click: Option>, pub right_mouse_down: Option>, pub right_click: Option>, - pub drag: Option>, + pub drag: Option>, pub mouse_down_out: Option>, pub right_mouse_down_out: Option>, } diff --git a/crates/workspace/src/sidebar.rs b/crates/workspace/src/sidebar.rs index 341026aecf..c31998aa93 100644 --- a/crates/workspace/src/sidebar.rs +++ b/crates/workspace/src/sidebar.rs @@ -188,7 +188,7 @@ impl Sidebar { }) .with_cursor_style(CursorStyle::ResizeLeftRight) .on_mouse_down(|_, _| {}) // This prevents the mouse down event from being propagated elsewhere - .on_drag(move |delta, cx| { + .on_drag(move |delta, _, cx| { let prev_width = *actual_width.borrow(); *custom_width.borrow_mut() = 0f32 .max(match side { From baa011ccf451f88fee8e1a4da11834ee9512d308 Mon Sep 17 00:00:00 2001 From: Mikayla Maki Date: Wed, 6 Jul 2022 17:57:32 -0700 Subject: [PATCH 2/3] added inline hint --- crates/gpui/src/presenter.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/crates/gpui/src/presenter.rs b/crates/gpui/src/presenter.rs index fd3b70916a..9810277a87 100644 --- a/crates/gpui/src/presenter.rs +++ b/crates/gpui/src/presenter.rs @@ -651,6 +651,7 @@ impl<'a> PaintContext<'a> { } } + #[inline] pub fn paint_layer(&mut self, clip_bounds: Option, f: F) where F: FnOnce(&mut Self) -> (), From 9fd2bf2fa1a6dd3025682ac1723fe28381e54d51 Mon Sep 17 00:00:00 2001 From: Mikayla Maki Date: Thu, 7 Jul 2022 16:07:24 -0700 Subject: [PATCH 3/3] Updated drag API to pass old,new, instead of delta,new --- crates/gpui/src/presenter.rs | 11 ++++------- crates/workspace/src/sidebar.rs | 7 ++++--- 2 files changed, 8 insertions(+), 10 deletions(-) diff --git a/crates/gpui/src/presenter.rs b/crates/gpui/src/presenter.rs index 9810277a87..d04a1df674 100644 --- a/crates/gpui/src/presenter.rs +++ b/crates/gpui/src/presenter.rs @@ -306,11 +306,8 @@ impl Presenter { .as_ref() .zip(self.prev_drag_position.as_mut()) { - dragged_region = Some(( - clicked_region.clone(), - position - *prev_drag_position, - position, - )); + dragged_region = + Some((clicked_region.clone(), *prev_drag_position, position)); *prev_drag_position = position; } @@ -369,11 +366,11 @@ impl Presenter { } } - if let Some((dragged_region, delta, position)) = dragged_region { + if let Some((dragged_region, prev_position, position)) = dragged_region { handled = true; if let Some(drag_callback) = dragged_region.drag { event_cx.with_current_view(dragged_region.view_id, |event_cx| { - drag_callback(delta, position, event_cx); + drag_callback(prev_position, position, event_cx); }) } } diff --git a/crates/workspace/src/sidebar.rs b/crates/workspace/src/sidebar.rs index c31998aa93..a500f99492 100644 --- a/crates/workspace/src/sidebar.rs +++ b/crates/workspace/src/sidebar.rs @@ -188,12 +188,13 @@ impl Sidebar { }) .with_cursor_style(CursorStyle::ResizeLeftRight) .on_mouse_down(|_, _| {}) // This prevents the mouse down event from being propagated elsewhere - .on_drag(move |delta, _, cx| { + .on_drag(move |old_position, new_position, cx| { + let delta = new_position.x() - old_position.x(); let prev_width = *actual_width.borrow(); *custom_width.borrow_mut() = 0f32 .max(match side { - Side::Left => prev_width + delta.x(), - Side::Right => prev_width - delta.x(), + Side::Left => prev_width + delta, + Side::Right => prev_width - delta, }) .round();