Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.
gpui: Fix cursor reverting when a drag starts (#66540)
Ben Kunklec7bea343838f
diff --git a/crates/gpui/src/platform/test/platform.rs b/crates/gpui/src/platform/test/platform.rsindex b417fd0ab21..9d112b30836 100644--- a/crates/gpui/src/platform/test/platform.rs+++ b/crates/gpui/src/platform/test/platform.rs@@ -126,6 +126,11 @@ pub(crate) struct TestPrompts {}impl TestPlatform {+ #[cfg(test)]+ pub(crate) fn cursor_style(&self) -> CursorStyle {+ *self.active_cursor.lock()+ }+#[cfg(any(test, feature = "test-support"))]pub fn new(executor: BackgroundExecutor, foreground_executor: ForegroundExecutor) -> Rc<Self> {Self::with_platform(diff --git a/crates/gpui/src/window.rs b/crates/gpui/src/window.rsindex 12eb24ea1e1..4e2e1a798c4 100644--- a/crates/gpui/src/window.rs+++ b/crates/gpui/src/window.rs@@ -5810,9 +5810,10 @@ impl Window {fn reset_cursor_style(&self, cx: &mut App) {// Set the cursor only if we're the active window.if self.is_window_hovered() {- let style = self- .rendered_frame- .cursor_style(self)+ // A drag can start before the next paint records its cursor in the frame.+ let style = cx+ .active_drag_cursor_style()+ .or_else(|| self.rendered_frame.cursor_style(self)).unwrap_or(CursorStyle::Arrow);cx.platform.set_cursor_style(style);}@@ -6210,14 +6211,11 @@ impl Window {}fn dispatch_mouse_event(&mut self, event: &dyn Any, cx: &mut App) {- let hit_test = self.rendered_frame.hit_test(self.mouse_position());- if hit_test != self.mouse_hit_test {- self.mouse_hit_test = hit_test;- self.reset_cursor_style(cx);- }+ self.mouse_hit_test = self.rendered_frame.hit_test(self.mouse_position());#[cfg(any(feature = "inspector", debug_assertions))]if self.is_inspector_picking(cx) {+ self.reset_cursor_style(cx);self.handle_inspector_mouse_event(event, cx);// When inspector is picking, all other mouse handling is skipped.return;@@ -6265,6 +6263,10 @@ impl Window {if event.is::<MouseUpEvent>() && self.captured_hitbox.is_some() {self.captured_hitbox = None;}++ // Runs after the listeners so that a drag they start takes effect in this event. Resolving+ // the cursor any earlier would briefly select whatever is under the pointer instead.+ self.reset_cursor_style(cx);}// If modifiers are not excessive (e.g. AltGr), and the input handler is accepting text input,@@ -8172,13 +8174,14 @@ mod tests {use crate::{AnyWindowHandle, AppContext as _, BackGestureEvent, BackGesturePhase, Bounds, ContentMask,- Context, DispatchPhase, DragMoveEvent, Empty, ExternalDragPayload, ExternalPaths,- FileDragPaths, FileDropEvent, FocusHandle, InputEvent as _, InteractiveElement as _,- IntoElement, KeyDownEvent, Keystroke, LongPressEvent, MouseButton, MouseDownEvent,- MouseMoveEvent, ParentElement, Pixels, PlatformInput, Point, Render, RequestFrameOptions,- ScaledPixels, StatefulInteractiveElement as _, Styled, TestAppContext, ThermalState,- TouchDragEvent, TouchEvent, TouchId, TouchPhase, Underline, UnderlineStyle, Window,- WindowAppearance, WindowOptions, canvas, div, hsla, point, px, size,+ Context, CursorStyle, DispatchPhase, DragMoveEvent, Empty, ExternalDragPayload,+ ExternalPaths, FileDragPaths, FileDropEvent, FocusHandle, InputEvent as _,+ InteractiveElement as _, IntoElement, KeyDownEvent, Keystroke, LongPressEvent, MouseButton,+ MouseDownEvent, MouseMoveEvent, MouseUpEvent, ParentElement, Pixels, PlatformInput, Point,+ Render, RequestFrameOptions, ScaledPixels, StatefulInteractiveElement as _, Styled,+ TestAppContext, ThermalState, TouchDragEvent, TouchEvent, TouchId, TouchPhase, Underline,+ UnderlineStyle, Window, WindowAppearance, WindowOptions, canvas, div, hsla, point, px,+ size,};#[gpui::test]@@ -10431,6 +10434,87 @@ mod tests {let widen = |root: &mut Root, _: &mut Window, _: &mut Context<Root>| root.width = px(300.);assert_eq!(update_and_draw(window, &child, cx, widen).0, 1.);}++ #[gpui::test]+ fn test_drag_cursor_applies_before_next_paint(cx: &mut TestAppContext) {+ struct ResizeHandle;++ impl Render for ResizeHandle {+ fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement {+ div().size_full().cursor_text().child(+ div()+ .id("resize-handle")+ .w(px(4.))+ .h_full()+ .cursor_col_resize()+ .on_drag((), |_, _, _, cx| cx.new(|_| Empty)),+ )+ }+ }++ let window = cx.add_window(|_, _| ResizeHandle);+ let handle_position = point(px(1.), px(10.));+ let drag_position = point(px(20.), px(10.));+ let cursor_observer = cx.clone();++ let update_result = cx.update_window(window.into(), |_, window, cx| {+ window.hovered.set(true);+ window.active.set(true);+ window.draw(cx).clear(cx);+ window.dispatch_event(+ MouseMoveEvent {+ position: handle_position,+ ..Default::default()+ }+ .to_platform_input(),+ cx,+ );+ window.dispatch_event(+ MouseDownEvent {+ position: handle_position,+ button: MouseButton::Left,+ ..Default::default()+ }+ .to_platform_input(),+ cx,+ );+ window.dispatch_event(+ MouseMoveEvent {+ position: drag_position,+ pressed_button: Some(MouseButton::Left),+ ..Default::default()+ }+ .to_platform_input(),+ cx,+ );+ assert!(cx.has_active_drag());+ // Asserted here because leaving this update flushes effects, which paints a frame.+ assert_eq!(+ cursor_observer.test_platform().cursor_style(),+ CursorStyle::ResizeColumn+ );++ window.draw(cx).clear(cx);+ window.dispatch_event(+ MouseUpEvent {+ position: drag_position,+ button: MouseButton::Left,+ ..Default::default()+ }+ .to_platform_input(),+ cx,+ );+ window.draw(cx).clear(cx);+ });+ assert!(+ update_result.is_ok(),+ "failed to run drag: {update_result:?}"+ );+ assert_eq!(+ cursor_observer.test_platform().cursor_style(),+ CursorStyle::IBeam+ );+ }}#[cfg(all(test, any(feature = "inspector", debug_assertions)))]