Skip to content

Commit

Permalink
Mergele main
Browse files Browse the repository at this point in the history
  • Loading branch information
m-bert committed Dec 5, 2024
2 parents da90c04 + f0604b0 commit 9ada0d6
Showing 1 changed file with 20 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import com.facebook.react.views.scroll.ReactScrollView
import com.facebook.react.views.swiperefresh.ReactSwipeRefreshLayout
import com.facebook.react.views.text.ReactTextView
import com.facebook.react.views.textinput.ReactEditText
import com.facebook.react.views.view.ReactViewGroup
import com.swmansion.gesturehandler.react.RNGestureHandlerButtonViewManager
import com.swmansion.gesturehandler.react.isScreenReaderOn

Expand Down Expand Up @@ -85,6 +86,7 @@ class NativeViewGestureHandler : GestureHandler<NativeViewGestureHandler>() {
is ReactSwipeRefreshLayout -> this.hook = SwipeRefreshLayoutHook(this, view)
is ReactScrollView -> this.hook = ScrollViewHook()
is ReactTextView -> this.hook = TextViewHook()
is ReactViewGroup -> this.hook = ReactViewGroupHook()
}
}

Expand All @@ -105,7 +107,7 @@ class NativeViewGestureHandler : GestureHandler<NativeViewGestureHandler>() {
if (state == STATE_UNDETERMINED && !hook.canBegin(event)) {
cancel()
} else {
view.onTouchEvent(event)
hook.sendTouchEvent(view, event)

if ((state == STATE_UNDETERMINED || state == STATE_BEGAN) && hook.canActivate(view)) {
activate()
Expand All @@ -123,12 +125,12 @@ class NativeViewGestureHandler : GestureHandler<NativeViewGestureHandler>() {
when {
shouldActivateOnStart -> {
tryIntercept(view, event)
view.onTouchEvent(event)
hook.sendTouchEvent(view, event)
activate()
}

tryIntercept(view, event) -> {
view.onTouchEvent(event)
hook.sendTouchEvent(view, event)
activate()
}

Expand All @@ -143,7 +145,7 @@ class NativeViewGestureHandler : GestureHandler<NativeViewGestureHandler>() {
}
}
} else if (state == STATE_ACTIVE) {
view.onTouchEvent(event)
hook.sendTouchEvent(view, event)
}
}

Expand All @@ -152,7 +154,7 @@ class NativeViewGestureHandler : GestureHandler<NativeViewGestureHandler>() {
val event = MotionEvent.obtain(time, time, MotionEvent.ACTION_CANCEL, 0f, 0f, 0).apply {
action = MotionEvent.ACTION_CANCEL
}
view!!.onTouchEvent(event)
hook.sendTouchEvent(view, event)
event.recycle()
}

Expand Down Expand Up @@ -211,6 +213,11 @@ class NativeViewGestureHandler : GestureHandler<NativeViewGestureHandler>() {
* by this one.
*/
fun shouldCancelRootViewGestureHandlerIfNecessary() = false

/**
* Passes the event down to the underlying view using the correct method.
*/
fun sendTouchEvent(view: View?, event: MotionEvent) = view?.onTouchEvent(event)
}

private class TextViewHook() : NativeViewGestureHandlerHook {
Expand Down Expand Up @@ -298,4 +305,12 @@ class NativeViewGestureHandler : GestureHandler<NativeViewGestureHandler>() {
private class ScrollViewHook : NativeViewGestureHandlerHook {
override fun shouldCancelRootViewGestureHandlerIfNecessary() = true
}

private class ReactViewGroupHook : NativeViewGestureHandlerHook {
// There are cases where a native component is wrapped with a `ReactViewGroup` (the component is rendered
// inside a `<View />` component in JS). In such cases, calling `onTouchEvent` wouldn't work as those are
// ignored by the wrapper view. Instead `dispatchTouchEvent` can be used, which causes the view to dispatch
// the event to its children.
override fun sendTouchEvent(view: View?, event: MotionEvent) = view?.dispatchTouchEvent(event)
}
}

0 comments on commit 9ada0d6

Please sign in to comment.