Skip to content

Commit ec8ba40

Browse files
committed
feat(core): the release filter's stillness reset (spec:AST-100 FR2)
AST-100 now specifies iOS's blend with a stillness reset: a contact still for longer than the stillness interval before lifting releases at 0 (#8312). Pan, scroll-view pan, pinch and rotation pass the lift's event time to the tracker, which reads 0 when no sample came within velocity.stillness: 40 ms, Android's ASSUME_POINTER_STOPPED_TIME (AOSP VelocityTracker), since iOS has no time cutoff and keeps the last motion. A lift while moving keeps the blend's value, so the recorded iOS releases are unchanged. The constants table accepts an AOSP source.
1 parent a5f26dc commit ec8ba40

12 files changed

Lines changed: 108 additions & 37 deletions

‎.changeset/gesture-recognizer-fixes.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22
'@astryxdesign/core': patch
33
---
44

5-
[experimental] Gesture recognizer fixes: a timer that fires late lets queued input run first and moves the event clock only to its deadline; `cancel()` on an idle recognizer leaves it `possible`; a waiting recognition that fails gives its claimed touches back; a two-finger double tap compares centroids; a swipe fails only once no allowed direction is reachable; Hover recovers after a mid-hover cancel; setting `rotation` keeps the velocity continuous; a handoff pan's translation never reads below zero. The early claim is now opt-in (`claimDistance: 'early'`, from `UIKIT_GESTURES.pan.earlyClaimDistance`) (a numeric `claimDistance` applies to any direction, so an axis pan can take a diagonal from the browser before its own claim) and a committed claim survives a second finger and scrollable content. `VelocityTracker` is one release filter (`spec:AST-100`), UIKit's measured blend on event time for every pointer: a pan reads it as `velocity(in:)`, and the new `ScrollViewPanGestureRecognizer` reads it as a scroll view's release, for components that coast like one. The `strategy` option and `VelocityStrategy` are removed. A finger the browser reports on both pointer and touch events is sampled from its pointer events alone, so the two copies of one move never both reach a velocity.
5+
[experimental] Gesture recognizer fixes: a timer that fires late lets queued input run first and moves the event clock only to its deadline; `cancel()` on an idle recognizer leaves it `possible`; a waiting recognition that fails gives its claimed touches back; a two-finger double tap compares centroids; a swipe fails only once no allowed direction is reachable; Hover recovers after a mid-hover cancel; setting `rotation` keeps the velocity continuous; a handoff pan's translation never reads below zero. The early claim is now opt-in (`claimDistance: 'early'`, from `UIKIT_GESTURES.pan.earlyClaimDistance`) (a numeric `claimDistance` applies to any direction, so an axis pan can take a diagonal from the browser before its own claim) and a committed claim survives a second finger and scrollable content. `VelocityTracker` is one release filter (`spec:AST-100`), UIKit's measured blend on event time for every pointer, with FR2's stillness reset (a contact still for longer than 40 ms, Android's value, before lifting releases at 0): a pan reads it as `velocity(in:)`, and the new `ScrollViewPanGestureRecognizer` reads it as a scroll view's release, for components that coast like one. The `strategy` option and `VelocityStrategy` are removed. A finger the browser reports on both pointer and touch events is sampled from its pointer events alone, so the two copies of one move never both reach a velocity.
66

77
@vjeux

‎packages/core/src/gestures/PanGestureRecognizer.ts‎

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,8 @@ export class PanGestureRecognizer extends GestureRecognizer {
139139
/** Translation since touch-down, before `setTranslation` offsets. */
140140
private raw: Point = ZERO;
141141
private offset: Point = ZERO;
142+
/** The lift's event time, once the last finger lifted. */
143+
private liftTime: number | undefined;
142144
private virtual: Point = ZERO;
143145
private isObserving = false;
144146
private anchor: Point | null = null;
@@ -201,12 +203,15 @@ export class PanGestureRecognizer extends GestureRecognizer {
201203
* is the last moving value, since a finger at rest sends no events.
202204
*/
203205
velocity(): Point {
204-
return this.locked(this.readVelocity(this.tracker));
206+
return this.locked(this.readVelocity(this.tracker, this.liftTime));
205207
}
206208

207-
/** The tracker's reading this kind of recognizer reports: a pan's. */
208-
protected readVelocity(tracker: VelocityTracker): Point {
209-
return tracker.velocity();
209+
/**
210+
* The tracker's reading this kind of recognizer reports: a pan's. `at` is
211+
* the lift's event time once the last finger lifted (the stillness reset).
212+
*/
213+
protected readVelocity(tracker: VelocityTracker, at?: number): Point {
214+
return tracker.velocity(at);
210215
}
211216

212217
/**
@@ -295,9 +300,16 @@ export class PanGestureRecognizer extends GestureRecognizer {
295300
}
296301
}
297302

298-
protected override touchesEnded(touches: ReadonlyArray<GestureTouch>): void {
299-
// UIKit reads neither the translation nor the velocity from the lift.
303+
protected override touchesEnded(
304+
touches: ReadonlyArray<GestureTouch>,
305+
event: GestureEventContext,
306+
): void {
307+
// UIKit reads neither the translation nor the velocity from the lift;
308+
// its time is kept for the stillness reset.
300309
const remaining = this.numberOfTouches - touches.length;
310+
if (remaining === 0) {
311+
this.liftTime = event.time;
312+
}
301313
if (isActive(this.state)) {
302314
if (remaining < this.minimumNumberOfTouches) {
303315
this.setState('ended');
@@ -331,6 +343,7 @@ export class PanGestureRecognizer extends GestureRecognizer {
331343
}
332344

333345
protected override reset(): void {
346+
this.liftTime = undefined;
334347
this.isClaimCommitted = false;
335348
this.lockedAxis = null;
336349
this.travelAtBegan = ZERO;

‎packages/core/src/gestures/PinchGestureRecognizer.ts‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ export class PinchGestureRecognizer extends GestureRecognizer {
4141
private initialSpan = 0;
4242
private scaleValue = 1;
4343
private readonly tracker = new VelocityTracker();
44+
/** The lift's event time, for the stillness reset. */
45+
private liftTime: number | undefined;
4446
private lastTime = 0;
4547

4648
constructor(options: PinchGestureRecognizerOptions = {}) {
@@ -68,7 +70,7 @@ export class PinchGestureRecognizer extends GestureRecognizer {
6870

6971
/** Scale per second (UIKit's filter; measured for pans, assumed here). */
7072
get velocity(): number {
71-
return this.tracker.velocity().x;
73+
return this.tracker.velocity(this.liftTime).x;
7274
}
7375

7476
/** The midpoint between the two fingers. */
@@ -126,11 +128,16 @@ export class PinchGestureRecognizer extends GestureRecognizer {
126128
}
127129
}
128130

129-
protected override touchesEnded(): void {
131+
protected override touchesEnded(
132+
_touches: ReadonlyArray<GestureTouch>,
133+
event: GestureEventContext,
134+
): void {
135+
this.liftTime = event.time;
130136
this.setState(isActive(this.state) ? 'ended' : 'failed');
131137
}
132138

133139
protected override reset(): void {
140+
this.liftTime = undefined;
134141
this.initialSpan = 0;
135142
this.scaleValue = 1;
136143
this.tracker.reset();

‎packages/core/src/gestures/README.md‎

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -388,16 +388,17 @@ every gesture and every pointer (`spec:AST-100`), UIKit's own, measured:
388388
velocities blended once more, 0.25 times the newest plus 0.75 times the
389389
one before. It reproduces four recorded UIPickerView releases. Wheels,
390390
pickers, pagers and momentum carousels use it.
391-
- Neither has a timer. A finger at rest sends no events, so a release after
392-
a silent pause reports the last moving value (a UIScrollView coasts after
393-
it too); moves that report the same point bring a pan to 0 on the second
394-
(a scroll view on the third). The lift updates nothing.
391+
- Moves that report the same point bring a pan to 0 on the second (a
392+
scroll view on the third). A contact still for longer than the stillness
393+
interval (`velocity.stillness`, 40 ms, Android's `ASSUME_POINTER_STOPPED_TIME`)
394+
before lifting releases at 0: a still finger never flicks (`spec:AST-100`
395+
FR2). That is the one place it differs from iOS, whose blend keeps the
396+
last motion through a silent rest.
397+
The lift itself adds no sample.
395398
- Times are the events' own. A finger the browser reports on both pointer
396-
and touch events is sampled from its pointer events alone, so the two
397-
copies of one move never both count.
398-
399-
FR2's fixed window and stillness interval depart from UIKit, which has
400-
neither: proposed, pending owner review on the spec.
399+
and touch events is sampled from its pointer events alone (from its touch
400+
events once the browser cancels its pointer), so the two copies of one
401+
move never both count.
401402

402403
## Haptics
403404

‎packages/core/src/gestures/RotationGestureRecognizer.ts‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ export class RotationGestureRecognizer extends GestureRecognizer {
3636
private lastAngle = 0;
3737
private rotationValue = 0;
3838
private readonly tracker = new VelocityTracker();
39+
/** The lift's event time, for the stillness reset. */
40+
private liftTime: number | undefined;
3941
private lastTime = 0;
4042

4143
constructor(options: RotationGestureRecognizerOptions = {}) {
@@ -60,7 +62,7 @@ export class RotationGestureRecognizer extends GestureRecognizer {
6062

6163
/** Radians per second. */
6264
get velocity(): number {
63-
return this.tracker.velocity().x;
65+
return this.tracker.velocity(this.liftTime).x;
6466
}
6567

6668
focalPoint(): Point {
@@ -112,11 +114,16 @@ export class RotationGestureRecognizer extends GestureRecognizer {
112114
}
113115
}
114116

115-
protected override touchesEnded(): void {
117+
protected override touchesEnded(
118+
_touches: ReadonlyArray<GestureTouch>,
119+
event: GestureEventContext,
120+
): void {
121+
this.liftTime = event.time;
116122
this.setState(isActive(this.state) ? 'ended' : 'failed');
117123
}
118124

119125
protected override reset(): void {
126+
this.liftTime = undefined;
120127
this.initialAngle = 0;
121128
this.lastAngle = 0;
122129
this.rotationValue = 0;

‎packages/core/src/gestures/ScrollViewPanGestureRecognizer.ts‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,10 @@ export type ScrollViewPanGestureRecognizerOptions = PanGestureRecognizerOptions;
2929
/** @experimental */
3030
export class ScrollViewPanGestureRecognizer extends PanGestureRecognizer {
3131
/** A scroll view's release: its pan's last two velocities, blended. */
32-
protected override readVelocity(tracker: VelocityTracker): Point {
33-
return tracker.scrollReleaseVelocity();
32+
protected override readVelocity(
33+
tracker: VelocityTracker,
34+
at?: number,
35+
): Point {
36+
return tracker.scrollReleaseVelocity(at);
3437
}
3538
}

‎packages/core/src/gestures/VelocityTracker.ts‎

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,13 @@
2222
*
2323
* scroll(n) = s * pan(n) + (1 - s) * pan(n-1), s = `velocity.scrollReleaseWeight`.
2424
*
25-
* Only samples update it, with no timer: a resting contact that keeps
26-
* reporting brings it to 0 within two samples (three for a scroll view), and
27-
* a silent rest keeps the last motion's value, as UIKit's does. Times are the
25+
* Samples update it: a resting contact that keeps reporting brings it to 0
26+
* within two samples (three for a scroll view). Read at the lift, a contact
27+
* still for longer than the stillness interval (`velocity.stillness`) reads
28+
* 0 (FR2: a still finger never flicks), the one place it differs from iOS,
29+
* whose blend keeps the last motion through a silent rest. Times are the
2830
* input events' own timestamps (FR4); thresholds stay with each component
29-
* (FR3). FR2's fixed window and stillness interval depart from UIKit, which
30-
* has neither: an open question on the spec.
31+
* (FR3).
3132
*
3233
* SYNC: When modified, update VelocityTracker.test.ts.
3334
*/
@@ -42,6 +43,7 @@ export interface VelocityTrackerOptions {
4243

4344
const ZERO: Point = {x: 0, y: 0};
4445
const PAN_WEIGHT = UIKIT_GESTURES.velocity.currentWeight.value;
46+
const STILLNESS = UIKIT_GESTURES.velocity.stillness.value;
4547
const SCROLL_RELEASE_WEIGHT = UIKIT_GESTURES.velocity.scrollReleaseWeight.value;
4648

4749
export class VelocityTracker {
@@ -88,8 +90,14 @@ export class VelocityTracker {
8890
return this.samples[this.samples.length - 1] ?? null;
8991
}
9092

91-
/** The velocity in px/s as a pan recognizer reports it: pan(n). */
92-
velocity(): Point {
93+
/**
94+
* The velocity in px/s as a pan recognizer reports it: pan(n). `at`, the
95+
* lift's event time, applies the stillness reset.
96+
*/
97+
velocity(at?: number): Point {
98+
if (this.isStill(at)) {
99+
return ZERO;
100+
}
93101
return panVelocity(this.samples, this.samples.length);
94102
}
95103

@@ -98,7 +106,10 @@ export class VelocityTracker {
98106
* recognizer's last two values blended once more. With one interval so
99107
* far, the pan's value.
100108
*/
101-
scrollReleaseVelocity(): Point {
109+
scrollReleaseVelocity(at?: number): Point {
110+
if (this.isStill(at)) {
111+
return ZERO;
112+
}
102113
const end = this.samples.length;
103114
if (end < 3) {
104115
return panVelocity(this.samples, end);
@@ -109,6 +120,12 @@ export class VelocityTracker {
109120
SCROLL_RELEASE_WEIGHT,
110121
);
111122
}
123+
124+
/** No sample for longer than the stillness interval before `at`. */
125+
private isStill(at: number | undefined): boolean {
126+
const newest = this.samples[this.samples.length - 1];
127+
return at != null && newest != null && at - newest.time > STILLNESS;
128+
}
112129
}
113130

114131
/** pan(n) over `samples[0..end)`: one interval so far is reported whole. */

‎packages/core/src/gestures/__tests__/PanGestureRecognizer.test.ts‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ describe('PanGestureRecognizer', () => {
7575
expect(releaseVelocity.y).toBeCloseTo(0, 5);
7676
});
7777

78-
it('releases at 0 when a resting finger reports without moving, and keeps the speed through a silent rest', () => {
78+
it('releases at 0 after a still pause or when a resting finger reports without moving, and at speed when it lifts moving', () => {
7979
const h = createHarness();
8080
const pan = new PanGestureRecognizer({name: 'pan'});
8181
let releaseVelocity = {x: -1, y: -1};
@@ -86,8 +86,14 @@ describe('PanGestureRecognizer', () => {
8686
});
8787
const finger = h.finger(1).down(0, 0, 0, {recognizers: [pan]});
8888
finger.drag(200, 0, 100, 10);
89-
// A silent rest sends no events: the release keeps the speed, as UIKit's.
89+
// Still for 100 ms before the lift, past the stillness interval: a
90+
// still finger never flicks (spec:AST-100 FR2), where iOS would keep it.
9091
finger.up(200);
92+
expect(releaseVelocity.x).toBe(0);
93+
// Lifted while moving (within the interval): the drag's own speed.
94+
const moving = h.finger(3).down(0, 0, 2000, {recognizers: [pan]});
95+
moving.drag(200, 0, 100, 10);
96+
moving.up(2120);
9197
expect(releaseVelocity.x).toBeCloseTo(2000, 6);
9298
expect(releaseVelocity.y).toBe(0);
9399
// A resting finger that reports without moving releases at 0.

‎packages/core/src/gestures/__tests__/VelocityTracker.test.ts‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
*/
1212

1313
import {describe, expect, it} from 'vitest';
14+
import {UIKIT_GESTURES} from '../../motion/uikit/gestures';
1415
import {VelocityTracker} from '../VelocityTracker';
1516

1617
/** Feed `position(t)` sampled every `step` ms over [0, end]. */
@@ -167,12 +168,17 @@ describe('VelocityTracker', () => {
167168
expect(tracker.scrollReleaseVelocity().x).toBeCloseTo(1000, 6);
168169
});
169170

170-
it('keeps the last motion through a silent rest, as UIKit does', () => {
171+
it('reads 0 at a lift after a still pause longer than the stillness interval, and keeps the motion for a shorter one', () => {
171172
const tracker = new VelocityTracker();
172173
trace(tracker, t => t * 2, 160);
173-
// No event arrives while the finger rests: nothing changes the reading.
174+
const still = UIKIT_GESTURES.velocity.stillness.value;
175+
// No event arrives while the finger rests: the blend keeps the motion.
174176
expect(tracker.velocity().x).toBeCloseTo(2000, 6);
175-
expect(tracker.scrollReleaseVelocity().x).toBeCloseTo(2000, 6);
177+
expect(tracker.velocity(160 + still).x).toBeCloseTo(2000, 6);
178+
expect(tracker.scrollReleaseVelocity(160 + still).x).toBeCloseTo(2000, 6);
179+
// Past the interval, a still finger never flicks (spec:AST-100 FR2).
180+
expect(tracker.velocity(160 + still + 1).x).toBe(0);
181+
expect(tracker.scrollReleaseVelocity(160 + still + 1).x).toBe(0);
176182
});
177183

178184
it('reaches 0 when a resting finger keeps reporting: two samples for a pan, three for a scroll view', () => {

‎packages/core/src/motion/uikit/gestures.test.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ describe('UIKIT_GESTURES', () => {
4040
expect('source' in constant).toBe(false);
4141
} else {
4242
expect(constant.source).toMatch(
43-
/^(apple-docs https:\/\/developer\.apple\.com\/|wwdc \d{4}-\d+ |measured: )/,
43+
/^(apple-docs https:\/\/developer\.apple\.com\/|wwdc \d{4}-\d+ |aosp frameworks\/|measured: )/,
4444
);
4545
}
4646
},

0 commit comments

Comments
 (0)