Fix links not working when message is partially offscreen

This commit is contained in:
Andrew 2023-05-11 22:58:12 +09:30 committed by GitHub
commit e6dc5f2128
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -38,13 +38,12 @@ object TextUtilities {
fun TextView.getIntersectedModalSpans(hitRect: Rect): List<ModalURLSpan> { fun TextView.getIntersectedModalSpans(hitRect: Rect): List<ModalURLSpan> {
val textLayout = layout ?: return emptyList() val textLayout = layout ?: return emptyList()
val lineRect = Rect() val lineRect = Rect()
val bodyTextRect = Rect() val offset = intArrayOf(0, 0).also { getLocationOnScreen(it) }
getGlobalVisibleRect(bodyTextRect)
val textSpannable = text.toSpannable() val textSpannable = text.toSpannable()
return (0 until textLayout.lineCount).flatMap { line -> return (0 until textLayout.lineCount).flatMap { line ->
textLayout.getLineBounds(line, lineRect) textLayout.getLineBounds(line, lineRect)
lineRect.offset(bodyTextRect.left + totalPaddingLeft, bodyTextRect.top + totalPaddingTop) lineRect.offset(offset[0] + totalPaddingLeft, offset[1] + totalPaddingTop)
if ((Rect(lineRect)).contains(hitRect)) { if (lineRect.contains(hitRect)) {
// calculate the url span intersected with (if any) // calculate the url span intersected with (if any)
val off = textLayout.getOffsetForHorizontal(line, hitRect.left.toFloat()) // left and right will be the same val off = textLayout.getOffsetForHorizontal(line, hitRect.left.toFloat()) // left and right will be the same
textSpannable.getSpans<ModalURLSpan>(off, off).toList() textSpannable.getSpans<ModalURLSpan>(off, off).toList()