Skip to content

Commit 17fcb15

Browse files
authored
feat(core): one shared context request from a long press, a right click and Shift+F10 (#8670)
* feat(core): one context request for a hold, a right click and Shift+F10 A context menu should open the same way from a held finger or pen, a right click, and Shift+F10 or the context-menu key. Each component wired its own handlers, so the rules differed: a held finger on Android could open the browser's own menu beside Astryx's. attachContextRequest(element, {onRequest}) is the one internal request a context menu answers. Each gesture makes one request, naming its input (a hold, a pointer or the keyboard), so a keyboard-opened menu can focus its first row. A held key's repeats make no request. The hold is UIKit's long press and an arena participant, so it loses to a claim another recognizer made first. A hold disabled before it requests reports nothing. A mouse held still makes no request. A control that keeps its press for a hold-repeat owns only its hold, while its right click and Shift+F10 still reach the request. The browser's own menu is refused once, for a finger while it is down inside the element and for a pen during and right after any hold of it there. Inside nested requests, a right click or a key reaches only the innermost. It listens on its own element only, and only while enabled. * Merge main * docs(specs): name the shared context request module in AST-094 FR43 FR43 says one shared context request answers a long press, a right click and Shift+F10. The requirement now names that module, gestures/contextRequest.ts, and what it does: one request per gesture, the input that made it, the browser's own menu kept closed, and a hold that loses to an earlier claim. The headless context menu makes its request through it, and ContextMenu through that menu, so every menu opens by the same rules. The record's path list links the module. * fix(gestures): share the context request's answered set across copies A right click or a key inside nested context requests reaches only the innermost: the first request adds the event to a set, and the others skip it. The set was module state, so a request in a swizzled copy of Core kept its own set and answered the same event again. It is now a shared weak set under a registered key, as the shared-identity check asks for module state an export uses.
1 parent e5984b2 commit 17fcb15

8 files changed

Lines changed: 1065 additions & 6 deletions

File tree

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: one shared context request from a long press, a right click, or Shift+F10 and the context-menu key (AST-094 FR43), reporting which input made it, one per gesture. A held finger's hold loses to a claim another gesture already made on that finger, and a control that keeps its press for a hold-repeat owns only its hold, so its right click and Shift+F10 still reach the request. A held pen's own context menu event is refused like a held finger's. The hold is UIKit's long press and requests as it begins. Not exported from the package entry point.
6+
7+
@vjeux

‎docs/specs/AST-094-touch-presentations/spec.md‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ verified_by:
3434
"paths": [
3535
"packages/core/src/Layer/holdEntranceInput.ts",
3636
"packages/core/src/Layer/inputHold.ts",
37+
"packages/core/src/gestures/contextRequest.ts",
3738
"packages/core/src/gestures/tapWobble.ts",
3839
"packages/core/src/hooks/usePager.ts",
3940
"packages/core/src/motion/uikit/segmented.ts",
@@ -870,8 +871,13 @@ under a finger, and a mouse follows `module:DropdownMenu/useMenuPress` FR3 and
870871
Every followed trackpad pan runs one shared path (the splitter, the pan, the
871872
snap-point drag and the animator), and a component declares only its detents
872873
and actions, a row's being `spec:AST-057`'s. One shared
873-
context request answers a finger's or a pen's long press, a right click, and
874-
Shift+F10; the
874+
context request module, `gestures/contextRequest.ts`, answers a finger's or
875+
a pen's long press, a right click, and Shift+F10 or the context-menu key. It
876+
makes one request per gesture, names the input that made it, and keeps the
877+
browser's own menu closed. A hold loses to a claim another recognizer made
878+
first. The headless context menu (`spec:AST-107` FR8) makes its request
879+
through it, and ContextMenu through that menu (`component:ContextMenu`
880+
FR10). One module keeps these rules the same in every menu. The
875881
press model has a fine-pointer profile with no delay (FR11); and with a
876882
mouse, a trackpad, or a pen where the primary pointer is fine a reorder
877883
starts on a drag from its handle without a hold, while a pen where the

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

Lines changed: 125 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,11 @@ import {
130130
resolvePresented,
131131
watchPresentedElements,
132132
} from '../motion/presentation';
133-
import {UIKIT_PRESS, UIKIT_TAP} from '../motion/uikit/gestures';
133+
import {
134+
UIKIT_LONG_PRESS,
135+
UIKIT_PRESS,
136+
UIKIT_TAP,
137+
} from '../motion/uikit/gestures';
134138
import {
135139
browserClock,
136140
GestureArena,
@@ -794,6 +798,13 @@ export class GestureHost {
794798
*/
795799
private readonly layoutWatched = new Set<Element>();
796800
private layoutObserver: ResizeObserver | null = null;
801+
/** Where and when the last pen hold the host tracked ended. */
802+
private lastPenLift: {target: unknown; time: number} | null = null;
803+
/**
804+
* Arena ids of the pen contacts that left the long press's tolerance
805+
* before its duration. No such contact is a hold.
806+
*/
807+
private readonly driftedPens = new Set<number>();
797808
/** Fingers whose first uncancelable `touchmove` was already judged. */
798809
private readonly judgedUncancelable = new Set<number>();
799810
/**
@@ -1430,6 +1441,37 @@ export class GestureHost {
14301441
);
14311442
}
14321443

1444+
/**
1445+
* @internal Whether a pen that went down inside `node` is down now,
1446+
* whichever participant keeps it. A pen's own menu event is that hold's
1447+
* (contextRequest.ts).
1448+
*/
1449+
isPenDownIn(node: Node): boolean {
1450+
return this.arena.touches.some(
1451+
touch =>
1452+
touch.pointerType === 'pen' &&
1453+
// A lifted pen that a recognizer still decides on is no longer down.
1454+
touch.phase !== 'ended' &&
1455+
touch.phase !== 'cancelled' &&
1456+
touch.target instanceof Node &&
1457+
containsAlongFlatTree(node, touch.target),
1458+
);
1459+
}
1460+
1461+
/**
1462+
* @internal When a pen hold that went down inside `node` last ended, at
1463+
* its lift or a cancel. The time is on the host's clock: negative infinity
1464+
* when none did.
1465+
*/
1466+
lastPenLiftIn(node: Node): number {
1467+
const lift = this.lastPenLift;
1468+
return lift != null &&
1469+
lift.target instanceof Node &&
1470+
containsAlongFlatTree(node, lift.target)
1471+
? lift.time
1472+
: Number.NEGATIVE_INFINITY;
1473+
}
1474+
14331475
private registrationFor(element: Element): Registration {
14341476
this.watchPointer();
14351477
let registration = this.registry.get(element);
@@ -2986,6 +3028,33 @@ export class GestureHost {
29863028
};
29873029
}
29883030

3031+
/**
3032+
* @internal Whether a press consumer that keeps its press (a hold-repeat
3033+
* control, `keepsTouch`) sits on the path from `target` up to `boundary`,
3034+
* both included. Such a control keeps its long press its own. Its right
3035+
* click and Shift+F10 still reach a context request around it
3036+
* (contextRequest.ts).
3037+
*/
3038+
keepsPressBetween(target: unknown, boundary: Element): boolean {
3039+
for (
3040+
let node: Node | null = target instanceof Node ? target : null;
3041+
node != null;
3042+
node = parentAcrossShadow(node)
3043+
) {
3044+
if (!(node instanceof Element)) {
3045+
continue;
3046+
}
3047+
const consumer = this.registry.get(node)?.consumer;
3048+
if (consumer?.keepsTouch != null && consumer.isEnabled?.() !== false) {
3049+
return true;
3050+
}
3051+
if (node === boundary) {
3052+
break;
3053+
}
3054+
}
3055+
return false;
3056+
}
3057+
29893058
/**
29903059
* How many fingers are on the screen: the length of `touches` in the last
29913060
* touch event the host's listeners heard (`touchstart`, `touchmove`,
@@ -3834,6 +3903,14 @@ export class GestureHost {
38343903
}
38353904
const id = this.pointerIds.get(event.pointerId);
38363905
if (id != null) {
3906+
if (event.pointerType === 'pen') {
3907+
this.notePenDrift(
3908+
id,
3909+
event.clientX,
3910+
event.clientY,
3911+
eventTime(event, this.timing),
3912+
);
3913+
}
38373914
this.arena.moved([
38383915
{
38393916
id,
@@ -3876,6 +3953,11 @@ export class GestureHost {
38763953
this.pointerKinds.delete(event.pointerId);
38773954
const hold = this.claimHolds.get(id);
38783955
this.unlockSelection(id);
3956+
const target = this.arena.getTouch(id)?.target;
3957+
const isPenHold =
3958+
event.pointerType === 'pen' &&
3959+
this.isPenHold(id, eventTime(event, this.timing));
3960+
this.driftedPens.delete(id);
38793961
const dispositions = this.arena.ended([
38803962
{
38813963
id,
@@ -3886,6 +3968,9 @@ export class GestureHost {
38863968
]);
38873969
// A selection the lift's own handlers made stays, as a click's does.
38883970
hold?.keepLiftSelection();
3971+
if (isPenHold) {
3972+
this.lastPenLift = {target, time: this.arena.now()};
3973+
}
38893974
// A retargeted tap's own click arms the click guard for the engine's.
38903975
const landed = this.retargeted.get(id);
38913976
this.retargeted.delete(id);
@@ -3993,10 +4078,48 @@ export class GestureHost {
39934078
this.pointerKinds.delete(event.pointerId);
39944079
this.unlockSelection(id, true);
39954080
this.retargeted.delete(id);
4081+
const target = this.arena.getTouch(id)?.target;
4082+
const isPenHold =
4083+
event.pointerType === 'pen' &&
4084+
this.isPenHold(id, eventTime(event, this.timing));
4085+
this.driftedPens.delete(id);
39964086
this.arena.cancelled([id], eventTime(event, this.timing));
4087+
if (isPenHold) {
4088+
this.lastPenLift = {target, time: this.arena.now()};
4089+
}
39974090
}
39984091
};
39994092

4093+
/**
4094+
* Whether a pen contact is a hold: it stays within the long press's
4095+
* tolerance for its duration. Only such a hold fires the platform's own
4096+
* menu event, so the host notes no other pen's lift.
4097+
*/
4098+
private isPenHold(id: number, time: number): boolean {
4099+
const touch = this.arena.getTouch(id);
4100+
return (
4101+
touch != null &&
4102+
!this.driftedPens.has(id) &&
4103+
time - touch.startTime >= UIKIT_LONG_PRESS.minimumPressDuration.value
4104+
);
4105+
}
4106+
4107+
/**
4108+
* A pen contact that leaves the long press's tolerance before its duration
4109+
* is no hold, so its lift arms no menu refusal.
4110+
*/
4111+
private notePenDrift(id: number, x: number, y: number, time: number): void {
4112+
const touch = this.arena.getTouch(id);
4113+
if (
4114+
touch != null &&
4115+
time - touch.startTime < UIKIT_LONG_PRESS.minimumPressDuration.value &&
4116+
Math.hypot(x - touch.startX, y - touch.startY) >
4117+
UIKIT_LONG_PRESS.allowableMovement.value
4118+
) {
4119+
this.driftedPens.add(id);
4120+
}
4121+
}
4122+
40004123
/**
40014124
* Which live finger a touch `pointercancel` is about. The pointer keeps
40024125
* the target the finger landed on (implicit capture), as the Touch does;
@@ -4272,6 +4395,7 @@ export class GestureHost {
42724395
}
42734396
this.pointerIds.clear();
42744397
this.pointerKinds.clear();
4398+
this.driftedPens.clear();
42754399
this.retargeted.clear();
42764400
this.fingerPointers.clear();
42774401
this.pendingPointers.clear();

‎packages/core/src/gestures/README.md‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -560,6 +560,27 @@ the rest of that stream is the page's. A ctrl-wheel pinch keeps its stream,
560560
so the page never zooms for it, and a pan its lift released keeps its
561561
momentum latched until the stream ends.
562562

563+
## Context requests
564+
565+
`attachContextRequest(element, {onRequest})` (`contextRequest.ts`, internal)
566+
is the one request a context menu answers (`spec:AST-094` FR43): a finger or
567+
a pen held still for UIKit's long press, a pointer's context click (a right
568+
click, a ctrl-click on macOS, a pen's barrel button), or Shift+F10 and the
569+
context-menu key. Each gesture makes one request, reporting its `kind`
570+
(`hold`, `pointer`, `keyboard`), its pointer, and its point (none from
571+
the keyboard). The hold is an arena participant, so it loses to a claim
572+
another recognizer made on that finger first (a reorder handle's hold); the
573+
browser's own menu that a held finger, a held pen or a key also fires is
574+
refused, never requested again; and a mouse held still makes no request. A
575+
control that keeps its press for a hold-repeat owns only its hold: its right
576+
click and Shift+F10 still reach the request. A right click or a ctrl-click on macOS starts no press.
577+
The request listens on its own element only, and only while enabled.
578+
579+
The hold is UIKit's long press, 500 ms and 10 px as a distance, and it
580+
requests as it begins; `onHold` reports it committed, then ended at its lift
581+
or cancelled. A hold disabled before it requests reports nothing, and a held
582+
key's repeats make no request.
583+
563584
## Time
564585

565586
Every input carries its own timestamp (`event.timeStamp`), and timers are

‎packages/core/src/gestures/__tests__/browser/bareFixture.ts‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,16 @@
55
* @input Nothing: bundled by esbuild and injected into a blank page
66
* @output A page with nothing attached. `window.__api` holds the document's
77
* gesture host and the host lookup for another document. It also holds
8-
* the recognizers (pan, screen-edge pan, tap, long press) and the press
9-
* tracker, for a test to attach what it needs. `window.__pageTouch` is
10-
* the count of touch listeners on the window and the document right now.
8+
* the recognizers (pan, screen-edge pan, tap, long press), the press
9+
* tracker and the context request. A test attaches what it needs.
10+
* `window.__pageTouch` is the count of touch listeners on the window and
11+
* the document right now.
1112
* @position Browser fixture of gestures.interaction.browser.spec.ts for the
1213
* checks that need a page with no other gesture on it (FR19, FR24);
1314
* test-only
1415
*/
1516

17+
import {attachContextRequest} from '../../contextRequest';
1618
import {getGestureHost} from '../../GestureHost';
1719
import {LongPressGestureRecognizer} from '../../LongPressGestureRecognizer';
1820
import {PanGestureRecognizer} from '../../PanGestureRecognizer';
@@ -26,7 +28,9 @@ declare global {
2628
interface Window {
2729
__pageTouch: number;
2830
__wheelLog: string[];
31+
__requests: string[];
2932
__api: {
33+
attachContextRequest: typeof attachContextRequest;
3034
host: ReturnType<typeof getGestureHost>;
3135
getGestureHost: typeof getGestureHost;
3236
LongPressGestureRecognizer: typeof LongPressGestureRecognizer;
@@ -96,7 +100,9 @@ EventTarget.prototype.removeEventListener = function (
96100
};
97101

98102
document.body.style.margin = '0';
103+
window.__requests = [];
99104
window.__api = {
105+
attachContextRequest,
100106
host: getGestureHost(document),
101107
getGestureHost,
102108
LongPressGestureRecognizer,

‎packages/core/src/gestures/__tests__/browser/gestures.interaction.browser.spec.ts‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1194,6 +1194,49 @@ test('a zero-delay hold that brings its own lock turns selection off only for a
11941194
expect(held.lens).toEqual([true, false]);
11951195
});
11961196

1197+
test('one context request per gesture from a finger held still, a right click and Shift+F10, each naming its input (spec:AST-094 FR43)', async ({
1198+
page,
1199+
browserName,
1200+
}) => {
1201+
const driver = await openBare(page, browserName);
1202+
await page.evaluate(() => {
1203+
const {attachContextRequest} = window.__api;
1204+
document.body.innerHTML =
1205+
'<div id="row" tabindex="0" style="height: 80px">Row</div>';
1206+
attachContextRequest(document.getElementById('row') as HTMLElement, {
1207+
onRequest: request =>
1208+
window.__requests.push(
1209+
`${request.kind}:${request.pointerType ?? 'none'}`,
1210+
),
1211+
});
1212+
});
1213+
const at = await center(page, 'row');
1214+
// Held past the long press: one request. Android fires its own context
1215+
// menu during the hold too, as a touch's. The request refuses that one and
1216+
// makes no second request.
1217+
await driver.play([{t: 0, down: [{id: 1, ...at}]}]);
1218+
await page.waitForTimeout(900);
1219+
const nativeMenuRefused = await page.evaluate(() => {
1220+
const menu = new PointerEvent('contextmenu', {
1221+
bubbles: true,
1222+
cancelable: true,
1223+
pointerType: 'touch',
1224+
});
1225+
document.getElementById('row')?.dispatchEvent(menu);
1226+
return menu.defaultPrevented;
1227+
});
1228+
await driver.play([{t: 0, down: []}]);
1229+
expect(nativeMenuRefused).toBe(true);
1230+
await page.mouse.click(at.x, at.y, {button: 'right'});
1231+
await page.focus('#row');
1232+
await page.keyboard.press('Shift+F10');
1233+
expect(await page.evaluate(() => window.__requests)).toEqual([
1234+
'hold:touch',
1235+
'pointer:mouse',
1236+
'keyboard:none',
1237+
]);
1238+
});
1239+
11971240
test('a tap recognizer replaces the click: no ghost click, no compatibility mouse events', async ({
11981241
page,
11991242
browserName,

0 commit comments

Comments
 (0)