Skip to content

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

TypeScript

fix(gestures): read a finger's lift on the clock its moves came on (#10583)

Christopher Chedeauf543ec7b39e0
diff --git a/.changeset/gesture-lift-on-the-move-clock.md b/.changeset/gesture-lift-on-the-move-clock.md
new file mode 100644
index 0000000000..d681e024a6
--- /dev/null
+++ b/.changeset/gesture-lift-on-the-move-clock.md
@@ -0,0 +1,7 @@
+---
+'@astryxdesign/core': patch
+---
+
+[fix] Gestures: a finger's lift is read on the clock its moves came on. In WebKit on Linux, a page pointerup listener that runs long no longer makes a fast flick read as a resting finger, or a tap lifted in time read as a long press.
+
+@vjeux
diff --git a/packages/core/src/gestures/GestureHost.ts b/packages/core/src/gestures/GestureHost.ts
index a6cd3a96bb..1e6907abd4 100644
--- a/packages/core/src/gestures/GestureHost.ts
+++ b/packages/core/src/gestures/GestureHost.ts
@@ -791,6 +791,16 @@ export class GestureHost {
private readonly fingerPointers = new Map<number, number>();
/** Finger pointerdowns still waiting for their touchstart. */
private readonly pendingPointers = new Map<number, PlacedContact>();
+ /**
+ * A paired finger's lift, at its `pointerup`: the place and clock its moves
+ * came on. Linux WebKit stamps the `touchend` that follows only after the
+ * page's pointerup listeners finish. A busy listener would then make a flick
+ * read as a resting finger. Read at the touchend, dropped with the finger.
+ */
+ private readonly liftStamps = new Map<
+ number,
+ {x: number; y: number; time: number}
+ >();
/**
* Where fingers come from their pointer events, where claimed fingers
* lifted and when: the `touchend` that follows each `pointerup` is
@@ -3314,19 +3324,31 @@ export class GestureHost {
}
const tracked = this.touchIds.get(touch.identifier);
const landed = tracked == null ? undefined : this.retargeted.get(tracked);
+ const lift = tracked == null ? undefined : this.liftStamps.get(tracked);
const id = this.forgetFinger(touch.identifier);
if (id == null) {
continue;
}
- inputs.push({id, x: touch.clientX, y: touch.clientY, time});
+ inputs.push({
+ id,
+ x: lift?.x ?? touch.clientX,
+ y: lift?.y ?? touch.clientY,
+ time: lift?.time ?? time,
+ });
if (landed != null) {
retargetedEnds.push({id, landed, x: touch.clientX, y: touch.clientY});
}
}
+ // The arena takes the lifts in time order, so it never raises an earlier
+ // lift to a later one.
+ inputs.sort((a, b) => a.time - b.time);
// The lift is when momentum starts: the clock the brake wants most.
const landed = landedOn(event);
if (isNode(landed)) {
- this.lastTouch = {target: landed, time};
+ this.lastTouch = {
+ target: landed,
+ time: inputs.length > 0 ? Math.max(...inputs.map(i => i.time)) : time,
+ };
}
const dispositions =
inputs.length > 0
@@ -3668,6 +3690,7 @@ export class GestureHost {
this.retargeted.delete(id);
this.lastFed.delete(id);
this.pointerFed.delete(id);
+ this.liftStamps.delete(id);
this.judgedUncancelable.delete(id);
this.unpairedTouches.delete(id);
for (const [pointerId, paired] of this.fingerPointers) {
@@ -3805,7 +3828,16 @@ export class GestureHost {
private readonly onPointerUp = (event: PointerEvent): void => {
this.releaseArmedPress();
if (event.pointerType === 'touch' && this.usesTouchEvents) {
- // The lift itself is the touchend's: it alone can cancel the click.
+ // The lift itself is the touchend's: it alone can cancel the click. Its
+ // place and time are this event's, where the finger's moves were read.
+ const id = this.fingerPointers.get(event.pointerId);
+ if (id != null && this.pointerFed.has(id)) {
+ this.liftStamps.set(id, {
+ x: event.clientX,
+ y: event.clientY,
+ time: eventTime(event),
+ });
+ }
this.fingerPointers.delete(event.pointerId);
this.pendingPointers.delete(event.pointerId);
return;
@@ -4219,6 +4251,7 @@ export class GestureHost {
this.unpairedTouches.clear();
this.lastFed.clear();
this.pointerFed.clear();
+ this.liftStamps.clear();
// A trackpad pan a recognizer still drives is cancelled with them.
ids.push(...this.dropDrivenWheel());
if (ids.length > 0) {
diff --git a/packages/core/src/gestures/__tests__/GestureHost.test.ts b/packages/core/src/gestures/__tests__/GestureHost.test.ts
index 20480251d3..d9e7a648cb 100644
--- a/packages/core/src/gestures/__tests__/GestureHost.test.ts
+++ b/packages/core/src/gestures/__tests__/GestureHost.test.ts
@@ -1207,6 +1207,215 @@ describe('GestureHost', () => {
expect(released).toBeCloseTo(625, 6);
});
+ /** A paired finger drags 10 px every 16 ms, then lifts at 168 ms. */
+ const pairedDrag = (
+ box: HTMLElement,
+ touchendAt: number,
+ withPointer: boolean,
+ ) => {
+ const finger = {id: 0, x: 10, y: 10};
+ if (withPointer) {
+ pointer('pointerdown', box, {
+ id: 21,
+ pointerType: 'touch',
+ x: 10,
+ y: 10,
+ buttons: 1,
+ time: 0,
+ });
+ }
+ touch('touchstart', box, [finger], [finger], 0);
+ let x = 10;
+ for (let step = 1; step <= 10; step++) {
+ x += 10;
+ const time = step * 16;
+ if (withPointer) {
+ pointer('pointermove', box, {
+ id: 21,
+ pointerType: 'touch',
+ x,
+ y: 10,
+ buttons: 1,
+ time,
+ });
+ }
+ touch('touchmove', box, [{...finger, x}], [{...finger, x}], time);
+ }
+ if (withPointer) {
+ pointer('pointerup', box, {
+ id: 21,
+ pointerType: 'touch',
+ x,
+ y: 10,
+ time: 168,
+ });
+ }
+ touch('touchend', box, [{...finger, x}], [], touchendAt);
+ };
+
+ it.each([
+ ['with it', 168],
+ ['60 ms after it, held by a busy pointerup listener', 228],
+ ])(
+ 'reads a finger sampled from its pointer events as lifting at its pointerup: the touchend %s',
+ (_, touchendAt) => {
+ const {box} = mount('<div id="box"></div>');
+ const pan = new PanGestureRecognizer({name: 'pan'});
+ let released = Number.NaN;
+ pan.addListener(r => {
+ if (r.state === 'ended') {
+ released = pan.velocity().x;
+ }
+ });
+ detachers.push(host.attach(box, pan));
+ pairedDrag(box, touchendAt, true);
+ // 10 px every 16 ms: 625 px/s, whenever the touchend came.
+ expect(released).toBeCloseTo(625, 6);
+ },
+ );
+
+ it('reads a finger sampled from its touch events as lifting at its touchend', () => {
+ const {box} = mount('<div id="box"></div>');
+ const pan = new PanGestureRecognizer({name: 'pan'});
+ let released = Number.NaN;
+ pan.addListener(r => {
+ if (r.state === 'ended') {
+ released = pan.velocity().x;
+ }
+ });
+ detachers.push(host.attach(box, pan));
+ // No pointer events: the touchend is the lift, 68 ms after the last move.
+ pairedDrag(box, 228, false);
+ expect(released).toBe(0);
+ });
+
+ it('ends a tap whose pointerup comes before the long press’s time as a tap, however late its touchend', () => {
+ const {box} = mount('<div id="box"></div>');
+ const recognized: string[] = [];
+ const tap = new TapGestureRecognizer({name: 'tap'});
+ tap.addListener(r => {
+ if (r.state === 'ended') {
+ recognized.push('tap');
+ }
+ });
+ const hold = new LongPressGestureRecognizer({name: 'hold'});
+ hold.addListener(r => {
+ if (r.state === 'began') {
+ recognized.push('hold');
+ }
+ });
+ detachers.push(host.attach(box, tap), host.attach(box, hold));
+ const finger = {id: 0, x: 10, y: 10};
+ pointer('pointerdown', box, {
+ id: 21,
+ pointerType: 'touch',
+ x: 10,
+ y: 10,
+ buttons: 1,
+ time: 0,
+ });
+ touch('touchstart', box, [finger], [finger], 0);
+ pointer('pointerup', box, {
+ id: 21,
+ pointerType: 'touch',
+ x: 10,
+ y: 10,
+ time: 450,
+ });
+ // A pointerup listener ran 70 ms: past the long press's 500 ms.
+ touch('touchend', box, [finger], [], 520);
+ expect(recognized).toEqual(['tap']);
+ });
+
+ /** Each finger's place and time as it ends. */
+ const endings = () => {
+ const ended: Pick<GestureTouch, 'x' | 'y' | 'time'>[] = [];
+ return {
+ ended,
+ consumer: {
+ touchBegan() {},
+ touchMoved() {},
+ touchEnded(touch: GestureTouch) {
+ ended.push({x: touch.x, y: touch.y, time: touch.time});
+ },
+ touchCancelled() {},
+ allowsAncestorRecognizer: () => true,
+ },
+ };
+ };
+
+ it('lifts each finger of one touchend at its own time, whatever order its touch list gives', () => {
+ const {box} = mount('<div id="box"></div>');
+ const {ended, consumer} = endings();
+ detachers.push(host.attachConsumer(box, consumer));
+ const sampled = {id: 0, x: 10, y: 10};
+ const touchOnly = {id: 1, x: 200, y: 10};
+ pointer('pointerdown', box, {
+ id: 21,
+ pointerType: 'touch',
+ x: 10,
+ y: 10,
+ buttons: 1,
+ time: 0,
+ });
+ touch('touchstart', box, [sampled], [sampled], 0);
+ touch('touchstart', box, [touchOnly], [sampled, touchOnly], 10);
+ pointer('pointerup', box, {
+ id: 21,
+ pointerType: 'touch',
+ x: 10,
+ y: 10,
+ time: 168,
+ });
+ // The later lift comes first in the list.
+ touch('touchend', box, [touchOnly, sampled], [], 228);
+ expect(ended).toEqual([
+ {x: 10, y: 10, time: 168},
+ {x: 200, y: 10, time: 228},
+ ]);
+ });
+
+ it('ends a finger sampled from its pointer events where its pointerup lifted', () => {
+ const {box} = mount('<div id="box"></div>');
+ const {ended, consumer} = endings();
+ detachers.push(host.attachConsumer(box, consumer));
+ const finger = {id: 0, x: 10, y: 10};
+ pointer('pointerdown', box, {
+ id: 21,
+ pointerType: 'touch',
+ x: 10,
+ y: 10,
+ buttons: 1,
+ time: 0,
+ });
+ touch('touchstart', box, [finger], [finger], 0);
+ pointer('pointermove', box, {
+ id: 21,
+ pointerType: 'touch',
+ x: 40.5,
+ y: 10.5,
+ buttons: 1,
+ time: 16,
+ });
+ touch(
+ 'touchmove',
+ box,
+ [{...finger, x: 40, y: 10}],
+ [{...finger, x: 40, y: 10}],
+ 16,
+ );
+ pointer('pointerup', box, {
+ id: 21,
+ pointerType: 'touch',
+ x: 40.5,
+ y: 10.5,
+ time: 32,
+ });
+ // The touch list reports the place on its own channel, half a pixel off.
+ touch('touchend', box, [{...finger, x: 40, y: 10}], [], 32);
+ expect(ended).toEqual([{x: 40.5, y: 10.5, time: 32}]);
+ });
+
it('samples a finger from its touch events once the browser cancels its pointer', () => {
const {box} = mount('<div id="box"></div>');
// A watcher follows the touch the browser takes (AST-094 FR20, DEC-7).
@@ -2337,6 +2546,41 @@ describe('GestureHost', () => {
expect(host.isScrollBraking(row)).toBe(true);
});
+ it('counts a scroll as the finger’s momentum from its lift at the pointerup, not its late touchend', () => {
+ const {list, row} = mount('<div id="list"><div id="row"></div></div>');
+ const consumer = new RecordingConsumer();
+ detachers.push(host.attachConsumer(row, consumer));
+ const finger = {id: 1, x: 10, y: 10, target: row};
+ pointer('pointerdown', row, {
+ id: 21,
+ pointerType: 'touch',
+ x: 10,
+ y: 10,
+ buttons: 1,
+ time: 1000,
+ });
+ touch('touchstart', row, [finger], [finger], 1000);
+ pointer('pointerup', row, {
+ id: 21,
+ pointerType: 'touch',
+ x: 10,
+ y: 10,
+ time: 1100,
+ });
+ // A pointerup listener ran 60 ms before the touchend.
+ touch('touchend', row, [finger], [], 1160);
+ const scroll = (time: number) => {
+ const event = new Event('scroll');
+ Object.defineProperty(event, 'timeStamp', {value: time});
+ list.dispatchEvent(event);
+ };
+ const memory = UIKIT_GESTURES.press.momentumMemory.value;
+ scroll(1100 + memory + 20);
+ expect(host.isScrollBraking(row)).toBe(false);
+ scroll(1100 + memory - 20);
+ expect(host.isScrollBraking(row)).toBe(true);
+ });
+
it('reads a scroll with no finger down as the program’s, even right after a tap', () => {
const {list, row} = mount('<div id="list"><div id="row"></div></div>');
detachers.push(watchFingerScrolls(host, list));
@@ -7082,6 +7326,7 @@ describe('nothing left after the last detach (spec:AST-094 FR21, FR24)', () => {
'unpairedTouches',
'lastFed',
'pointerFed',
+ 'liftStamps',
'ownedScrolls',
'touchGates',
'fingerListeners',
@@ -7217,6 +7462,25 @@ describe('nothing left after the last detach (spec:AST-094 FR21, FR24)', () => {
expectNothingLeft(subject);
});
+ it('after a finger’s pointerup whose touchend never comes', async () => {
+ const {box} = mount('<div id="box"></div>');
+ const subject = new GestureHost(document, {
+ clock,
+ isPrimaryPointerCoarse: () => true,
+ });
+ const detach = subject.attach(
+ box,
+ new PanGestureRecognizer({direction: 'horizontal'}),
+ );
+ fingerDrag(box, 60, false);
+ pointer('pointerup', box, {pointerType: 'touch', x: 70, y: 10, time: 80});
+ touch('touchcancel', box, [{id: 1, x: 70, y: 10}], [], 90);
+ detach();
+ await settle();
+ __resetClickGuardForTest();
+ expectNothingLeft(subject);
+ });
+
it('after a claimed pan from Touch Events under a coarse pointer', async () => {
const {box} = mount('<div id="box"></div>');
const subject = new GestureHost(document, {
diff --git a/packages/core/src/gestures/__tests__/browser/fixture.ts b/packages/core/src/gestures/__tests__/browser/fixture.ts
index d61cd87d76..fe1e886247 100644
--- a/packages/core/src/gestures/__tests__/browser/fixture.ts
+++ b/packages/core/src/gestures/__tests__/browser/fixture.ts
@@ -54,6 +54,11 @@ declare global {
__attachListeners: string[];
/** Every touch listener added to the document or the window, by type. */
__pageTouchListeners: string[];
+ /**
+ * The last release velocities, read as each ends: the pan's for a dragged
+ * surface and for a scrolling one, and the pinch's.
+ */
+ __releases: {hpan?: number; hpanScroll?: number; pinch?: number};
/**
* The same, as the page left them once loaded: the test driver adds its
* own window listeners when it first runs a script.
@@ -212,6 +217,13 @@ function record(recognizer: GestureRecognizer, extra: () => Partial<Entry>) {
const hpan = new PanGestureRecognizer({name: 'hpan', direction: 'horizontal'});
record(hpan, () => ({x: hpan.translation().x, y: hpan.translation().y}));
host.attach(element('hpan'), hpan);
+window.__releases = {};
+hpan.addTransitionListener((_current, _from, to) => {
+ if (to === 'ended') {
+ window.__releases.hpan = hpan.velocity().x;
+ window.__releases.hpanScroll = hpan.scrollVelocity().x;
+ }
+});
const tap = new TapGestureRecognizer({name: 'tap'});
record(tap, () => ({}));
@@ -237,6 +249,11 @@ const pinchPan = new PanGestureRecognizer({
});
pinch.recognizeSimultaneouslyWith(pinchPan);
record(pinch, () => ({scale: pinch.scale}));
+pinch.addTransitionListener((_current, _from, to) => {
+ if (to === 'ended') {
+ window.__releases.pinch = pinch.velocity;
+ }
+});
record(pinchPan, () => ({x: pinchPan.translation().x}));
host.attach(element('pinchable'), pinch);
host.attach(element('pinchable'), pinchPan);
diff --git a/packages/core/src/gestures/__tests__/browser/gestures.interaction.browser.spec.ts b/packages/core/src/gestures/__tests__/browser/gestures.interaction.browser.spec.ts
index cb4b0e838b..0b7a1da017 100644
--- a/packages/core/src/gestures/__tests__/browser/gestures.interaction.browser.spec.ts
+++ b/packages/core/src/gestures/__tests__/browser/gestures.interaction.browser.spec.ts
@@ -1357,6 +1357,107 @@ function spread(at: Point, from: number, by: number, drift = 0): Step[] {
return steps;
}
+/**
+ * A page pointerup listener that runs `ms`, as a busy app's can. WebKit
+ * stamps the touchend that follows only once it is done.
+ */
+async function holdEveryLift(page: Page, ms: number): Promise<void> {
+ await page.evaluate(spin => {
+ addEventListener('pointerup', () => {
+ const end = performance.now() + spin;
+ while (performance.now() < end) {
+ // The listener's own work.
+ }
+ });
+ }, ms);
+}
+
+test('a flick keeps its release speed when a page pointerup listener holds its touchend: the lift is read where the moves were', async ({
+ page,
+ browserName,
+}) => {
+ const driver = await open(page, browserName);
+ const start = await center(page, 'hpan');
+ const flickOnPan = () => drag(start, {x: start.x + 120, y: start.y}, 6);
+ await driver.play(flickOnPan());
+ const releases = async () =>
+ page.evaluate(() => ({
+ drag: window.__releases.hpan ?? 0,
+ scroll: window.__releases.hpanScroll ?? 0,
+ }));
+ const free = await releases();
+ await holdEveryLift(page, 50);
+ await driver.play(flickOnPan());
+ const held = await releases();
+ expect(free.drag).toBeGreaterThan(0);
+ expect(free.scroll).toBeGreaterThan(0);
+ // On the touchend's clock, the held lift read as a resting finger: 0. That
+ // holds for a dragged surface (a sheet, a pop) and a scrolling one (a pager).
+ expect(held.drag).toBeGreaterThan(free.drag / 2);
+ expect(held.scroll).toBeGreaterThan(free.scroll / 2);
+});
+
+test('a pinch keeps its release speed when a page pointerup listener holds its touchend', async ({
+ page,
+ browserName,
+}) => {
+ const driver = await open(page, browserName);
+ const at = await center(page, 'pinchable');
+ await driver.play(spread(at, 20, 40));
+ const free = await page.evaluate(() => window.__releases.pinch ?? 0);
+ await holdEveryLift(page, 50);
+ await driver.play(spread(at, 20, 40));
+ const held = await page.evaluate(() => window.__releases.pinch ?? 0);
+ expect(free).toBeGreaterThan(0);
+ expect(held).toBeGreaterThan(free / 2);
+});
+
+test('a tap lifted before the long press’s time stays a tap when a page pointerup listener holds its touchend past that time', async ({
+ page,
+ browserName,
+}) => {
+ const driver = await openBare(page, browserName);
+ await page.evaluate(() => {
+ const target = document.createElement('div');
+ target.id = 'tapOrHold';
+ target.style.height = '80px';
+ document.body.prepend(target);
+ const {host, LongPressGestureRecognizer, TapGestureRecognizer} =
+ window.__api;
+ const seen: string[] = [];
+ Object.assign(window, {__tapOrHold: seen});
+ const tap = new TapGestureRecognizer({name: 'tap'});
+ tap.addListener(r => {
+ if (r.state === 'ended') {
+ seen.push('tap');
+ }
+ });
+ const hold = new LongPressGestureRecognizer({name: 'hold'});
+ hold.addListener(r => {
+ if (r.state === 'began') {
+ seen.push('hold');
+ }
+ });
+ host.attach(target, tap);
+ host.attach(target, hold);
+ });
+ const at = await center(page, 'tapOrHold');
+ await holdEveryLift(page, 160);
+ await driver.play([
+ {t: 0, down: [{...at, id: 1}]},
+ {t: 380, down: []},
+ ]);
+ await expect
+ .poll(
+ async () =>
+ page.evaluate(
+ () => (window as unknown as {__tapOrHold: string[]}).__tapOrHold,
+ ),
+ {message: 'the tap stays a tap'},
+ )
+ .toEqual(['tap']);
+});
+
test('pinch-zoom stays the browser’s on an element without a pinch recognizer', async ({
page,
browserName,