@@ -27,6 +27,7 @@ import androidx.core.view.children
2727import androidx.interpolator.view.animation.FastOutSlowInInterpolator
2828import com.facebook.react.R
2929import com.facebook.react.bridge.Dynamic
30+ import com.facebook.react.bridge.ReactContext
3031import com.facebook.react.bridge.ReadableArray
3132import com.facebook.react.module.annotations.ReactModule
3233import com.facebook.react.uimanager.BackgroundStyleApplicator
@@ -35,6 +36,7 @@ import com.facebook.react.uimanager.PixelUtil
3536import com.facebook.react.uimanager.PointerEvents
3637import com.facebook.react.uimanager.ReactPointerEventsView
3738import com.facebook.react.uimanager.ThemedReactContext
39+ import com.facebook.react.uimanager.UIManagerHelper
3840import com.facebook.react.uimanager.ViewGroupManager
3941import com.facebook.react.uimanager.ViewManagerDelegate
4042import com.facebook.react.uimanager.ViewProps
@@ -48,6 +50,7 @@ import com.swmansion.gesturehandler.core.GestureHandler
4850import com.swmansion.gesturehandler.core.HoverGestureHandler
4951import com.swmansion.gesturehandler.core.NativeViewGestureHandler
5052import com.swmansion.gesturehandler.react.RNGestureHandlerButtonViewManager.ButtonViewGroup
53+ import com.swmansion.gesturehandler.react.events.RNGestureHandlerButtonEvent
5154
5255@ReactModule(name = RNGestureHandlerButtonViewManager .REACT_CLASS )
5356class RNGestureHandlerButtonViewManager :
@@ -63,6 +66,11 @@ class RNGestureHandlerButtonViewManager :
6366
6467 public override fun createViewInstance (context : ThemedReactContext ) = ButtonViewGroup (context)
6568
69+ @ReactProp(name = " hasLongPressHandler" )
70+ override fun setHasLongPressHandler (view : ButtonViewGroup , hasLongPressHandler : Boolean ) {
71+ view.hasLongPressHandler = hasLongPressHandler
72+ }
73+
6674 @ReactProp(name = " foreground" )
6775 override fun setForeground (view : ButtonViewGroup , useDrawableOnForeground : Boolean ) {
6876 view.useDrawableOnForeground = useDrawableOnForeground
@@ -398,6 +406,7 @@ class RNGestureHandlerButtonViewManager :
398406 var useBorderlessDrawable = false
399407
400408 var exclusive = true
409+ var hasLongPressHandler = false
401410 var tapAnimationInDuration: Int = 50
402411 var tapAnimationOutDuration: Int = 100
403412 var longPressDuration: Int = - 1
@@ -534,9 +543,9 @@ class RNGestureHandlerButtonViewManager :
534543 }
535544
536545 if (handler.isWithinBounds) {
537- dispatchJSEvent(EventType .PressIn )
546+ dispatchJSEvent(EventType .PressIn , handler )
538547 } else {
539- dispatchJSEvent(EventType .PressOut )
548+ dispatchJSEvent(EventType .PressOut , handler )
540549
541550 pendingLongPress?.let {
542551 this .handler?.removeCallbacks(it)
@@ -551,24 +560,26 @@ class RNGestureHandlerButtonViewManager :
551560 val localLastEventWasInside = lastEventWasInside
552561
553562 if (newState == GestureHandler .STATE_BEGAN ) {
554- dispatchJSEvent(EventType .PressIn )
563+ dispatchJSEvent(EventType .PressIn , handler)
564+ longPressDetected = false
555565
556- val runnable = Runnable {
557- pendingLongPress = null
558- longPressDetected = true
559- dispatchJSEvent(EventType .LongPress )
566+ if (hasLongPressHandler && longPressDuration >= 0 ) {
567+ val runnable = Runnable {
568+ pendingLongPress = null
569+ longPressDetected = true
570+ dispatchJSEvent(EventType .LongPress , handler)
571+ }
572+ pendingLongPress = runnable
573+ this .handler?.postDelayed(runnable, longPressDuration.toLong())
560574 }
561- longPressDetected = false
562- pendingLongPress = runnable
563- this .handler?.postDelayed(runnable, longPressDuration.toLong())
564575 }
565576
566577 if (newState == GestureHandler .STATE_END ||
567578 newState == GestureHandler .STATE_FAILED ||
568579 newState == GestureHandler .STATE_CANCELLED
569580 ) {
570581 if (localLastEventWasInside) {
571- dispatchJSEvent(EventType .PressOut )
582+ dispatchJSEvent(EventType .PressOut , handler )
572583 }
573584
574585 pendingLongPress?.let {
@@ -578,33 +589,26 @@ class RNGestureHandlerButtonViewManager :
578589 }
579590
580591 if (newState == GestureHandler .STATE_END && ! longPressDetected && localLastEventWasInside) {
581- dispatchJSEvent(EventType .Press )
592+ dispatchJSEvent(EventType .Press , handler )
582593 }
583594
584595 if (newState == GestureHandler .STATE_END ||
585596 newState == GestureHandler .STATE_FAILED ||
586597 newState == GestureHandler .STATE_CANCELLED
587598 ) {
588- dispatchJSEvent(EventType .InteractionFinished )
599+ dispatchJSEvent(EventType .InteractionFinished , handler )
589600 }
590601 }
591602
592- private fun dispatchJSEvent (type : EventType ) {
593- when (type) {
594- EventType .Press -> {
595- Log .w(" RNGH" , " onPress" )
596- }
597- EventType .PressIn -> {
598- lastEventWasInside = true
599- Log .w(" RNGH" , " onPressIn" )
600- }
601- EventType .PressOut -> {
602- lastEventWasInside = false
603- Log .w(" RNGH" , " onPressOut" )
604- }
605- EventType .LongPress -> {
606- Log .w(" RNGH" , " longPress" )
607- }
603+ private fun dispatchJSEvent (type : EventType , handler : NativeViewGestureHandler ) {
604+ val reactContext = context as ? ReactContext ? : return
605+ val eventDispatcher = UIManagerHelper .getEventDispatcher(reactContext) ? : return
606+ eventDispatcher.dispatchEvent(RNGestureHandlerButtonEvent .obtain(this , handler, type))
607+
608+ if (type == EventType .PressIn ) {
609+ lastEventWasInside = true
610+ } else if (type == EventType .PressOut ) {
611+ lastEventWasInside = false
608612 }
609613 }
610614
@@ -1199,7 +1203,8 @@ class RNGestureHandlerButtonViewManager :
11991203 Press ,
12001204 PressIn ,
12011205 PressOut ,
1202- LongPress
1206+ LongPress ,
1207+ InteractionFinished ,
12031208 }
12041209 }
12051210
0 commit comments