Is there a way for a newly added element to immedi...
# compose
a
Is there a way for a newly added element to immediately read the layout bounds of its parent, and what is the best way in general to read that? Code inside.
e.g.
Copy code
Box(Modifier.size(300.dp)) {
    if (showBox) {
        val parentLayoutBounds = ...
    }
}
We currently add an empty layout and attach this to it:
Copy code
private inline fun Modifier.parentBoundsInWindow(
    crossinline onBoundsChanged: (IntRect) -> Unit
) = this.onPlaced { childCoordinates ->
    childCoordinates.parentCoordinates?.let {
        val layoutPosition = it.positionInWindow().round()
        val layoutSize = it.size
        onBoundsChanged(IntRect(layoutPosition, layoutSize))
    }
}
So:
Copy code
var parentLayoutBounds: IntRect? by remember { mutableStateOf(null) }
EmptyLayout(Modifier.parentBoundsInWindow { parentLayoutBounds = it })
But this means we only know the bounds on the next recomposition (and we force a recomposition).
Hmm, maybe I just need to not read
parentLayoutBounds
in the composition
l
Yeah, if you do something with bounds in layout / draw it will be fine. As a general rule it's suspicious to need layout information in composition
2
a
Yeah, I just changed
parentLayoutBounds
to be a
MutableState
and passed that to the measure policy that needs it