https://kotlinlang.org logo
#compose
Title
# compose
p

Paul Woitaschek

08/01/2022, 9:29 PM
Could someone explain what is going on here? I can absolutely not make sense of how the composition works here
t

Tash

08/01/2022, 9:42 PM
Does
currentLines
ever get updated from the
onTextLayout
block? Like if you removed everything else.
p

Paul Woitaschek

08/01/2022, 9:44 PM
Yes, as you can see in the preview -it shows “extraLines=2”
c

Chris Johnson

08/01/2022, 9:57 PM
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

Alex Vanyo

08/01/2022, 10:06 PM
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

Chris Johnson

08/01/2022, 10:21 PM
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

Alex Vanyo

08/01/2022, 10:28 PM
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

Paul Woitaschek

08/02/2022, 7:07 AM
Do you have an idea how this could be done otherwise? The goal is to have a Text with a minLines
a

Alex Vanyo

08/02/2022, 7:17 PM
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?
14 Views