Skip to content

Commit 0ddfa89

Browse files
committed
feat(core): the internal touch foundation: press tracking and the animator
Core's internal touch foundation on the gesture recognizers (meta#8061) and the gesture host (meta#8071 + meta#8072): UIControl press tracking with its press profiles and hooks, built by createPressTracker; and the animator, which starts a motion from rest at its commit, holds the value on screen inline while a curve runs, and lets a call wait for a presented frame. It absorbs meta#8073 (closed: the press tracking this rests on) and meta#8340 (the animator's start and hold fixes), with the held dip's load fixes, and keeps meta#8641's rule: a gesture that cancels itself while its finger moves keeps that finger until it lifts. A gesture that ends on an unmount or a replaced node calls no listener from the effect. Co-authored-by: Cindy Zhang <[email protected]>
1 parent cc1238a commit 0ddfa89

40 files changed

Lines changed: 5850 additions & 210 deletions
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
'@astryxdesign/core': patch
3+
---
4+
5+
[chore] Internal: a gesture that cancels itself on a finger's move (a long press dragged past its tolerance) keeps that finger claimed until it lifts, so the page never takes the drag over midway; a resting finger is still given back when its gesture is cancelled. Not exported from the package entry point.
6+
7+
@vjeux
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
'@astryxdesign/core': patch
3+
---
4+
5+
[chore] Internal: UIControl press tracking with control and cell profiles and the gesture hooks. Not exported from the package entry point.
6+
7+
@vjeux

‎.changeset/gesture-readme.md‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
'@astryxdesign/core': patch
3+
---
4+
5+
[chore] Internal: the gesture system's README for component authors, with its browser proof in Chromium and WebKit.
6+
7+
@vjeux
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
'@astryxdesign/core': patch
3+
---
4+
5+
[chore] Internal: under Reduce Motion, a call that names the opacity a waiting dip returns to, with no travel, joins that dip instead of fading the element out a second time, and `reducedMotionFade()` resolves the cross-fade's curve as a registered motion role where it plays. Not exported from the package entry point.
6+
7+
@vjeux

‎packages/core/package.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -864,6 +864,7 @@
864864
"devDependencies": {
865865
"@astryxdesign/a11y-spec": "workspace:*",
866866
"@astryxdesign/cli": "workspace:*",
867+
"@astryxdesign/interaction-harness": "workspace:*",
867868
"@babel/cli": "catalog:",
868869
"@babel/core": "catalog:",
869870
"@babel/preset-react": "catalog:",
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
// Copyright (c) Meta Platforms, Inc. and affiliates.
2+
3+
/**
4+
* @file internalModulesNotExperimental.test.ts
5+
* @input vitest, the source of core's internal motion, gesture and keyboard
6+
* modules, and of the package-private utility module (touchPresentation)
7+
* @output Fails when one of them declares `@experimental`: those modules are
8+
* internal and never exported (spec:AST-017 FR21, DEC-27), and the tag
9+
* belongs only to a deliberately experimental public API
10+
* @position Testing; a repository guard over src/motion, src/gestures,
11+
* src/keyboard and src/utils/touchPresentation.ts. The rest of src/utils
12+
* backs the public `@astryxdesign/core/utils` entry, where an experimental
13+
* API may carry the tag
14+
*/
15+
16+
import fs from 'node:fs';
17+
import path from 'node:path';
18+
import {describe, expect, it} from 'vitest';
19+
20+
const SRC = path.resolve(__dirname, '..');
21+
const INTERNAL_DIRS = ['motion', 'gestures', 'keyboard'];
22+
/** Package-private files in a directory that also backs a public entry. */
23+
const INTERNAL_FILES = ['utils/touchPresentation.ts'];
24+
25+
/** Every source file under `dir`, tests and declarations left out. */
26+
function sourcesUnder(dir: string): string[] {
27+
const files: string[] = [];
28+
for (const entry of fs.readdirSync(dir, {withFileTypes: true})) {
29+
const full = path.join(dir, entry.name);
30+
if (entry.isDirectory()) {
31+
if (entry.name !== '__tests__') {
32+
files.push(...sourcesUnder(full));
33+
}
34+
} else if (
35+
/\.(ts|tsx|mjs|js)$/.test(entry.name) &&
36+
!/\.(test|spec)\.[cm]?[jt]sx?$/.test(entry.name) &&
37+
!entry.name.endsWith('.d.ts')
38+
) {
39+
files.push(full);
40+
}
41+
}
42+
return files;
43+
}
44+
45+
/** The internal files the guard reads, relative to src. */
46+
function checkedFiles(): string[] {
47+
const files: string[] = [];
48+
for (const dir of INTERNAL_DIRS) {
49+
const root = path.join(SRC, dir);
50+
if (fs.existsSync(root)) {
51+
files.push(...sourcesUnder(root));
52+
}
53+
}
54+
files.push(...INTERNAL_FILES.map(file => path.join(SRC, file)));
55+
return files.map(file => path.relative(SRC, file));
56+
}
57+
58+
describe('internal modules', () => {
59+
it('declare no @experimental: only an experimental public API carries it', () => {
60+
const offenders: string[] = [];
61+
for (const file of checkedFiles()) {
62+
fs.readFileSync(path.join(SRC, file), 'utf8')
63+
.split('\n')
64+
.forEach((line, index) => {
65+
if (/@experimental\b/.test(line)) {
66+
offenders.push(`${file}:${index + 1}`);
67+
}
68+
});
69+
}
70+
expect(offenders).toEqual([]);
71+
});
72+
73+
it('checks the directories and files it names', () => {
74+
expect(fs.existsSync(path.join(SRC, 'motion'))).toBe(true);
75+
expect(fs.existsSync(path.join(SRC, 'gestures'))).toBe(true);
76+
expect(sourcesUnder(path.join(SRC, 'motion')).length).toBeGreaterThan(5);
77+
for (const file of INTERNAL_FILES) {
78+
expect(fs.existsSync(path.join(SRC, file)), file).toBe(true);
79+
}
80+
});
81+
82+
it('leaves the public utils entry alone: of src/utils, only its package-private files are checked', () => {
83+
const utils = checkedFiles().filter(file =>
84+
file.startsWith('utils' + path.sep),
85+
);
86+
expect(utils).toEqual([path.join('utils', 'touchPresentation.ts')]);
87+
});
88+
});

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

Lines changed: 76 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -119,10 +119,15 @@ interface Timer {
119119
callback: () => void;
120120
}
121121

122+
/**
123+
* The arena that decides which recognizer gets a touch.
124+
*/
122125
export class GestureArena {
123126
readonly environment: GestureEnvironment;
124127
private readonly clock: GestureClock;
125128
private readonly options: GestureArenaOptions;
129+
/** The touches whose move the arena is delivering right now. */
130+
private movingIds: ReadonlySet<number> = new Set();
126131
private readonly records = new Map<number, TouchRecord>();
127132
/** Recognizers involved in the current interaction, in resolution order. */
128133
private active: GestureRecognizer[] = [];
@@ -134,6 +139,10 @@ export class GestureArena {
134139
/** The latest time processed: input never moves the clock backwards. */
135140
private latestTime = Number.NEGATIVE_INFINITY;
136141
private depth = 0;
142+
/** Open `deferResolution` calls: what their stops unblock waits. */
143+
private deferDepth = 0;
144+
/** A deferred stop left recognitions, held touches or resets to settle. */
145+
private owesResolution = false;
137146

138147
constructor(options: GestureArenaOptions = {}) {
139148
this.options = options;
@@ -308,8 +317,14 @@ export class GestureArena {
308317
return;
309318
}
310319
const context = this.context();
311-
for (const recognizer of [...this.active]) {
312-
recognizer.deliverMoved(changed, context);
320+
const previous = this.movingIds;
321+
this.movingIds = new Set(changed.map(touch => touch.id));
322+
try {
323+
for (const recognizer of [...this.active]) {
324+
recognizer.deliverMoved(changed, context);
325+
}
326+
} finally {
327+
this.movingIds = previous;
313328
}
314329
for (const touch of changed) {
315330
const record = this.records.get(touch.id);
@@ -457,12 +472,42 @@ export class GestureArena {
457472
}
458473

459474
/** Cancel an active recognizer, or fail a possible one. */
475+
/**
476+
* Runs `body`, code outside input that stops recognizers (an Effect that
477+
* turns one off or detaches it), without resolving what those stops
478+
* unblock: the recognitions waiting on them, the touches they held from a
479+
* view, the resets. A pass a microtask later resolves all of it, at the
480+
* last event's time, so the listeners and consumers it reaches hear it
481+
* outside the caller's frame and read the recognized state
482+
* (react-component-runtime INV2).
483+
*/
484+
deferResolution(body: () => void): void {
485+
this.deferDepth += 1;
486+
try {
487+
body();
488+
} finally {
489+
this.deferDepth -= 1;
490+
}
491+
if (this.deferDepth === 0 && this.owesResolution) {
492+
queueMicrotask(() => {
493+
if (this.owesResolution && this.deferDepth === 0) {
494+
this.run(this.lastEventTime(), () => {});
495+
}
496+
});
497+
}
498+
}
499+
500+
/** The last event's time, or now before any event. */
501+
private lastEventTime(): number {
502+
return Number.isFinite(this.latestTime)
503+
? this.latestTime
504+
: this.clock.now();
505+
}
506+
460507
stop(recognizer: GestureRecognizer): void {
461508
// A cancel from code between events happens at the last event's time:
462509
// wall time would run deadlines ahead of input still queued (FR16).
463-
const time =
464-
this.currentTime ??
465-
(Number.isFinite(this.latestTime) ? this.latestTime : this.clock.now());
510+
const time = this.currentTime ?? this.lastEventTime();
466511
this.run(time, () => {
467512
if (isActive(recognizer.state)) {
468513
this.apply(recognizer, 'cancelled');
@@ -569,19 +614,31 @@ export class GestureArena {
569614
}
570615
recognizer.pending = null;
571616
this.apply(recognizer, 'failed');
617+
if (this.deferDepth > 0) {
618+
// Inside `deferResolution`, the next pass settles.
619+
this.owesResolution = true;
620+
return;
621+
}
572622
this.reviewPending();
573623
}
574624

575625
/**
576626
* A recognition that claimed its touches and then fails or is cancelled (a
577627
* waiting recognition that fails, a gesture a second finger or the app
578628
* cancels) gives them back to the browser unless another recognition owns
579-
* them, so a finger still resting there can pinch-zoom the page.
629+
* them, so a finger still resting there can pinch-zoom the page. A gesture
630+
* that cancels itself on a finger's move (a long press past its tolerance)
631+
* keeps that finger until it lifts: the drag goes on, and the page never
632+
* takes it over midway (`spec:AST-094` FR14), as UIKit's scroll view never
633+
* takes a touch another recognizer had.
580634
*/
581-
private releaseClaim(recognizer: GestureRecognizer): void {
635+
private releaseClaim(
636+
recognizer: GestureRecognizer,
637+
keep: ReadonlySet<number> = new Set(),
638+
): void {
582639
for (const touch of recognizer.trackedTouches) {
583640
const record = this.records.get(touch.id);
584-
if (record == null || !record.isClaimed) {
641+
if (record == null || !record.isClaimed || keep.has(touch.id)) {
585642
continue;
586643
}
587644
// A recognized discrete gesture (`ended`, a swipe) still owns its
@@ -612,7 +669,9 @@ export class GestureArena {
612669
if (isTerminal(next)) {
613670
this.clearTimer(recognizer);
614671
}
615-
if (next === 'cancelled' || next === 'failed') {
672+
if (next === 'cancelled') {
673+
this.releaseClaim(recognizer, this.movingIds);
674+
} else if (next === 'failed') {
616675
this.releaseClaim(recognizer);
617676
}
618677
}
@@ -870,9 +929,15 @@ export class GestureArena {
870929
this.currentTime = this.clampTime(time);
871930
body();
872931
// Re-entrant calls settle with the outermost one: settling mid-delivery
873-
// would reset recognizers that have not heard the event yet.
932+
// would reset recognizers that have not heard the event yet. Inside
933+
// `deferResolution`, the next pass settles.
874934
if (this.depth === 1) {
875-
this.settle();
935+
if (this.deferDepth > 0) {
936+
this.owesResolution = true;
937+
} else {
938+
this.owesResolution = false;
939+
this.settle();
940+
}
876941
}
877942
} finally {
878943
this.depth -= 1;

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,9 @@ export function isActive(state: GestureState): boolean {
118118
return state === 'began' || state === 'changed';
119119
}
120120

121-
/** The browser behaviors `touch-action` can keep or remove. */
121+
/**
122+
* The browser behaviors `touch-action` can keep or remove.
123+
*/
122124
export interface NativeTouchBehavior {
123125
readonly panX: boolean;
124126
readonly panY: boolean;

0 commit comments

Comments
 (0)