Could someone explain what is going on here? I can...
# compose
p
Could someone explain what is going on here? I can absolutely not make sense of how the composition works here
t
Does
currentLines
ever get updated from the
onTextLayout
block? Like if you removed everything else.
p
Yes, as you can see in the preview -it shows “extraLines=2”
c
I think you found a bug(limit) with preview + onTextLayout potentially. If I run this code in an app, I get extraLines = 1 and it works as I would've expected it to. If I run it in preview I get what you get
a
A case like this is a recomposition loop, which you generally want to avoid: https://developer.android.com/jetpack/compose/phases#recomp-loop
If I run this code in an app, I get extraLines = 1 and it works as I would’ve expected it to.
In an app, you will likely see what is displayed in the preview for a single frame, and then on the following frame, you’ll see what you expect. The preview is only showing that first frame, where
currentLines
is
0
.
Copy code
@Preview
@Composable
fun Repro() {
    var show by remember { mutableStateOf(false) }
    if (show) {
        TextRepeatEmptyLines(text = "Hey", lines = 2)
    }
    LaunchedEffect(Unit) {
        delay(1000)
        show = true
    }
}
That should make the single frame a bit more obvious if you run it on a device, by delaying it 1 second after launch
c
I haven't seen this article on the developer docs yet, wow. This actually kind of addresses a lot of problems I've been dealing with. Quick question for you Alex! In the article they recommend using a "layout primitive" to solve this cyclic problem of changing state via
onSizeChanged
. I'm not 100% sure what a layout primitive is. Is it just a
Column, Box, Row
?
Edit: Got it 😅
a
Right, start with built in elements like
Column
,
Box
,
Row
and if you need to do something more custom that isn’t supported with those (or a combination), then go a level deeper and use
Modifier.layout
or
Layout
(custom layouts describes those in detail)
p
Do you have an idea how this could be done otherwise? The goal is to have a Text with a minLines
a
I think https://issuetracker.google.com/issues/122476634 is the tracking issue for something built-in, but in the meantime
Paragraph
is the utility for measuring the size of text directly. That’s how
maxLines
is implemented: https://cs.android.com/androidx/platform/frameworks/support/+/androidx-main:compose/[…]in/androidx/compose/foundation/text/MaxLinesHeightModifier.kt It should be possible to fork a version of that for
minLines
pretty easily?