GlobalPositionAwareModifierNode

Interface

Common
interface GlobalPositionAwareModifierNode : DelegatableNode

A androidx.compose.ui.Modifier.Node whose onGloballyPositioned is called with the final LayoutCoordinates of the Layout when the global position of the content may have changed. Note that it will be called after a composition when the coordinates are finalized.

This is the androidx.compose.ui.Modifier.Node equivalent of androidx.compose.ui.layout.OnGloballyPositionedModifier

Functions

fun onGloballyPositioned(coordinates: LayoutCoordinates)

Called with the final LayoutCoordinates of the Layout after measuring. Note that it will be called after a composition when the coordinates are finalized. The position in the modifier chain makes no difference in either the LayoutCoordinates argument or when the onGloballyPositioned is called.

Code Examples

OnGloballyPositioned

@Composable
fun OnGloballyPositioned() {
    Column(
        Modifier.onGloballyPositioned { coordinates ->
            // This will be the size of the Column.
            coordinates.size
            // The position of the Column relative to the application window.
            coordinates.positionInWindow()
            // The position of the Column relative to the Compose root.
            coordinates.positionInRoot()
            // These will be the alignment lines provided to the layout (empty here for Column).
            coordinates.providedAlignmentLines
            // This will be a LayoutCoordinates instance corresponding to the parent of Column.
            coordinates.parentLayoutCoordinates
        }
    ) {
        Box(Modifier.size(20.dp).background(Color.Green))
        Box(Modifier.size(20.dp).background(Color.Blue))
    }
}

GlobalPositionAwareModifierNodeSample

@Composable
fun GlobalPositionAwareModifierNodeSample() {
    class PositionLoggerNode(var id: String) : GlobalPositionAwareModifierNode, Modifier.Node() {
        override fun onGloballyPositioned(coordinates: LayoutCoordinates) {
            // This will be the size of the Layout.
            coordinates.size
            // The position of the Layout relative to the application window.
            coordinates.positionInWindow()
            // The position of the Layout relative to the Compose root.
            coordinates.positionInRoot()
            // These will be the alignment lines provided to the Layout
            coordinates.providedAlignmentLines
            // This will be a LayoutCoordinates instance corresponding to the parent of the Layout.
            coordinates.parentLayoutCoordinates
        }
    }
    data class PositionLoggerElement(val id: String) : ModifierNodeElement<PositionLoggerNode>() {
        override fun create() = PositionLoggerNode(id)
        override fun update(node: PositionLoggerNode) {
            node.id = id
        }
        override fun InspectorInfo.inspectableProperties() {
            name = "logPosition"
            properties["id"] = id
        }
    }
    fun Modifier.logPosition(id: String) = this then PositionLoggerElement(id)
}