Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,14 @@ class LongPressGestureHandler(context: Context) : GestureHandler() {
val systemDefaultMaxDist = DEFAULT_MAX_DIST_DP * context.resources.displayMetrics.density
defaultMaxDist = systemDefaultMaxDist
maxDist = defaultMaxDist
numberOfPointersRequired = 1
numberOfPointersRequired = DEFAULT_NUMBER_OF_POINTERS_REQUIRED
}

override fun resetConfig() {
super.resetConfig()
minDurationMs = DEFAULT_MIN_DURATION_MS
maxDist = defaultMaxDist
numberOfPointersRequired = DEFAULT_NUMBER_OF_POINTERS_REQUIRED
shouldCancelWhenOutside = DEFAULT_SHOULD_CANCEL_WHEN_OUTSIDE
}

Expand Down Expand Up @@ -211,5 +212,6 @@ class LongPressGestureHandler(context: Context) : GestureHandler() {
private const val DEFAULT_SHOULD_CANCEL_WHEN_OUTSIDE = true
private const val DEFAULT_MIN_DURATION_MS: Long = 500
private const val DEFAULT_MAX_DIST_DP = 10f
private const val DEFAULT_NUMBER_OF_POINTERS_REQUIRED = 1
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,10 @@ - (void)resetConfig

recognizer.minimumPressDuration = 0.5;
recognizer.allowableMovement = 10;

#if !TARGET_OS_TV
recognizer.numberOfTouchesRequired = 1;
#endif
}

- (void)updateConfig:(NSDictionary *)config
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,15 @@ import type IGestureHandler from './IGestureHandler';

const DEFAULT_MIN_DURATION_MS = 500;
const DEFAULT_MAX_DIST_DP = 10;
const DEFAULT_NUMBER_OF_POINTERS = 1;
const SCALING_FACTOR = 10;

export default class LongPressGestureHandler extends GestureHandler {
private minDurationMs = DEFAULT_MIN_DURATION_MS;
private defaultMaxDistSq = DEFAULT_MAX_DIST_DP * SCALING_FACTOR;

private maxDistSq = this.defaultMaxDistSq;
private numberOfPointers = 1;
private numberOfPointers = DEFAULT_NUMBER_OF_POINTERS;
private startX = 0;
private startY = 0;

Expand Down Expand Up @@ -77,6 +78,7 @@ export default class LongPressGestureHandler extends GestureHandler {
super.resetConfig();
this.minDurationMs = DEFAULT_MIN_DURATION_MS;
this.maxDistSq = this.defaultMaxDistSq;
this.numberOfPointers = DEFAULT_NUMBER_OF_POINTERS;
}

protected override onStateChange(_newState: State, _oldState: State): void {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import { ActionType } from '../../../ActionType';
import { PointerType } from '../../../PointerType';
import { State } from '../../../State';
import type { AdaptedEvent } from '../../interfaces';
import { EventTypes } from '../../interfaces';
import type { GestureHandlerDelegate } from '../../tools/GestureHandlerDelegate';
import GestureHandlerOrchestrator from '../../tools/GestureHandlerOrchestrator';
import type IGestureHandler from '../IGestureHandler';
import LongPressGestureHandler from '../LongPressGestureHandler';

class TestLongPressGestureHandler extends LongPressGestureHandler {
public pointerDown(event: AdaptedEvent): void {
this.onPointerDown(event);
}
}

function touchEvent(pointerId: number, x: number, y: number): AdaptedEvent {
return {
x,
y,
offsetX: x,
offsetY: y,
pointerId,
eventType: EventTypes.DOWN,
pointerType: PointerType.TOUCH,
time: 0,
};
}

function createHandler() {
const delegate = {
init: jest.fn(),
detach: jest.fn(),
reset: jest.fn(),
onActivate: jest.fn(),
onFail: jest.fn(),
onCancel: jest.fn(),
onEnd: jest.fn(),
onEnabledChange: jest.fn(),
updateDOM: jest.fn(),
} as unknown as GestureHandlerDelegate<unknown, IGestureHandler>;

const handler = new TestLongPressGestureHandler(delegate);
handler.init(1, { current: {} } as never, ActionType.JS_FUNCTION_OLD_API);

// The full event pipeline is not under test, silence event emission.
handler.sendEvent = jest.fn();

return handler;
}

describe('LongPressGestureHandler config reset', () => {
afterEach(() => {
// The orchestrator is a singleton, drop handlers recorded by the test.
(
GestureHandlerOrchestrator.instance as unknown as {
gestureHandlers: IGestureHandler[];
}
).gestureHandlers = [];
});

test('a config without numberOfPointers restores the single pointer default', () => {
const handler = createHandler();

handler.setGestureConfig({
enabled: true,
minDurationMs: 0,
numberOfPointers: 2,
});
handler.setGestureConfig({ enabled: true, minDurationMs: 0 });

handler.pointerDown(touchEvent(0, 100, 100));

expect(handler.state).toBe(State.ACTIVE);
});

test('numberOfPointers still applies while it stays in the config', () => {
const handler = createHandler();

handler.setGestureConfig({
enabled: true,
minDurationMs: 0,
numberOfPointers: 2,
});

handler.pointerDown(touchEvent(0, 100, 100));

expect(handler.state).toBe(State.BEGAN);
});
});
Loading