Skip to content
Open
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 @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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 {
Expand Down Expand Up @@ -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)
}

Expand Down