Skip to content

Commit 661f791

Browse files
authored
chore(gestures): a pan's begin threshold can be read from each press (#9953)
chore(gestures): a pan's begin threshold can be read from each press PanGestureRecognizer's `hysteresis` may be a function. When a press's first touches land, the pan reads that press's threshold from its first touch. So a recognizer can give a mouse or a pen its own threshold and keep UIKit's 10 px for a finger. A number keeps today's behavior. Lightbox's zoom pan is the first user: a mouse's or a pen's drag on a zoomed photo pans from its first move, while a finger keeps the 10 px that tells a tap from a pan. Without this, it sets the threshold from its own pointerdown listener.
1 parent a1a6151 commit 661f791

5 files changed

Lines changed: 55 additions & 4 deletions

File tree

‎.changeset/pan-press-threshold.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: a pan gesture's begin threshold can be read from each press's first touch, so a mouse or a pen can begin a pan sooner than a finger. It is not exported from the package entry point, and nothing changes for existing callers.
6+
7+
@vjeux

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

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,12 @@ export interface PanGestureRecognizerOptions extends GestureRecognizerOptions<Pa
5555
minimumNumberOfTouches?: number;
5656
/** @default Infinity */
5757
maximumNumberOfTouches?: number;
58-
hysteresis?: number;
58+
/**
59+
* The travel that begins the pan, px. A function gives each press its own
60+
* threshold: it reads the press's first touch when that touch lands.
61+
* @default UIKit's 10
62+
*/
63+
hysteresis?: number | ((touch: GestureTouch) => number);
5964
/** Movement that may begin the pan; anything else fails it. @default 'any' */
6065
direction?: PanDirection;
6166
/**
@@ -117,6 +122,11 @@ export class PanGestureRecognizer extends GestureRecognizer {
117122
travelAtBegan: Point = ZERO;
118123

119124
private readonly tracker: VelocityTracker;
125+
/**
126+
* Read once per press. A finger that lands later keeps the press's
127+
* threshold.
128+
*/
129+
private readonly hysteresisFor: ((touch: GestureTouch) => number) | null;
120130
/** The lift's event time once the pan ended: its release (AST-100 FR2). */
121131
private releaseTime: number | undefined = undefined;
122132
/** The centroid where the gesture started, in client px. */
@@ -132,7 +142,10 @@ export class PanGestureRecognizer extends GestureRecognizer {
132142
this.minimumNumberOfTouches = options.minimumNumberOfTouches ?? 1;
133143
this.maximumNumberOfTouches =
134144
options.maximumNumberOfTouches ?? Number.POSITIVE_INFINITY;
135-
this.hysteresis = options.hysteresis ?? UIKIT_PAN.hysteresis.value;
145+
const {hysteresis} = options;
146+
this.hysteresisFor = typeof hysteresis === 'function' ? hysteresis : null;
147+
this.hysteresis =
148+
typeof hysteresis === 'number' ? hysteresis : UIKIT_PAN.hysteresis.value;
136149
this.direction = options.direction ?? 'any';
137150
this.axisRatio = options.axisRatio ?? 1;
138151
this.lockAxis = options.lockAxis ?? false;
@@ -237,6 +250,10 @@ export class PanGestureRecognizer extends GestureRecognizer {
237250
event: GestureEventContext,
238251
): void {
239252
if (this.numberOfTouches === touches.length) {
253+
const first = touches[0];
254+
if (this.hysteresisFor != null && first != null) {
255+
this.hysteresis = this.hysteresisFor(first);
256+
}
240257
this.origin = centroid(this.touches.values());
241258
this.adjust = ZERO;
242259
this.raw = ZERO;

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,8 @@ The pan reads its numbers the way `UIPanGestureRecognizer` does, as measured
118118
in the UIKit lab:
119119

120120
- It begins once the centroid has moved `hysteresis` (10 px) along either
121-
axis, not as a distance.
121+
axis, not as a distance. A function gives each press its own threshold,
122+
read from the press's first touch when it lands.
122123
- `translation()` leaves out the travel the threshold took: a slow drag reads
123124
zero when the pan begins, and a step that overshoots keeps only the
124125
overshoot. A dragged surface does not jump. `totalTranslation()` is the

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,17 @@ interface EdgeLayout {
7676
*/
7777
export interface ScreenEdgePanGestureRecognizerOptions extends Omit<
7878
PanGestureRecognizerOptions,
79-
'direction' | 'allowedPointerTypes' | 'allowedScrollTypes'
79+
'direction' | 'allowedPointerTypes' | 'allowedScrollTypes' | 'hysteresis'
8080
> {
8181
/** The edges a touch may start from. @default ['leading'] */
8282
edges?: ReadonlyArray<ScreenEdge>;
8383
/** Width of the band at each edge, px. */
8484
edgeRegion?: number;
85+
/**
86+
* The travel that begins the pan in its edge zone, px. It is one number for
87+
* every press, since the zone sets the threshold itself. @default UIKit's 15
88+
*/
89+
hysteresis?: number;
8590
}
8691

8792
export class ScreenEdgePanGestureRecognizer extends PanGestureRecognizer {

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,27 @@ describe('PanGestureRecognizer', () => {
8484
expect(pan.totalTranslation()).toEqual({x: 50, y: 20});
8585
});
8686

87+
it('reads each press’s begin threshold from its first touch', () => {
88+
const h = createHarness();
89+
const pan = new PanGestureRecognizer({
90+
name: 'pan',
91+
allowedPointerTypes: ['touch', 'mouse'],
92+
hysteresis: touch => (touch.pointerType === 'mouse' ? 0 : 10),
93+
});
94+
h.finger(1)
95+
.down(100, 100, 0, {recognizers: [pan], pointerType: 'mouse'})
96+
.move(101, 100, 16);
97+
expect(pan.state).toBe('began');
98+
h.finger(1).up(32);
99+
// The next press is a finger's: 5 px is under its threshold.
100+
h.finger(2)
101+
.down(100, 100, 100, {recognizers: [pan]})
102+
.move(105, 100, 116);
103+
expect(pan.state).toBe('possible');
104+
h.finger(2).move(111, 100, 132);
105+
expect(pan.state).toBe('began');
106+
});
107+
87108
it('applies the hysteresis per axis, as UIKit does', () => {
88109
const h = createHarness();
89110
const pan = new PanGestureRecognizer({name: 'pan'});

0 commit comments

Comments
 (0)