I need to know the global position of a Composable...
# compose
m
I need to know the global position of a Composable in order to choose how to draw it, i.e. what
onGloballyPositioned
returns, but I need it before it's placed. I tried
SubcomposeLayout
, but it only allows me to measure the content itself on the spot, not where it would be drawn. How do I get the "current" global position in my Composable?
a
PlacementScope.coordinates is probably the tool you want: it gives the ability to check the global coordinates of a layout as it is being placed. The ordering of phases can be a bit tricky to get correct without introducing a composition loop or an incorrect frame. You can delay measuring a child until the placement step when you have the
coordinates
available, but then that means the child’s measurement can’t influence the size of the parent (but its intrinsic size can, and that might be enough)
m
I want to display a tooltip anchored to the right if it's attached to something on the left half of the window, or to the left if it's attached to something on the right half. Since it's a floating tooltip, it doesn't influence any parent size anyway.
Hmm,
PlacementScope.coordinates
does allow me to get the info before choosing where to display it, but I don't have the ability to change what is displayed. I need to display the tooltip's anchor point triangle on the left or right of it depending on the outcome.
I guess I could pass both versions to
Layout
and then only display one of the two placeables. But that feels a bit hacky.
👍 1
a
If changing the side of the triangle is only a part of drawing, then since drawing happens after placement you should be able to decide what to do based on reading
PlacementScope.coordinates
and doing the drawing logic after that. Otherwise, if it has to be choosing between two different
@Composable
s based on the side, then passing both versions and only placing one is probably your best bet.
m
The fact the tooltip has to be a
Popup
threw a wrench into this, since I can't control the
Popup
position from a custom layout, it does it's own thing ignoring where I
place(x, y)
it.
a
Ah for tooltips with
Popup
specifically there’s a
PopupPositionProvider
which is involved
m
I can probably achieve the positioning behavior I want with the
PopupPositionProvider
alone, but then I'm back to being unable to control which anchor triangle image I display and where.
a
I’m not sure what the ordering guarantees are for when the
PopupPositionProvider.calculatePosition
is called, maybe you could try a custom one to know where it will be put, and update some state based on that and see if that works?
m
Thanks for the help, I've got it working with a custom
PopupPositionProvider
that in addition to the popup position also sets some state to control the anchor triangle type and position. The issue with this approach is that I only have that state set on the second frame, and the triangle image shows up 1 frame delayed after the actual popup which is noticeable. I solved that by setting the
alpha
to 0 on the popup until I have the anchor info so they appear together on the second frame.