react-native-gesture-handler 2.2.0 → 2.3.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (260) hide show
  1. package/LICENSE +1 -1
  2. package/README.md +5 -1
  3. package/RNGestureHandler.podspec +27 -3
  4. package/android/build.gradle +81 -8
  5. package/android/lib/src/main/java/com/swmansion/gesturehandler/GestureHandler.kt +5 -1
  6. package/android/lib/src/main/java/com/swmansion/gesturehandler/GestureHandlerOrchestrator.kt +14 -12
  7. package/android/lib/src/main/java/com/swmansion/gesturehandler/PinchGestureHandler.kt +2 -4
  8. package/android/lib/src/main/java/com/swmansion/gesturehandler/ScaleGestureDetector.java +558 -0
  9. package/android/src/fabric/java/com/swmansion/gesturehandler/RNGestureHandlerComponentsRegistry.java +29 -0
  10. package/android/src/fabric/java/com/swmansion/gesturehandler/ReactContextExtensions.kt +12 -0
  11. package/android/src/main/AndroidManifest.xml +1 -1
  12. package/android/src/main/java/com/swmansion/gesturehandler/RNGestureHandlerPackage.kt +29 -0
  13. package/android/src/main/java/com/swmansion/gesturehandler/react/RNGestureHandlerButtonViewManager.kt +40 -13
  14. package/android/src/main/java/com/swmansion/gesturehandler/react/RNGestureHandlerModule.kt +111 -47
  15. package/android/src/main/java/com/swmansion/gesturehandler/react/RNGestureHandlerRegistry.kt +9 -4
  16. package/android/src/main/java/com/swmansion/gesturehandler/react/RNGestureHandlerRootHelper.kt +1 -1
  17. package/android/src/main/java/com/swmansion/gesturehandler/react/RNGestureHandlerRootViewManager.kt +17 -2
  18. package/android/src/main/jni/Android.mk +53 -0
  19. package/android/src/main/jni/OnLoad.cpp +9 -0
  20. package/android/src/main/jni/RNGestureHandlerComponentsRegistry.cpp +71 -0
  21. package/android/src/main/jni/RNGestureHandlerComponentsRegistry.h +34 -0
  22. package/android/src/main/jni/cpp-adapter.cpp +41 -0
  23. package/android/src/paper/java/com/facebook/react/viewmanagers/RNGestureHandlerButtonManagerDelegate.java +47 -0
  24. package/android/src/paper/java/com/facebook/react/viewmanagers/RNGestureHandlerButtonManagerInterface.java +22 -0
  25. package/android/src/paper/java/com/facebook/react/viewmanagers/RNGestureHandlerRootViewManagerDelegate.java +25 -0
  26. package/android/src/paper/java/com/facebook/react/viewmanagers/RNGestureHandlerRootViewManagerInterface.java +16 -0
  27. package/android/src/paper/java/com/swmansion/gesturehandler/ReactContextExtensions.kt +13 -0
  28. package/ios/Handlers/RNFlingHandler.m +2 -0
  29. package/ios/Handlers/RNPanHandler.m +2 -0
  30. package/ios/Handlers/RNPinchHandler.m +2 -0
  31. package/ios/Handlers/RNRotationHandler.m +2 -0
  32. package/ios/RNGestureHandler.h +3 -8
  33. package/ios/RNGestureHandler.m +2 -10
  34. package/ios/RNGestureHandler.xcodeproj/project.xcworkspace/xcuserdata/jakubpiasecki.xcuserdatad/UserInterfaceState.xcuserstate +0 -0
  35. package/ios/RNGestureHandlerActionType.h +8 -0
  36. package/ios/RNGestureHandlerButtonComponentView.h +17 -0
  37. package/ios/RNGestureHandlerButtonComponentView.mm +48 -0
  38. package/ios/RNGestureHandlerButtonManager.h +5 -0
  39. package/ios/RNGestureHandlerButtonManager.m +30 -0
  40. package/ios/RNGestureHandlerManager.h +2 -4
  41. package/ios/{RNGestureHandlerManager.m → RNGestureHandlerManager.mm} +92 -20
  42. package/ios/{RNGestureHandlerModule.m → RNGestureHandlerModule.mm} +91 -36
  43. package/ios/RNGestureHandlerRegistry.h +1 -2
  44. package/ios/RNGestureHandlerRegistry.m +2 -11
  45. package/ios/RNGestureHandlerRootViewComponentView.mm +21 -0
  46. package/jest-utils/package.json +6 -0
  47. package/lib/commonjs/ActionType.js +15 -0
  48. package/lib/commonjs/ActionType.js.map +1 -0
  49. package/lib/commonjs/GestureHandlerRootView.android.js +6 -6
  50. package/lib/commonjs/GestureHandlerRootView.android.js.map +1 -1
  51. package/lib/commonjs/GestureHandlerRootView.js +2 -3
  52. package/lib/commonjs/GestureHandlerRootView.js.map +1 -1
  53. package/lib/commonjs/RNGestureHandlerModule.js +4 -2
  54. package/lib/commonjs/RNGestureHandlerModule.js.map +1 -1
  55. package/lib/commonjs/RNGestureHandlerModule.web.js +1 -1
  56. package/lib/commonjs/RNGestureHandlerModule.web.js.map +1 -1
  57. package/lib/commonjs/components/DrawerLayout.js +2 -2
  58. package/lib/commonjs/components/DrawerLayout.js.map +1 -1
  59. package/lib/commonjs/components/GestureHandlerButton.js +5 -2
  60. package/lib/commonjs/components/GestureHandlerButton.js.map +1 -1
  61. package/lib/commonjs/components/Swipeable.js +22 -20
  62. package/lib/commonjs/components/Swipeable.js.map +1 -1
  63. package/lib/commonjs/components/touchables/TouchableOpacity.js +3 -1
  64. package/lib/commonjs/components/touchables/TouchableOpacity.js.map +1 -1
  65. package/lib/commonjs/fabric/RNGestureHandlerButtonNativeComponent.js +21 -0
  66. package/lib/commonjs/fabric/RNGestureHandlerButtonNativeComponent.js.map +1 -0
  67. package/lib/commonjs/fabric/RNGestureHandlerRootViewNativeComponent.js +21 -0
  68. package/lib/commonjs/fabric/RNGestureHandlerRootViewNativeComponent.js.map +1 -0
  69. package/lib/commonjs/getShadowNodeFromRef.js +24 -0
  70. package/lib/commonjs/getShadowNodeFromRef.js.map +1 -0
  71. package/lib/commonjs/getShadowNodeFromRef.web.js +15 -0
  72. package/lib/commonjs/getShadowNodeFromRef.web.js.map +1 -0
  73. package/lib/commonjs/handlers/FlingGestureHandler.js +4 -2
  74. package/lib/commonjs/handlers/FlingGestureHandler.js.map +1 -1
  75. package/lib/commonjs/handlers/ForceTouchGestureHandler.js +8 -4
  76. package/lib/commonjs/handlers/ForceTouchGestureHandler.js.map +1 -1
  77. package/lib/commonjs/handlers/LongPressGestureHandler.js +4 -2
  78. package/lib/commonjs/handlers/LongPressGestureHandler.js.map +1 -1
  79. package/lib/commonjs/handlers/NativeViewGestureHandler.js +4 -2
  80. package/lib/commonjs/handlers/NativeViewGestureHandler.js.map +1 -1
  81. package/lib/commonjs/handlers/PanGestureHandler.js +4 -2
  82. package/lib/commonjs/handlers/PanGestureHandler.js.map +1 -1
  83. package/lib/commonjs/handlers/PinchGestureHandler.js +4 -2
  84. package/lib/commonjs/handlers/PinchGestureHandler.js.map +1 -1
  85. package/lib/commonjs/handlers/RotationGestureHandler.js +4 -2
  86. package/lib/commonjs/handlers/RotationGestureHandler.js.map +1 -1
  87. package/lib/commonjs/handlers/TapGestureHandler.js +4 -2
  88. package/lib/commonjs/handlers/TapGestureHandler.js.map +1 -1
  89. package/lib/commonjs/handlers/createHandler.js +63 -9
  90. package/lib/commonjs/handlers/createHandler.js.map +1 -1
  91. package/lib/commonjs/handlers/createNativeWrapper.js +6 -2
  92. package/lib/commonjs/handlers/createNativeWrapper.js.map +1 -1
  93. package/lib/commonjs/handlers/gestureHandlerCommon.js.map +1 -1
  94. package/lib/commonjs/handlers/gestures/GestureDetector.js +84 -32
  95. package/lib/commonjs/handlers/gestures/GestureDetector.js.map +1 -1
  96. package/lib/commonjs/handlers/gestures/eventReceiver.js +25 -8
  97. package/lib/commonjs/handlers/gestures/eventReceiver.js.map +1 -1
  98. package/lib/commonjs/handlers/gestures/forceTouchGesture.js.map +1 -1
  99. package/lib/commonjs/handlers/gestures/gesture.js +16 -1
  100. package/lib/commonjs/handlers/gestures/gesture.js.map +1 -1
  101. package/lib/commonjs/handlers/gestures/gestureStateManager.js +7 -4
  102. package/lib/commonjs/handlers/gestures/gestureStateManager.js.map +1 -1
  103. package/lib/commonjs/handlers/gestures/panGesture.js.map +1 -1
  104. package/lib/commonjs/handlers/gestures/pinchGesture.js.map +1 -1
  105. package/lib/commonjs/handlers/gestures/reanimatedWrapper.js +4 -1
  106. package/lib/commonjs/handlers/gestures/reanimatedWrapper.js.map +1 -1
  107. package/lib/commonjs/handlers/handlersRegistry.js +42 -6
  108. package/lib/commonjs/handlers/handlersRegistry.js.map +1 -1
  109. package/lib/commonjs/index.js.map +1 -1
  110. package/lib/commonjs/init.js +10 -0
  111. package/lib/commonjs/init.js.map +1 -1
  112. package/lib/commonjs/jestUtils/index.js +20 -0
  113. package/lib/commonjs/jestUtils/index.js.map +1 -0
  114. package/lib/commonjs/jestUtils/jestUtils.js +375 -0
  115. package/lib/commonjs/jestUtils/jestUtils.js.map +1 -0
  116. package/lib/commonjs/utils.js +38 -0
  117. package/lib/commonjs/utils.js.map +1 -1
  118. package/lib/module/ActionType.js +7 -0
  119. package/lib/module/ActionType.js.map +1 -0
  120. package/lib/module/GestureHandlerRootView.android.js +5 -6
  121. package/lib/module/GestureHandlerRootView.android.js.map +1 -1
  122. package/lib/module/GestureHandlerRootView.js +2 -3
  123. package/lib/module/GestureHandlerRootView.js.map +1 -1
  124. package/lib/module/RNGestureHandlerModule.js +3 -2
  125. package/lib/module/RNGestureHandlerModule.js.map +1 -1
  126. package/lib/module/RNGestureHandlerModule.web.js +1 -1
  127. package/lib/module/RNGestureHandlerModule.web.js.map +1 -1
  128. package/lib/module/components/DrawerLayout.js +2 -2
  129. package/lib/module/components/DrawerLayout.js.map +1 -1
  130. package/lib/module/components/GestureHandlerButton.js +4 -2
  131. package/lib/module/components/GestureHandlerButton.js.map +1 -1
  132. package/lib/module/components/Swipeable.js +22 -20
  133. package/lib/module/components/Swipeable.js.map +1 -1
  134. package/lib/module/components/touchables/TouchableOpacity.js +4 -2
  135. package/lib/module/components/touchables/TouchableOpacity.js.map +1 -1
  136. package/lib/module/fabric/RNGestureHandlerButtonNativeComponent.js +9 -0
  137. package/lib/module/fabric/RNGestureHandlerButtonNativeComponent.js.map +1 -0
  138. package/lib/module/fabric/RNGestureHandlerRootViewNativeComponent.js +9 -0
  139. package/lib/module/fabric/RNGestureHandlerRootViewNativeComponent.js.map +1 -0
  140. package/lib/module/getShadowNodeFromRef.js +17 -0
  141. package/lib/module/getShadowNodeFromRef.js.map +1 -0
  142. package/lib/module/getShadowNodeFromRef.web.js +8 -0
  143. package/lib/module/getShadowNodeFromRef.web.js.map +1 -0
  144. package/lib/module/handlers/FlingGestureHandler.js +2 -1
  145. package/lib/module/handlers/FlingGestureHandler.js.map +1 -1
  146. package/lib/module/handlers/ForceTouchGestureHandler.js +5 -3
  147. package/lib/module/handlers/ForceTouchGestureHandler.js.map +1 -1
  148. package/lib/module/handlers/LongPressGestureHandler.js +2 -1
  149. package/lib/module/handlers/LongPressGestureHandler.js.map +1 -1
  150. package/lib/module/handlers/NativeViewGestureHandler.js +2 -1
  151. package/lib/module/handlers/NativeViewGestureHandler.js.map +1 -1
  152. package/lib/module/handlers/PanGestureHandler.js +2 -1
  153. package/lib/module/handlers/PanGestureHandler.js.map +1 -1
  154. package/lib/module/handlers/PinchGestureHandler.js +2 -1
  155. package/lib/module/handlers/PinchGestureHandler.js.map +1 -1
  156. package/lib/module/handlers/RotationGestureHandler.js +2 -1
  157. package/lib/module/handlers/RotationGestureHandler.js.map +1 -1
  158. package/lib/module/handlers/TapGestureHandler.js +2 -1
  159. package/lib/module/handlers/TapGestureHandler.js.map +1 -1
  160. package/lib/module/handlers/createHandler.js +62 -10
  161. package/lib/module/handlers/createHandler.js.map +1 -1
  162. package/lib/module/handlers/createNativeWrapper.js +6 -2
  163. package/lib/module/handlers/createNativeWrapper.js.map +1 -1
  164. package/lib/module/handlers/gestureHandlerCommon.js.map +1 -1
  165. package/lib/module/handlers/gestures/GestureDetector.js +79 -32
  166. package/lib/module/handlers/gestures/GestureDetector.js.map +1 -1
  167. package/lib/module/handlers/gestures/eventReceiver.js +25 -7
  168. package/lib/module/handlers/gestures/eventReceiver.js.map +1 -1
  169. package/lib/module/handlers/gestures/forceTouchGesture.js.map +1 -1
  170. package/lib/module/handlers/gestures/gesture.js +16 -1
  171. package/lib/module/handlers/gestures/gesture.js.map +1 -1
  172. package/lib/module/handlers/gestures/gestureStateManager.js +6 -4
  173. package/lib/module/handlers/gestures/gestureStateManager.js.map +1 -1
  174. package/lib/module/handlers/gestures/panGesture.js.map +1 -1
  175. package/lib/module/handlers/gestures/pinchGesture.js.map +1 -1
  176. package/lib/module/handlers/gestures/reanimatedWrapper.js +2 -1
  177. package/lib/module/handlers/gestures/reanimatedWrapper.js.map +1 -1
  178. package/lib/module/handlers/handlersRegistry.js +34 -6
  179. package/lib/module/handlers/handlersRegistry.js.map +1 -1
  180. package/lib/module/index.js.map +1 -1
  181. package/lib/module/init.js +6 -0
  182. package/lib/module/init.js.map +1 -1
  183. package/lib/module/jestUtils/index.js +2 -0
  184. package/lib/module/jestUtils/index.js.map +1 -0
  185. package/lib/module/jestUtils/jestUtils.js +350 -0
  186. package/lib/module/jestUtils/jestUtils.js.map +1 -0
  187. package/lib/module/utils.js +28 -0
  188. package/lib/module/utils.js.map +1 -1
  189. package/lib/typescript/ActionType.d.ts +7 -0
  190. package/lib/typescript/GestureHandlerRootView.android.d.ts +5 -2
  191. package/lib/typescript/GestureHandlerRootView.d.ts +1 -1
  192. package/lib/typescript/RNGestureHandlerModule.d.ts +3 -1
  193. package/lib/typescript/RNGestureHandlerModule.web.d.ts +2 -1
  194. package/lib/typescript/components/GestureHandlerButton.d.ts +2 -2
  195. package/lib/typescript/components/Swipeable.d.ts +12 -4
  196. package/lib/typescript/components/touchables/TouchableOpacity.d.ts +5 -1
  197. package/lib/typescript/getShadowNodeFromRef.d.ts +1 -0
  198. package/lib/typescript/getShadowNodeFromRef.web.d.ts +1 -0
  199. package/lib/typescript/handlers/FlingGestureHandler.d.ts +1 -0
  200. package/lib/typescript/handlers/ForceTouchGestureHandler.d.ts +2 -1
  201. package/lib/typescript/handlers/LongPressGestureHandler.d.ts +1 -0
  202. package/lib/typescript/handlers/NativeViewGestureHandler.d.ts +1 -0
  203. package/lib/typescript/handlers/PanGestureHandler.d.ts +1 -0
  204. package/lib/typescript/handlers/PinchGestureHandler.d.ts +1 -0
  205. package/lib/typescript/handlers/RotationGestureHandler.d.ts +1 -0
  206. package/lib/typescript/handlers/TapGestureHandler.d.ts +1 -0
  207. package/lib/typescript/handlers/gestureHandlerCommon.d.ts +2 -4
  208. package/lib/typescript/handlers/gestures/GestureDetector.d.ts +4 -1
  209. package/lib/typescript/handlers/gestures/forceTouchGesture.d.ts +1 -2
  210. package/lib/typescript/handlers/gestures/gesture.d.ts +5 -0
  211. package/lib/typescript/handlers/gestures/panGesture.d.ts +1 -2
  212. package/lib/typescript/handlers/gestures/pinchGesture.d.ts +1 -2
  213. package/lib/typescript/handlers/handlersRegistry.d.ts +11 -3
  214. package/lib/typescript/index.d.ts +3 -0
  215. package/lib/typescript/jestUtils/index.d.ts +1 -0
  216. package/lib/typescript/jestUtils/jestUtils.d.ts +28 -0
  217. package/lib/typescript/utils.d.ts +6 -0
  218. package/lib/typescript/web/constants.d.ts +0 -1
  219. package/package.json +40 -16
  220. package/src/ActionType.ts +9 -0
  221. package/src/GestureHandlerRootView.android.tsx +14 -14
  222. package/src/GestureHandlerRootView.tsx +4 -4
  223. package/src/RNGestureHandlerModule.ts +11 -6
  224. package/src/RNGestureHandlerModule.web.ts +2 -1
  225. package/src/components/DrawerLayout.tsx +8 -2
  226. package/src/components/GestureHandlerButton.tsx +7 -4
  227. package/src/components/Swipeable.tsx +28 -22
  228. package/src/components/touchables/TouchableOpacity.tsx +6 -2
  229. package/src/fabric/RNGestureHandlerButtonNativeComponent.js +27 -0
  230. package/src/fabric/RNGestureHandlerRootViewNativeComponent.js +19 -0
  231. package/src/getShadowNodeFromRef.ts +19 -0
  232. package/src/getShadowNodeFromRef.web.ts +7 -0
  233. package/src/handlers/FlingGestureHandler.ts +3 -1
  234. package/src/handlers/ForceTouchGestureHandler.ts +8 -2
  235. package/src/handlers/LongPressGestureHandler.ts +3 -1
  236. package/src/handlers/NativeViewGestureHandler.ts +3 -1
  237. package/src/handlers/PanGestureHandler.ts +3 -1
  238. package/src/handlers/PinchGestureHandler.ts +3 -1
  239. package/src/handlers/RotationGestureHandler.ts +3 -1
  240. package/src/handlers/TapGestureHandler.ts +3 -1
  241. package/src/handlers/createHandler.ts +69 -7
  242. package/src/handlers/createNativeWrapper.tsx +7 -1
  243. package/src/handlers/gestureHandlerCommon.ts +2 -5
  244. package/src/handlers/gestures/GestureDetector.tsx +110 -37
  245. package/src/handlers/gestures/eventReceiver.ts +23 -19
  246. package/src/handlers/gestures/forceTouchGesture.ts +1 -1
  247. package/src/handlers/gestures/gesture.ts +21 -1
  248. package/src/handlers/gestures/gestureStateManager.ts +9 -12
  249. package/src/handlers/gestures/panGesture.ts +1 -1
  250. package/src/handlers/gestures/pinchGesture.ts +1 -1
  251. package/src/handlers/gestures/reanimatedWrapper.ts +4 -1
  252. package/src/handlers/handlersRegistry.ts +44 -6
  253. package/src/index.ts +3 -0
  254. package/src/init.ts +6 -0
  255. package/src/jestUtils/index.ts +1 -0
  256. package/src/jestUtils/jestUtils.ts +506 -0
  257. package/src/utils.ts +38 -0
  258. package/android/lib/src/main/java/com/swmansion/gesturehandler/BaseGestureHandlerInteractionController.kt +0 -18
  259. package/android/lib/src/main/java/com/swmansion/gesturehandler/GestureHandlerRegistryImpl.kt +0 -21
  260. package/android/src/main/java/com/swmansion/gesturehandler/react/RNGestureHandlerPackage.kt +0 -17
@@ -0,0 +1,558 @@
1
+ /*
2
+ * Copyright (C) 2010 The Android Open Source Project
3
+ *
4
+ * Copied from https://android.googlesource.com/platform/frameworks/base/+/master/core/java/android/view/ScaleGestureDetector.java
5
+ * Modified line 189 to set initial min span to 0 instead of copying it from the system configuration
6
+ */
7
+
8
+ package com.swmansion.gesturehandler;
9
+
10
+ import android.content.Context;
11
+ import android.os.Build;
12
+ import android.os.Handler;
13
+ import android.view.GestureDetector;
14
+ import android.view.MotionEvent;
15
+ import android.view.View;
16
+ import android.view.ViewConfiguration;
17
+
18
+ /**
19
+ * Detects scaling transformation gestures using the supplied {@link MotionEvent}s.
20
+ * The {@link OnScaleGestureListener} callback will notify users when a particular
21
+ * gesture event has occurred.
22
+ *
23
+ * This class should only be used with {@link MotionEvent}s reported via touch.
24
+ *
25
+ * To use this class:
26
+ * <ul>
27
+ * <li>Create an instance of the {@code ScaleGestureDetector} for your
28
+ * {@link View}
29
+ * <li>In the {@link View#onTouchEvent(MotionEvent)} method ensure you call
30
+ * {@link #onTouchEvent(MotionEvent)}. The methods defined in your
31
+ * callback will be executed when the events occur.
32
+ * </ul>
33
+ */
34
+ public class ScaleGestureDetector {
35
+ private static final String TAG = "ScaleGestureDetector";
36
+
37
+ /**
38
+ * The listener for receiving notifications when gestures occur.
39
+ * If you want to listen for all the different gestures then implement
40
+ * this interface. If you only want to listen for a subset it might
41
+ * be easier to extend {@link SimpleOnScaleGestureListener}.
42
+ *
43
+ * An application will receive events in the following order:
44
+ * <ul>
45
+ * <li>One {@link OnScaleGestureListener#onScaleBegin(ScaleGestureDetector)}
46
+ * <li>Zero or more {@link OnScaleGestureListener#onScale(ScaleGestureDetector)}
47
+ * <li>One {@link OnScaleGestureListener#onScaleEnd(ScaleGestureDetector)}
48
+ * </ul>
49
+ */
50
+ public interface OnScaleGestureListener {
51
+ /**
52
+ * Responds to scaling events for a gesture in progress.
53
+ * Reported by pointer motion.
54
+ *
55
+ * @param detector The detector reporting the event - use this to
56
+ * retrieve extended info about event state.
57
+ * @return Whether or not the detector should consider this event
58
+ * as handled. If an event was not handled, the detector
59
+ * will continue to accumulate movement until an event is
60
+ * handled. This can be useful if an application, for example,
61
+ * only wants to update scaling factors if the change is
62
+ * greater than 0.01.
63
+ */
64
+ public boolean onScale(ScaleGestureDetector detector);
65
+
66
+ /**
67
+ * Responds to the beginning of a scaling gesture. Reported by
68
+ * new pointers going down.
69
+ *
70
+ * @param detector The detector reporting the event - use this to
71
+ * retrieve extended info about event state.
72
+ * @return Whether or not the detector should continue recognizing
73
+ * this gesture. For example, if a gesture is beginning
74
+ * with a focal point outside of a region where it makes
75
+ * sense, onScaleBegin() may return false to ignore the
76
+ * rest of the gesture.
77
+ */
78
+ public boolean onScaleBegin(ScaleGestureDetector detector);
79
+
80
+ /**
81
+ * Responds to the end of a scale gesture. Reported by existing
82
+ * pointers going up.
83
+ *
84
+ * Once a scale has ended, {@link ScaleGestureDetector#getFocusX()}
85
+ * and {@link ScaleGestureDetector#getFocusY()} will return focal point
86
+ * of the pointers remaining on the screen.
87
+ *
88
+ * @param detector The detector reporting the event - use this to
89
+ * retrieve extended info about event state.
90
+ */
91
+ public void onScaleEnd(ScaleGestureDetector detector);
92
+ }
93
+
94
+ /**
95
+ * A convenience class to extend when you only want to listen for a subset
96
+ * of scaling-related events. This implements all methods in
97
+ * {@link OnScaleGestureListener} but does nothing.
98
+ * {@link OnScaleGestureListener#onScale(ScaleGestureDetector)} returns
99
+ * {@code false} so that a subclass can retrieve the accumulated scale
100
+ * factor in an overridden onScaleEnd.
101
+ * {@link OnScaleGestureListener#onScaleBegin(ScaleGestureDetector)} returns
102
+ * {@code true}.
103
+ */
104
+ public static class SimpleOnScaleGestureListener implements OnScaleGestureListener {
105
+
106
+ public boolean onScale(ScaleGestureDetector detector) {
107
+ return false;
108
+ }
109
+
110
+ public boolean onScaleBegin(ScaleGestureDetector detector) {
111
+ return true;
112
+ }
113
+
114
+ public void onScaleEnd(ScaleGestureDetector detector) {
115
+ // Intentionally empty
116
+ }
117
+ }
118
+
119
+ private final Context mContext;
120
+ private final OnScaleGestureListener mListener;
121
+
122
+ private float mFocusX;
123
+ private float mFocusY;
124
+
125
+ private boolean mQuickScaleEnabled;
126
+ private boolean mStylusScaleEnabled;
127
+
128
+ private float mCurrSpan;
129
+ private float mPrevSpan;
130
+ private float mInitialSpan;
131
+ private float mCurrSpanX;
132
+ private float mCurrSpanY;
133
+ private float mPrevSpanX;
134
+ private float mPrevSpanY;
135
+ private long mCurrTime;
136
+ private long mPrevTime;
137
+ private boolean mInProgress;
138
+ private int mSpanSlop;
139
+ private int mMinSpan;
140
+
141
+ private final Handler mHandler;
142
+
143
+ private float mAnchoredScaleStartX;
144
+ private float mAnchoredScaleStartY;
145
+ private int mAnchoredScaleMode = ANCHORED_SCALE_MODE_NONE;
146
+
147
+ private static final long TOUCH_STABILIZE_TIME = 128; // ms
148
+ private static final float SCALE_FACTOR = .5f;
149
+ private static final int ANCHORED_SCALE_MODE_NONE = 0;
150
+ private static final int ANCHORED_SCALE_MODE_DOUBLE_TAP = 1;
151
+ private static final int ANCHORED_SCALE_MODE_STYLUS = 2;
152
+
153
+
154
+ private GestureDetector mGestureDetector;
155
+
156
+ private boolean mEventBeforeOrAboveStartingGestureEvent;
157
+
158
+ /**
159
+ * Creates a ScaleGestureDetector with the supplied listener.
160
+ * You may only use this constructor from a {@link android.os.Looper Looper} thread.
161
+ *
162
+ * @param context the application's context
163
+ * @param listener the listener invoked for all the callbacks, this must
164
+ * not be null.
165
+ *
166
+ * @throws NullPointerException if {@code listener} is null.
167
+ */
168
+ public ScaleGestureDetector(Context context, OnScaleGestureListener listener) {
169
+ this(context, listener, null);
170
+ }
171
+
172
+ /**
173
+ * Creates a ScaleGestureDetector with the supplied listener.
174
+ * @see android.os.Handler#Handler()
175
+ *
176
+ * @param context the application's context
177
+ * @param listener the listener invoked for all the callbacks, this must
178
+ * not be null.
179
+ * @param handler the handler to use for running deferred listener events.
180
+ *
181
+ * @throws NullPointerException if {@code listener} is null.
182
+ */
183
+ public ScaleGestureDetector(Context context, OnScaleGestureListener listener,
184
+ Handler handler) {
185
+ mContext = context;
186
+ mListener = listener;
187
+ final ViewConfiguration viewConfiguration = ViewConfiguration.get(context);
188
+ mSpanSlop = viewConfiguration.getScaledTouchSlop() * 2;
189
+ mMinSpan = 0; // set to zero, to allow for scaling when distance between fingers is small
190
+ mHandler = handler;
191
+ // Quick scale is enabled by default after JB_MR2
192
+ final int targetSdkVersion = context.getApplicationInfo().targetSdkVersion;
193
+ if (targetSdkVersion > Build.VERSION_CODES.JELLY_BEAN_MR2) {
194
+ setQuickScaleEnabled(true);
195
+ }
196
+ // Stylus scale is enabled by default after LOLLIPOP_MR1
197
+ if (targetSdkVersion > Build.VERSION_CODES.LOLLIPOP_MR1) {
198
+ setStylusScaleEnabled(true);
199
+ }
200
+ }
201
+
202
+ /**
203
+ * Accepts MotionEvents and dispatches events to a {@link OnScaleGestureListener}
204
+ * when appropriate.
205
+ *
206
+ * <p>Applications should pass a complete and consistent event stream to this method.
207
+ * A complete and consistent event stream involves all MotionEvents from the initial
208
+ * ACTION_DOWN to the final ACTION_UP or ACTION_CANCEL.</p>
209
+ *
210
+ * @param event The event to process
211
+ * @return true if the event was processed and the detector wants to receive the
212
+ * rest of the MotionEvents in this event stream.
213
+ */
214
+ public boolean onTouchEvent(MotionEvent event) {
215
+ mCurrTime = event.getEventTime();
216
+
217
+ final int action = event.getActionMasked();
218
+
219
+ // Forward the event to check for double tap gesture
220
+ if (mQuickScaleEnabled) {
221
+ mGestureDetector.onTouchEvent(event);
222
+ }
223
+
224
+ final int count = event.getPointerCount();
225
+ final boolean isStylusButtonDown =
226
+ (event.getButtonState() & MotionEvent.BUTTON_STYLUS_PRIMARY) != 0;
227
+
228
+ final boolean anchoredScaleCancelled =
229
+ mAnchoredScaleMode == ANCHORED_SCALE_MODE_STYLUS && !isStylusButtonDown;
230
+ final boolean streamComplete = action == MotionEvent.ACTION_UP ||
231
+ action == MotionEvent.ACTION_CANCEL || anchoredScaleCancelled;
232
+
233
+ if (action == MotionEvent.ACTION_DOWN || streamComplete) {
234
+ // Reset any scale in progress with the listener.
235
+ // If it's an ACTION_DOWN we're beginning a new event stream.
236
+ // This means the app probably didn't give us all the events. Shame on it.
237
+ if (mInProgress) {
238
+ mListener.onScaleEnd(this);
239
+ mInProgress = false;
240
+ mInitialSpan = 0;
241
+ mAnchoredScaleMode = ANCHORED_SCALE_MODE_NONE;
242
+ } else if (inAnchoredScaleMode() && streamComplete) {
243
+ mInProgress = false;
244
+ mInitialSpan = 0;
245
+ mAnchoredScaleMode = ANCHORED_SCALE_MODE_NONE;
246
+ }
247
+
248
+ if (streamComplete) {
249
+ return true;
250
+ }
251
+ }
252
+
253
+ if (!mInProgress && mStylusScaleEnabled && !inAnchoredScaleMode()
254
+ && !streamComplete && isStylusButtonDown) {
255
+ // Start of a button scale gesture
256
+ mAnchoredScaleStartX = event.getX();
257
+ mAnchoredScaleStartY = event.getY();
258
+ mAnchoredScaleMode = ANCHORED_SCALE_MODE_STYLUS;
259
+ mInitialSpan = 0;
260
+ }
261
+
262
+ final boolean configChanged = action == MotionEvent.ACTION_DOWN ||
263
+ action == MotionEvent.ACTION_POINTER_UP ||
264
+ action == MotionEvent.ACTION_POINTER_DOWN || anchoredScaleCancelled;
265
+
266
+ final boolean pointerUp = action == MotionEvent.ACTION_POINTER_UP;
267
+ final int skipIndex = pointerUp ? event.getActionIndex() : -1;
268
+
269
+ // Determine focal point
270
+ float sumX = 0, sumY = 0;
271
+ final int div = pointerUp ? count - 1 : count;
272
+ final float focusX;
273
+ final float focusY;
274
+ if (inAnchoredScaleMode()) {
275
+ // In anchored scale mode, the focal pt is always where the double tap
276
+ // or button down gesture started
277
+ focusX = mAnchoredScaleStartX;
278
+ focusY = mAnchoredScaleStartY;
279
+ if (event.getY() < focusY) {
280
+ mEventBeforeOrAboveStartingGestureEvent = true;
281
+ } else {
282
+ mEventBeforeOrAboveStartingGestureEvent = false;
283
+ }
284
+ } else {
285
+ for (int i = 0; i < count; i++) {
286
+ if (skipIndex == i) continue;
287
+ sumX += event.getX(i);
288
+ sumY += event.getY(i);
289
+ }
290
+
291
+ focusX = sumX / div;
292
+ focusY = sumY / div;
293
+ }
294
+
295
+ // Determine average deviation from focal point
296
+ float devSumX = 0, devSumY = 0;
297
+ for (int i = 0; i < count; i++) {
298
+ if (skipIndex == i) continue;
299
+
300
+ // Convert the resulting diameter into a radius.
301
+ devSumX += Math.abs(event.getX(i) - focusX);
302
+ devSumY += Math.abs(event.getY(i) - focusY);
303
+ }
304
+ final float devX = devSumX / div;
305
+ final float devY = devSumY / div;
306
+
307
+ // Span is the average distance between touch points through the focal point;
308
+ // i.e. the diameter of the circle with a radius of the average deviation from
309
+ // the focal point.
310
+ final float spanX = devX * 2;
311
+ final float spanY = devY * 2;
312
+ final float span;
313
+ if (inAnchoredScaleMode()) {
314
+ span = spanY;
315
+ } else {
316
+ span = (float) Math.hypot(spanX, spanY);
317
+ }
318
+
319
+ // Dispatch begin/end events as needed.
320
+ // If the configuration changes, notify the app to reset its current state by beginning
321
+ // a fresh scale event stream.
322
+ final boolean wasInProgress = mInProgress;
323
+ mFocusX = focusX;
324
+ mFocusY = focusY;
325
+ if (!inAnchoredScaleMode() && mInProgress && (span < mMinSpan || configChanged)) {
326
+ mListener.onScaleEnd(this);
327
+ mInProgress = false;
328
+ mInitialSpan = span;
329
+ }
330
+ if (configChanged) {
331
+ mPrevSpanX = mCurrSpanX = spanX;
332
+ mPrevSpanY = mCurrSpanY = spanY;
333
+ mInitialSpan = mPrevSpan = mCurrSpan = span;
334
+ }
335
+
336
+ final int minSpan = inAnchoredScaleMode() ? mSpanSlop : mMinSpan;
337
+ if (!mInProgress && span >= minSpan &&
338
+ (wasInProgress || Math.abs(span - mInitialSpan) > mSpanSlop)) {
339
+ mPrevSpanX = mCurrSpanX = spanX;
340
+ mPrevSpanY = mCurrSpanY = spanY;
341
+ mPrevSpan = mCurrSpan = span;
342
+ mPrevTime = mCurrTime;
343
+ mInProgress = mListener.onScaleBegin(this);
344
+ }
345
+
346
+ // Handle motion; focal point and span/scale factor are changing.
347
+ if (action == MotionEvent.ACTION_MOVE) {
348
+ mCurrSpanX = spanX;
349
+ mCurrSpanY = spanY;
350
+ mCurrSpan = span;
351
+
352
+ boolean updatePrev = true;
353
+
354
+ if (mInProgress) {
355
+ updatePrev = mListener.onScale(this);
356
+ }
357
+
358
+ if (updatePrev) {
359
+ mPrevSpanX = mCurrSpanX;
360
+ mPrevSpanY = mCurrSpanY;
361
+ mPrevSpan = mCurrSpan;
362
+ mPrevTime = mCurrTime;
363
+ }
364
+ }
365
+
366
+ return true;
367
+ }
368
+
369
+ private boolean inAnchoredScaleMode() {
370
+ return mAnchoredScaleMode != ANCHORED_SCALE_MODE_NONE;
371
+ }
372
+
373
+ /**
374
+ * Set whether the associated {@link OnScaleGestureListener} should receive onScale callbacks
375
+ * when the user performs a doubleTap followed by a swipe. Note that this is enabled by default
376
+ * if the app targets API 19 and newer.
377
+ * @param scales true to enable quick scaling, false to disable
378
+ */
379
+ public void setQuickScaleEnabled(boolean scales) {
380
+ mQuickScaleEnabled = scales;
381
+ if (mQuickScaleEnabled && mGestureDetector == null) {
382
+ GestureDetector.SimpleOnGestureListener gestureListener =
383
+ new GestureDetector.SimpleOnGestureListener() {
384
+ @Override
385
+ public boolean onDoubleTap(MotionEvent e) {
386
+ // Double tap: start watching for a swipe
387
+ mAnchoredScaleStartX = e.getX();
388
+ mAnchoredScaleStartY = e.getY();
389
+ mAnchoredScaleMode = ANCHORED_SCALE_MODE_DOUBLE_TAP;
390
+ return true;
391
+ }
392
+ };
393
+ mGestureDetector = new GestureDetector(mContext, gestureListener, mHandler);
394
+ }
395
+ }
396
+
397
+ /**
398
+ * Return whether the quick scale gesture, in which the user performs a double tap followed by a
399
+ * swipe, should perform scaling. {@see #setQuickScaleEnabled(boolean)}.
400
+ */
401
+ public boolean isQuickScaleEnabled() {
402
+ return mQuickScaleEnabled;
403
+ }
404
+
405
+ /**
406
+ * Sets whether the associates {@link OnScaleGestureListener} should receive
407
+ * onScale callbacks when the user uses a stylus and presses the button.
408
+ * Note that this is enabled by default if the app targets API 23 and newer.
409
+ *
410
+ * @param scales true to enable stylus scaling, false to disable.
411
+ */
412
+ public void setStylusScaleEnabled(boolean scales) {
413
+ mStylusScaleEnabled = scales;
414
+ }
415
+
416
+ /**
417
+ * Return whether the stylus scale gesture, in which the user uses a stylus and presses the
418
+ * button, should perform scaling. {@see #setStylusScaleEnabled(boolean)}
419
+ */
420
+ public boolean isStylusScaleEnabled() {
421
+ return mStylusScaleEnabled;
422
+ }
423
+
424
+ /**
425
+ * Returns {@code true} if a scale gesture is in progress.
426
+ */
427
+ public boolean isInProgress() {
428
+ return mInProgress;
429
+ }
430
+
431
+ /**
432
+ * Get the X coordinate of the current gesture's focal point.
433
+ * If a gesture is in progress, the focal point is between
434
+ * each of the pointers forming the gesture.
435
+ *
436
+ * If {@link #isInProgress()} would return false, the result of this
437
+ * function is undefined.
438
+ *
439
+ * @return X coordinate of the focal point in pixels.
440
+ */
441
+ public float getFocusX() {
442
+ return mFocusX;
443
+ }
444
+
445
+ /**
446
+ * Get the Y coordinate of the current gesture's focal point.
447
+ * If a gesture is in progress, the focal point is between
448
+ * each of the pointers forming the gesture.
449
+ *
450
+ * If {@link #isInProgress()} would return false, the result of this
451
+ * function is undefined.
452
+ *
453
+ * @return Y coordinate of the focal point in pixels.
454
+ */
455
+ public float getFocusY() {
456
+ return mFocusY;
457
+ }
458
+
459
+ /**
460
+ * Return the average distance between each of the pointers forming the
461
+ * gesture in progress through the focal point.
462
+ *
463
+ * @return Distance between pointers in pixels.
464
+ */
465
+ public float getCurrentSpan() {
466
+ return mCurrSpan;
467
+ }
468
+
469
+ /**
470
+ * Return the average X distance between each of the pointers forming the
471
+ * gesture in progress through the focal point.
472
+ *
473
+ * @return Distance between pointers in pixels.
474
+ */
475
+ public float getCurrentSpanX() {
476
+ return mCurrSpanX;
477
+ }
478
+
479
+ /**
480
+ * Return the average Y distance between each of the pointers forming the
481
+ * gesture in progress through the focal point.
482
+ *
483
+ * @return Distance between pointers in pixels.
484
+ */
485
+ public float getCurrentSpanY() {
486
+ return mCurrSpanY;
487
+ }
488
+
489
+ /**
490
+ * Return the previous average distance between each of the pointers forming the
491
+ * gesture in progress through the focal point.
492
+ *
493
+ * @return Previous distance between pointers in pixels.
494
+ */
495
+ public float getPreviousSpan() {
496
+ return mPrevSpan;
497
+ }
498
+
499
+ /**
500
+ * Return the previous average X distance between each of the pointers forming the
501
+ * gesture in progress through the focal point.
502
+ *
503
+ * @return Previous distance between pointers in pixels.
504
+ */
505
+ public float getPreviousSpanX() {
506
+ return mPrevSpanX;
507
+ }
508
+
509
+ /**
510
+ * Return the previous average Y distance between each of the pointers forming the
511
+ * gesture in progress through the focal point.
512
+ *
513
+ * @return Previous distance between pointers in pixels.
514
+ */
515
+ public float getPreviousSpanY() {
516
+ return mPrevSpanY;
517
+ }
518
+
519
+ /**
520
+ * Return the scaling factor from the previous scale event to the current
521
+ * event. This value is defined as
522
+ * ({@link #getCurrentSpan()} / {@link #getPreviousSpan()}).
523
+ *
524
+ * @return The current scaling factor.
525
+ */
526
+ public float getScaleFactor() {
527
+ if (inAnchoredScaleMode()) {
528
+ // Drag is moving up; the further away from the gesture
529
+ // start, the smaller the span should be, the closer,
530
+ // the larger the span, and therefore the larger the scale
531
+ final boolean scaleUp =
532
+ (mEventBeforeOrAboveStartingGestureEvent && (mCurrSpan < mPrevSpan)) ||
533
+ (!mEventBeforeOrAboveStartingGestureEvent && (mCurrSpan > mPrevSpan));
534
+ final float spanDiff = (Math.abs(1 - (mCurrSpan / mPrevSpan)) * SCALE_FACTOR);
535
+ return mPrevSpan <= mSpanSlop ? 1 : scaleUp ? (1 + spanDiff) : (1 - spanDiff);
536
+ }
537
+ return mPrevSpan > 0 ? mCurrSpan / mPrevSpan : 1;
538
+ }
539
+
540
+ /**
541
+ * Return the time difference in milliseconds between the previous
542
+ * accepted scaling event and the current scaling event.
543
+ *
544
+ * @return Time difference since the last scaling event in milliseconds.
545
+ */
546
+ public long getTimeDelta() {
547
+ return mCurrTime - mPrevTime;
548
+ }
549
+
550
+ /**
551
+ * Return the event time of the current event being processed.
552
+ *
553
+ * @return Current event time in milliseconds.
554
+ */
555
+ public long getEventTime() {
556
+ return mCurrTime;
557
+ }
558
+ }
@@ -0,0 +1,29 @@
1
+ package com.swmansion.gesturehandler.react;
2
+
3
+ import com.facebook.jni.HybridData;
4
+ import com.facebook.proguard.annotations.DoNotStrip;
5
+ import com.facebook.react.fabric.ComponentFactory;
6
+ import com.facebook.soloader.SoLoader;
7
+
8
+ @DoNotStrip
9
+ public class RNGestureHandlerComponentsRegistry {
10
+ static {
11
+ SoLoader.loadLibrary("fabricjni");
12
+ SoLoader.loadLibrary("rngesturehandler_modules");
13
+ }
14
+
15
+ @DoNotStrip private final HybridData mHybridData;
16
+
17
+ @DoNotStrip
18
+ private native HybridData initHybrid(ComponentFactory componentFactory);
19
+
20
+ @DoNotStrip
21
+ private RNGestureHandlerComponentsRegistry(ComponentFactory componentFactory) {
22
+ mHybridData = initHybrid(componentFactory);
23
+ }
24
+
25
+ @DoNotStrip
26
+ public static RNGestureHandlerComponentsRegistry register(ComponentFactory componentFactory) {
27
+ return new RNGestureHandlerComponentsRegistry(componentFactory);
28
+ }
29
+ }
@@ -0,0 +1,12 @@
1
+ package com.swmansion.gesturehandler
2
+
3
+ import com.facebook.react.bridge.ReactContext
4
+ import com.facebook.react.fabric.FabricUIManager
5
+ import com.facebook.react.uimanager.UIManagerHelper
6
+ import com.facebook.react.uimanager.common.UIManagerType
7
+ import com.facebook.react.uimanager.events.Event
8
+
9
+ fun ReactContext.dispatchEvent(event: Event<*>) {
10
+ val fabricUIManager = UIManagerHelper.getUIManager(this, UIManagerType.FABRIC) as FabricUIManager
11
+ fabricUIManager.eventDispatcher.dispatchEvent(event)
12
+ }
@@ -1,3 +1,3 @@
1
1
  <manifest xmlns:android="http://schemas.android.com/apk/res/android"
2
- package="com.swmansion.gesturehandler.react">
2
+ package="com.swmansion.gesturehandler">
3
3
  </manifest>
@@ -0,0 +1,29 @@
1
+ package com.swmansion.gesturehandler
2
+
3
+ import com.facebook.react.ReactPackage
4
+ import com.facebook.react.bridge.NativeModule
5
+ import com.facebook.react.bridge.ReactApplicationContext
6
+ import com.facebook.react.uimanager.ViewManager
7
+ import com.facebook.soloader.SoLoader
8
+ import com.swmansion.gesturehandler.BuildConfig
9
+ import com.swmansion.gesturehandler.react.RNGestureHandlerModule
10
+ import com.swmansion.gesturehandler.react.RNGestureHandlerRootViewManager
11
+ import com.swmansion.gesturehandler.react.RNGestureHandlerButtonViewManager
12
+
13
+ class RNGestureHandlerPackage : ReactPackage {
14
+ override fun createNativeModules(reactContext: ReactApplicationContext): List<NativeModule> {
15
+ if (BuildConfig.IS_NEW_ARCHITECTURE_ENABLED) {
16
+ // For Fabric, we load c++ native library here, this triggers gesture handler's
17
+ // Fabric component registration which is necessary in order to avoid asking users
18
+ // to manually add init calls in their application code.
19
+ // This should no longer be needed if RN's autolink mechanism has Fabric support
20
+ SoLoader.loadLibrary("rngesturehandler_modules")
21
+ }
22
+ return listOf<NativeModule>(RNGestureHandlerModule(reactContext))
23
+ }
24
+
25
+ override fun createViewManagers(reactContext: ReactApplicationContext) =
26
+ listOf<ViewManager<*, *>>(
27
+ RNGestureHandlerRootViewManager(),
28
+ RNGestureHandlerButtonViewManager())
29
+ }