Skip to content

armgit / zed

Public Imported from GitHub
0

Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.

gpui: Let a touch that catches a fling pick its own axis (#64239)

Jason Lee7699cb8bb8b7
diff --git a/crates/gpui/src/gestures.rs b/crates/gpui/src/gestures.rs
index e42fbaa4014..3fe5b12c581 100644
--- a/crates/gpui/src/gestures.rs
+++ b/crates/gpui/src/gestures.rs
@@ -522,6 +522,10 @@ enum TouchGestureState {
touch: ActiveTouch,
axis: Axis,
},
+ /// The touch caught a fling: it is a pan from its first pixel, and can
+ /// never be a tap, but its axis is its own — decided by its first
+ /// movement, not inherited from the fling it stopped.
+ Caught(ActiveTouch),
LongPressing(ActiveTouch),
TouchDragging(ActiveTouch),
}
@@ -558,7 +562,6 @@ struct Momentum {
position: Point<Pixels>,
/// Unit vector of the release velocity.
direction: Point<f32>,
- axis: Axis,
/// Release speed in pixels per second.
speed: f32,
started_at: Instant,
@@ -598,9 +601,9 @@ impl TouchGestureRecognizer {
Point::default(),
TouchPhase::Ended,
)));
- Some(momentum.axis)
+ true
} else {
- None
+ false
};
if matches!(self.state, TouchGestureState::Idle) {
let mut velocity_tracker = VelocityTracker::default();
@@ -613,18 +616,20 @@ impl TouchGestureRecognizer {
last_movement: Point::default(),
velocity_tracker,
};
- if let Some(axis) = caught_fling {
+ if caught_fling {
// A touch that catches a fling is a drag from the
// first pixel: waiting out the slop would freeze the
// content mid-scroll and then jump. It can also never
// be a tap; releasing it just leaves the content
- // stopped, as on Android and iOS.
+ // stopped, as on Android and iOS. Which way it pans
+ // is its own to decide: a finger that stops a
+ // sideways fling and moves up scrolls up.
recognized.push(RecognizedTouchGesture::Scroll(scroll_event(
touch.start_position,
Point::default(),
TouchPhase::Started,
)));
- self.state = TouchGestureState::Panning { touch, axis };
+ self.state = TouchGestureState::Caught(touch);
} else {
self.state = TouchGestureState::Pending {
touch,
@@ -676,6 +681,18 @@ impl TouchGestureRecognizer {
};
}
}
+ TouchGestureState::Caught(touch) if touch.id == event.id => {
+ let accumulated = event.position - touch.start_position;
+ if accumulated == Point::default() {
+ self.state = TouchGestureState::Caught(touch);
+ } else {
+ // The first movement picks the axis; from here on this
+ // is an ordinary pan, so let the pan arm emit it.
+ let axis = dominant_axis(accumulated);
+ self.state = TouchGestureState::Panning { touch, axis };
+ return self.handle_event_at(event, now);
+ }
+ }
TouchGestureState::Panning { mut touch, axis } if touch.id == event.id => {
let mut raw_delta = event.position - touch.last_position;
lock_delta_to_axis(&mut raw_delta, axis);
@@ -760,6 +777,14 @@ impl TouchGestureRecognizer {
},
});
}
+ TouchGestureState::Caught(touch) if touch.id == event.id => {
+ // Never moved: the content it stopped stays where it is.
+ recognized.push(RecognizedTouchGesture::Scroll(scroll_event(
+ touch.start_position,
+ Point::default(),
+ TouchPhase::Ended,
+ )));
+ }
TouchGestureState::Panning { touch, axis } if touch.id == event.id => {
// The release deliberately contributes no velocity
// sample: it usually repeats the last movement's position
@@ -812,7 +837,6 @@ impl TouchGestureRecognizer {
self.momentum = Some(Momentum {
position: touch.start_position,
direction,
- axis,
speed,
started_at: now,
duration,
@@ -844,7 +868,9 @@ impl TouchGestureRecognizer {
},
TouchPhase::Cancelled => match mem::replace(&mut self.state, TouchGestureState::Idle) {
TouchGestureState::Pending { touch, .. } if touch.id == event.id => {}
- TouchGestureState::Panning { touch, .. } if touch.id == event.id => {
+ TouchGestureState::Panning { touch, .. } | TouchGestureState::Caught(touch)
+ if touch.id == event.id =>
+ {
recognized.push(RecognizedTouchGesture::Scroll(scroll_event(
touch.start_position,
Point::default(),
@@ -1758,6 +1784,60 @@ mod tests {
assert_eq!(scroll.touch_phase, TouchPhase::Ended);
}
+ #[test]
+ fn catching_a_fling_takes_the_axis_from_the_new_touch() {
+ let mut recognizer = TouchGestureRecognizer::new(GestureTuning::default());
+ let now = Instant::now();
+
+ // A fast horizontal pan flings sideways.
+ recognizer.handle_event_at(
+ &touch_event(TouchId(1), TouchPhase::Started, 300., 300.),
+ now,
+ );
+ for step in 1..=3 {
+ recognizer.handle_event_at(
+ &touch_event(
+ TouchId(1),
+ TouchPhase::Moved,
+ 300. - step as f32 * 33.,
+ 300.,
+ ),
+ now + Duration::from_millis(step * 16),
+ );
+ }
+ recognizer.handle_event_at(
+ &touch_event(TouchId(1), TouchPhase::Ended, 200., 300.),
+ now + Duration::from_millis(64),
+ );
+ assert!(recognizer.has_momentum());
+
+ // A finger that catches it and moves up scrolls up: the fling's axis
+ // is not this touch's axis.
+ recognizer.handle_event_at(
+ &touch_event(TouchId(2), TouchPhase::Started, 200., 300.),
+ now + Duration::from_millis(200),
+ );
+ let recognized = recognizer.handle_event_at(
+ &touch_event(TouchId(2), TouchPhase::Moved, 201., 280.),
+ now + Duration::from_millis(216),
+ );
+ let [RecognizedTouchGesture::Scroll(scroll)] = recognized.as_slice() else {
+ panic!("expected scroll, got {recognized:?}");
+ };
+ assert_eq!(scroll.touch_phase, TouchPhase::Moved);
+ assert_eq!(scroll.delta.pixel_delta(px(16.)), point(px(0.), px(-20.)));
+
+ // And stays on that axis for the rest of the pan.
+ let recognized = recognizer.handle_event_at(
+ &touch_event(TouchId(2), TouchPhase::Moved, 205., 260.),
+ now + Duration::from_millis(232),
+ );
+ let [RecognizedTouchGesture::Scroll(scroll)] = recognized.as_slice() else {
+ panic!("expected scroll, got {recognized:?}");
+ };
+ assert_eq!(scroll.delta.pixel_delta(px(16.)), point(px(0.), px(-20.)));
+ }
+
#[test]
fn cancelled_pan_emits_cancelled_scroll_and_no_tap() {
let mut recognizer = TouchGestureRecognizer::new(GestureTuning::default());