Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package com.swmansion.gesturehandler
import android.os.SystemClock
import android.view.MotionEvent
import android.view.View
import android.view.ViewConfiguration
import android.view.ViewGroup
import com.facebook.react.views.textinput.ReactEditText

class NativeViewGestureHandler : GestureHandler<NativeViewGestureHandler>() {
private var shouldActivateOnStart = false
Expand Down Expand Up @@ -68,6 +70,7 @@ class NativeViewGestureHandler : GestureHandler<NativeViewGestureHandler>() {
override fun onPrepare() {
when (val view = view) {
is NativeViewGestureHandlerHook -> this.hook = view
is ReactEditText -> this.hook = EditTextHook(this, view)
}
}

Expand Down Expand Up @@ -166,4 +169,42 @@ class NativeViewGestureHandler : GestureHandler<NativeViewGestureHandler>() {
*/
fun shouldCancelRootViewGestureHandlerIfNecessary() = false
}

private class EditTextHook(
private val handler: NativeViewGestureHandler,
private val editText: ReactEditText
) : NativeViewGestureHandlerHook {
private var startX = 0f
private var startY = 0f
private var touchSlopSquared: Int

init {
val vc = ViewConfiguration.get(editText.context)
touchSlopSquared = vc.scaledTouchSlop * vc.scaledTouchSlop
}

override fun afterGestureEnd(event: MotionEvent) {
if ((event.x - startX) * (event.x - startX) + (event.y - startY) * (event.y - startY) < touchSlopSquared) {
editText.requestFocusFromJS()
}
}

// recognize alongside every handler besides RootViewGestureHandler, which is a private inner class
// of RNGestureHandlerRootHelper so no explicit type checks, but its tag is always negative
// also if other handler is NativeViewGestureHandler then don't override the default implementation
override fun shouldRecognizeSimultaneously(handler: GestureHandler<*>) =
handler.tag > 0 && handler !is NativeViewGestureHandler

override fun wantsToHandleEventBeforeActivation() = true

override fun handleEventBeforeActivation(event: MotionEvent) {
handler.activate()
editText.onTouchEvent(event)

startX = event.x
startY = event.y
}

override fun shouldCancelRootViewGestureHandlerIfNecessary() = true
}
}