From a9d7e7e5ab9b402d4d0921c4496c223bdc99a2ac Mon Sep 17 00:00:00 2001 From: Jorge Martin Espinosa Date: Wed, 7 Oct 2026 15:10:19 +0200 Subject: [PATCH] Fix multi-line split inline code background drawing (#7875) In the new timeline renderer, the line end position is sometimes incorrectly returned when you pass the offset of the last character in the line, returning `0.0f`. This caused the shape to draw for the line to be reversed, where the start was `0.0f` and the end was the actual start. When we detect a `0.0f` returned value for this, just get the coordinates for the end of the line instead. --- .../htmlrenderer/api/ui/HtmlMessageContent.kt | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/libraries/htmlrenderer/api/src/main/kotlin/io/element/android/libraries/htmlrenderer/api/ui/HtmlMessageContent.kt b/libraries/htmlrenderer/api/src/main/kotlin/io/element/android/libraries/htmlrenderer/api/ui/HtmlMessageContent.kt index 3c464a2ea8..e779b5086f 100644 --- a/libraries/htmlrenderer/api/src/main/kotlin/io/element/android/libraries/htmlrenderer/api/ui/HtmlMessageContent.kt +++ b/libraries/htmlrenderer/api/src/main/kotlin/io/element/android/libraries/htmlrenderer/api/ui/HtmlMessageContent.kt @@ -70,6 +70,7 @@ import androidx.compose.ui.text.TextStyle import androidx.compose.ui.text.buildAnnotatedString import androidx.compose.ui.text.font.FontFamily import androidx.compose.ui.text.rememberTextMeasurer +import androidx.compose.ui.text.style.ResolvedTextDirection import androidx.compose.ui.text.style.TextAlign import androidx.compose.ui.unit.Density import androidx.compose.ui.unit.Dp @@ -513,7 +514,7 @@ private fun Modifier.drawInlineCodeBackgrounds( val lineEnd = minOf(range.end, layout.getLineEnd(line, visibleEnd = true)) if (lineEnd <= lineStart) continue val startX = layout.getHorizontalPosition(lineStart, usePrimaryDirection = true) - val endX = layout.getHorizontalPosition(lineEnd, usePrimaryDirection = true) + val endX = layout.getHorizontalPosition(lineEnd, usePrimaryDirection = true).takeUnless { it == 0f } ?: layout.getLineEndPosition(line) val left = minOf(startX, endX) - horizontalPadding val right = maxOf(startX, endX) + horizontalPadding val topLeft = Offset(left, layout.getLineTop(line)) @@ -540,6 +541,16 @@ private fun Modifier.drawInlineCodeBackgrounds( } } +private fun TextLayoutResult.getLineEndPosition(line: Int): Float { + val lineEnd = getLineEnd(line, visibleEnd = true) + val direction = getParagraphDirection(lineEnd) + return if (direction == ResolvedTextDirection.Ltr) { + getLineRight(line) + } else { + getLineLeft(line) + } +} + /** * Detects taps and long presses on link-annotated ranges ([LINK_ANNOTATION_TAG]) without consuming the touch events if none are detected, and invokes the * appropriate callbacks in [context].