From 4721c4265543a21ba78ed90d1ea608ae0c0d258b Mon Sep 17 00:00:00 2001 From: leeseonghyun21 <33210765+leeseonghyun21@users.noreply.github.com> Date: Fri, 4 Dec 2026 12:17:38 +0900 Subject: [PATCH] Treat a vertical drag that scrolls a TextInput as a native gesture on Android Signed-off-by: leeseonghyun21 <33210765+leeseonghyun21@users.noreply.github.com> --- .../react/views/textinput/ReactEditText.kt | 69 ++++++++++++++++--- 1 file changed, 61 insertions(+), 8 deletions(-) diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/textinput/ReactEditText.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/textinput/ReactEditText.kt index 90b29341832..d96f0a636bc 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/textinput/ReactEditText.kt +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/textinput/ReactEditText.kt @@ -75,6 +75,7 @@ import com.facebook.react.uimanager.ReactAccessibilityDelegate import com.facebook.react.uimanager.StateWrapper import com.facebook.react.uimanager.UIManagerHelper import com.facebook.react.uimanager.events.EventDispatcher +import com.facebook.react.uimanager.events.NativeGestureUtil import com.facebook.react.uimanager.style.BorderRadiusProp import com.facebook.react.uimanager.style.BorderStyle import com.facebook.react.uimanager.style.LogicalEdge @@ -144,6 +145,9 @@ public open class ReactEditText public constructor(context: Context) : AppCompat private var touchDownX = 0f private var touchDownY = 0f private val touchSlop = ViewConfiguration.get(context).scaledTouchSlop + private var isTouching = false + private var scrolledDuringTouch = false + private var isHandlingNativeGesture = false private var onKeyPress = false private val textAttributes: TextAttributes private var typefaceDirty = false @@ -342,6 +346,9 @@ public open class ReactEditText public constructor(context: Context) : AppCompat touchDownX = ev.x touchDownY = ev.y detectScrollMovement = true + isTouching = true + scrolledDuringTouch = false + isHandlingNativeGesture = false // Disallow parent views to intercept touch events, until we can detect if we should be // capturing these touches or not. this.parent.requestDisallowInterceptTouchEvent(true) @@ -360,15 +367,58 @@ public open class ReactEditText public constructor(context: Context) : AppCompat detectScrollMovement = false } } + + MotionEvent.ACTION_UP, + MotionEvent.ACTION_CANCEL -> isTouching = false + } + + // A drag that is handled as a native gesture (see below) must not focus the input either. + // TextView focuses an unfocused input on ACTION_UP when the drag did not scroll it, for example + // when the drag starts at the end of the text. + val cancelsRelease = ev.action == MotionEvent.ACTION_UP && isHandlingNativeGesture && !isFocused + val handled = + try { + if (cancelsRelease) { + // Ancestors keep reading `ev` after this view, so cancel a copy. + val cancelEvent = MotionEvent.obtain(ev) + cancelEvent.action = MotionEvent.ACTION_CANCEL + try { + super.onTouchEvent(cancelEvent) + } finally { + cancelEvent.recycle() + } + } else { + super.onTouchEvent(ev) + } + } catch (e: IndexOutOfBoundsException) { + // AOSP Editor can query a stale Layout line when the text shrinks during a cursor or + // selection handle drag (e.g. Editor.performCursorDrag -> DynamicLayout.getLineStart). + logSoftException(TAG, e) + true + } + + // A vertical drag that scrolls the text is a native gesture, like a drag in a ScrollView, so + // cancel the JS touch. Otherwise Pressability takes a short drag for a press and focuses the + // input on release: the input then scrolls back to the caret, undoing the drag. Wait until the + // text has scrolled, or until it cannot scroll in the drag direction: cancelling the JS touch + // earlier also stops the scroll that is about to start. + val deltaY = ev.y - touchDownY + if ( + ev.action == MotionEvent.ACTION_MOVE && + !isHandlingNativeGesture && + abs(deltaY) > touchSlop && + (canScrollVertically(1) || canScrollVertically(-1)) && + (scrolledDuringTouch || !canScrollVertically(if (deltaY < 0) 1 else -1)) + ) { + isHandlingNativeGesture = true + NativeGestureUtil.notifyNativeGestureStarted(this, ev) + } else if ( + (ev.action == MotionEvent.ACTION_UP || ev.action == MotionEvent.ACTION_CANCEL) && + isHandlingNativeGesture + ) { + NativeGestureUtil.notifyNativeGestureEnded(this, ev) } - return try { - super.onTouchEvent(ev) - } catch (e: IndexOutOfBoundsException) { - // AOSP Editor can query a stale Layout line when the text shrinks during a cursor or - // selection handle drag (e.g. Editor.performCursorDrag -> DynamicLayout.getLineStart). - logSoftException(TAG, e) - true - } + return handled } private fun canScrollInGestureDirection(deltaX: Float, deltaY: Float): Boolean { @@ -405,6 +455,9 @@ public open class ReactEditText public constructor(context: Context) : AppCompat override fun onScrollChanged(horiz: Int, vert: Int, oldHoriz: Int, oldVert: Int) { super.onScrollChanged(horiz, vert, oldHoriz, oldVert) + if (isTouching && vert != oldVert) { + scrolledDuringTouch = true + } scrollWatcher?.onScrollChanged(horiz, vert, oldHoriz, oldVert) }