ModifierNodeElement


A Modifier.Element which manages an instance of a particular Modifier.Node implementation. A given Modifier.Node implementation can only be used when a ModifierNodeElement which creates and updates that implementation is applied to a Layout.

A ModifierNodeElement should be very lightweight, and do little more than hold the information necessary to create and maintain an instance of the associated Modifier.Node type.

import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.drawscope.ContentDrawScope
import androidx.compose.ui.node.DrawModifierNode
import androidx.compose.ui.node.ModifierNodeElement
import androidx.compose.ui.platform.InspectorInfo

class Circle(var color: Color) : DrawModifierNode, Modifier.Node() {
    override fun ContentDrawScope.draw() {
        drawCircle(color)
    }
}
data class CircleElement(val color: Color) : ModifierNodeElement<Circle>() {
    override fun create() = Circle(color)

    override fun update(node: Circle) {
        node.color = color
    }

    override fun InspectorInfo.inspectableProperties() {
        name = "circle"
        properties["color"] = color
    }
}
fun Modifier.circle(color: Color) = this then CircleElement(color)
import androidx.compose.ui.Modifier
import androidx.compose.ui.node.ModifierNodeElement
import androidx.compose.ui.node.SemanticsModifierNode
import androidx.compose.ui.platform.InspectorInfo
import androidx.compose.ui.semantics.SemanticsPropertyReceiver
import androidx.compose.ui.semantics.heading

class HeadingNode : SemanticsModifierNode, Modifier.Node() {
    override fun SemanticsPropertyReceiver.applySemantics() {
        heading()
    }
}

val HeadingElement =
    object : ModifierNodeElement<HeadingNode>() {
        override fun create() = HeadingNode()

        override fun update(node: HeadingNode) {
            // Nothing to update.
        }

        override fun InspectorInfo.inspectableProperties() {
            name = "heading"
        }

        override fun hashCode(): Int = "heading".hashCode()

        override fun equals(other: Any?) = (other === this)
    }

fun Modifier.heading() = this then HeadingElement

Summary

Public constructors

Cmn

Public functions

abstract N

This will be called the first time the modifier is applied to the Layout and it should construct and return the corresponding Modifier.Node instance.

Cmn
abstract operator Boolean
equals(other: Any?)

Require equals() to be implemented.

Cmn
abstract Int

Require hashCode() to be implemented.

Cmn
open Unit

Populates an InspectorInfo object with attributes to display in the layout inspector.

Cmn
abstract Unit
update(node: N)

Called when a modifier is applied to a Layout whose inputs have changed from the previous application.

Cmn

Public properties

final Sequence<ValueElement>

The elements of a compose value.

Cmn
final String?

Use this name as the reference name shown in tools of this value if there is no explicit reference name given to the value.

Cmn
final Any?

Use this value as a readable representation of the value.

Cmn

Inherited functions

From androidx.compose.ui.Modifier
open infix Modifier
then(other: Modifier)

Concatenates this modifier with another.

Cmn
From androidx.compose.ui.Modifier.Element
open Boolean
all(predicate: (Modifier.Element) -> Boolean)

Returns true if predicate returns true for all Elements in this Modifier or if this Modifier contains no Elements.

Cmn
open Boolean
any(predicate: (Modifier.Element) -> Boolean)

Returns true if predicate returns true for any Element in this Modifier.

Cmn
open R
<R : Any?> foldIn(initial: R, operation: (Modifier.Element, R) -> R)

Accumulates a value starting with initial and applying operation to the current value and each element from outside in.

Cmn
open R
<R : Any?> foldOut(initial: R, operation: (Modifier.Element, R) -> R)

Accumulates a value starting with initial and applying operation to the current value and each element from inside out.

Cmn

Public constructors

ModifierNodeElement

<N : Modifier.Node> ModifierNodeElement()

Public functions

create

abstract fun create(): N

This will be called the first time the modifier is applied to the Layout and it should construct and return the corresponding Modifier.Node instance.

equals

abstract operator fun equals(other: Any?): Boolean

Require equals() to be implemented. Using a data class is sufficient. Singletons may implement this function with referential equality (this === other). Modifiers with no inputs may implement this function by checking the type of the other object.

hashCode

abstract fun hashCode(): Int

Require hashCode() to be implemented. Using a data class is sufficient. Singletons and modifiers with no parameters may implement this function by returning an arbitrary constant.

inspectableProperties

open fun InspectorInfo.inspectableProperties(): Unit

Populates an InspectorInfo object with attributes to display in the layout inspector. This is called by tooling to resolve the properties of this modifier. By convention, implementors should set the name to the function name of the modifier.

The default implementation will attempt to reflectively populate the inspector info with the properties declared on the subclass. It will also set the name property to the name of this instance's class by default (not the name of the modifier function). Modifier property population depends on the kotlin-reflect library. If it is not in the classpath at runtime, the default implementation of this function will populate the properties with an error message.

If you override this function and provide the properties you wish to display, you do not need to call super. Doing so may result in duplicate properties appearing in the layout inspector.

update

abstract fun update(node: N): Unit

Called when a modifier is applied to a Layout whose inputs have changed from the previous application. This function will have the current node instance passed in as a parameter, and it is expected that the node will be brought up to date.

Public properties

inspectableElements

final val inspectableElementsSequence<ValueElement>

The elements of a compose value.

nameFallback

final val nameFallbackString?

Use this name as the reference name shown in tools of this value if there is no explicit reference name given to the value. Example: a modifier in a modifier list.

valueOverride

final val valueOverrideAny?

Use this value as a readable representation of the value.