Skip to content

Commit 5ca1d8f

Browse files
yungstersfacebook-github-bot
authored andcommitted
Pressability: Fix Missing onLongPress Gestures
Summary: The current implementation of `Pressability` has a bug related to `onLongPress`. When a user starts a press gesture, we keep track of the activation position (occurs after waiting `delayPressIn` milliseconds). If the touch moves away from that position by more than 10dp, we rule out the long press gesture. This means no matter how long you hold down the press, even if you move it back to within 10dp, we will not fire `onLongPress`. However, there is currently a bug where we never reset the cached activation position. This means that after the first press gesture, all subsequent long press gestures must start within 10dp of that first press gesture. This leads to seemingly intermittent missing long press gestures. This fixes the bug by ensuring that whenever a press gestures is terminated (either via a cancel or release), we reset the activation position. Changelog: [General][Fixed] - Fixed Pressability to properly fire `onLongPress`. Reviewed By: TheSavior Differential Revision: D20410075 fbshipit-source-id: e4727b7a9585ce3ea39481fc13e56b6b91740c8c
1 parent 147f0f2 commit 5ca1d8f

File tree

2 files changed

+91
-2
lines changed

2 files changed

+91
-2
lines changed

Libraries/Pressability/Pressability.js

+1
Original file line numberDiff line numberDiff line change
@@ -640,6 +640,7 @@ export default class Pressability {
640640
event: PressEvent,
641641
): void {
642642
if (isTerminalSignal(signal)) {
643+
this._touchActivatePosition = null;
643644
this._cancelLongPressDelayTimeout();
644645
}
645646

Libraries/Pressability/__tests__/Pressability-test.js

+90-2
Original file line numberDiff line numberDiff line change
@@ -397,9 +397,97 @@ describe('Pressability', () => {
397397
jest.advanceTimersByTime(1);
398398
expect(config.onLongPress).toBeCalled();
399399
});
400-
});
401400

402-
// TODO: onLongPressShouldCancelPress tests
401+
it('is called if touch moves within 10dp', () => {
402+
mockUIManagerMeasure();
403+
const {config, handlers} = createMockPressability();
404+
405+
handlers.onStartShouldSetResponder();
406+
handlers.onResponderGrant(createMockPressEvent('onResponderGrant'));
407+
handlers.onResponderMove(
408+
createMockPressEvent({
409+
registrationName: 'onResponderMove',
410+
pageX: 0,
411+
pageY: 0,
412+
}),
413+
);
414+
415+
jest.advanceTimersByTime(130);
416+
handlers.onResponderMove(
417+
// NOTE: Delta from (0, 0) is ~9.9 < 10.
418+
createMockPressEvent({
419+
registrationName: 'onResponderMove',
420+
pageX: 7,
421+
pageY: 7,
422+
}),
423+
);
424+
425+
jest.advanceTimersByTime(370);
426+
expect(config.onLongPress).toBeCalled();
427+
});
428+
429+
it('is not called if touch moves beyond 10dp', () => {
430+
mockUIManagerMeasure();
431+
const {config, handlers} = createMockPressability();
432+
433+
handlers.onStartShouldSetResponder();
434+
handlers.onResponderGrant(createMockPressEvent('onResponderGrant'));
435+
handlers.onResponderMove(
436+
createMockPressEvent({
437+
registrationName: 'onResponderMove',
438+
pageX: 0,
439+
pageY: 0,
440+
}),
441+
);
442+
443+
jest.advanceTimersByTime(130);
444+
handlers.onResponderMove(
445+
createMockPressEvent({
446+
registrationName: 'onResponderMove',
447+
// NOTE: Delta from (0, 0) is ~10.6 > 10.
448+
pageX: 7,
449+
pageY: 8,
450+
}),
451+
);
452+
453+
jest.advanceTimersByTime(370);
454+
expect(config.onLongPress).not.toBeCalled();
455+
});
456+
457+
it('is called independent of preceding long touch gesture', () => {
458+
mockUIManagerMeasure();
459+
const {config, handlers} = createMockPressability();
460+
461+
handlers.onStartShouldSetResponder();
462+
handlers.onResponderGrant(createMockPressEvent('onResponderGrant'));
463+
handlers.onResponderMove(
464+
createMockPressEvent({
465+
registrationName: 'onResponderMove',
466+
pageX: 0,
467+
pageY: 0,
468+
}),
469+
);
470+
471+
jest.advanceTimersByTime(500);
472+
expect(config.onLongPress).toHaveBeenCalledTimes(1);
473+
handlers.onResponderRelease(createMockPressEvent('onResponderRelease'));
474+
475+
// Subsequent long touch gesture should not carry over previous state.
476+
handlers.onStartShouldSetResponder();
477+
handlers.onResponderGrant(createMockPressEvent('onResponderGrant'));
478+
handlers.onResponderMove(
479+
// NOTE: Delta from (0, 0) is ~10.6 > 10, but should not matter.
480+
createMockPressEvent({
481+
registrationName: 'onResponderMove',
482+
pageX: 7,
483+
pageY: 8,
484+
}),
485+
);
486+
487+
jest.advanceTimersByTime(500);
488+
expect(config.onLongPress).toHaveBeenCalledTimes(2);
489+
});
490+
});
403491

404492
describe('onPress', () => {
405493
it('is called even when `measure` does not finish', () => {

0 commit comments

Comments
 (0)