Skip to content

Fix Facsimile Hit Testing Bugs #52065

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
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
@@ -162,7 +162,7 @@ internal class PreparedLayoutTextView(context: Context) : ViewGroup(context), Re
val x = event.x.toInt()
val y = event.y.toInt()

val clickableSpan = getClickableSpanInCoords(x, y)
val clickableSpan = getSpanInCoords(x, y, ClickableSpan::class.java)

if (clickableSpan == null) {
clearSelection()
@@ -182,33 +182,53 @@ internal class PreparedLayoutTextView(context: Context) : ViewGroup(context), Re
return true
}

/**
* Get the clickable span that is at the exact coordinates
*
* @param x x-position of the click
* @param y y-position of the click
* @return a clickable span that's located where the click occurred, or: `null` if no clickable
* span was located there
*/
private fun getClickableSpanInCoords(x: Int, y: Int): ClickableSpan? {
private fun <T> getSpanInCoords(x: Int, y: Int, clazz: Class<T>): T? {
val offset = getTextOffsetAt(x, y)
if (offset < 0) {
return null
}

val spanned = text as? Spanned ?: return null

val clickableSpans = spanned.getSpans(offset, offset, ClickableSpan::class.java)
if (clickableSpans.isNotEmpty()) {
return clickableSpans[0]
val spans = spanned.getSpans(offset, offset, clazz)
if (spans.isEmpty()) {
return null
}

// When we have multiple spans marked with SPAN_EXCLUSIVE_INCLUSIVE next to each other, both
// spans are returned by getSpans
check(spans.size <= 2)
for (span in spans) {
val spanFlags = spanned.getSpanFlags(span)
val inclusiveStart =
if ((spanFlags and Spanned.SPAN_INCLUSIVE_INCLUSIVE) != 0 ||
(spanFlags and Spanned.SPAN_INCLUSIVE_EXCLUSIVE) != 0) {
spanned.getSpanStart(span)
} else {
spanned.getSpanStart(span) + 1
}
val inclusiveEnd =
if ((spanFlags and Spanned.SPAN_INCLUSIVE_INCLUSIVE) != 0 ||
(spanFlags and Spanned.SPAN_EXCLUSIVE_INCLUSIVE) != 0) {
spanned.getSpanEnd(span)
} else {
spanned.getSpanEnd(span) - 1
}

if (offset >= inclusiveStart && offset <= inclusiveEnd) {
return span
}
}

return null
}

private fun getTextOffsetAt(x: Int, y: Int): Int {
val layoutX = x - paddingLeft
val layoutY = y - (paddingTop + (preparedLayout?.verticalOffset?.roundToInt() ?: 0))

val layout = preparedLayout?.layout ?: return -1
val line = layout.getLineForVertical(y)
val line = layout.getLineForVertical(layoutY)

val left: Float
val right: Float
@@ -238,12 +258,12 @@ internal class PreparedLayoutTextView(context: Context) : ViewGroup(context), Re
right = if (rtl) layout.getParagraphRight(line).toFloat() else layout.getLineMax(line)
}

if (x < left || x > right) {
if (layoutX < left || layoutX > right) {
return -1
}

return try {
layout.getOffsetForHorizontal(line, x.toFloat())
layout.getOffsetForHorizontal(line, layoutX.toFloat())
} catch (e: ArrayIndexOutOfBoundsException) {
// This happens for bidi text on Android 7-8.
// See
@@ -285,6 +305,10 @@ internal class PreparedLayoutTextView(context: Context) : ViewGroup(context), Re
// TODO T225199534: Add support for "needsOffscreenAlphaCompositing" to Text
override fun hasOverlappingRendering(): Boolean = false

override fun reactTagForTouch(touchX: Float, touchY: Float): Int =
getSpanInCoords(touchX.roundToInt(), touchY.roundToInt(), ReactTagSpan::class.java)?.reactTag
?: id

@RequiresApi(api = Build.VERSION_CODES.UPSIDE_DOWN_CAKE)
private object Api34Utils {
private var highlightPaths: List<Path>? = null
@@ -331,21 +355,4 @@ internal class PreparedLayoutTextView(context: Context) : ViewGroup(context), Re
return spans
}
}

override fun reactTagForTouch(touchX: Float, touchY: Float): Int {
val offset = getTextOffsetAt(touchX.roundToInt(), touchY.roundToInt())
if (offset < 0) {
return id
}

val spanned = text as? Spanned ?: return id
val reactSpans = spanned.getSpans(offset, offset, ReactTagSpan::class.java)
check(reactSpans.size <= 1)

return if (reactSpans.isNotEmpty()) {
reactSpans[0].reactTag
} else {
id
}
}
}
Loading
Oops, something went wrong.