Skip to content

An open source design system that's fully customizable and agent ready

TypeScript

fix(gestures): a bunched trackpad wheel reads as the steps it merged (#9675)

Christopher Chedeau9592511444ab
diff --git a/.changeset/trackpad-stall-merged-wheels.md b/.changeset/trackpad-stall-merged-wheels.md
new file mode 100644
index 0000000000..0d538e9f59
--- /dev/null
+++ b/.changeset/trackpad-stall-merged-wheels.md
@@ -0,0 +1,7 @@
+---
+'@astryxdesign/core': patch
+---
+
+[chore] Internal: the trackpad splitter reads a bunched wheel, one within 1.5 ms of the one before as a main-thread stall delivers it, as the steps it merged. During momentum a bunched wheel is not a jump, so momentum no longer restarts a followed pan, and the steps after it no longer read as a lift. Not exported from the package entry point.
+
+@vjeux
diff --git a/packages/core/src/gestures/__tests__/wheel.test.ts b/packages/core/src/gestures/__tests__/wheel.test.ts
index 854094d56a..928d3054cc 100644
--- a/packages/core/src/gestures/__tests__/wheel.test.ts
+++ b/packages/core/src/gestures/__tests__/wheel.test.ts
@@ -735,6 +735,50 @@ describe('wheel and trackpad input (spec:AST-094 FR43)', () => {
expect(Math.abs(released.stopped.scroll)).toBe(0);
});
+ it('never lets momentum move a trackpad pan when a stalled page merges two momentum wheels into one, 1 ms after the one before (spec:AST-094 FR43)', () => {
+ const {smooth} = mount('<div id="smooth"></div>');
+ const pan = new PanGestureRecognizer({
+ direction: 'vertical',
+ allowedScrollTypes: ['continuous'],
+ });
+ const states: string[] = [];
+ pan.addTransitionListener((_current, _from, to) => {
+ if (to !== 'changed') {
+ states.push(to);
+ }
+ });
+ detachers.push(host.attach(smooth, pan));
+ // A fling, then momentum that shrinks 7 % a frame. The page stalls and
+ // receives two momentum steps as one wheel, 1 ms after the one before.
+ const stream: [number, number][] = [];
+ let time = 500;
+ for (const dy of [8, 8, 8, 8, 7.6, 7.2, 6.8, 6.4, 6, 5.6]) {
+ stream.push([dy, time]);
+ time += 16;
+ }
+ let dy = 5.2;
+ for (let i = 0; i < 6; i++) {
+ stream.push([dy, time]);
+ dy *= 0.93;
+ time += 16;
+ }
+ const before = time - 16;
+ stream.push([dy + dy * 0.93, before + 1]);
+ dy *= 0.93 * 0.93;
+ time = before + 17;
+ for (let i = 0; i < 8; i++) {
+ stream.push([dy, time]);
+ dy *= 0.93;
+ time += 16;
+ }
+ for (const [delta, at] of stream) {
+ wheel(smooth, {trackpad: true, dy: delta, time: at});
+ }
+ clock.runDue(time + 400);
+ // The pan let go at the lift; the merged wheel starts no gesture it takes.
+ expect(states).toEqual(['began', 'ended', 'possible']);
+ });
+
it("releases a trackpad scroll whose fingers stop still whatever the host's clock reads: the release is stamped on the wheels' own times", () => {
// The host schedules a gesture's end on its clock, while the arena reads
// the wheels' own timestamps, which need not share the clock's base: a
@@ -1384,6 +1428,34 @@ describe("a trackpad scroll through the splitter's undecided stretch (spec:AST-0
},
);
+ it.each([
+ ['grow back to their size', [4.2, 4.1, 4, 3.9, 3.8]],
+ ['go on shrinking', [3.95, 3.9, 3.85, 3.8, 3.75]],
+ ])(
+ "ends a trackpad drag on the fingers' travel, still, when a stalled page merges two steps right after a shrinking one, and the steps after it %s",
+ (_after, tail) => {
+ const {box, seen} = followed();
+ // The fingers move 4 px every 16 ms, and one step shrinks. The page
+ // then receives the next two steps as one wheel 1 ms later. Five more
+ // steps follow, and the fingers stop.
+ const times = [0, 16, 32, 48, 64, 80, 81, 97, 113, 129, 145, 161];
+ const deltas = [4, 4, 4, 4, 4, 3.9, 8, ...tail];
+ deltas.forEach((dx, i) => {
+ wheel(box, {trackpad: true, dx, time: times[i]});
+ });
+ clock.runDue(times[times.length - 1] + 500);
+ const ended = seen.filter(entry => entry.state === 'ended');
+ expect(ended).toHaveLength(1);
+ // Nothing lifted, so the release is still. The pan reaches the
+ // fingers' travel, less its 10 px of hysteresis, and stays less than
+ // one of their steps ahead of it.
+ const travel = deltas.reduce((sum, dx) => sum + dx, 0) - 10;
+ expect(ended[0].velocity).toBe(0);
+ expect(ended[0].x).toBeLessThanOrEqual(-travel);
+ expect(ended[0].x).toBeGreaterThan(-travel - 4.2);
+ },
+ );
+
it.each(STREAMS)(
"keeps a trackpad pan moving at the fingers' speed through the undecided stretch at %s",
(_name, hz, stamp, jitter) => {
diff --git a/packages/core/src/gestures/__tests__/wheelSegmenter.test.ts b/packages/core/src/gestures/__tests__/wheelSegmenter.test.ts
index e77cd87e33..44f1a24389 100644
--- a/packages/core/src/gestures/__tests__/wheelSegmenter.test.ts
+++ b/packages/core/src/gestures/__tests__/wheelSegmenter.test.ts
@@ -185,3 +185,117 @@ describe("WheelSegmenter's merges: one stall's wheels, not frames", () => {
expect(isBunched(1000 / 60)).toBe(false);
});
});
+
+/** A fling: a trackpad's deltas growing as the fingers speed up. */
+const FLING = [8, 16, 24, 30, 32];
+
+/** The fling's momentum: `count` steps shrinking 5 % a frame from 30. */
+function momentum(count: number): number[] {
+ return Array.from({length: count}, (_, frame) => 30 * 0.95 ** frame);
+}
+
+describe("WheelSegmenter's reading of a stall's merge: no jump, no lift", () => {
+ it.each(STREAMS)(
+ "keeps a fling's momentum one gesture, and reads new fingers as a jump, at %s",
+ (_name, hz, stamp, jitter) => {
+ const segmenter = new WheelSegmenter();
+ const glide = momentum(60);
+ // New fingers: three times the momentum's last step, twice.
+ const sizes = [...FLING, ...glide, glide[59] * 3, glide[59] * 3];
+ const times = frames(hz, sizes.length, stamp, jitter);
+ const seen = sizes.map((size, index) =>
+ segmenter.push(times[index], size, 180),
+ );
+ const lift = seen.findIndex(segment => segment.liftNow);
+ expect(lift).toBeGreaterThanOrEqual(0);
+ const glided = seen.slice(lift, FLING.length + glide.length);
+ expect(glided.filter(segment => segment.boundary != null)).toEqual([]);
+ expect(seen.slice(-2).map(segment => segment.boundary)).toContain('jump');
+ },
+ );
+
+ it.each(STREAMS)(
+ "reads a momentum wheel 1 ms after the one before as one stall's merge of two steps, not a jump, at %s",
+ (_name, hz, stamp, jitter) => {
+ const segmenter = new WheelSegmenter();
+ const sizes = [...FLING, ...momentum(10)];
+ const times = frames(hz, sizes.length + 2, stamp, jitter);
+ sizes.forEach((size, index) => {
+ segmenter.push(times[index], size, 180);
+ });
+ // A stalled page receives the next two steps as one wheel.
+ const step = 30 * 0.95 ** 10;
+ const last = times[sizes.length - 1];
+ const merged = segmenter.push(last + 1, step + step * 0.95, 180);
+ const next = segmenter.push(times[sizes.length + 1], step * 0.9, 180);
+ expect([
+ merged.boundary,
+ merged.phase,
+ next.boundary,
+ next.phase,
+ ]).toEqual([null, 'momentum', null, 'momentum']);
+ },
+ );
+
+ it('still starts a new gesture at a jump up against the decay right after a merged momentum wheel', () => {
+ const segmenter = new WheelSegmenter();
+ feed(segmenter, [10, 10, 10, 10, 9.5, 9, 8.5, 8, 7.5]);
+ segmenter.push(144, 3, 180);
+ segmenter.push(145, 2.8 + 2.6, 180);
+ // New fingers: 5 px against the merged wheel's last step is a jump.
+ const fingers = segmenter.push(161, 5, 180);
+ expect({boundary: fingers.boundary, gesture: fingers.gesture}).toEqual({
+ boundary: 'jump',
+ gesture: 2,
+ });
+ });
+
+ it('reads no lift from the steps after a merged finger wheel', () => {
+ const segmenter = new WheelSegmenter();
+ // The fingers move 4 px every 16 ms, and one step shrinks. The page then
+ // receives the next two steps as one wheel 1 ms later. Steps of about
+ // 4 px follow, shrinking a little.
+ const times = [0, 16, 32, 48, 64, 80, 81, 97, 113, 129, 145, 161];
+ const sizes = [4, 4, 4, 4, 4, 3.9, 8, 4.2, 4.1, 4, 3.9, 3.8];
+ const seen = times.map((time, index) =>
+ segmenter.push(time, sizes[index], 180),
+ );
+ expect({
+ lift: seen.some(segment => segment.liftNow),
+ afterMerge: seen[7].decaying,
+ }).toEqual({lift: false, afterMerge: false});
+ });
+});
+
+describe('WheelSegmenter: a slowing drag with a merged wheel', () => {
+ it("reads a slowing drag's lift at the same frame whether or not a stall merged two of its steps", () => {
+ // Steady 10 px steps every 16 ms, then a drag slowing 1 % a frame.
+ const sizes = [10, 10, 10, 10];
+ for (let frame = 1; frame <= 30; frame++) {
+ sizes.push(10 * 0.99 ** frame);
+ }
+ const times = sizes.map((_, index) => index * 16);
+ const liftTime = (merge: number | null) => {
+ const segmenter = new WheelSegmenter();
+ for (let index = 0; index < sizes.length; index++) {
+ let size = sizes[index];
+ let time = times[index];
+ if (index === merge) {
+ // A stalled page receives this step and the next as one wheel,
+ // 1 ms after the one before.
+ size += sizes[index + 1];
+ time = times[index - 1] + 1;
+ } else if (merge != null && index === merge + 1) {
+ continue;
+ }
+ if (segmenter.push(time, size, 180).liftNow) {
+ return times[index];
+ }
+ }
+ return null;
+ };
+ const control = liftTime(null);
+ expect(control).not.toBeNull();
+ expect(liftTime(10)).toBe(control);
+ });
+});
diff --git a/packages/core/src/gestures/wheelSegmenter.ts b/packages/core/src/gestures/wheelSegmenter.ts
index e46f273bbb..39ff5eb0c1 100644
--- a/packages/core/src/gestures/wheelSegmenter.ts
+++ b/packages/core/src/gestures/wheelSegmenter.ts
@@ -56,7 +56,8 @@ export function isBunched(intervalMs: number): boolean {
* shrink smoothly. The lift is the start of a run of shrinking deltas that
* has lost a fifth of its size. In the finger phase, a pause of `idleMs`
* ends the gesture. In the momentum phase, a reversal, a jump up against the
- * decay, or a pause of 50 ms starts a new gesture. Momentum never pauses,
+ * decay, or a pause of 50 ms starts a new gesture. A bunched wheel is never
+ * a jump. The decay reads it by its last step. Momentum never pauses,
* reverses or grows.
*/
export class WheelSegmenter {
@@ -84,10 +85,16 @@ export class WheelSegmenter {
boundary = 'reversal';
} else if (gap >= MOMENTUM_GAP_MS) {
boundary = 'gap';
- } else if (size >= 2 && size > this.previous * 1.3 + 0.5) {
+ } else if (!bunched && size >= 2 && size > this.previous * 1.3 + 0.5) {
boundary = 'jump';
}
}
+ // A bunched wheel carries the steps of the frames it merged. The decay
+ // goes on from its last step, so the steps after it read as they are.
+ const steps =
+ boundary == null && bunched && this.previous > 0
+ ? Math.max(1, Math.round(size / this.previous))
+ : 1;
if (boundary != null) {
this.gesture += 1;
this.phase = 'finger';
@@ -97,7 +104,9 @@ export class WheelSegmenter {
let liftNow = false;
let decaying = false;
if (this.phase === 'finger' && this.count >= 3 && size > 0) {
- decaying = size <= this.previous * 0.995 && size >= this.previous * 0.5;
+ // A bunched wheel is read by its last step, as the decay goes on from it.
+ const step = size / steps;
+ decaying = step <= this.previous * 0.995 && step >= this.previous * 0.5;
if (decaying) {
if (this.run === 0) {
this.runStart = this.previous;
@@ -106,13 +115,13 @@ export class WheelSegmenter {
} else {
this.run = 0;
}
- if (this.run >= LIFT_RUN && size <= this.runStart * LIFT_DECAY) {
+ if (this.run >= LIFT_RUN && step <= this.runStart * LIFT_DECAY) {
this.phase = 'momentum';
liftNow = true;
}
}
this.count += 1;
- this.previous = size;
+ this.previous = size / steps;
if (sign !== 0) {
this.sign = sign;
}