Skip to content

Commit f5c2930

Browse files
committed
fix(core): a finger is sampled from one channel
The browser reports a finger on both pointer and touch events, and the host fed both, dropping the second copy of a move only when its time and position matched. WebKit delivers the pair 1 ms apart, so the copy entered as a sample without movement and corrupted the release velocity (a steady 600 px/s drag read 125 in one run). A finger whose pointerdown paired with its touchstart is now sampled from its pointer events alone; touch events sample only a finger no pointer event reported, and keep their control duties (cancelling, claiming, the browser's takeover). No clock comparison: the copy never reaches the tracker, at any spacing.
1 parent b7dd883 commit f5c2930

3 files changed

Lines changed: 127 additions & 11 deletions

File tree

‎.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`) 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.
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`) 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.
66

77
@vjeux

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

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -297,8 +297,18 @@ export class GestureHost {
297297
private readonly pendingPointers = new Map<number, PlacedContact>();
298298
/** Fingers still waiting for their pointerdown, by arena id. */
299299
private readonly unpairedTouches = new Map<number, PlacedContact>();
300-
/** The last position fed per contact, so one channel's echo is dropped. */
300+
/** The last sample fed per contact: a contact's time never runs back. */
301301
private readonly lastFed = new Map<number, TouchSample>();
302+
/**
303+
* Fingers whose samples come from their pointer events. A finger the
304+
* browser reports on both channels is sampled from ONE: the pointer
305+
* channel, where its pointerdown paired with its touchstart; touch events
306+
* sample only a finger no pointer event reported (no Pointer Events). The
307+
* other channel keeps its control duties (cancelling, claiming, the
308+
* browser's takeover) but never feeds a sample, so the two copies of one
309+
* move cannot both reach a velocity, however far apart they arrive.
310+
*/
311+
private readonly pointerFed = new Set<number>();
302312
private readonly hovered = new Map<number, Set<HoverGestureRecognizer>>();
303313
/** When a finger was last down or lifted, and where it landed. */
304314
private lastTouch: {readonly target: Node; readonly time: number} | null =
@@ -975,7 +985,8 @@ export class GestureHost {
975985
this.arena.browserTookOver(taken, time);
976986
}
977987
}
978-
this.feedMoves(inputs);
988+
// A finger its pointer events sample is fed from there, never from here.
989+
this.feedMoves(inputs.filter(input => !this.pointerFed.has(input.id)));
979990
};
980991

981992
private readonly onHoldTouchStart = (event: TouchEvent): void => {
@@ -1295,6 +1306,7 @@ export class GestureHost {
12951306
if (isSamePlace(pending, place)) {
12961307
this.pendingPointers.delete(pointerId);
12971308
this.fingerPointers.set(pointerId, id);
1309+
this.pointerFed.add(id);
12981310
return;
12991311
}
13001312
}
@@ -1306,23 +1318,18 @@ export class GestureHost {
13061318
if (isSamePlace(unpaired, place)) {
13071319
this.unpairedTouches.delete(id);
13081320
this.fingerPointers.set(pointerId, id);
1321+
this.pointerFed.add(id);
13091322
return;
13101323
}
13111324
}
13121325
this.pendingPointers.set(pointerId, place);
13131326
}
13141327

1315-
/** Feed moves, dropping the echo of a move the other channel delivered. */
1328+
/** Feed moves from a contact's sampling channel, in time order. */
13161329
private feedMoves(inputs: ReadonlyArray<TouchInput>): void {
13171330
const fresh = inputs.filter(input => {
13181331
const last = this.lastFed.get(input.id);
1319-
if (
1320-
last != null &&
1321-
(input.time < last.time ||
1322-
(input.time === last.time &&
1323-
input.x === last.x &&
1324-
input.y === last.y))
1325-
) {
1332+
if (last != null && input.time < last.time) {
13261333
return false;
13271334
}
13281335
this.lastFed.set(input.id, {x: input.x, y: input.y, time: input.time});
@@ -1337,6 +1344,7 @@ export class GestureHost {
13371344
private forgetContact(id: number): void {
13381345
this.unlockSelection(id);
13391346
this.lastFed.delete(id);
1347+
this.pointerFed.delete(id);
13401348
this.judgedUncancelable.delete(id);
13411349
this.unpairedTouches.delete(id);
13421350
for (const [pointerId, paired] of this.fingerPointers) {
@@ -1749,6 +1757,7 @@ export class GestureHost {
17491757
this.pendingPointers.clear();
17501758
this.unpairedTouches.clear();
17511759
this.lastFed.clear();
1760+
this.pointerFed.clear();
17521761
if (ids.length > 0) {
17531762
this.arena.cancelled(ids, time);
17541763
}

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

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import {GestureHost} from '../GestureHost';
1919
import {HoverGestureRecognizer} from '../HoverGestureRecognizer';
2020
import {LongPressGestureRecognizer} from '../LongPressGestureRecognizer';
2121
import {PanGestureRecognizer} from '../PanGestureRecognizer';
22+
import {VelocityTracker} from '../VelocityTracker';
2223
import {TapGestureRecognizer} from '../TapGestureRecognizer';
2324
import {createNativeScrollProbe} from '../nativeScroll';
2425
import {ManualClock, RecordingConsumer} from './harness';
@@ -322,6 +323,112 @@ describe('GestureHost', () => {
322323
expect(host.arena.getTouch(1)?.takenByBrowser).toBe(true);
323324
});
324325

326+
it.each([
327+
['WebKit, the touch copy 1 ms later', 1],
328+
['Chromium, both copies at one time', 0],
329+
])('samples a finger from its pointer events alone: %s', (_, offset) => {
330+
const {box} = mount('<div id="box"></div>');
331+
const pan = new PanGestureRecognizer({name: 'pan'});
332+
let released = Number.NaN;
333+
pan.addListener(r => {
334+
if (r.state === 'ended') {
335+
released = pan.velocity().x;
336+
}
337+
});
338+
detachers.push(host.attach(box, pan));
339+
pointer('pointerdown', box, {
340+
id: 21,
341+
pointerType: 'touch',
342+
x: 10,
343+
y: 10,
344+
buttons: 1,
345+
time: 0,
346+
});
347+
const finger = {id: 0, x: 10, y: 10};
348+
touch('touchstart', box, [finger], [finger], 0);
349+
let x = 10;
350+
for (let step = 1; step <= 10; step++) {
351+
x += 10;
352+
const time = step * 16;
353+
pointer('pointermove', box, {
354+
id: 21,
355+
pointerType: 'touch',
356+
x,
357+
y: 10,
358+
buttons: 1,
359+
time,
360+
});
361+
touch(
362+
'touchmove',
363+
box,
364+
[{...finger, x}],
365+
[{...finger, x}],
366+
time + offset,
367+
);
368+
}
369+
pointer('pointerup', box, {
370+
id: 21,
371+
pointerType: 'touch',
372+
x,
373+
y: 10,
374+
time: 168,
375+
});
376+
touch('touchend', box, [{...finger, x}], [], 168);
377+
// 10 px every 16 ms: 625 px/s, the steady drag's own speed.
378+
expect(released).toBeCloseTo(625, 6);
379+
});
380+
381+
it('keeps every report of a 120 Hz finger', () => {
382+
const {box} = mount('<div id="box"></div>');
383+
const pan = new PanGestureRecognizer({name: 'pan'});
384+
let released = Number.NaN;
385+
pan.addListener(r => {
386+
if (r.state === 'ended') {
387+
released = pan.velocity().x;
388+
}
389+
});
390+
detachers.push(host.attach(box, pan));
391+
pointer('pointerdown', box, {
392+
id: 21,
393+
pointerType: 'touch',
394+
x: 10,
395+
y: 10,
396+
buttons: 1,
397+
time: 0,
398+
});
399+
const finger = {id: 0, x: 10, y: 10};
400+
touch('touchstart', box, [finger], [finger], 0);
401+
// An accelerating drag reported every 8 ms on both channels.
402+
const expected = new VelocityTracker();
403+
expected.add(10, 10, 0);
404+
let last = {x: 10, time: 0};
405+
for (let step = 1; step <= 12; step++) {
406+
const x = 10 + 3 * step * step;
407+
const time = step * 8;
408+
pointer('pointermove', box, {
409+
id: 21,
410+
pointerType: 'touch',
411+
x,
412+
y: 10,
413+
buttons: 1,
414+
time,
415+
});
416+
touch('touchmove', box, [{...finger, x}], [{...finger, x}], time);
417+
expected.add(x, 10, time);
418+
last = {x, time};
419+
}
420+
pointer('pointerup', box, {
421+
id: 21,
422+
pointerType: 'touch',
423+
x: last.x,
424+
y: 10,
425+
time: last.time,
426+
});
427+
touch('touchend', box, [{...finger, x: last.x}], [], last.time);
428+
// Every report counted once: the blend of the last two intervals.
429+
expect(released).toBeCloseTo(expected.velocity().x, 6);
430+
});
431+
325432
it('gives the blocking touchmove listener only to elements whose recognizers claim while moving', () => {
326433
const {taps, pan, press, both, row} = mount(
327434
'<div id="taps"></div><div id="pan"></div><div id="press"></div><div id="both"></div><div id="row"></div>',

0 commit comments

Comments
 (0)