Vinay Gaba
03/23/2022, 8:17 PMLayout
and noticed that modifiers like weight weren't being propagated through the constraints inside the MeasurePolicy
. I'm wondering what's the best way to handle this so I don't have to manually hardcode weight calculations when I place my composables there. Adding a code snippet in the thread.Layout(content = {
leading?.let { leadingComposable ->
Box(modifier = Modifier.layoutId("leading")) {
this@BaseRowLayout.leadingComposable()
}
}
// added a weight to this composable
Box(modifier = Modifier.layoutId("content").weight(1f)) {
this@BaseRowLayout.content()
}
trailing?.let { trailingComposable ->
Box(modifier = Modifier.layoutId("trailing")) {
this@BaseRowLayout.trailingComposable()
}
}
}) { measurables, constraints ->
val contentMeasurable = measurables.find { it.layoutId == "content" }
}
Doing contentMeasureable.width
here gives me the width that it occupies, however it doesn't occupy the entire available space that the weight modifier should've caused it to do soZach Klippenstein (he/him) [MOD]
03/23/2022, 8:30 PMweight
modifier is an extension on `ColumnScope`/`RowScope`, it just gives a parent data to those measurables that Column/Row read to use in their internal calculations. It doesn’t do anything on layout nodes that aren’t directly children of Column/Row.weight
modifiers that you can read in your own layoutso I don’t have to manually hardcode weight calculationsYou’re already hard-coding these by the looks of it, just in a slightly different place
// Measure leading and trailing first so we know how much space is left for content.
val leadingPlaceable = leading?.measure(constraints)
val trailingPlaceable = trailing?.measure(constraints - leadingPlaceable?.size)
// Content takes remaining space.
val contentPlaceable =
content.measure(constraints - leadingPlaceable?.size - trailingPlaceable?.size)
I think that still reads pretty clearly.Vinay Gaba
03/23/2022, 8:53 PMYou’re already hard-coding these by the looks of it, just in a slightly different placeThis was just for the example, won't always be the case. I wanted the user to have the flexibility through the composables they are passing through the slots
Zach Klippenstein (he/him) [MOD]
03/23/2022, 8:56 PMVinay Gaba
03/23/2022, 8:59 PMZach Klippenstein (he/him) [MOD]
03/23/2022, 9:04 PMModifier.padding
is an actual layout modifier – it has its own measure policy, reads constraints, and changes them for the modified element. That should work.RowScope
? I’m looking at the one from Row
and it uses a parent data class that’s internal, so you’d have to define your own.Vinay Gaba
03/23/2022, 9:21 PMZach Klippenstein (he/him) [MOD]
03/23/2022, 9:23 PMVinay Gaba
03/23/2022, 9:24 PMBoxWithConstraint
but I wanted something more performant.
Some examples are trailing element overflow to the next line when its a wider screen, for some components it gets added to the content itself but below it, etcZach Klippenstein (he/him) [MOD]
03/23/2022, 9:27 PM