@react-native-oh-tpl/react-native-gesture-handler 2.14.8-rc.1 → 2.14.14
Sign up to get free protection for your applications and to get access to all the features.
- package/DrawerLayout/index.ts +2 -2
- package/Swipeable/index.ts +2 -2
- package/harmony/gesture_handler/BuildProfile.ets +17 -0
- package/harmony/gesture_handler/build-profile.json5 +19 -0
- package/harmony/gesture_handler/hvigorfile.ts +2 -0
- package/harmony/gesture_handler/index.ets +2 -0
- package/harmony/gesture_handler/oh-package-lock.json5 +18 -0
- package/harmony/gesture_handler/oh-package.json5 +12 -0
- package/harmony/gesture_handler/src/main/cpp/CMakeLists.txt +8 -0
- package/harmony/gesture_handler/src/main/cpp/GestureHandlerPackage.h +12 -0
- package/harmony/gesture_handler/src/main/cpp/RnohReactNativeHarmonyGestureHandlerPackage.cpp +123 -0
- package/harmony/gesture_handler/src/main/cpp/RnohReactNativeHarmonyGestureHandlerPackage.h +15 -0
- package/harmony/gesture_handler/src/main/cpp/componentInstances/RNGestureHandlerButtonComponentInstance.h +27 -0
- package/harmony/gesture_handler/src/main/cpp/componentInstances/RNGestureHandlerRootViewComponentInstance.h +245 -0
- package/harmony/gesture_handler/src/main/ets/RNOHPackage.ets +17 -0
- package/harmony/gesture_handler/src/main/ets/core/CircularBuffer.ts +42 -0
- package/harmony/gesture_handler/src/main/ets/core/GestureHandler.ts +739 -0
- package/harmony/gesture_handler/src/main/ets/core/GestureHandlerOrchestrator.ts +344 -0
- package/harmony/gesture_handler/src/main/ets/core/GestureHandlerRegistry.ts +63 -0
- package/harmony/gesture_handler/src/main/ets/core/IncomingEvent.ts +78 -0
- package/harmony/gesture_handler/src/main/ets/core/InteractionManager.ts +144 -0
- package/harmony/gesture_handler/src/main/ets/core/LeastSquareSolver.ts +182 -0
- package/harmony/gesture_handler/src/main/ets/core/Multiset.ts +26 -0
- package/harmony/gesture_handler/src/main/ets/core/OutgoingEvent.ts +34 -0
- package/harmony/gesture_handler/src/main/ets/core/OutgoingEventDispatcher.ts +12 -0
- package/harmony/gesture_handler/src/main/ets/core/PointerTracker.ts +239 -0
- package/harmony/gesture_handler/src/main/ets/core/RNGHError.ts +5 -0
- package/harmony/gesture_handler/src/main/ets/core/RNGHLogger.ts +16 -0
- package/harmony/gesture_handler/src/main/ets/core/State.ts +47 -0
- package/harmony/gesture_handler/src/main/ets/core/Vector2D.ts +80 -0
- package/harmony/gesture_handler/src/main/ets/core/VelocityTracker.ts +106 -0
- package/harmony/gesture_handler/src/main/ets/core/View.ts +21 -0
- package/harmony/gesture_handler/src/main/ets/core/ViewRegistry.ts +7 -0
- package/harmony/gesture_handler/src/main/ets/core/index.ts +15 -0
- package/harmony/gesture_handler/src/main/ets/detectors/ScaleGestureDetector.ts +169 -0
- package/harmony/gesture_handler/src/main/ets/gesture-handlers/FlingGestureHandler.ts +219 -0
- package/harmony/gesture_handler/src/main/ets/gesture-handlers/GestureHandlerFactory.ts +67 -0
- package/harmony/gesture_handler/src/main/ets/gesture-handlers/LongPressGestureHandler.ts +139 -0
- package/harmony/gesture_handler/src/main/ets/gesture-handlers/ManualGestureHandler.ts +50 -0
- package/harmony/gesture_handler/src/main/ets/gesture-handlers/NativeViewGestureHandler.ts +124 -0
- package/harmony/gesture_handler/src/main/ets/gesture-handlers/PanGestureHandler.ts +361 -0
- package/harmony/gesture_handler/src/main/ets/gesture-handlers/PinchGestureHandler.ts +174 -0
- package/harmony/gesture_handler/src/main/ets/gesture-handlers/RotationGestureHandler.ts +172 -0
- package/harmony/gesture_handler/src/main/ets/gesture-handlers/TapGestureHandler.ts +216 -0
- package/harmony/gesture_handler/src/main/ets/gesture-handlers/detectors/RotationGestureDetector.ts +167 -0
- package/harmony/gesture_handler/src/main/ets/gesture-handlers/index.ts +1 -0
- package/harmony/gesture_handler/src/main/ets/rnoh/GestureHandlerPackage.ts +25 -0
- package/harmony/gesture_handler/src/main/ets/rnoh/Logger.ts +107 -0
- package/harmony/gesture_handler/src/main/ets/rnoh/OutgoingEventDispatchers.ts +94 -0
- package/harmony/gesture_handler/src/main/ets/rnoh/RNGHRootViewController.ts +182 -0
- package/harmony/gesture_handler/src/main/ets/rnoh/RNGHView.ts +62 -0
- package/harmony/gesture_handler/src/main/ets/rnoh/RNGHViewController.ts +262 -0
- package/harmony/gesture_handler/src/main/ets/rnoh/RNGHViewRegistry.ts +19 -0
- package/harmony/gesture_handler/src/main/ets/rnoh/RNGestureHandlerModule.ts +267 -0
- package/harmony/gesture_handler/src/main/ets/rnoh/RNOHGestureResponder.ts +15 -0
- package/harmony/gesture_handler/src/main/ets/rnoh/RNOHScrollLocker.ts +25 -0
- package/harmony/gesture_handler/src/main/ets/rnoh/types.ts +25 -0
- package/harmony/gesture_handler/src/main/module.json5 +9 -0
- package/harmony/gesture_handler/src/main/resources/base/element/color.json +8 -0
- package/harmony/gesture_handler/src/main/resources/base/element/string.json +16 -0
- package/harmony/gesture_handler/src/main/resources/base/media/icon.png +0 -0
- package/harmony/gesture_handler/src/main/resources/base/profile/main_pages.json +5 -0
- package/harmony/gesture_handler/src/main/resources/en_US/element/string.json +16 -0
- package/harmony/gesture_handler/src/main/resources/zh_CN/element/string.json +16 -0
- package/harmony/gesture_handler/ts.ts +2 -0
- package/harmony/gesture_handler.har +0 -0
- package/lib/commonjs/RNGestureHandlerModule.js +3 -2
- package/lib/commonjs/RNGestureHandlerModule.js.map +1 -1
- package/lib/commonjs/components/GestureHandlerRootView.js +3 -3
- package/lib/commonjs/components/GestureHandlerRootView.js.map +1 -1
- package/lib/commonjs/handlers/createHandler.js +18 -15
- package/lib/commonjs/handlers/createHandler.js.map +1 -1
- package/lib/commonjs/index.js +36 -8
- package/lib/commonjs/index.js.map +1 -1
- package/lib/commonjs/specs/NativeRNGestureHandlerModule.js +2 -1
- package/lib/commonjs/specs/NativeRNGestureHandlerModule.js.map +1 -1
- package/lib/commonjs/specs/RNGestureHandlerButtonNativeComponent.js +3 -2
- package/lib/commonjs/specs/RNGestureHandlerButtonNativeComponent.js.map +1 -1
- package/lib/commonjs/specs/RNGestureHandlerRootViewNativeComponent.js +3 -2
- package/lib/commonjs/specs/RNGestureHandlerRootViewNativeComponent.js.map +1 -1
- package/lib/module/RNGestureHandlerModule.js.map +1 -1
- package/lib/module/components/GestureHandlerRootView.js.map +1 -1
- package/lib/module/handlers/createHandler.js +15 -12
- package/lib/module/handlers/createHandler.js.map +1 -1
- package/lib/module/index.js +5 -7
- package/lib/module/index.js.map +1 -1
- package/lib/module/specs/NativeRNGestureHandlerModule.js.map +1 -1
- package/lib/module/specs/RNGestureHandlerButtonNativeComponent.js.map +1 -1
- package/lib/module/specs/RNGestureHandlerRootViewNativeComponent.js.map +1 -1
- package/lib/typescript/RNGestureHandlerModule.d.ts +2 -2
- package/lib/typescript/components/GestureHandlerRootView.d.ts +6 -6
- package/lib/typescript/handlers/createHandler.d.ts +11 -11
- package/lib/typescript/index.d.ts +47 -42
- package/lib/typescript/index.d.ts.map +1 -1
- package/lib/typescript/specs/NativeRNGestureHandlerModule.d.ts +14 -14
- package/lib/typescript/specs/RNGestureHandlerButtonNativeComponent.d.ts +14 -14
- package/lib/typescript/specs/RNGestureHandlerRootViewNativeComponent.d.ts +6 -6
- package/package.json +78 -70
- package/src/RNGestureHandlerModule.ts +4 -4
- package/src/components/GestureHandlerRootView.tsx +23 -23
- package/src/handlers/createHandler.tsx +534 -534
- package/src/index.ts +172 -172
- package/src/specs/NativeRNGestureHandlerModule.ts +26 -26
- package/src/specs/RNGestureHandlerButtonNativeComponent.ts +18 -18
- package/src/specs/RNGestureHandlerRootViewNativeComponent.ts +6 -6
@@ -0,0 +1,174 @@
|
|
1
|
+
import {
|
2
|
+
GestureHandler,
|
3
|
+
DEFAULT_TOUCH_SLOP,
|
4
|
+
IncomingEvent,
|
5
|
+
GestureConfig,
|
6
|
+
State,
|
7
|
+
GestureHandlerDependencies
|
8
|
+
} from "../core"
|
9
|
+
import ScaleGestureDetector, { ScaleGestureListener } from "../detectors/ScaleGestureDetector";
|
10
|
+
|
11
|
+
|
12
|
+
export class PinchGestureHandler extends GestureHandler {
|
13
|
+
private scale = 1;
|
14
|
+
private velocity = 0;
|
15
|
+
|
16
|
+
private startingSpan = 0;
|
17
|
+
private spanSlop = DEFAULT_TOUCH_SLOP;
|
18
|
+
|
19
|
+
private scaleDetectorListener: ScaleGestureListener = {
|
20
|
+
onScaleBegin: (detector: ScaleGestureDetector): boolean => {
|
21
|
+
this.startingSpan = detector.getCurrentSpan();
|
22
|
+
return true;
|
23
|
+
},
|
24
|
+
onScale: (detector: ScaleGestureDetector): boolean => {
|
25
|
+
const prevScaleFactor: number = this.scale;
|
26
|
+
this.scale *= detector.getScaleFactor(
|
27
|
+
this.tracker.getTrackedPointersCount()
|
28
|
+
);
|
29
|
+
|
30
|
+
const delta = detector.getTimeDelta();
|
31
|
+
if (delta > 0) {
|
32
|
+
this.velocity = (this.scale - prevScaleFactor) / delta;
|
33
|
+
}
|
34
|
+
|
35
|
+
if (
|
36
|
+
Math.abs(this.startingSpan - detector.getCurrentSpan()) >=
|
37
|
+
this.spanSlop &&
|
38
|
+
this.currentState === State.BEGAN
|
39
|
+
) {
|
40
|
+
this.activate();
|
41
|
+
}
|
42
|
+
return true;
|
43
|
+
},
|
44
|
+
onScaleEnd: (
|
45
|
+
_detector: ScaleGestureDetector
|
46
|
+
// eslint-disable-next-line @typescript-eslint/no-empty-function
|
47
|
+
): void => {
|
48
|
+
},
|
49
|
+
};
|
50
|
+
|
51
|
+
private scaleGestureDetector: ScaleGestureDetector = new ScaleGestureDetector(
|
52
|
+
this.scaleDetectorListener
|
53
|
+
);
|
54
|
+
|
55
|
+
|
56
|
+
public constructor(deps: GestureHandlerDependencies) {
|
57
|
+
super({ ...deps, logger: deps.logger.cloneAndJoinPrefix("PinchGestureHandler") })
|
58
|
+
this.setShouldCancelWhenOutside(false);
|
59
|
+
}
|
60
|
+
|
61
|
+
public getName(): string {
|
62
|
+
return "PinchGestureHandler"
|
63
|
+
}
|
64
|
+
|
65
|
+
public override isGestureContinuous(): boolean {
|
66
|
+
return true
|
67
|
+
}
|
68
|
+
|
69
|
+
public getDefaultConfig() {
|
70
|
+
return {}
|
71
|
+
}
|
72
|
+
|
73
|
+
public updateGestureConfig({ enabled = true, ...props }: GestureConfig): void {
|
74
|
+
super.updateGestureConfig({ enabled: enabled, ...props });
|
75
|
+
}
|
76
|
+
|
77
|
+
protected transformNativeEvent() {
|
78
|
+
return {
|
79
|
+
focalX: this.scaleGestureDetector.getFocusX(),
|
80
|
+
focalY: this.scaleGestureDetector.getFocusY(),
|
81
|
+
velocity: this.velocity,
|
82
|
+
scale: this.scale,
|
83
|
+
};
|
84
|
+
}
|
85
|
+
|
86
|
+
public onPointerDown(event: IncomingEvent): void {
|
87
|
+
this.tracker.addToTracker(event);
|
88
|
+
super.onPointerDown(event);
|
89
|
+
}
|
90
|
+
|
91
|
+
public onAdditionalPointerAdd(event: IncomingEvent): void {
|
92
|
+
this.tracker.addToTracker(event);
|
93
|
+
super.onAdditionalPointerAdd(event);
|
94
|
+
this.tryBegin();
|
95
|
+
this.scaleGestureDetector.onTouchEvent(event, this.tracker);
|
96
|
+
}
|
97
|
+
|
98
|
+
public onPointerUp(event: IncomingEvent): void {
|
99
|
+
super.onPointerUp(event);
|
100
|
+
this.tracker.removeFromTracker(event.pointerId);
|
101
|
+
if (this.currentState !== State.ACTIVE) {
|
102
|
+
return;
|
103
|
+
}
|
104
|
+
this.scaleGestureDetector.onTouchEvent(event, this.tracker);
|
105
|
+
|
106
|
+
if (this.currentState === State.ACTIVE) {
|
107
|
+
this.end();
|
108
|
+
} else {
|
109
|
+
this.fail();
|
110
|
+
}
|
111
|
+
}
|
112
|
+
|
113
|
+
public onAdditionalPointerRemove(event: IncomingEvent): void {
|
114
|
+
super.onAdditionalPointerRemove(event);
|
115
|
+
this.scaleGestureDetector.onTouchEvent(event, this.tracker);
|
116
|
+
this.tracker.removeFromTracker(event.pointerId);
|
117
|
+
|
118
|
+
if (
|
119
|
+
this.currentState === State.ACTIVE &&
|
120
|
+
this.tracker.getTrackedPointersCount() < 2
|
121
|
+
) {
|
122
|
+
this.end();
|
123
|
+
}
|
124
|
+
}
|
125
|
+
|
126
|
+
public onPointerMove(event: IncomingEvent): void {
|
127
|
+
if (this.tracker.getTrackedPointersCount() < 2) {
|
128
|
+
return;
|
129
|
+
}
|
130
|
+
this.tracker.track(event);
|
131
|
+
|
132
|
+
this.scaleGestureDetector.onTouchEvent(event, this.tracker);
|
133
|
+
super.onPointerMove(event);
|
134
|
+
}
|
135
|
+
|
136
|
+
public onPointerOutOfBounds(event: IncomingEvent): void {
|
137
|
+
if (this.tracker.getTrackedPointersCount() < 2) {
|
138
|
+
return;
|
139
|
+
}
|
140
|
+
this.tracker.track(event);
|
141
|
+
|
142
|
+
this.scaleGestureDetector.onTouchEvent(event, this.tracker);
|
143
|
+
super.onPointerOutOfBounds(event);
|
144
|
+
}
|
145
|
+
|
146
|
+
private tryBegin(): void {
|
147
|
+
if (this.currentState !== State.UNDETERMINED) {
|
148
|
+
return;
|
149
|
+
}
|
150
|
+
|
151
|
+
this.resetProgress();
|
152
|
+
this.begin();
|
153
|
+
}
|
154
|
+
|
155
|
+
public activate(force?: boolean): void {
|
156
|
+
if (this.currentState !== State.ACTIVE) {
|
157
|
+
this.resetProgress();
|
158
|
+
}
|
159
|
+
|
160
|
+
super.activate();
|
161
|
+
}
|
162
|
+
|
163
|
+
protected onReset(): void {
|
164
|
+
this.resetProgress();
|
165
|
+
}
|
166
|
+
|
167
|
+
protected resetProgress(): void {
|
168
|
+
if (this.currentState === State.ACTIVE) {
|
169
|
+
return;
|
170
|
+
}
|
171
|
+
this.velocity = 0;
|
172
|
+
this.scale = 1;
|
173
|
+
}
|
174
|
+
}
|
@@ -0,0 +1,172 @@
|
|
1
|
+
import { GestureHandler, State, IncomingEvent, GestureConfig, GestureHandlerDependencies } from '../core';
|
2
|
+
import RotationGestureDetector, { RotationGestureListener, } from './detectors/RotationGestureDetector';
|
3
|
+
|
4
|
+
const ROTATION_RECOGNITION_THRESHOLD = Math.PI / 36;
|
5
|
+
|
6
|
+
export class RotationGestureHandler extends GestureHandler {
|
7
|
+
private rotation = 0;
|
8
|
+
private velocity = 0;
|
9
|
+
|
10
|
+
private cachedAnchorX = 0;
|
11
|
+
private cachedAnchorY = 0;
|
12
|
+
private unlockScrolls: (() => void) | undefined
|
13
|
+
|
14
|
+
constructor(deps: GestureHandlerDependencies) {
|
15
|
+
super({ ...deps, logger: deps.logger.cloneAndJoinPrefix("RotationGestureHandler") })
|
16
|
+
}
|
17
|
+
|
18
|
+
public override getName(): string {
|
19
|
+
return "RotationGestureHandler"
|
20
|
+
}
|
21
|
+
|
22
|
+
public override isGestureContinuous(): boolean {
|
23
|
+
return true
|
24
|
+
}
|
25
|
+
|
26
|
+
private rotationGestureListener: RotationGestureListener = {
|
27
|
+
onRotationBegin: (_detector: RotationGestureDetector): boolean => true,
|
28
|
+
onRotation: (detector: RotationGestureDetector): boolean => {
|
29
|
+
const previousRotation: number = this.rotation;
|
30
|
+
this.rotation += detector.getRotation();
|
31
|
+
|
32
|
+
const delta = detector.getTimeDelta();
|
33
|
+
|
34
|
+
if (delta > 0) {
|
35
|
+
this.velocity = (this.rotation - previousRotation) / delta;
|
36
|
+
}
|
37
|
+
|
38
|
+
if (
|
39
|
+
Math.abs(this.rotation) >= ROTATION_RECOGNITION_THRESHOLD &&
|
40
|
+
this.currentState === State.BEGAN
|
41
|
+
) {
|
42
|
+
this.activate();
|
43
|
+
}
|
44
|
+
|
45
|
+
return true;
|
46
|
+
},
|
47
|
+
onRotationEnd: (_detector: RotationGestureDetector): void => {
|
48
|
+
this.end();
|
49
|
+
},
|
50
|
+
};
|
51
|
+
|
52
|
+
private rotationGestureDetector: RotationGestureDetector =
|
53
|
+
new RotationGestureDetector(this.rotationGestureListener);
|
54
|
+
|
55
|
+
getDefaultConfig(): GestureConfig {
|
56
|
+
return { shouldCancelWhenOutside: false }
|
57
|
+
}
|
58
|
+
|
59
|
+
protected transformNativeEvent() {
|
60
|
+
return {
|
61
|
+
rotation: this.rotation ? this.rotation : 0,
|
62
|
+
anchorX: this.getAnchorX(),
|
63
|
+
anchorY: this.getAnchorY(),
|
64
|
+
velocity: this.velocity ? this.velocity : 0,
|
65
|
+
};
|
66
|
+
}
|
67
|
+
|
68
|
+
public getAnchorX(): number {
|
69
|
+
const anchorX = this.rotationGestureDetector.getAnchorX();
|
70
|
+
|
71
|
+
return anchorX ? anchorX : this.cachedAnchorX;
|
72
|
+
}
|
73
|
+
|
74
|
+
public getAnchorY(): number {
|
75
|
+
const anchorY = this.rotationGestureDetector.getAnchorY();
|
76
|
+
|
77
|
+
return anchorY ? anchorY : this.cachedAnchorY;
|
78
|
+
}
|
79
|
+
|
80
|
+
public onPointerDown(event: IncomingEvent): void {
|
81
|
+
this.tracker.addToTracker(event);
|
82
|
+
super.onPointerDown(event);
|
83
|
+
}
|
84
|
+
|
85
|
+
public onAdditionalPointerAdd(event: IncomingEvent): void {
|
86
|
+
this.tracker.addToTracker(event);
|
87
|
+
super.onAdditionalPointerAdd(event);
|
88
|
+
this.tryBegin();
|
89
|
+
this.rotationGestureDetector.onTouchEvent(event, this.tracker);
|
90
|
+
}
|
91
|
+
|
92
|
+
public onPointerMove(event: IncomingEvent): void {
|
93
|
+
if (this.tracker.getTrackedPointersCount() < 2) {
|
94
|
+
return;
|
95
|
+
}
|
96
|
+
if (this.getAnchorX()) {
|
97
|
+
this.cachedAnchorX = this.getAnchorX();
|
98
|
+
}
|
99
|
+
if (this.getAnchorY()) {
|
100
|
+
this.cachedAnchorY = this.getAnchorY();
|
101
|
+
}
|
102
|
+
this.tracker.track(event);
|
103
|
+
this.rotationGestureDetector.onTouchEvent(event, this.tracker);
|
104
|
+
super.onPointerMove(event);
|
105
|
+
}
|
106
|
+
|
107
|
+
public onPointerOutOfBounds(event: IncomingEvent): void {
|
108
|
+
if (this.tracker.getTrackedPointersCount() < 2) {
|
109
|
+
return;
|
110
|
+
}
|
111
|
+
if (this.getAnchorX()) {
|
112
|
+
this.cachedAnchorX = this.getAnchorX();
|
113
|
+
}
|
114
|
+
if (this.getAnchorY()) {
|
115
|
+
this.cachedAnchorY = this.getAnchorY();
|
116
|
+
}
|
117
|
+
this.tracker.track(event);
|
118
|
+
this.rotationGestureDetector.onTouchEvent(event, this.tracker);
|
119
|
+
super.onPointerOutOfBounds(event);
|
120
|
+
}
|
121
|
+
|
122
|
+
public onPointerUp(event: IncomingEvent): void {
|
123
|
+
super.onPointerUp(event);
|
124
|
+
this.tracker.removeFromTracker(event.pointerId);
|
125
|
+
this.rotationGestureDetector.onTouchEvent(event, this.tracker);
|
126
|
+
if (this.currentState !== State.ACTIVE) {
|
127
|
+
return;
|
128
|
+
}
|
129
|
+
if (this.currentState === State.ACTIVE) {
|
130
|
+
this.end();
|
131
|
+
} else {
|
132
|
+
this.fail();
|
133
|
+
}
|
134
|
+
}
|
135
|
+
|
136
|
+
public onAdditionalPointerRemove(event: IncomingEvent): void {
|
137
|
+
super.onAdditionalPointerRemove(event);
|
138
|
+
this.rotationGestureDetector.onTouchEvent(event, this.tracker);
|
139
|
+
this.tracker.removeFromTracker(event.pointerId);
|
140
|
+
}
|
141
|
+
|
142
|
+
protected tryBegin(): void {
|
143
|
+
if (this.currentState !== State.UNDETERMINED) {
|
144
|
+
return;
|
145
|
+
}
|
146
|
+
|
147
|
+
this.begin();
|
148
|
+
}
|
149
|
+
|
150
|
+
public activate(): void {
|
151
|
+
super.activate();
|
152
|
+
}
|
153
|
+
|
154
|
+
protected onReset(): void {
|
155
|
+
if (this.currentState === State.ACTIVE) {
|
156
|
+
return;
|
157
|
+
}
|
158
|
+
|
159
|
+
this.rotation = 0;
|
160
|
+
this.velocity = 0;
|
161
|
+
this.rotationGestureDetector.reset();
|
162
|
+
}
|
163
|
+
|
164
|
+
protected onStateChange(newState: State, oldState: State) {
|
165
|
+
super.onStateChange(newState, oldState)
|
166
|
+
if (newState === State.BEGAN) {
|
167
|
+
this.unlockScrolls = this.scrollLocker.lockScrollContainingViewTag(this.view?.getTag())
|
168
|
+
} else if (newState !== State.ACTIVE) {
|
169
|
+
this.unlockScrolls?.()
|
170
|
+
}
|
171
|
+
}
|
172
|
+
}
|
@@ -0,0 +1,216 @@
|
|
1
|
+
import { GestureHandler, GestureHandlerDependencies, IncomingEvent, EventType, State, getStateName } from "../core"
|
2
|
+
|
3
|
+
const DEFAULT_MAX_DURATION_MS = 500;
|
4
|
+
const DEFAULT_NUMBER_OF_TAPS = 1;
|
5
|
+
const DEFAULT_MAX_DELAY_MS = 500;
|
6
|
+
|
7
|
+
export class TapGestureHandler extends GestureHandler {
|
8
|
+
private startX = 0;
|
9
|
+
private startY = 0;
|
10
|
+
private offsetX = 0;
|
11
|
+
private offsetY = 0;
|
12
|
+
private lastX = 0;
|
13
|
+
private lastY = 0;
|
14
|
+
private maxNumberOfPointersSoFar = 0;
|
15
|
+
private numberOfTapsSoFar: number = 0;
|
16
|
+
private waitTimeout: number | undefined;
|
17
|
+
private delayTimeout: number | undefined;
|
18
|
+
|
19
|
+
constructor(deps: GestureHandlerDependencies) {
|
20
|
+
super({ ...deps, logger: deps.logger.cloneAndJoinPrefix(`TapGestureHandler${deps.handlerTag}`) })
|
21
|
+
}
|
22
|
+
|
23
|
+
public override getName(): string {
|
24
|
+
return "TapGestureHandler"
|
25
|
+
}
|
26
|
+
|
27
|
+
public override isGestureContinuous(): boolean {
|
28
|
+
return false
|
29
|
+
}
|
30
|
+
|
31
|
+
onPointerDown(event) {
|
32
|
+
this.tracker.addToTracker(event);
|
33
|
+
super.onPointerDown(event);
|
34
|
+
this.trySettingPosition(event);
|
35
|
+
this.lastX = event.x;
|
36
|
+
this.lastY = event.y;
|
37
|
+
this.updateState(event);
|
38
|
+
}
|
39
|
+
|
40
|
+
onAdditionalPointerAdd(event: IncomingEvent): void {
|
41
|
+
super.onAdditionalPointerAdd(event);
|
42
|
+
this.tracker.addToTracker(event);
|
43
|
+
this.trySettingPosition(event);
|
44
|
+
|
45
|
+
this.offsetX += this.lastX - this.startX;
|
46
|
+
this.offsetY += this.lastY - this.startY;
|
47
|
+
|
48
|
+
this.lastX = this.tracker.getLastAvgX();
|
49
|
+
this.lastY = this.tracker.getLastAvgY();
|
50
|
+
|
51
|
+
this.startX = this.tracker.getLastAvgX();
|
52
|
+
this.startY = this.tracker.getLastAvgY();
|
53
|
+
|
54
|
+
this.updateState(event);
|
55
|
+
}
|
56
|
+
|
57
|
+
onPointerUp(event: IncomingEvent): void {
|
58
|
+
super.onPointerUp(event);
|
59
|
+
this.lastX = this.tracker.getLastAvgX();
|
60
|
+
this.lastY = this.tracker.getLastAvgY();
|
61
|
+
|
62
|
+
this.tracker.removeFromTracker(event.pointerId);
|
63
|
+
|
64
|
+
this.updateState(event);
|
65
|
+
}
|
66
|
+
|
67
|
+
onAdditionalPointerRemove(event: IncomingEvent): void {
|
68
|
+
super.onAdditionalPointerRemove(event);
|
69
|
+
this.tracker.removeFromTracker(event.pointerId);
|
70
|
+
|
71
|
+
this.offsetX += this.lastX - this.startX;
|
72
|
+
this.offsetY += this.lastY = this.startY;
|
73
|
+
|
74
|
+
this.lastX = this.tracker.getLastAvgX();
|
75
|
+
this.lastY = this.tracker.getLastAvgY();
|
76
|
+
|
77
|
+
this.startX = this.lastX;
|
78
|
+
this.startY = this.lastY;
|
79
|
+
|
80
|
+
this.updateState(event);
|
81
|
+
}
|
82
|
+
|
83
|
+
onPointerMove(event: IncomingEvent): void {
|
84
|
+
this.trySettingPosition(event);
|
85
|
+
this.tracker.track(event);
|
86
|
+
|
87
|
+
this.lastX = this.tracker.getLastAvgX();
|
88
|
+
this.lastY = this.tracker.getLastAvgY();
|
89
|
+
|
90
|
+
this.updateState(event);
|
91
|
+
|
92
|
+
super.onPointerMove(event);
|
93
|
+
}
|
94
|
+
|
95
|
+
onPointerOutOfBounds(event: IncomingEvent): void {
|
96
|
+
this.trySettingPosition(event);
|
97
|
+
this.tracker.track(event);
|
98
|
+
|
99
|
+
this.lastX = this.tracker.getLastAvgX();
|
100
|
+
this.lastY = this.tracker.getLastAvgY();
|
101
|
+
|
102
|
+
this.updateState(event);
|
103
|
+
|
104
|
+
super.onPointerOutOfBounds(event);
|
105
|
+
}
|
106
|
+
|
107
|
+
getDefaultConfig() {
|
108
|
+
return {}
|
109
|
+
}
|
110
|
+
|
111
|
+
private trySettingPosition(event: IncomingEvent): void {
|
112
|
+
if (this.currentState !== State.UNDETERMINED) {
|
113
|
+
return;
|
114
|
+
}
|
115
|
+
this.offsetX = 0;
|
116
|
+
this.offsetY = 0;
|
117
|
+
this.startX = event.x;
|
118
|
+
this.startY = event.y;
|
119
|
+
}
|
120
|
+
|
121
|
+
private updateState(event: IncomingEvent): void {
|
122
|
+
const logger = this.logger.cloneAndJoinPrefix("updateState")
|
123
|
+
if (this.maxNumberOfPointersSoFar < this.tracker.getTrackedPointersCount()) {
|
124
|
+
this.maxNumberOfPointersSoFar = this.tracker.getTrackedPointersCount()
|
125
|
+
}
|
126
|
+
if (this.shouldFail()) {
|
127
|
+
logger.info("fail")
|
128
|
+
this.fail()
|
129
|
+
return;
|
130
|
+
}
|
131
|
+
switch (this.currentState) {
|
132
|
+
case State.UNDETERMINED:
|
133
|
+
if (event.eventType === EventType.DOWN) {
|
134
|
+
this.begin()
|
135
|
+
}
|
136
|
+
this.startTap();
|
137
|
+
break;
|
138
|
+
case State.BEGAN:
|
139
|
+
if (event.eventType === EventType.UP) {
|
140
|
+
logger.info("endTap")
|
141
|
+
this.endTap();
|
142
|
+
}
|
143
|
+
if (event.eventType === EventType.DOWN) {
|
144
|
+
this.startTap();
|
145
|
+
}
|
146
|
+
break;
|
147
|
+
default:
|
148
|
+
logger.info(`default case - currentState ${getStateName(this.currentState)}`)
|
149
|
+
break;
|
150
|
+
}
|
151
|
+
}
|
152
|
+
|
153
|
+
private shouldFail(): boolean {
|
154
|
+
const maxDeltaX = this.config.maxDeltaX ?? Number.MIN_SAFE_INTEGER
|
155
|
+
const maxDeltaY = this.config.maxDeltaY ?? Number.MIN_SAFE_INTEGER
|
156
|
+
const maxDistSq = this.config.maxDistSq ?? Number.MIN_SAFE_INTEGER
|
157
|
+
|
158
|
+
const dx = this.lastX - this.startX + this.offsetX;
|
159
|
+
if (
|
160
|
+
maxDeltaX !== Number.MIN_SAFE_INTEGER &&
|
161
|
+
Math.abs(dx) > maxDeltaX
|
162
|
+
) {
|
163
|
+
return true;
|
164
|
+
}
|
165
|
+
const dy = this.lastY - this.startY + this.offsetY;
|
166
|
+
if (
|
167
|
+
maxDeltaY !== Number.MIN_SAFE_INTEGER &&
|
168
|
+
Math.abs(dy) > maxDeltaY
|
169
|
+
) {
|
170
|
+
return true;
|
171
|
+
}
|
172
|
+
const distSq = dy * dy + dx * dx;
|
173
|
+
const result = maxDistSq !== Number.MIN_SAFE_INTEGER && distSq > maxDistSq
|
174
|
+
return result;
|
175
|
+
}
|
176
|
+
|
177
|
+
private startTap() {
|
178
|
+
this.clearTimeouts();
|
179
|
+
this.waitTimeout = setTimeout(() => this.fail(), this.config.maxDurationMs ?? DEFAULT_MAX_DURATION_MS);
|
180
|
+
}
|
181
|
+
|
182
|
+
private clearTimeouts() {
|
183
|
+
clearTimeout(this.waitTimeout);
|
184
|
+
clearTimeout(this.delayTimeout);
|
185
|
+
}
|
186
|
+
|
187
|
+
private endTap() {
|
188
|
+
const logger = this.logger.cloneAndJoinPrefix("endTap")
|
189
|
+
this.clearTimeouts();
|
190
|
+
if (
|
191
|
+
++this.numberOfTapsSoFar === (this.config.numberOfTaps ?? DEFAULT_NUMBER_OF_TAPS) &&
|
192
|
+
this.maxNumberOfPointersSoFar >= (this.config.minNumberOfPointers ?? 0)
|
193
|
+
) {
|
194
|
+
logger.info(JSON.stringify({ numberOfTapsSoFar: this.numberOfTapsSoFar }))
|
195
|
+
this.activate();
|
196
|
+
} else {
|
197
|
+
this.delayTimeout = setTimeout(() => this.fail(), this.config.maxDelayMs ?? DEFAULT_MAX_DELAY_MS);
|
198
|
+
}
|
199
|
+
}
|
200
|
+
|
201
|
+
public activate(): void {
|
202
|
+
super.activate();
|
203
|
+
this.end();
|
204
|
+
}
|
205
|
+
|
206
|
+
protected onCancel() {
|
207
|
+
super.onCancel()
|
208
|
+
this.resetProgress()
|
209
|
+
}
|
210
|
+
|
211
|
+
protected resetProgress(): void {
|
212
|
+
this.clearTimeouts();
|
213
|
+
this.numberOfTapsSoFar = 0;
|
214
|
+
this.maxNumberOfPointersSoFar = 0;
|
215
|
+
}
|
216
|
+
}
|