Skip to content

Commit ebd808f

Browse files
committed
fix(core): a finger on page content reaches tracking recognizers as began-elsewhere; a recognizer's own scroll is not the browser taking the finger
From CodeBunny on meta#8195, fixed once here where the host enters: - spec:AST-094 FR28: a touch with no recognizer or consumer on its path never reached the arena, so a recognizer tracking a finger was never told about a second finger beside it (a menu press could not end on one). GestureArena.untrackedBegan reports such a touch to recognizers that are tracking touches; it creates no record, claim or lock, and its moves and end stay the page's. - spec:AST-094 FR30: a recognizer that scrolls a scroller itself (a menu's edge autoscroll) was cancelled by its own write. The internal GestureHost.ownScroll(scroller) makes onScroll skip that scroller until two frames pass; a real pan in that window still reports itself through pointercancel or touchcancel. Three tests; two fail on the previous head.
1 parent dfab196 commit ebd808f

3 files changed

Lines changed: 140 additions & 1 deletion

File tree

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

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,41 @@ export class GestureArena {
262262
});
263263
}
264264

265+
/**
266+
* A contact began where nothing takes part (plain page content). It is
267+
* never tracked and stays the browser's; recognizers already tracking a
268+
* gesture hear of it, as of any touch off their hit path.
269+
*/
270+
untrackedBegan(input: TouchBeganInput): void {
271+
if (!this.active.some(recognizer => recognizer.numberOfTouches > 0)) {
272+
return;
273+
}
274+
this.run(input.time, () => {
275+
const time = this.clampTime(input.time);
276+
const touch: GestureTouch = {
277+
id: input.id,
278+
pointerType: input.pointerType,
279+
phase: 'began',
280+
x: input.x,
281+
y: input.y,
282+
time,
283+
startX: input.x,
284+
startY: input.y,
285+
startTime: time,
286+
samples: [{x: input.x, y: input.y, time}],
287+
target: input.target,
288+
scroll: input.scroll ?? null,
289+
takenByBrowser: false,
290+
};
291+
const context = this.context();
292+
for (const recognizer of [...this.active]) {
293+
if (recognizer.numberOfTouches > 0) {
294+
recognizer.deliverBeganElsewhere([touch], context);
295+
}
296+
}
297+
});
298+
}
299+
265300
/** Contacts moved. */
266301
moved(inputs: ReadonlyArray<TouchInput>): void {
267302
if (inputs.length === 0) {

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

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ import type {
9191
GestureEnvironment,
9292
GestureTouch,
9393
PointerKind,
94+
TouchBeganInput,
9495
TouchConsumer,
9596
TouchInput,
9697
TouchSample,
@@ -276,6 +277,8 @@ export class GestureHost {
276277
private lastTouch: {readonly target: Node; readonly time: number} | null =
277278
null;
278279
/** The last scroll a finger drove, and when. */
280+
/** Scrollers a recognizer is scrolling itself this frame (ownScroll). */
281+
private readonly ownedScrolls = new Set<Node>();
279282
private lastScroll: {readonly scroller: Node; readonly time: number} | null =
280283
null;
281284
private nextId = 1;
@@ -702,6 +705,7 @@ export class GestureHost {
702705
}
703706
const time = eventTime(event);
704707
const inputs: ArenaBeganInput[] = [];
708+
const untracked: TouchBeganInput[] = [];
705709
const stale: number[] = [];
706710
for (const touch of touchesOf(event.changedTouches)) {
707711
if ((touch as StylusTouch).touchType === 'stylus') {
@@ -716,6 +720,17 @@ export class GestureHost {
716720
const target = touch.target instanceof Node ? touch.target : event.target;
717721
const {recognizers, consumer} = this.participantsAt(target, 'touch');
718722
if (recognizers.length === 0 && consumer == null) {
723+
// Nothing here takes part: the touch stays the page's, but a
724+
// recognizer tracking another finger hears it began (a second
725+
// finger beside an open menu ends its press, spec:AST-094 FR28).
726+
untracked.push({
727+
id: this.nextId++,
728+
x: touch.clientX,
729+
y: touch.clientY,
730+
time,
731+
pointerType: 'touch',
732+
target,
733+
});
719734
continue;
720735
}
721736
const id = this.nextId++;
@@ -744,6 +759,9 @@ export class GestureHost {
744759
this.arena.cancelled(stale, time);
745760
}
746761
this.arena.began(inputs);
762+
for (const input of untracked) {
763+
this.arena.untrackedBegan(input);
764+
}
747765
// Stamped after the consumers judged the touch: the brake asks whether
748766
// an EARLIER finger is still driving a scroll.
749767
if (event.target instanceof Node) {
@@ -1433,10 +1451,30 @@ export class GestureHost {
14331451
}
14341452
};
14351453

1454+
/**
1455+
* A recognizer is about to scroll `scroller` itself (a menu's edge
1456+
* autoscroll, spec:AST-094 FR30): the scroll that write fires is not a
1457+
* finger's and not the browser taking one. It passes until the end of the
1458+
* next frame, when that event has fired; a real pan in that window still
1459+
* reports itself through pointercancel or touchcancel.
1460+
*/
1461+
ownScroll(scroller: Node): void {
1462+
this.ownedScrolls.add(scroller);
1463+
const view = this.doc.defaultView;
1464+
if (view == null) {
1465+
return;
1466+
}
1467+
view.requestAnimationFrame(() => {
1468+
view.requestAnimationFrame(() => {
1469+
this.ownedScrolls.delete(scroller);
1470+
});
1471+
});
1472+
}
1473+
14361474
private readonly onScroll = (event: Event): void => {
14371475
const time = eventTime(event);
14381476
const scroller = event.target === this.doc ? this.doc : event.target;
1439-
if (!(scroller instanceof Node)) {
1477+
if (!(scroller instanceof Node) || this.ownedScrolls.has(scroller)) {
14401478
return;
14411479
}
14421480
// A scroll is a finger's only if that finger was inside this scroller,

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

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import {LongPressGestureRecognizer} from '../LongPressGestureRecognizer';
2020
import {PanGestureRecognizer} from '../PanGestureRecognizer';
2121
import {TapGestureRecognizer} from '../TapGestureRecognizer';
2222
import {ManualClock, RecordingConsumer} from './harness';
23+
import type {GestureTouch} from '../types';
2324

2425
interface Contact {
2526
id: number;
@@ -149,6 +150,71 @@ afterEach(() => {
149150
});
150151

151152
describe('GestureHost', () => {
153+
it('tells a recognizer tracking a finger about a second finger on plain page content, and leaves that finger to the page (spec:AST-094 FR28)', () => {
154+
const {box} = mount('<div id="box"></div><p id="page">text</p>');
155+
const page = document.getElementById('page')!;
156+
const elsewhere: number[] = [];
157+
class Recorder extends LongPressGestureRecognizer {
158+
protected override touchBeganElsewhere(
159+
touches: ReadonlyArray<GestureTouch>,
160+
): void {
161+
elsewhere.push(...touches.map(t => t.id));
162+
}
163+
}
164+
const recorder = new Recorder({name: 'recorder'});
165+
detachers.push(host.attach(box, recorder));
166+
const first = {id: 1, x: 10, y: 10};
167+
touch('touchstart', box, [first], [first], 0);
168+
const second = {id: 2, x: 200, y: 300};
169+
const start = touch('touchstart', page, [second], [first, second], 20);
170+
expect(elsewhere).toHaveLength(1);
171+
expect(start.defaultPrevented).toBe(false);
172+
const moved = {...second, y: 340};
173+
const move = touch('touchmove', page, [moved], [first, moved], 36);
174+
expect(move.defaultPrevented).toBe(false);
175+
const end = touch('touchend', page, [moved], [first], 50);
176+
expect(end.defaultPrevented).toBe(false);
177+
touch('touchend', box, [first], [], 60);
178+
});
179+
180+
it('leaves a finger on plain page content alone when no finger is being tracked', () => {
181+
const {box} = mount('<div id="box"></div><p id="page">text</p>');
182+
const page = document.getElementById('page')!;
183+
const elsewhere: number[] = [];
184+
class Recorder extends LongPressGestureRecognizer {
185+
protected override touchBeganElsewhere(
186+
touches: ReadonlyArray<GestureTouch>,
187+
): void {
188+
elsewhere.push(...touches.map(t => t.id));
189+
}
190+
}
191+
detachers.push(host.attach(box, new Recorder({name: 'recorder'})));
192+
const finger = {id: 1, x: 200, y: 300};
193+
touch('touchstart', page, [finger], [finger], 0);
194+
touch('touchend', page, [finger], [], 30);
195+
expect(elsewhere).toEqual([]);
196+
});
197+
198+
it("does not read a recognizer's own scroll write as the browser taking the finger (spec:AST-094 FR30)", async () => {
199+
const {list} = mount('<div id="list"><div id="row"></div></div>');
200+
scrollable(list, {scrollHeight: 1000, clientHeight: 200});
201+
const row = document.getElementById('row')!;
202+
const press = new LongPressGestureRecognizer({name: 'press'});
203+
detachers.push(host.attach(row, press));
204+
const finger = {id: 1, x: 10, y: 10};
205+
touch('touchstart', row, [finger], [finger], 0);
206+
host.ownScroll(list);
207+
list.dispatchEvent(new Event('scroll'));
208+
expect(press.numberOfTouches).toBe(1);
209+
// After the next frame, a scroll nobody announced is the browser's again.
210+
await new Promise(resolve =>
211+
requestAnimationFrame(() => requestAnimationFrame(resolve)),
212+
);
213+
await new Promise(resolve => requestAnimationFrame(resolve));
214+
list.dispatchEvent(new Event('scroll'));
215+
expect(press.numberOfTouches).toBe(0);
216+
});
217+
152218
it('watches the pointer kind only while something is attached (spec:AST-094 FR24)', () => {
153219
const {box} = mount('<div id="box"></div>');
154220
const add = vi.fn();

0 commit comments

Comments
 (0)