@react-native-oh-tpl/react-native-gesture-handler 2.14.7 → 2.14.13

Sign up to get free protection for your applications and to get access to all the features.
Files changed (117) hide show
  1. package/DrawerLayout/index.ts +2 -2
  2. package/Swipeable/index.ts +2 -2
  3. package/harmony/gesture_handler/BuildProfile.ets +17 -0
  4. package/harmony/gesture_handler/build-profile.json5 +19 -0
  5. package/harmony/gesture_handler/hvigorfile.ts +2 -0
  6. package/harmony/gesture_handler/index.ets +3 -0
  7. package/harmony/gesture_handler/oh-package-lock.json5 +18 -0
  8. package/harmony/gesture_handler/oh-package.json5 +12 -0
  9. package/harmony/gesture_handler/src/main/cpp/CMakeLists.txt +8 -0
  10. package/harmony/gesture_handler/src/main/cpp/GestureHandlerPackage.cpp +149 -0
  11. package/harmony/gesture_handler/src/main/cpp/GestureHandlerPackage.h +21 -0
  12. package/harmony/gesture_handler/src/main/cpp/RNGestureHandlerButtonComponentDescriptor.h +36 -0
  13. package/harmony/gesture_handler/src/main/cpp/RNGestureHandlerButtonJSIBinder.h +32 -0
  14. package/harmony/gesture_handler/src/main/cpp/RNGestureHandlerModule.cpp +22 -0
  15. package/harmony/gesture_handler/src/main/cpp/RNGestureHandlerModule.h +15 -0
  16. package/harmony/gesture_handler/src/main/cpp/RNGestureHandlerRootViewComponentDescriptor.h +36 -0
  17. package/harmony/gesture_handler/src/main/cpp/RNGestureHandlerRootViewJSIBinder.h +25 -0
  18. package/harmony/gesture_handler/src/main/cpp/componentInstances/RNGestureHandlerButtonComponentInstance.h +27 -0
  19. package/harmony/gesture_handler/src/main/cpp/componentInstances/RNGestureHandlerRootViewComponentInstance.h +242 -0
  20. package/harmony/gesture_handler/src/main/ets/core/CircularBuffer.ts +42 -0
  21. package/harmony/gesture_handler/src/main/ets/core/GestureHandler.ts +690 -0
  22. package/harmony/gesture_handler/src/main/ets/core/GestureHandlerOrchestrator.ts +335 -0
  23. package/harmony/gesture_handler/src/main/ets/core/GestureHandlerRegistry.ts +63 -0
  24. package/harmony/gesture_handler/src/main/ets/core/IncomingEvent.ts +78 -0
  25. package/harmony/gesture_handler/src/main/ets/core/InteractionManager.ts +144 -0
  26. package/harmony/gesture_handler/src/main/ets/core/LeastSquareSolver.ts +182 -0
  27. package/harmony/gesture_handler/src/main/ets/core/OutgoingEvent.ts +34 -0
  28. package/harmony/gesture_handler/src/main/ets/core/OutgoingEventDispatcher.ts +12 -0
  29. package/harmony/gesture_handler/src/main/ets/core/PointerTracker.ts +239 -0
  30. package/harmony/gesture_handler/src/main/ets/core/RNGHError.ts +5 -0
  31. package/harmony/gesture_handler/src/main/ets/core/RNGHLogger.ts +12 -0
  32. package/harmony/gesture_handler/src/main/ets/core/State.ts +47 -0
  33. package/harmony/gesture_handler/src/main/ets/core/Vector2D.ts +80 -0
  34. package/harmony/gesture_handler/src/main/ets/core/VelocityTracker.ts +106 -0
  35. package/harmony/gesture_handler/src/main/ets/core/View.ts +21 -0
  36. package/harmony/gesture_handler/src/main/ets/core/ViewFinder.ts +11 -0
  37. package/harmony/gesture_handler/src/main/ets/core/ViewRegistry.ts +8 -0
  38. package/harmony/gesture_handler/src/main/ets/core/index.ts +15 -0
  39. package/harmony/gesture_handler/src/main/ets/detectors/ScaleGestureDetector.ts +169 -0
  40. package/harmony/gesture_handler/src/main/ets/gesture-handlers/FlingGestureHandler.ts +211 -0
  41. package/harmony/gesture_handler/src/main/ets/gesture-handlers/GestureHandlerFactory.ts +64 -0
  42. package/harmony/gesture_handler/src/main/ets/gesture-handlers/LongPressGestureHandler.ts +127 -0
  43. package/harmony/gesture_handler/src/main/ets/gesture-handlers/ManualGestureHandler.ts +42 -0
  44. package/harmony/gesture_handler/src/main/ets/gesture-handlers/NativeViewGestureHandler.ts +115 -0
  45. package/harmony/gesture_handler/src/main/ets/gesture-handlers/PanGestureHandler.ts +342 -0
  46. package/harmony/gesture_handler/src/main/ets/gesture-handlers/PinchGestureHandler.ts +159 -0
  47. package/harmony/gesture_handler/src/main/ets/gesture-handlers/RotationGestureHandler.ts +164 -0
  48. package/harmony/gesture_handler/src/main/ets/gesture-handlers/TapGestureHandler.ts +206 -0
  49. package/harmony/gesture_handler/src/main/ets/gesture-handlers/detectors/RotationGestureDetector.ts +167 -0
  50. package/harmony/gesture_handler/src/main/ets/gesture-handlers/index.ts +1 -0
  51. package/harmony/gesture_handler/src/main/ets/namespace/RNGestureHandlerModule.ts +24 -0
  52. package/harmony/gesture_handler/src/main/ets/namespace/components/RNGestureHandlerButton.ts +139 -0
  53. package/harmony/gesture_handler/src/main/ets/namespace/components/RNGestureHandlerRootView.ts +101 -0
  54. package/harmony/gesture_handler/src/main/ets/namespace/components/ts.ts +2 -0
  55. package/harmony/gesture_handler/src/main/ets/namespace/ts.ts +2 -0
  56. package/harmony/gesture_handler/src/main/ets/rnoh/GestureHandlerArkUIAdapter.ts +240 -0
  57. package/harmony/gesture_handler/src/main/ets/rnoh/GestureHandlerPackage.ts +22 -0
  58. package/harmony/gesture_handler/src/main/ets/rnoh/Logger.ts +49 -0
  59. package/harmony/gesture_handler/src/main/ets/rnoh/OutgoingEventDispatchers.ts +71 -0
  60. package/harmony/gesture_handler/src/main/ets/rnoh/RNGHRootTouchHandlerArkTS.ts +98 -0
  61. package/harmony/gesture_handler/src/main/ets/rnoh/RNGHRootTouchHandlerCAPI.ts +110 -0
  62. package/harmony/gesture_handler/src/main/ets/rnoh/RNGestureHandlerButton.ets +38 -0
  63. package/harmony/gesture_handler/src/main/ets/rnoh/RNGestureHandlerModule.ts +233 -0
  64. package/harmony/gesture_handler/src/main/ets/rnoh/RNGestureHandlerRootView.ets +53 -0
  65. package/harmony/gesture_handler/src/main/ets/rnoh/RNOHGestureResponder.ts +24 -0
  66. package/harmony/gesture_handler/src/main/ets/rnoh/RNOHScrollLocker.ts +32 -0
  67. package/harmony/gesture_handler/src/main/ets/rnoh/View.ts +134 -0
  68. package/harmony/gesture_handler/src/main/ets/rnoh/ViewRegistry.ts +97 -0
  69. package/harmony/gesture_handler/src/main/ets/rnoh/types.ts +25 -0
  70. package/harmony/gesture_handler/src/main/module.json5 +9 -0
  71. package/harmony/gesture_handler/src/main/resources/base/element/color.json +8 -0
  72. package/harmony/gesture_handler/src/main/resources/base/element/string.json +16 -0
  73. package/harmony/gesture_handler/src/main/resources/base/media/icon.png +0 -0
  74. package/harmony/gesture_handler/src/main/resources/base/profile/main_pages.json +5 -0
  75. package/harmony/gesture_handler/src/main/resources/en_US/element/string.json +16 -0
  76. package/harmony/gesture_handler/src/main/resources/zh_CN/element/string.json +16 -0
  77. package/harmony/gesture_handler/ts.ts +2 -0
  78. package/harmony/gesture_handler.har +0 -0
  79. package/lib/commonjs/RNGestureHandlerModule.js +3 -2
  80. package/lib/commonjs/RNGestureHandlerModule.js.map +1 -1
  81. package/lib/commonjs/components/GestureHandlerRootView.js +3 -3
  82. package/lib/commonjs/components/GestureHandlerRootView.js.map +1 -1
  83. package/lib/commonjs/handlers/createHandler.js +18 -15
  84. package/lib/commonjs/handlers/createHandler.js.map +1 -1
  85. package/lib/commonjs/index.js +36 -8
  86. package/lib/commonjs/index.js.map +1 -1
  87. package/lib/commonjs/specs/NativeRNGestureHandlerModule.js +2 -1
  88. package/lib/commonjs/specs/NativeRNGestureHandlerModule.js.map +1 -1
  89. package/lib/commonjs/specs/RNGestureHandlerButtonNativeComponent.js +3 -2
  90. package/lib/commonjs/specs/RNGestureHandlerButtonNativeComponent.js.map +1 -1
  91. package/lib/commonjs/specs/RNGestureHandlerRootViewNativeComponent.js +3 -2
  92. package/lib/commonjs/specs/RNGestureHandlerRootViewNativeComponent.js.map +1 -1
  93. package/lib/module/RNGestureHandlerModule.js.map +1 -1
  94. package/lib/module/components/GestureHandlerRootView.js.map +1 -1
  95. package/lib/module/handlers/createHandler.js +15 -12
  96. package/lib/module/handlers/createHandler.js.map +1 -1
  97. package/lib/module/index.js +5 -7
  98. package/lib/module/index.js.map +1 -1
  99. package/lib/module/specs/NativeRNGestureHandlerModule.js.map +1 -1
  100. package/lib/module/specs/RNGestureHandlerButtonNativeComponent.js.map +1 -1
  101. package/lib/module/specs/RNGestureHandlerRootViewNativeComponent.js.map +1 -1
  102. package/lib/typescript/RNGestureHandlerModule.d.ts +2 -2
  103. package/lib/typescript/components/GestureHandlerRootView.d.ts +6 -6
  104. package/lib/typescript/handlers/createHandler.d.ts +11 -11
  105. package/lib/typescript/index.d.ts +47 -42
  106. package/lib/typescript/index.d.ts.map +1 -1
  107. package/lib/typescript/specs/NativeRNGestureHandlerModule.d.ts +14 -14
  108. package/lib/typescript/specs/RNGestureHandlerButtonNativeComponent.d.ts +14 -14
  109. package/lib/typescript/specs/RNGestureHandlerRootViewNativeComponent.d.ts +6 -6
  110. package/package.json +73 -72
  111. package/src/RNGestureHandlerModule.ts +4 -4
  112. package/src/components/GestureHandlerRootView.tsx +23 -23
  113. package/src/handlers/createHandler.tsx +534 -534
  114. package/src/index.ts +172 -172
  115. package/src/specs/NativeRNGestureHandlerModule.ts +26 -26
  116. package/src/specs/RNGestureHandlerButtonNativeComponent.ts +18 -18
  117. package/src/specs/RNGestureHandlerRootViewNativeComponent.ts +6 -6
@@ -0,0 +1,690 @@
1
+ import type { GestureHandlerOrchestrator } from "./GestureHandlerOrchestrator"
2
+ import type { PointerTracker } from "./PointerTracker"
3
+ import type { View } from "./View"
4
+ import type { InteractionManager } from "./InteractionManager"
5
+ import type { RNGHLogger } from './RNGHLogger'
6
+ import { OutgoingEventDispatcher } from "./OutgoingEventDispatcher"
7
+ import { State, getStateName } from "./State"
8
+ import { HitSlop, Directions, IncomingEvent, PointerType, TouchEventType, EventType } from "./IncomingEvent"
9
+ import { GestureStateChangeEvent, GestureTouchEvent, TouchData } from "./OutgoingEvent"
10
+
11
+
12
+ export type GHTag = number
13
+
14
+ export interface Handler {
15
+ handlerTag: GHTag;
16
+ }
17
+
18
+ export const DEFAULT_TOUCH_SLOP = 15;
19
+
20
+ export interface GestureConfig {
21
+ enabled?: boolean;
22
+ manualActivation?: boolean;
23
+ simultaneousHandlers?: Handler[] | null;
24
+ waitFor?: Handler[] | null;
25
+ blocksHandlers?: Handler[] | null;
26
+ hitSlop?: HitSlop;
27
+ shouldCancelWhenOutside?: boolean;
28
+ activateAfterLongPress?: number;
29
+ failOffsetXStart?: number;
30
+ failOffsetYStart?: number;
31
+ failOffsetXEnd?: number;
32
+ failOffsetYEnd?: number;
33
+ activeOffsetXStart?: number;
34
+ activeOffsetXEnd?: number;
35
+ activeOffsetYStart?: number;
36
+ activeOffsetYEnd?: number;
37
+ minPointers?: number;
38
+ maxPointers?: number;
39
+ minDist?: number;
40
+ minDistSq?: number;
41
+ minVelocity?: number;
42
+ minVelocityX?: number;
43
+ minVelocityY?: number;
44
+ minVelocitySq?: number;
45
+ maxDist?: number;
46
+ maxDistSq?: number;
47
+ numberOfPointers?: number;
48
+ minDurationMs?: number;
49
+ numberOfTaps?: number;
50
+ maxDurationMs?: number;
51
+ maxDelayMs?: number;
52
+ maxDeltaX?: number;
53
+ maxDeltaY?: number;
54
+ shouldActivateOnStart?: boolean;
55
+ disallowInterruption?: boolean;
56
+ direction?: Directions;
57
+ needsPointerData?: boolean
58
+ // --- Tap
59
+ minNumberOfPointers?: number
60
+ }
61
+
62
+ type PointerId = number
63
+
64
+ export interface ScrollLocker {
65
+ lockScrollContainingViewTag(viewTag: number): () => void
66
+ }
67
+
68
+ export interface RNGestureResponder {
69
+ lock: (viewTag: number) => () => void
70
+ }
71
+
72
+ export type GestureHandlerDependencies = {
73
+ handlerTag: number
74
+ orchestrator: GestureHandlerOrchestrator
75
+ tracker: PointerTracker
76
+ interactionManager: InteractionManager
77
+ logger: RNGHLogger
78
+ scrollLocker: ScrollLocker
79
+ rnGestureResponder: RNGestureResponder
80
+ }
81
+
82
+ export abstract class GestureHandler<TGestureConfig extends GestureConfig = GestureConfig> {
83
+ protected config: TGestureConfig = this.getDefaultConfig()
84
+ protected currentState: State = State.UNDETERMINED
85
+ protected view: View | undefined = undefined
86
+ protected lastSentState: State | undefined = undefined
87
+ protected shouldCancelWhenOutside = false
88
+
89
+ protected isActivated = false
90
+ protected isAwaiting_ = false
91
+ protected pointerType: PointerType
92
+ protected activationIndex = 0
93
+ protected shouldResetProgress = false;
94
+
95
+ protected handlerTag: number
96
+ protected orchestrator: GestureHandlerOrchestrator
97
+ protected tracker: PointerTracker
98
+ protected eventDispatcher: OutgoingEventDispatcher
99
+ protected interactionManager: InteractionManager
100
+ protected logger: RNGHLogger
101
+ protected scrollLocker: ScrollLocker
102
+ protected rnGestureResponder: RNGestureResponder
103
+
104
+ constructor(deps: GestureHandlerDependencies
105
+ ) {
106
+ this.handlerTag = deps.handlerTag
107
+ this.orchestrator = deps.orchestrator
108
+ this.tracker = deps.tracker
109
+ this.interactionManager = deps.interactionManager
110
+ this.logger = deps.logger
111
+ this.scrollLocker = deps.scrollLocker
112
+ this.rnGestureResponder = deps.rnGestureResponder
113
+ }
114
+
115
+ public setEventDispatcher(eventDispatcher: OutgoingEventDispatcher) {
116
+ // TurboModule provides info about kind of event dispatcher when attaching GH to a view, not when GH is created.
117
+ // This method must be called before any other
118
+ this.eventDispatcher = eventDispatcher
119
+ }
120
+
121
+ public onPointerDown(e: IncomingEvent) {
122
+ this.logger.info("onPointerDown")
123
+ this.orchestrator.registerHandlerIfNotPresent(this);
124
+ this.pointerType = e.pointerType;
125
+ if (this.pointerType === PointerType.TOUCH) {
126
+ this.orchestrator.cancelMouseAndPenGestures(this);
127
+ }
128
+ if (this.config.needsPointerData) {
129
+ this.sendTouchEvent(e);
130
+ }
131
+ }
132
+
133
+ protected sendTouchEvent(e: IncomingEvent) {
134
+ if (!this.config.enabled) {
135
+ return;
136
+ }
137
+
138
+
139
+ const touchEvent: GestureTouchEvent | undefined =
140
+ this.transformToTouchEvent(e);
141
+
142
+ if (touchEvent) {
143
+ this.eventDispatcher.onGestureHandlerEvent(touchEvent)
144
+ }
145
+ }
146
+
147
+ protected transformToTouchEvent(event: IncomingEvent): GestureTouchEvent | undefined {
148
+ const rect = this.view.getBoundingRect();
149
+
150
+ const all: TouchData[] = [];
151
+ const changed: TouchData[] = [];
152
+
153
+ const trackerData = this.tracker.getData();
154
+
155
+ // This if handles edge case where all pointers have been cancelled
156
+ // When pointercancel is triggered, reset method is called. This means that tracker will be reset after first pointer being cancelled
157
+ // The problem is, that handler will receive another pointercancel event from the rest of the pointers
158
+ // To avoid crashing, we don't send event if tracker tracks no pointers, i.e. has been reset
159
+ if (trackerData.size === 0 || !trackerData.has(event.pointerId)) {
160
+ return;
161
+ }
162
+
163
+ trackerData.forEach((element, key): void => {
164
+ const id: number = this.tracker.getMappedTouchEventId(key);
165
+
166
+ all.push({
167
+ id: id,
168
+ x: element.lastX - rect.x,
169
+ y: element.lastY - rect.y,
170
+ absoluteX: element.lastX,
171
+ absoluteY: element.lastY,
172
+ });
173
+ });
174
+
175
+ // Each pointer sends its own event, so we want changed touches to contain only the pointer that has changed.
176
+ // However, if the event is cancel, we want to cancel all pointers to avoid crashes
177
+ if (event.eventType !== EventType.CANCEL) {
178
+ changed.push({
179
+ id: this.tracker.getMappedTouchEventId(event.pointerId),
180
+ x: event.x - rect.x,
181
+ y: event.y - rect.y,
182
+ absoluteX: event.x,
183
+ absoluteY: event.y,
184
+ });
185
+ } else {
186
+ trackerData.forEach((element, key: number): void => {
187
+ const id: number = this.tracker.getMappedTouchEventId(key);
188
+
189
+ changed.push({
190
+ id: id,
191
+ x: element.lastX - rect.x,
192
+ y: element.lastY - rect.y,
193
+ absoluteX: element.lastX,
194
+ absoluteY: element.lastY,
195
+ });
196
+ });
197
+ }
198
+
199
+ let eventType: TouchEventType = TouchEventType.UNDETERMINED;
200
+
201
+ switch (event.eventType) {
202
+ case EventType.DOWN:
203
+ case EventType.ADDITIONAL_POINTER_DOWN:
204
+ eventType = TouchEventType.DOWN;
205
+ break;
206
+ case EventType.UP:
207
+ case EventType.ADDITIONAL_POINTER_UP:
208
+ eventType = TouchEventType.UP;
209
+ break;
210
+ case EventType.MOVE:
211
+ eventType = TouchEventType.MOVE;
212
+ break;
213
+ case EventType.CANCEL:
214
+ eventType = TouchEventType.CANCELLED;
215
+ break;
216
+ }
217
+
218
+ // Here, when we receive up event, we want to decrease number of touches
219
+ // That's because we want handler to send information that there's one pointer less
220
+ // However, we still want this pointer to be present in allTouches array, so that its data can be accessed
221
+ let numberOfTouches: number = all.length;
222
+
223
+ if (
224
+ event.eventType === EventType.UP ||
225
+ event.eventType === EventType.ADDITIONAL_POINTER_UP
226
+ ) {
227
+ --numberOfTouches;
228
+ }
229
+
230
+ return {
231
+ handlerTag: this.handlerTag,
232
+ state: this.currentState,
233
+ eventType: event.touchEventType ?? eventType,
234
+ changedTouches: changed,
235
+ allTouches: all,
236
+ numberOfTouches: numberOfTouches,
237
+ };
238
+ }
239
+
240
+ public onPointerUp(e: IncomingEvent): void {
241
+ this.logger.info("onPointerUp")
242
+ if (this.config.needsPointerData) this.sendTouchEvent(e)
243
+ }
244
+
245
+ public onAdditionalPointerAdd(e: IncomingEvent): void {
246
+ this.logger.info("onAdditionalPointerAdd")
247
+ if (this.config.needsPointerData) this.sendTouchEvent(e)
248
+ }
249
+
250
+ public onAdditionalPointerRemove(e: IncomingEvent): void {
251
+ this.logger.info("onAdditionalPointerRemove")
252
+ if (this.config.needsPointerData) this.sendTouchEvent(e)
253
+ }
254
+
255
+ public onPointerMove(e: IncomingEvent): void {
256
+ this.logger.info("onPointerMove")
257
+ this.tryToSendMoveEvent(false);
258
+ if (this.config.needsPointerData) {
259
+ this.sendTouchEvent(e);
260
+ }
261
+ }
262
+
263
+ private tryToSendMoveEvent(out: boolean): void {
264
+ this.logger.info(`tryToSendMoveEvent ${JSON.stringify({
265
+ out,
266
+ enabled: this.config.enabled,
267
+ isActivated: this.isActivated,
268
+ shouldCancelWhenOutside: this.shouldCancelWhenOutside
269
+ })}`)
270
+ if (
271
+ this.config.enabled &&
272
+ this.isActivated &&
273
+ (!out || (out && !this.shouldCancelWhenOutside))
274
+ ) {
275
+ this.sendEvent({ newState: this.currentState, oldState: this.currentState });
276
+ }
277
+ }
278
+
279
+ public onPointerEnter(e: IncomingEvent): void {
280
+ this.logger.info("onPointerEnter")
281
+ if (this.config.needsPointerData) {
282
+ this.sendTouchEvent(e)
283
+ }
284
+ }
285
+
286
+ public onPointerOut(e: IncomingEvent): void {
287
+ this.logger.info("onPointerOut")
288
+ if (this.shouldCancelWhenOutside) {
289
+ switch (this.currentState) {
290
+ case State.ACTIVE:
291
+ this.cancel();
292
+ break;
293
+ case State.BEGAN:
294
+ this.fail();
295
+ break;
296
+ }
297
+ return;
298
+ }
299
+ if (this.config.needsPointerData) {
300
+ this.sendTouchEvent(e);
301
+ }
302
+ }
303
+
304
+ public onPointerCancel(e: IncomingEvent): void {
305
+ this.logger.info("onPointerCancel")
306
+ if (this.config.needsPointerData) {
307
+ this.sendTouchEvent(e);
308
+ }
309
+ this.cancel();
310
+ this.reset();
311
+ }
312
+
313
+ public onPointerOutOfBounds(e: IncomingEvent): void {
314
+ this.logger.info("onPointerOutOfBounds")
315
+ this.tryToSendMoveEvent(true);
316
+ if (this.config.needsPointerData) {
317
+ this.sendTouchEvent(e);
318
+ }
319
+ }
320
+
321
+ public onViewAttached(view: View) {
322
+ this.logger.info("onViewAttached")
323
+ this.view = view
324
+ }
325
+
326
+ public getTag(): number {
327
+ return this.handlerTag
328
+ }
329
+
330
+ public getTracker() {
331
+ return this.tracker
332
+ }
333
+
334
+ public getView() {
335
+ return this.view
336
+ }
337
+
338
+ public begin(): void {
339
+ this.logger.info("begin")
340
+ if (!this.isWithinHitSlop()) return;
341
+ if (this.currentState === State.UNDETERMINED) {
342
+ this.moveToState(State.BEGAN);
343
+ }
344
+ }
345
+
346
+ private isWithinHitSlop(): boolean {
347
+ if (!this.config.hitSlop) {
348
+ return true;
349
+ }
350
+
351
+ const width = this.view.getBoundingRect().width;
352
+ const height = this.view.getBoundingRect().height;
353
+
354
+ let left = 0;
355
+ let top = 0;
356
+ let right: number = width;
357
+ let bottom: number = height;
358
+
359
+ if (this.config.hitSlop.horizontal !== undefined) {
360
+ left -= this.config.hitSlop.horizontal;
361
+ right += this.config.hitSlop.horizontal;
362
+ }
363
+
364
+ if (this.config.hitSlop.vertical !== undefined) {
365
+ top -= this.config.hitSlop.vertical;
366
+ bottom += this.config.hitSlop.vertical;
367
+ }
368
+
369
+ if (this.config.hitSlop.left !== undefined) {
370
+ left = -this.config.hitSlop.left;
371
+ }
372
+
373
+ if (this.config.hitSlop.right !== undefined) {
374
+ right = width + this.config.hitSlop.right;
375
+ }
376
+
377
+ if (this.config.hitSlop.top !== undefined) {
378
+ top = -this.config.hitSlop.top;
379
+ }
380
+
381
+ if (this.config.hitSlop.bottom !== undefined) {
382
+ bottom = width + this.config.hitSlop.bottom;
383
+ }
384
+ if (this.config.hitSlop.width !== undefined) {
385
+ if (this.config.hitSlop.left !== undefined) {
386
+ right = left + this.config.hitSlop.width;
387
+ } else if (this.config.hitSlop.right !== undefined) {
388
+ left = right - this.config.hitSlop.width;
389
+ }
390
+ }
391
+
392
+ if (this.config.hitSlop.height !== undefined) {
393
+ if (this.config.hitSlop.top !== undefined) {
394
+ bottom = top + this.config.hitSlop.height;
395
+ } else if (this.config.hitSlop.bottom !== undefined) {
396
+ top = bottom - this.config.hitSlop.height;
397
+ }
398
+ }
399
+
400
+ const rect = this.view.getBoundingRect();
401
+ const offsetX: number = this.tracker.getLastX() - rect.x;
402
+ const offsetY: number = this.tracker.getLastY() - rect.y;
403
+
404
+ if (
405
+ offsetX >= left &&
406
+ offsetX <= right &&
407
+ offsetY >= top &&
408
+ offsetY <= bottom
409
+ ) {
410
+ return true;
411
+ }
412
+ return false;
413
+ }
414
+
415
+ public activate(): void {
416
+ this.logger.info("activate")
417
+ if (!this.config.manualActivation || this.currentState === State.UNDETERMINED || this.currentState === State.BEGAN) {
418
+ this.moveToState(State.ACTIVE)
419
+ }
420
+ }
421
+
422
+ protected moveToState(state: State) {
423
+ this.logger.info(`moveToState ${getStateName(state)}`)
424
+ if (state === this.currentState) return;
425
+ const oldState = this.currentState
426
+ this.currentState = state;
427
+ if (this.tracker.getTrackedPointersCount() > 0 && this.config.needsPointerData && this.isFinished()) {
428
+ this.cancelTouches()
429
+ }
430
+ this.orchestrator.onHandlerStateChange(this, state, oldState)
431
+ this.onStateChange(state, oldState)
432
+
433
+ if (!this.isEnabled() && this.isFinished()) {
434
+ this.currentState = State.UNDETERMINED;
435
+ }
436
+ }
437
+
438
+ private isFinished() {
439
+ return (
440
+ this.currentState === State.END ||
441
+ this.currentState === State.FAILED ||
442
+ this.currentState === State.CANCELLED
443
+ );
444
+ }
445
+
446
+ private cancelTouches(): void {
447
+ const rect = this.view.getBoundingRect();
448
+ const all: TouchData[] = [];
449
+ const changed: TouchData[] = [];
450
+ const trackerData = this.tracker.getData();
451
+ if (trackerData.size === 0) {
452
+ return;
453
+ }
454
+ trackerData.forEach((element, key): void => {
455
+ const id: number = this.tracker.getMappedTouchEventId(key);
456
+ all.push({
457
+ id: id,
458
+ x: element.lastX - rect.x,
459
+ y: element.lastY - rect.y,
460
+ absoluteX: element.lastX,
461
+ absoluteY: element.lastY,
462
+ });
463
+ changed.push({
464
+ id: id,
465
+ x: element.lastX - rect.x,
466
+ y: element.lastY - rect.y,
467
+ absoluteX: element.lastX,
468
+ absoluteY: element.lastY,
469
+ });
470
+ });
471
+ const cancelEvent: GestureTouchEvent = {
472
+ handlerTag: this.handlerTag,
473
+ state: this.currentState,
474
+ eventType: TouchEventType.CANCELLED,
475
+ changedTouches: changed,
476
+ allTouches: all,
477
+ numberOfTouches: all.length,
478
+ };
479
+ this.eventDispatcher.onGestureHandlerEvent(cancelEvent)
480
+ }
481
+
482
+ protected onStateChange(newState: State, oldState: State) {
483
+ this.logger.info(`onStateChange: from ${getStateName(oldState)} to ${getStateName(newState)}`)
484
+ }
485
+
486
+
487
+ public updateGestureConfig(config: TGestureConfig): void {
488
+ this.config = { enabled: true, ...config }
489
+ if (this.config.shouldCancelWhenOutside !== undefined) {
490
+ this.setShouldCancelWhenOutside(this.config.shouldCancelWhenOutside);
491
+ }
492
+
493
+ // this.validateHitSlops();
494
+ if (this.config.enabled) {
495
+ return;
496
+ }
497
+ // switch (this.currentState) {
498
+ // case State.ACTIVE:
499
+ // this.fail(true);
500
+ // break;
501
+ // case State.UNDETERMINED:
502
+ // this.orchestrator.removeHandlerFromOrchestrator(this);
503
+ // break;
504
+ // default:
505
+ // this.cancel(true);
506
+ // break;
507
+ }
508
+
509
+ protected resetConfig(): void {
510
+ this.config = this.getDefaultConfig()
511
+ }
512
+
513
+ abstract getDefaultConfig(): TGestureConfig
514
+
515
+
516
+ public isEnabled(): boolean {
517
+ return Boolean(this.config.enabled)
518
+ }
519
+
520
+ public isActive(): boolean {
521
+ return this.isActivated
522
+ }
523
+
524
+ public cancel(): void {
525
+ this.logger.info(`cancel`)
526
+ if (
527
+ this.currentState === State.ACTIVE ||
528
+ this.currentState === State.UNDETERMINED ||
529
+ this.currentState === State.BEGAN
530
+ ) {
531
+ this.onCancel();
532
+ this.moveToState(State.CANCELLED);
533
+ }
534
+ }
535
+
536
+ protected onCancel(): void {
537
+ }
538
+
539
+ protected onReset(): void {
540
+ }
541
+
542
+ protected resetProgress(): void {
543
+ }
544
+
545
+ public getState(): State {
546
+ return this.currentState
547
+ }
548
+
549
+ public sendEvent({newState, oldState}: {
550
+ oldState: State,
551
+ newState: State
552
+ }): void {
553
+ const logger = this.logger.cloneWithPrefix(`sendEvent(newState=${getStateName(newState)}, oldState=${getStateName(oldState)})`)
554
+ const stateChangeEvent = this.createStateChangeEvent(newState, oldState);
555
+ if (this.lastSentState !== newState) {
556
+ this.lastSentState = newState;
557
+ logger.debug("calling onGestureHandlerStateChange")
558
+ this.eventDispatcher.onGestureHandlerStateChange(stateChangeEvent);
559
+ }
560
+ if (this.currentState === State.ACTIVE) {
561
+ stateChangeEvent.oldState = undefined;
562
+ logger.debug("calling onGestureHandlerEvent")
563
+ this.eventDispatcher.onGestureHandlerEvent(stateChangeEvent);
564
+ }
565
+ }
566
+
567
+ private createStateChangeEvent(newState: State, oldState: State): GestureStateChangeEvent {
568
+ return {
569
+ numberOfPointers: this.tracker.getTrackedPointersCount(),
570
+ state: newState,
571
+ pointerInside: this.view.isPositionInBounds({
572
+ x: this.tracker.getLastAvgX(),
573
+ y: this.tracker.getLastAvgY(),
574
+ }),
575
+ ...this.transformNativeEvent(),
576
+ handlerTag: this.handlerTag,
577
+ target: this.view.getTag(),
578
+ oldState: newState !== oldState ? oldState : undefined,
579
+ };
580
+ }
581
+
582
+ protected transformNativeEvent(): Record<string, unknown> {
583
+ const rect = this.view.getBoundingRect();
584
+ return {
585
+ x: this.tracker.getLastAvgX() - rect.x,
586
+ y: this.tracker.getLastAvgY() - rect.y,
587
+ absoluteX: this.tracker.getLastAvgX(),
588
+ absoluteY: this.tracker.getLastAvgY(),
589
+ };
590
+ }
591
+
592
+ setAwaiting(isAwaiting: boolean): void {
593
+ this.isAwaiting_ = isAwaiting
594
+ }
595
+
596
+ shouldWaitForHandlerFailure(handler: GestureHandler): boolean {
597
+ if (handler === this)
598
+ return false;
599
+ return this.interactionManager.shouldWaitForHandlerFailure(this, handler);
600
+ }
601
+
602
+ shouldRequireToWaitForFailure(handler: GestureHandler): boolean {
603
+ if (handler === this)
604
+ return false;
605
+ return this.interactionManager.shouldRequireHandlerToWaitForFailure(this, handler);
606
+ }
607
+
608
+ shouldWaitFor(otherHandler: GestureHandler): boolean {
609
+ const logger = this.logger.cloneWithPrefix(`shouldWaitFor(${otherHandler.getTag()})`)
610
+ const result = (
611
+ this !== otherHandler &&
612
+ (this.shouldWaitForHandlerFailure(otherHandler) ||
613
+ otherHandler.shouldRequireToWaitForFailure(this))
614
+ );
615
+ logger.debug(result)
616
+ return result
617
+ }
618
+
619
+ reset(): void {
620
+ this.logger.info("reset")
621
+ this.tracker.resetTracker();
622
+ this.onReset();
623
+ this.resetProgress();
624
+ // this.eventManagers.forEach((manager: EventManager) =>
625
+ // manager.resetManager()
626
+ // );
627
+ this.currentState = State.UNDETERMINED;
628
+ }
629
+
630
+ isAwaiting(): boolean {
631
+ return this.isAwaiting_
632
+ }
633
+
634
+ setActive(isActivated: boolean): void {
635
+ this.isActivated = isActivated
636
+ }
637
+
638
+ setActivationIndex(value: number): void {
639
+ this.activationIndex = value
640
+ }
641
+
642
+ setShouldResetProgress(value: boolean): void {
643
+ this.shouldResetProgress = value;
644
+ }
645
+
646
+ fail(): void {
647
+ this.logger.info('fail')
648
+ if (
649
+ this.currentState === State.ACTIVE ||
650
+ this.currentState === State.BEGAN
651
+ ) {
652
+ this.moveToState(State.FAILED);
653
+ }
654
+ this.resetProgress();
655
+ }
656
+
657
+ shouldBeCancelledByOther(otherHandler: GestureHandler): boolean {
658
+ if (otherHandler === this)
659
+ return false;
660
+ return this.interactionManager.shouldHandlerBeCancelledBy(this, otherHandler);
661
+ }
662
+
663
+ getTrackedPointersID(): PointerId[] {
664
+ return this.tracker.getTrackedPointersID()
665
+ }
666
+
667
+ shouldRecognizeSimultaneously(otherHandler: GestureHandler): boolean {
668
+ this.logger.cloneWithPrefix(`shouldRecognizeSimultaneously(${otherHandler.getTag()})`).debug("")
669
+ if (otherHandler === this) {
670
+ return true;
671
+ }
672
+ return this.interactionManager.shouldRecognizeSimultaneously(this, otherHandler);
673
+ }
674
+
675
+ public getPointerType(): PointerType {
676
+ return this.pointerType
677
+ }
678
+
679
+ protected setShouldCancelWhenOutside(shouldCancel: boolean) {
680
+ this.shouldCancelWhenOutside = shouldCancel
681
+ }
682
+
683
+ public end() {
684
+ this.logger.info("end")
685
+ if (this.currentState === State.BEGAN || this.currentState === State.ACTIVE) {
686
+ this.moveToState(State.END);
687
+ }
688
+ this.resetProgress();
689
+ }
690
+ }