Skip to content

Commit

Permalink
Detect tapping on a link only if the actual text is selected.
Browse files Browse the repository at this point in the history
This will prevent tapping on the empty space next to a link that splits through several lines from stealing the click the touch event.
  • Loading branch information
jmartinesp committed Feb 29, 2024
1 parent a7d3939 commit 67c34f7
Showing 1 changed file with 9 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -228,8 +228,16 @@ open class EditorStyledTextView : AppCompatTextView {
}

private fun findSpansForTouchEvent(event: MotionEvent): Array<out Any> {
val layout = this.layout ?: return emptyArray()
// Find selection matching the pointer coordinates
val offset = getOffsetForPosition(event.x, event.y)
return (text as? Spanned)?.getSpans<Any>(offset, offset).orEmpty()
// For links that wrap several lines, we want to avoid opening the link if the touch event
// happened on the empty space after the line wrapped.
val currentLineWidth = layout.getLineWidth(layout.getLineForOffset(offset))
return if (event.x <= currentLineWidth) {
(text as? Spanned)?.getSpans<Any>(offset, offset).orEmpty()
} else {
emptyArray()
}
}
}

0 comments on commit 67c34f7

Please sign in to comment.