Working with a custom `Layout{}` I wanted to have ...
# compose
s
Working with a custom
Layout{}
I wanted to have all children have the exact same height. But I only get once chance to measure all of them and giving them constraints. Optimally I’d like to get a measurement of all of them, get the max height among them and have them all measure with the constraints to exactly match that height. This feels like one of the things that intrinsic layouts should be achieving, but I am not quite sure how to use intrinsics in my custom
Layout {}
measurePolicy
. Any ideas of what I’m missing or an example I could look at? Currently starting off by doing something like but I can’t quite find what I want to do instead:
Copy code
Layout(
    content = content
) { measurables, constraints ->
    val width = (constraints.maxWidth / 2) - (horizontalSpacingInPx / 2) // I am placing 2 side by side
    val height = measurables.maxOf { it.maxIntrinsicHeight(width) } // What would I do here instead
    val placeables = measurables.map { measurable ->
        val halfWidthAndIntrinsicMaxHeightConstraint = constraints.copy(
            minHeight = height,
            maxHeight = height,
            maxWidth = width,
            minWidth = width
        )
        measurable.measure(halfWidthAndIntrinsicMaxHeightConstraint)
    }
a
I think your code will work. Why do you want something instead?
s
Wow yeah I replaced my
content
with a simple
Text()
and it worked, and I realized I was messing this up by doing adding a modifier on those children. I was using this before.
Copy code
.requiredHeightIn(min = 80.dp)
Replaced it with this and it now works
Copy code
.defaultMinSize(minHeight = 80.dp)
That was interesting, I don’t know why I used the former, probably because I saw the name and it looked fitting and didn’t know about defaultMinSize before 😄
t
On a side note: What you described is achievable with a SubcomposeLayout:
Analogue of
Layout
which allows to subcompose the actual content during the measuring stage for example to use the values calculated during the measurement as params for the composition of the children.
s
Yup, never delved into that, if I don’t manage to achieve what I am ultimately going for I might have to try it out. Thanks for the link!
a
There's no point in using
SubcomposeLayout
here. It doesn't break the rule that a layout can only be measured once so you have to use intrinsics anyway.
☝🏻 1
z
Intrinsics is probably indeed what you want for now. @Doris Liu is working on something called “speculative layout” that might also be useful although if intrinsics work for your case I think they would still be more efficient.
s
Yeah I am getting more familiar with the concept. It now does seem to work, apparently I was simply breaking this somehow here. Haven’t heard anything really about “speculative layout” before, is there any discussion about what it’s planning to achieve?
z
If not, i may have said too much 😅
🔍 1
Anyway, glad intrinsics are working for you
s
Haha oh no, delete all evidence now 😂
z
I guess you'll just have to… speculate 🙈
😂 3
d
This seems like a use case that the intrinsics are designed for. 🙂 The speculative layout I'm working on would allow you to speculate what the UI would look like when layout changes. That speculation happens before actual measure/layout pass, therefore making it possible to animate the layout changes. With that said, it won't be super helpful for this use case. And to Zach's point, it is doing a lot more work than intrinsics calculation.