ModifierLocalModifierNode


A androidx.compose.ui.Modifier.Node that is capable of consuming and providing ModifierLocal values.

This is the androidx.compose.ui.Modifier.Node equivalent of the ModifierLocalConsumer and ModifierLocalProvider interfaces.

import androidx.compose.ui.Modifier
import androidx.compose.ui.modifier.ModifierLocalModifierNode
import androidx.compose.ui.modifier.modifierLocalMapOf
import androidx.compose.ui.modifier.modifierLocalOf
import androidx.compose.ui.node.LayoutAwareModifierNode
import androidx.compose.ui.node.ModifierNodeElement
import androidx.compose.ui.platform.InspectorInfo
import androidx.compose.ui.unit.IntSize

class Logger {
    fun log(string: String) {
        println(string)
    }
}

val loggerLocal = modifierLocalOf { Logger() }

class ProvideLoggerNode(logger: Logger) : ModifierLocalModifierNode, Modifier.Node() {
    override val providedValues = modifierLocalMapOf(loggerLocal to logger)
}

data class ProvideLoggerElement(val logger: Logger) : ModifierNodeElement<ProvideLoggerNode>() {
    override fun create() = ProvideLoggerNode(logger)

    override fun update(node: ProvideLoggerNode) {
        node.provide(loggerLocal, logger)
    }

    override fun InspectorInfo.inspectableProperties() {
        name = "provideLogger"
        properties["logger"] = logger
    }
}

class SizeLoggerNode(var id: String) :
    ModifierLocalModifierNode, LayoutAwareModifierNode, Modifier.Node() {
    override fun onRemeasured(size: IntSize) {
        loggerLocal.current.log("The size of $id was $size")
    }
}

data class SizeLoggerElement(val id: String) : ModifierNodeElement<SizeLoggerNode>() {
    override fun create() = SizeLoggerNode(id)

    override fun update(node: SizeLoggerNode) {
        node.id = id
    }

    override fun InspectorInfo.inspectableProperties() {
        name = "logSize"
        properties["id"] = id
    }
}

fun Modifier.logSize(id: String) = this then SizeLoggerElement(id)
fun Modifier.provideLogger(logger: Logger) = this then ProvideLoggerElement(logger)

Summary

Public functions

open Unit
<T : Any?> provide(key: ModifierLocal<T>, value: T)

This method will cause this node to provide a new value for key.

Cmn

Public properties

open T

Read a ModifierLocal that was provided by other modifiers to the left of this modifier, or above this modifier in the layout tree.

Cmn
open ModifierLocalMap

The map of provided ModifierLocal <-> value pairs that this node is providing.

Cmn

Inherited functions

From androidx.compose.ui.node.DelegatableNode
open Unit

Invoked when the density changes for this node.

Cmn
open Unit

Invoked when the layout direction changes for this node.

Cmn

Inherited properties

From androidx.compose.ui.node.DelegatableNode
Modifier.Node

A reference of the Modifier.Node that holds this node's position in the node hierarchy.

Cmn

Public functions

provide

open fun <T : Any?> provide(key: ModifierLocal<T>, value: T): Unit

This method will cause this node to provide a new value for key. This can be called at any time on the UI thread, but in order to use this API, providedValues must be implemented and key must be a key that was included in it.

By providing this new value, any ModifierLocalModifierNode below it in the tree will read this value when reading current, until another ModifierLocalModifierNode provides a value for the same key, however, consuming ModifierLocalModifierNodes will NOT be notified that a new value was provided.

Public properties

current

open val ModifierLocal<T>.current: T

Read a ModifierLocal that was provided by other modifiers to the left of this modifier, or above this modifier in the layout tree.

providedValues

open val providedValuesModifierLocalMap

The map of provided ModifierLocal <-> value pairs that this node is providing. This value must be overridden if you are going to provide any values. It should be overridden as a field-backed property initialized with values for all of the keys that it will ever possibly provide.

By default, this property will be set to an empty map, which means that this node will only consume ModifierLocals and will not provide any new values.

If you would like to change a value provided in the map over time, you must use the provide API.