Skip to content

Commit e44c585

Browse files
committed
Send touch events on Android
1 parent fa41bd1 commit e44c585

3 files changed

Lines changed: 25 additions & 5 deletions

File tree

packages/react-native-gesture-handler/android/src/main/java/com/swmansion/gesturehandler/react/RNGestureHandlerEventDispatcher.kt

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,14 +179,21 @@ class RNGestureHandlerEventDispatcher(private val reactApplicationContext: React
179179
when (handler.actionType) {
180180
GestureHandler.ACTION_TYPE_REANIMATED_WORKLET -> {
181181
// Reanimated worklet
182-
val event = RNGestureHandlerTouchEvent.obtain(handler)
182+
val event = RNGestureHandlerTouchEvent.obtain(handler, handler.actionType)
183183
sendEventForReanimated(event)
184184
}
185185
GestureHandler.ACTION_TYPE_JS_FUNCTION_NEW_API -> {
186186
// JS function, Animated.event with useNativeDriver: false with new API
187187
val data = RNGestureHandlerTouchEvent.createEventData(handler)
188188
sendEventForDeviceEvent(RNGestureHandlerEvent.EVENT_NAME, data)
189189
}
190+
GestureHandler.ACTION_TYPE_NATIVE_DETECTOR -> {
191+
val view = handler.view
192+
if (view is RNGestureHandlerDetectorView) {
193+
val event = RNGestureHandlerTouchEvent.obtain(handler, handler.actionType)
194+
view.dispatchEvent(event)
195+
}
196+
}
190197
}
191198
}
192199

packages/react-native-gesture-handler/android/src/main/java/com/swmansion/gesturehandler/react/RNGestureHandlerTouchEvent.kt

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,28 @@ import com.swmansion.gesturehandler.core.GestureHandler
1010
class RNGestureHandlerTouchEvent private constructor() : Event<RNGestureHandlerTouchEvent>() {
1111
private var extraData: WritableMap? = null
1212
private var coalescingKey: Short = 0
13-
private fun <T : GestureHandler> init(handler: T) {
13+
private var actionType = GestureHandler.ACTION_TYPE_JS_FUNCTION_NEW_API
14+
15+
private fun <T : GestureHandler> init(handler: T, actionType: Int) {
1416
val view = handler.view!!
1517
super.init(UIManagerHelper.getSurfaceId(view), view.id)
1618
extraData = createEventData(handler)
1719
coalescingKey = handler.eventCoalescingKey
20+
this.actionType = actionType
1821
}
1922

2023
override fun onDispose() {
2124
extraData = null
2225
EVENTS_POOL.release(this)
2326
}
2427

25-
override fun getEventName() = EVENT_NAME
28+
override fun getEventName() = if (actionType ==
29+
GestureHandler.ACTION_TYPE_NATIVE_DETECTOR
30+
) {
31+
NATIVE_EVENT_NAME
32+
} else {
33+
EVENT_NAME
34+
}
2635

2736
override fun canCoalesce() = true
2837

@@ -37,14 +46,15 @@ class RNGestureHandlerTouchEvent private constructor() : Event<RNGestureHandlerT
3746
const val EVENT_TOUCH_CANCELLED = 4
3847

3948
const val EVENT_NAME = "onGestureHandlerEvent"
49+
const val NATIVE_EVENT_NAME = "onGestureHandlerTouchEvent"
4050
private const val TOUCH_EVENTS_POOL_SIZE = 7 // magic
4151
private val EVENTS_POOL = Pools.SynchronizedPool<RNGestureHandlerTouchEvent>(
4252
TOUCH_EVENTS_POOL_SIZE,
4353
)
4454

45-
fun <T : GestureHandler> obtain(handler: T): RNGestureHandlerTouchEvent =
55+
fun <T : GestureHandler> obtain(handler: T, actionType: Int): RNGestureHandlerTouchEvent =
4656
(EVENTS_POOL.acquire() ?: RNGestureHandlerTouchEvent()).apply {
47-
init(handler)
57+
init(handler, actionType)
4858
}
4959

5060
fun <T : GestureHandler> createEventData(handler: T): WritableMap = Arguments.createMap().apply {

packages/react-native-gesture-handler/src/NativeDetector.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ export function NativeDetector({ gesture, children }: NativeDetectorProps) {
1717
onGestureHandlerEvent={(event) => {
1818
console.log('onGestureHandlerEvent', event.nativeEvent);
1919
}}
20+
onGestureHandlerTouchEvent={(event) => {
21+
console.log('onGestureHandlerTouchEvent', event.nativeEvent);
22+
}}
2023
// @ts-expect-error _RNGH_MODULE_ID is injected via JSI
2124
moduleId={globalThis._RNGH_MODULE_ID}
2225
handlerTags={[gesture.tag]}

0 commit comments

Comments
 (0)