ModifierNodeElement
-
Cmn
abstract class ModifierNodeElement<N : Modifier.Node> : Modifier.Element, InspectableValue
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
| See also | |
|---|---|
Modifier.Node |
|
Modifier.Element |
Summary
Public constructors |
|
|---|---|
<N : Modifier.Node> ModifierNodeElement() |
Cmn
|
Public functions |
||
|---|---|---|
abstract N |
create()This will be called the first time the modifier is applied to the Layout and it should construct and return the corresponding |
Cmn
|
abstract operator Boolean |
Require equals() to be implemented. |
Cmn
|
abstract Int |
hashCode()Require hashCode() to be implemented. |
Cmn
|
open Unit |
Populates an |
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 |
||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
Public constructors
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.
Public properties
inspectableElements
final val inspectableElements: Sequence<ValueElement>
The elements of a compose value.
nameFallback
final val nameFallback: 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. Example: a modifier in a modifier list.
valueOverride
final val valueOverride: Any?
Use this value as a readable representation of the value.