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

Travis Griggs

10/05/2023, 5:26 PM
I don't understand why the following doesn't layout the sub boxes as (I) expected. I understand that using something like Layout() would probably be better for something like this. But I was honestly just trying to explore messing around with the .layout modifier, and thought I would build up from a simple example. But now feel like I've missed something, as it seems to ignore the layout width/height i give the box.
Copy code
@Composable
private fun ManyBoxes(rects: List<Rect>, modifier: Modifier = Modifier) {
   Box(modifier = modifier) {
      rects.forEach { rect ->
         Box(modifier = Modifier
            .layout { measurable, constraints ->
               val placeable = measurable.measure(constraints)
               layout(rect.width.roundToInt(), rect.height.roundToInt()) {
                  placeable.place(rect.left.roundToInt(), rect.top.roundToInt())
               }
            }
            .background(Color._Random)) 
      }
   }
}

@Preview(showBackground = true)
@Composable
private fun Preview() {
   ManyBoxes(rects = listOf(Rect(100f, 200f, 300f, 500f)), modifier = Modifier.size(800.dp))
}
r

romainguy

10/05/2023, 5:35 PM
You are creating a
Box
for each
Rect
but the
.layout{}
modifier you use would layout children of the
Box
, which you don’t have
t

Travis Griggs

10/05/2023, 5:46 PM
Confused then. Elsewhere, I have a little composable that is
Copy code
Box
  Text(Modifier.layout { ... })
  Slider
and I use the layout modifier on the text to adjust where it goes relative to the slider's value. That isn't modifying it's children. I don't think the Text has children, does it? Is this a case where it's working, but not for the reasons I think it is working?
r

romainguy

10/05/2023, 5:56 PM
You’re right, sorry
I got confused 🙂
t

Travis Griggs

10/05/2023, 6:10 PM
Yay, now there are 2 confused people. I appreciate the company though. I like Compose. I really do. I like that it feels more "consistent" than the amalgamation of Android stuff did otherwise. BUT, I find that it often makes me feel stupid too. It still feels very opaque to me in some regards. I'm still waiting for the "it's turtles all the way down. Get your head around turtles, and you will be able to reason about all of it"
r

romainguy

10/05/2023, 6:13 PM
It’s a natural feeling when learning a new large library/framework
t

Travis Griggs

10/05/2023, 6:15 PM
But the shape of the curve looks different for each one. This one is requiring a lot of patience for me
a

Albert Chang

10/06/2023, 4:10 AM
The size you are setting with
layout(width, height) {}
is the size of the layout itself, instead of the size of the
placeable
. The
placeable
is measured with a min size of zero (specified by
constraints
), and since it's empty, its size will be zero. If you want to force a size from the parent, measure it with
measurable.measure(Constraints.fixed(rect.width.roundToInt(), rect.height.roundToInt()))
.
🙏 1
Btw I believe using a
Canvas
will be better for this case.
👆 1
r

romainguy

10/06/2023, 4:41 AM
Or at least a Layout()