Skip to content

Commit 513a2ec

Browse files
committed
feat(core): code outside a component relates to its gesture recognizers by name
Components publish their main recognizers under stable Component.role names (PUBLISHED_GESTURES). gestureNamed(name, {within, containing}) matches them, telling instances apart by the element they are attached to, evaluated when a touch asks. App code relates its own recognizer through its existing delegate options, and adds delegate methods to recognizers it does not own with addGestureDelegate / useGestureDelegate: UIKit's delegate model, additive to the component's own (any false veto wins, any true relationship applies). The arena consults the registered delegates; with none, nothing changes. The browser drivers move to a shared module for the new Chromium and WebKit spec.
1 parent b496085 commit 513a2ec

14 files changed

Lines changed: 1259 additions & 202 deletions

‎.changeset/gesture-delegates.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+
[experimental] `@astryxdesign/core/gestures/experimental` lets code outside a component relate to the component's own gesture recognizers, the way a UIKit app uses a recognizer's name and its own delegate: components publish their main recognizers under stable `Component.role` names (`PUBLISHED_GESTURES`), `gestureNamed(name, {within, containing})` matches them, and `addGestureDelegate` / `useGestureDelegate` add delegate methods (vetoes and require-to-fail or simultaneous relationships) on top of the component's own. Code that declares nothing behaves as before.
6+
7+
@vjeux

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

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@
2222
* - Failure requirements. A recognizer that requires another to fail waits
2323
* (still `possible`) until that one fails, then recognizes; if that one
2424
* recognizes, it fails. An idle recognizer holds nothing up.
25+
* - Delegates from outside. Code that does not own a recognizer (an app
26+
* relating its pan to a component's) registers delegate methods against a
27+
* matcher (`addDelegate`). They add to the owner's own: any `false` from
28+
* should-begin or should-receive vetoes, any `true` relationship applies.
29+
* With none registered, nothing changes.
2530
* - Delivery to the view. The touch's consumer (a pressable control) gets
2631
* began / moved / ended unless a recognizer delays them
2732
* (`delaysTouchesBegan`, `delaysTouchesEnded`) or recognizing cancels them
@@ -45,6 +50,8 @@
4550
import {
4651
isActive,
4752
isTerminal,
53+
type GestureDelegate,
54+
type GestureMatcher,
4855
type GestureRecognizer,
4956
} from './GestureRecognizer';
5057
import type {
@@ -138,6 +145,11 @@ export class GestureArena {
138145
/** The latest time processed: input never moves the clock backwards. */
139146
private latestTime = Number.NEGATIVE_INFINITY;
140147
private depth = 0;
148+
/** Delegates for recognizers someone else owns, by what they match. */
149+
private readonly delegates = new Set<{
150+
readonly match: GestureMatcher;
151+
readonly delegate: GestureDelegate;
152+
}>();
141153

142154
constructor(options: GestureArenaOptions = {}) {
143155
this.options = options;
@@ -172,6 +184,36 @@ export class GestureArena {
172184
return this.active;
173185
}
174186

187+
// --- Delegates for recognizers someone else owns ---------------------------
188+
189+
/**
190+
* Register delegate methods for every recognizer `match` picks, on top of
191+
* its owner's own (UIKit's delegate model for code that does not own the
192+
* recognizer, such as an app relating its pan to a component's). With none
193+
* registered, nothing changes. Returns the unregister function.
194+
*/
195+
addDelegate(match: GestureMatcher, delegate: GestureDelegate): () => void {
196+
const entry = {match, delegate};
197+
this.delegates.add(entry);
198+
return () => {
199+
this.delegates.delete(entry);
200+
};
201+
}
202+
203+
/** @internal The registered delegates that apply to `recognizer` now. */
204+
delegatesFor(recognizer: GestureRecognizer): ReadonlyArray<GestureDelegate> {
205+
if (this.delegates.size === 0) {
206+
return [];
207+
}
208+
const found: GestureDelegate[] = [];
209+
for (const {match, delegate} of this.delegates) {
210+
if (match(recognizer)) {
211+
found.push(delegate);
212+
}
213+
}
214+
return found;
215+
}
216+
175217
// --- Input -----------------------------------------------------------------
176218

177219
/** New contacts, each with the recognizers on its hit path. */
@@ -205,11 +247,12 @@ export class GestureArena {
205247
};
206248
const participants: GestureRecognizer[] = [];
207249
for (const recognizer of input.recognizers) {
250+
// Before it is asked: its outside delegates live on this arena.
251+
recognizer.arena = this;
208252
if (!recognizer.wantsTouch(touch)) {
209253
continue;
210254
}
211255
participants.push(recognizer);
212-
recognizer.arena = this;
213256
if (!this.active.includes(recognizer)) {
214257
this.active.push(recognizer);
215258
}

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

Lines changed: 82 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
* @file GestureRecognizer.ts
55
* @input Touches delivered by a GestureArena
66
* @output Exports GestureRecognizer, the base every recognizer extends
7-
* (UIGestureRecognizer), and its options and listener types
7+
* (UIGestureRecognizer), its options and listener types, and the
8+
* GestureDelegate and GestureMatcher types for delegates that code other
9+
* than a recognizer's owner registers with the arena
810
* @position Core of packages/core/src/gestures: subclasses interpret touches
911
* and request state changes; the arena decides whether a change is allowed
1012
* (require-to-fail, exclusivity, should-begin) and applies it
@@ -79,6 +81,47 @@ export interface GestureRecognizerOptions<R = GestureRecognizer> {
7981
) => boolean;
8082
}
8183

84+
/**
85+
* UIKit's UIGestureRecognizerDelegate, for a recognizer someone else owns (a
86+
* component's internal pan, matched by its published name). Registered with
87+
* the arena against a matcher, it is additive: the owner's own delegate
88+
* still runs, any `false` from `shouldBegin` or `shouldReceiveTouch` vetoes,
89+
* and any `true` from a relationship applies.
90+
* @experimental
91+
*/
92+
export interface GestureDelegate {
93+
/** gestureRecognizerShouldBegin: return false to make it fail instead. */
94+
shouldBegin?: (recognizer: GestureRecognizer) => boolean;
95+
/** gestureRecognizer(_:shouldReceive:): return false to keep the touch from it. */
96+
shouldReceiveTouch?: (
97+
recognizer: GestureRecognizer,
98+
touch: GestureTouch,
99+
) => boolean;
100+
/** gestureRecognizer(_:shouldRecognizeSimultaneouslyWith:). */
101+
shouldRecognizeSimultaneously?: (
102+
recognizer: GestureRecognizer,
103+
other: GestureRecognizer,
104+
) => boolean;
105+
/** gestureRecognizer(_:shouldRequireFailureOf:): it waits for `other` to fail. */
106+
shouldRequireFailureOf?: (
107+
recognizer: GestureRecognizer,
108+
other: GestureRecognizer,
109+
) => boolean;
110+
/** gestureRecognizer(_:shouldBeRequiredToFailBy:): `other` waits for it to fail. */
111+
shouldBeRequiredToFailBy?: (
112+
recognizer: GestureRecognizer,
113+
other: GestureRecognizer,
114+
) => boolean;
115+
}
116+
117+
/**
118+
* Picks the recognizers a delegate applies to (see `gestureNamed`).
119+
* @experimental
120+
*/
121+
export type GestureMatcher = (recognizer: GestureRecognizer) => boolean;
122+
123+
const NO_DELEGATES: ReadonlyArray<GestureDelegate> = [];
124+
82125
/** @experimental */
83126
export type GestureListener<R> = (recognizer: R) => void;
84127

@@ -299,7 +342,15 @@ export abstract class GestureRecognizer {
299342
return (
300343
this.failureRequirements.has(other) ||
301344
this.options.shouldRequireFailureOf?.(this, other) === true ||
302-
other.options.shouldBeRequiredToFailBy?.(other, this) === true
345+
other.options.shouldBeRequiredToFailBy?.(other, this) === true ||
346+
this.outsideDelegates().some(
347+
delegate => delegate.shouldRequireFailureOf?.(this, other) === true,
348+
) ||
349+
other
350+
.outsideDelegates()
351+
.some(
352+
delegate => delegate.shouldBeRequiredToFailBy?.(other, this) === true,
353+
)
303354
);
304355
}
305356

@@ -308,13 +359,33 @@ export abstract class GestureRecognizer {
308359
return (
309360
this.simultaneousWith.has(other) ||
310361
this.options.shouldRecognizeSimultaneously?.(this, other) === true ||
311-
other.options.shouldRecognizeSimultaneously?.(other, this) === true
362+
other.options.shouldRecognizeSimultaneously?.(other, this) === true ||
363+
this.outsideDelegates().some(
364+
delegate =>
365+
delegate.shouldRecognizeSimultaneously?.(this, other) === true,
366+
) ||
367+
other
368+
.outsideDelegates()
369+
.some(
370+
delegate =>
371+
delegate.shouldRecognizeSimultaneously?.(other, this) === true,
372+
)
312373
);
313374
}
314375

315-
/** @internal The delegate's should-begin answer. */
376+
/** @internal The delegates' should-begin answer: every one must agree. */
316377
shouldBegin(): boolean {
317-
return this.options.shouldBegin?.(this) ?? true;
378+
return (
379+
(this.options.shouldBegin?.(this) ?? true) &&
380+
this.outsideDelegates().every(
381+
delegate => delegate.shouldBegin?.(this) ?? true,
382+
)
383+
);
384+
}
385+
386+
/** The delegates registered with the arena for this recognizer. */
387+
private outsideDelegates(): ReadonlyArray<GestureDelegate> {
388+
return this.arena?.delegatesFor(this) ?? NO_DELEGATES;
318389
}
319390

320391
/** UIKit's `canPrevent(_:)`: whether recognizing fails `other`. */
@@ -430,7 +501,12 @@ export abstract class GestureRecognizer {
430501
}
431502
}
432503
}
433-
return this.options.shouldReceiveTouch?.(this, touch) ?? true;
504+
return (
505+
(this.options.shouldReceiveTouch?.(this, touch) ?? true) &&
506+
this.outsideDelegates().every(
507+
delegate => delegate.shouldReceiveTouch?.(this, touch) ?? true,
508+
)
509+
);
434510
}
435511

436512
/**

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,33 @@ single.requireToFail(double);
129129
innerPan.requireToFail(edgePan);
130130
```
131131

132+
## Relating to a component's recognizers
133+
134+
Components publish their main recognizers under stable names,
135+
`Component.role` (`PUBLISHED_GESTURES`): `NavigationStack.backSwipe`,
136+
`NavigationStack.contentBackSwipe`, `Item.swipe`, `Lightbox.dismissPan`,
137+
`Lightbox.zoomPan`, `Lightbox.zoomPinch`, `Carousel.pager`, `Toast.swipe`
138+
and `BottomSheet.drag`. Code outside a component relates to them the way a
139+
UIKit app uses a recognizer's `name` and its own delegate:
140+
141+
- From your own recognizer, with its delegate options and a matcher:
142+
`shouldRequireFailureOf: (_, other) => gestureNamed('BottomSheet.drag',
143+
{containing: canvasRef})(other)` makes a canvas pan inside a sheet wait
144+
for the sheet's drag (and get the drags it fails on);
145+
`shouldBeRequiredToFailBy` makes the sheet's drag wait for yours;
146+
`shouldRecognizeSimultaneously` lets both run.
147+
- On a recognizer you do not own, with `addGestureDelegate(document, match,
148+
delegate)` or `useGestureDelegate(match, delegate)`: a veto through
149+
`shouldBegin` or `shouldReceiveTouch` (no back swipe while a form has
150+
unsaved changes), or a relationship between two components' recognizers.
151+
These add to the component's own delegate, which still runs: any `false`
152+
veto wins, any `true` relationship applies.
153+
154+
`gestureNamed(name, {within, containing})` picks instances by the element
155+
their recognizer is attached to (elements or refs), and is evaluated when a
156+
touch asks, so nothing needs redoing when a component remounts. Code that
157+
declares nothing changes nothing.
158+
132159
## Touches in the view: `PressTracker`
133160

134161
A pressable control's view-level touch handling, with `UIControl`

0 commit comments

Comments
 (0)