What is the best approach to get the bounds of a c...
# compose-android
k
What is the best approach to get the bounds of a composable in the coordinates space of another composable up the tree? e.g.: What are the bounds of
B
in the coordinates space of
Box
?
Copy code
Box(Modifier.fillMaxSize()) {
    Column(Modifier.padding(left = 100.dp)) {
        A()
        B()
    }
}
What I do is I get
LayoutCoordinates
of
Box
and
B
in their respective
onGloballyPositioned
callback and do the conversion once I have both of the
LayoutCoordinates
. There must be a better way. I thought about using
LookaheadScope
inside
Box
, because that gives me
lookaheadScopeCoordinates
during the placement of
B
. Might be overkill though?
s
If Column has the same bounds as Box, you can add a onPlaced{} modifier on your item and do .positionInParent or something like that
In your case since you have a 100.dp padding you can offset that yourself in the calculation.
k
This should have been just an example.
B
could be any indirect child of
Box
deep down the tree
j
Out of curiosity what are you trying to achieve? I do not see why the
LayoutCoordinates.parentLayoutCoordinates
does not give you exactly what you want.
k
Because that are the layout coordinates of the direct parent. I want to climb higher up the tree. You could chain
parentLayoutCoordines
but I not sure how I would know when to stop traversing
j
Do you mind sharing what you’re trying to build? Maybe there is a simpler way to achieve it.
k
Would be useful for spotlights and particle systems. I built something working for https://github.com/KlassenKonstantin/ComposePhysicsLayout but was curious how other people would approach this