Skip to content

Commit cfdcadc

Browse files
committed
Remove fill workaround, shortcut calculations if there's only one line of text
1 parent 55aaf9b commit cfdcadc

File tree

3 files changed

+10
-28
lines changed

3 files changed

+10
-28
lines changed

sentry-android-replay/src/main/java/io/sentry/android/replay/util/Nodes.kt

Lines changed: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -13,28 +13,15 @@ import androidx.compose.ui.node.LayoutNode
1313
import androidx.compose.ui.text.TextLayoutResult
1414
import kotlin.math.roundToInt
1515

16-
internal class ComposeTextLayout(
17-
internal val layout: TextLayoutResult,
18-
private val hasFillModifier: Boolean,
19-
) : TextLayout {
16+
internal class ComposeTextLayout(internal val layout: TextLayoutResult) : TextLayout {
2017
override val lineCount: Int
2118
get() = layout.lineCount
2219

2320
override val dominantTextColor: Int?
2421
get() = null
2522

26-
override fun getPrimaryHorizontal(line: Int, offset: Int): Float {
27-
val horizontalPos = layout.getHorizontalPosition(offset, usePrimaryDirection = true)
28-
// when there's no `fill` modifier on a Text composable, compose still thinks that there's
29-
// one and wrongly calculates horizontal position relative to node's start, not text's start
30-
// for some reason. This is only the case for single-line text (multiline works fien).
31-
// So we subtract line's left to get the correct position
32-
return if (!hasFillModifier && lineCount == 1) {
33-
horizontalPos - layout.getLineLeft(line)
34-
} else {
35-
horizontalPos
36-
}
37-
}
23+
override fun getPrimaryHorizontal(line: Int, offset: Int) =
24+
layout.getHorizontalPosition(offset, usePrimaryDirection = true)
3825

3926
override fun getEllipsisCount(line: Int): Int = if (layout.isLineEllipsized(line)) 1 else 0
4027

@@ -92,8 +79,6 @@ internal fun Painter.isMaskable(): Boolean {
9279
!className.contains("Brush")
9380
}
9481

95-
internal data class TextAttributes(val color: Color?, val hasFillModifier: Boolean)
96-
9782
/**
9883
* This method is necessary to mask text in Compose.
9984
*
@@ -108,10 +93,9 @@ internal data class TextAttributes(val color: Color?, val hasFillModifier: Boole
10893
*
10994
* We also add special proguard rules to keep the `Text` class names and their `color` member.
11095
*/
111-
internal fun LayoutNode.findTextAttributes(): TextAttributes {
96+
internal fun LayoutNode.findColor(): Color? {
11297
val modifierInfos = getModifierInfo()
11398
var color: Color? = null
114-
var hasFillModifier = false
11599
for (index in modifierInfos.indices) {
116100
val modifier = modifierInfos[index].modifier
117101
val modifierClassName = modifier::class.java.name
@@ -127,11 +111,9 @@ internal fun LayoutNode.findTextAttributes(): TextAttributes {
127111
} catch (e: Throwable) {
128112
null
129113
}
130-
} else if (modifierClassName.contains("Fill")) {
131-
hasFillModifier = true
132114
}
133115
}
134-
return TextAttributes(color, hasFillModifier)
116+
return color
135117
}
136118

137119
/**

sentry-android-replay/src/main/java/io/sentry/android/replay/util/Views.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,8 @@ internal fun TextLayout?.getVisibleRects(
122122
paddingLeft: Int,
123123
paddingTop: Int,
124124
): List<Rect> {
125-
if (this == null) {
125+
126+
if (this == null || lineCount <= 1) {
126127
return listOf(globalRect)
127128
}
128129

sentry-android-replay/src/main/java/io/sentry/android/replay/viewhierarchy/ComposeViewHierarchyNode.kt

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ import io.sentry.SentryMaskingOptions
2323
import io.sentry.android.replay.SentryReplayModifiers
2424
import io.sentry.android.replay.util.ComposeTextLayout
2525
import io.sentry.android.replay.util.boundsInWindow
26+
import io.sentry.android.replay.util.findColor
2627
import io.sentry.android.replay.util.findPainter
27-
import io.sentry.android.replay.util.findTextAttributes
2828
import io.sentry.android.replay.util.isMaskable
2929
import io.sentry.android.replay.util.toOpaque
3030
import io.sentry.android.replay.viewhierarchy.ViewHierarchyNode.GenericViewHierarchyNode
@@ -189,11 +189,10 @@ internal object ComposeViewHierarchyNode {
189189
?.action
190190
?.invoke(textLayoutResults)
191191

192-
val (color, hasFillModifier) = node.findTextAttributes()
193192
val textLayoutResult = textLayoutResults.firstOrNull()
194193
var textColor = textLayoutResult?.layoutInput?.style?.color
195194
if (textColor?.isUnspecified == true) {
196-
textColor = color
195+
textColor = node.findColor()
197196
}
198197
val isLaidOut = textLayoutResult?.layoutInput?.style?.fontSize != TextUnit.Unspecified
199198
// TODO: support editable text (currently there's a way to get @Composable's padding only
@@ -202,7 +201,7 @@ internal object ComposeViewHierarchyNode {
202201
TextViewHierarchyNode(
203202
layout =
204203
if (textLayoutResult != null && !isEditable && isLaidOut) {
205-
ComposeTextLayout(textLayoutResult, hasFillModifier)
204+
ComposeTextLayout(textLayoutResult)
206205
} else {
207206
null
208207
},

0 commit comments

Comments
 (0)