An open source design system that's fully customizable and agent ready
TypeScript
fix(gestures): a stall's late wheel and the bunch after it share its interval (#9688)
Christopher Chedeauf9ac85f1f7c5
diff --git a/.changeset/trackpad-late-wheel-speed.md b/.changeset/trackpad-late-wheel-speed.mdnew file mode 100644index 0000000000..4e319f0e27--- /dev/null+++ b/.changeset/trackpad-late-wheel-speed.md@@ -0,0 +1,7 @@+---+'@astryxdesign/core': patch+---++[chore] Internal: when a main-thread stall delivers a late trackpad wheel and bunched wheels after it, a followed pan reads the fingers' speed over the late wheel's interval, so a flick no longer coasts short. The reading never passes the speed before the stall. Not exported from the package entry point.++@vjeuxdiff --git a/packages/core/src/gestures/GestureHost.ts b/packages/core/src/gestures/GestureHost.tsindex 0d76e45f55..f07d6db591 100644--- a/packages/core/src/gestures/GestureHost.ts+++ b/packages/core/src/gestures/GestureHost.ts@@ -475,6 +475,16 @@ interface WheelFollow {/** The fingers' speed at that move, content px per ms. */speedX: number;speedY: number;+ /**+ * The reading behind that speed: the fingers' travel where it began, and+ * its interval in ms.+ */+ readFromX: number;+ readFromY: number;+ readSpan: number;+ /** The fingers' speed before that reading, content px per ms. */+ priorSpeedX: number;+ priorSpeedY: number;}/**@@ -503,6 +513,39 @@ function absorbedDebt(debt: number, moved: number): number {return Math.sign(debt) * Math.max(0, Math.abs(debt) - Math.abs(moved) / 2);}+/**+ * A stall held a bunched wheel's frames back, and the move just before it took+ * their time. So the wheel joins that move's reading. It can restore what the+ * late move lowered, but never passes the speed before it.+ */+function joinReading(f: WheelFollow, trueX: number, trueY: number): void {+ if (f.readSpan <= 0) {+ return;+ }+ f.speedX = joinedSpeed(+ f.speedX,+ (trueX - f.readFromX) / f.readSpan,+ f.priorSpeedX,+ );+ f.speedY = joinedSpeed(+ f.speedY,+ (trueY - f.readFromY) / f.readSpan,+ f.priorSpeedY,+ );+}++/**+ * A joined reading, held to the speed before the late move. It only ever+ * raises the current speed, in the current direction.+ */+function joinedSpeed(current: number, reading: number, prior: number): number {+ const held =+ Math.sign(reading) * Math.min(Math.abs(reading), Math.abs(prior));+ return held * current >= 0 && Math.abs(held) > Math.abs(current)+ ? held+ : current;+}+/*** `target` where it is ahead of `shown` in the direction of `speed`, else* `shown`: a reconcile that never steps back.@@ -2276,9 +2319,22 @@ export class GestureHost {}if (this.wheelSegment?.decaying === true) {// Undecided: the real deltas wait for the splitter while the- // fingers' last speed carries the pan on.+ // fingers' last speed carries the pan on. A held wheel with its own+ // frame ends the reading a stall's bunch joins.f.heldX += dx;f.heldY += dy;+ if (this.wheelSegment.bunched) {+ // A raised speed moves on from where the pan is shown, so the+ // position never jumps inside the bunch.+ const since = time - f.lastMove;+ const speedX = f.speedX;+ const speedY = f.speedY;+ joinReading(f, f.trueX - f.heldX, f.trueY - f.heldY);+ f.baseX += (speedX - f.speedX) * since;+ f.baseY += (speedY - f.speedY) * since;+ } else {+ f.readSpan = 0;+ }this.followProvisionally(gesture, f, time);return;}@@ -2291,10 +2347,18 @@ export class GestureHost {const debtY = absorbedDebt(gesture.y - beforeY, dy);const trueX = beforeX - dx;const trueY = beforeY - dy;- // A wheel the splitter reads as one stall's merge sets no speed. Its- // speed is one the fingers never had.+ // A wheel the splitter reads as one stall's merge has no speed of its+ // own. It joins the reading of the move before its bunch, whose+ // interval holds its time.const elapsed = time - f.lastMove;- if (elapsed > 0 && this.wheelSegment?.bunched !== true) {+ if (this.wheelSegment?.bunched === true) {+ joinReading(f, trueX, trueY);+ } else if (elapsed > 0) {+ f.priorSpeedX = f.speedX;+ f.priorSpeedY = f.speedY;+ f.readFromX = f.trueX;+ f.readFromY = f.trueY;+ f.readSpan = elapsed;f.speedX = (trueX - f.trueX) / elapsed;f.speedY = (trueY - f.trueY) / elapsed;}@@ -2445,6 +2509,11 @@ export class GestureHost {baseY: kept.y,speedX: 0,speedY: 0,+ readFromX: kept.x,+ readFromY: kept.y,+ readSpan: 0,+ priorSpeedX: 0,+ priorSpeedY: 0,};}this.arena.began(diff --git a/packages/core/src/gestures/__tests__/wheel.test.ts b/packages/core/src/gestures/__tests__/wheel.test.tsindex 928d3054cc..58c2b45fad 100644--- a/packages/core/src/gestures/__tests__/wheel.test.ts+++ b/packages/core/src/gestures/__tests__/wheel.test.ts@@ -1456,6 +1456,351 @@ describe("a trackpad scroll through the splitter's undecided stretch (spec:AST-0},);+ /**+ * Trackpad flicks as WebKit delivered them to a page: 12 px finger steps,+ * then momentum. Each wheel is [time in whole ms, delta].+ */+ const RECORDED: Record<string, [number, number][]> = {+ run9: [+ [0, 12],+ [16, 12],+ [36, 12],+ [49, 12],+ [65, 12],+ [81, 12],+ [97, 12],+ [113, 12],+ [129, 12],+ [145, 12],+ [161, 12],+ [177, 12],+ [194, 11],+ [210, 10],+ [226, 9],+ [242, 8],+ [259, 7],+ [274, 6],+ [297, 5],+ [297, 4],+ [323, 3],+ [339, 2],+ [355, 1],+ ],+ run0: [+ [0, 12],+ [16, 12],+ [39, 12],+ [39, 12],+ [68, 12],+ [81, 12],+ [97, 12],+ [113, 12],+ [145, 12],+ [145, 12],+ [161, 12],+ [177, 12],+ [193, 11],+ [209, 10],+ [225, 9],+ [241, 8],+ [258, 7],+ [274, 6],+ [297, 5],+ [297, 4],+ [322, 3],+ [338, 2],+ [354, 1],+ ],+ run4: [+ [0, 12],+ [15, 12],+ [40, 12],+ [40, 12],+ [73, 12],+ [81, 12],+ [98, 12],+ [115, 12],+ [150, 12],+ [150, 24],+ [167, 12],+ [197, 11],+ [209, 10],+ [225, 9],+ [242, 8],+ [257, 7],+ [282, 6],+ [283, 5],+ [306, 4],+ [322, 3],+ [338, 2],+ [354, 1],+ ],+ run6: [+ [0, 12],+ [16, 12],+ [43, 12],+ [43, 12],+ [76, 12],+ [83, 12],+ [105, 12],+ [105, 12],+ [133, 12],+ [133, 12],+ [162, 12],+ [177, 12],+ [193, 11],+ [209, 10],+ [225, 9],+ [241, 8],+ [258, 7],+ [299, 6],+ [299, 9],+ [327, 3],+ [327, 2],+ [354, 1],+ ],+ run18: [+ [0, 12],+ [15, 12],+ [41, 12],+ [42, 12],+ [75, 12],+ [83, 12],+ [101, 12],+ [113, 12],+ [142, 12],+ [143, 12],+ [153, 12],+ [193, 12],+ [193, 11],+ [209, 10],+ [225, 9],+ [241, 8],+ [258, 7],+ [273, 6],+ [298, 5],+ [298, 4],+ [321, 3],+ [338, 2],+ [354, 1],+ ],+ run28: [+ [0, 12],+ [17, 12],+ [43, 12],+ [43, 12],+ [56, 12],+ [87, 12],+ [87, 12],+ [129, 12],+ [129, 12],+ [145, 12],+ [178, 12],+ [178, 12],+ [194, 11],+ [210, 10],+ [226, 9],+ [242, 8],+ [259, 7],+ [274, 6],+ [302, 5],+ [302, 4],+ [322, 3],+ [339, 2],+ [354, 1],+ ],+ };++ it.each([+ ['with no stall', 'run9', -12 / 16],+ ['with no stall, again', 'run0', -12 / 16],+ ['whose stalls end before its last finger step', 'run4', -12 / 17],+ ['whose momentum a stall merges', 'run6', -12 / 15],+ [+ 'whose last finger step comes 40 ms late, with the first momentum step bunched 0 ms after it',+ 'run18',+ -(12 + 11) / 40,+ ],+ [+ 'whose last finger step but one comes 33 ms late, with the last bunched 0 ms after it',+ 'run28',+ -(12 + 12) / 33,+ ],+ ] as const)(+ "releases a recorded WebKit flick at the fingers' speed: a flick %s",+ (_flick, run, speed) => {+ const {box, seen} = followed();+ let last = 0;+ for (const [time, dx] of RECORDED[run]) {+ wheel(box, {trackpad: true, dx, time});+ last = time;+ }+ clock.runDue(last + 500);+ const ended = seen.filter(entry => entry.state === 'ended');+ expect(ended).toHaveLength(1);+ // In px/s. A late step and the wheel bunched after it share the late+ // step's interval, which the stall took.+ expect(ended[0].velocity).toBeCloseTo(speed * 1000, 6);+ },+ );++ it("releases a flick at the fingers' speed when a stall delivers its held-back steps one by one, 1 ms apart, at 500 Hz", () => {+ const {box, seen} = followed();+ // 1.5 px every 2 ms, 750 px/s. A 50 ms stall holds 25 steps: the page+ // receives one late, then the other 24 one by one, 1 ms apart. Momentum+ // follows, and its fourth step reads as the lift.+ const times: number[] = [];+ const deltas: number[] = [];+ for (let i = 0; i < 20; i++) {+ times.push(i * 2);+ deltas.push(1.5);+ }+ const late = times[times.length - 1] + 50;+ for (let k = 0; k < 25; k++) {+ times.push(late + k);+ deltas.push(1.5);+ }+ for (const [k, dx] of [1.4, 1.3, 1.2, 1.1].entries()) {+ times.push(late + 24 + 2 * (k + 1));+ deltas.push(dx);+ }+ 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);+ // The 25 steps' 37.5 px took the late step's 50 ms, not the delivery's.+ expect(ended[0].velocity).toBeCloseTo(-750, 6);+ });++ it.each([+ ['one 36 px wheel', [[137, 36]]],+ [+ 'a 24 px and a 12 px wheel 1 ms apart',+ [+ [137, 24],+ [138, 12],+ ],+ ],+ ] as const)(+ 'reads a bunch after a held wheel the same however the stall splits it: %s',+ (_bunch, bunch) => {+ const {box, seen} = followed();+ // 12 px every 16 ms. A step comes 40 ms late, and a held 11 px wheel 16+ // ms after it. The bunch comes 1 ms after the held wheel, so no stamped+ // interval holds its time. Momentum follows.+ const stream: (readonly [number, number])[] = [+ [0, 12],+ [16, 12],+ [32, 12],+ [48, 12],+ [64, 12],+ [80, 12],+ [120, 12],+ [136, 11],+ ...bunch,+ [154, 11],+ [170, 10],+ [186, 9],+ [202, 8],+ ];+ for (const [time, dx] of stream) {+ wheel(box, {trackpad: true, dx, time});+ }+ clock.runDue(202 + 500);+ const ended = seen.filter(entry => entry.state === 'ended');+ expect(ended).toHaveLength(1);+ // The late step's reading stands: 12 px in 40 ms.+ expect(ended[0].velocity).toBeCloseTo(-300, 6);+ },+ );++ it.each([+ ['one 21 px wheel', [[121, 21]]],+ [+ 'an 11 px and a 10 px wheel 1 ms apart',+ [+ [121, 11],+ [122, 10],+ ],+ ],+ ] as const)(+ 'reads a bunch that starts shrinking right after a late step the same however the stall splits it: %s',+ (_bunch, bunch) => {+ const {box, seen} = followed();+ // 12 px every 16 ms. A step comes 40 ms late, and the bunch the stall+ // held back arrives 1 ms after it. Momentum follows into the lift.+ const stream: (readonly [number, number])[] = [+ [0, 12],+ [16, 12],+ [32, 12],+ [48, 12],+ [64, 12],+ [80, 12],+ [120, 12],+ ...bunch,+ [137, 9],+ [153, 8],+ [169, 7],+ [185, 6],+ ];+ for (const [time, dx] of stream) {+ wheel(box, {trackpad: true, dx, time});+ }+ clock.runDue(185 + 500);+ const ended = seen.filter(entry => entry.state === 'ended');+ expect(ended).toHaveLength(1);+ // The whole bunch joins the late step's 40 ms, held to the 750 px/s+ // before it.+ expect(ended[0].velocity).toBeCloseTo(-750, 6);+ },+ );++ it("never moves or releases faster than the fingers when a stall's bunch of shrinking wheels arrives 1 ms apart", () => {+ const {box, seen} = followed();+ // 12 px every 16 ms. A step comes 60 ms late, then 11, 10 and 9 px wheels+ // 1 ms apart. The 8 px wheel on the next frame reads as the lift.+ const times = [0, 16, 32, 48, 64, 80, 140, 141, 142, 143, 159];+ const deltas = [12, 12, 12, 12, 12, 12, 12, 11, 10, 9, 8];+ deltas.forEach((dx, i) => {+ wheel(box, {trackpad: true, dx, time: times[i]});+ });+ clock.runDue(times[times.length - 1] + 500);+ const shown = seen.filter(+ entry => entry.state === 'changed' && entry.time >= 140,+ );+ for (let i = 1; i < shown.length; i++) {+ const speed =+ Math.abs(shown[i].x - shown[i - 1].x) /+ (shown[i].time - shown[i - 1].time);+ expect(speed).toBeLessThanOrEqual(12 / 16 + 1e-9);+ }+ const ended = seen.filter(entry => entry.state === 'ended');+ expect(ended).toHaveLength(1);+ // Each 1 ms of the bunch runs at the speed before its join. The release+ // blends the last two intervals: 0.2 × 700 plus 0.8 × 550 px/s.+ expect(ended[0].velocity).toBeCloseTo(-580, 6);+ });++ it('never reads the fingers faster than they last were when a bunched wheel carries more than its stall took', () => {+ const {box, seen} = followed();+ // 12 px every 16 ms. A step comes 20 ms late. Three more arrive as one+ // 36 px wheel 0 ms after it. That is more travel than 20 ms at the+ // fingers' speed covers. Momentum follows, and its fourth step reads as+ // the lift.+ const times = [0, 16, 32, 48, 64, 80, 100, 100, 116, 132, 148, 164];+ const deltas = [12, 12, 12, 12, 12, 12, 12, 36, 11, 10, 9, 8];+ 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);+ // The joined reading, 48 px in 20 ms, holds at the 12 px in 16 ms before.+ expect(ended[0].velocity).toBeCloseTo(-750, 6);+ });+it.each(STREAMS)("keeps a trackpad pan moving at the fingers' speed through the undecided stretch at %s",(_name, hz, stamp, jitter) => {