Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit c282e6cf authored by Jordan Demeulenaere's avatar Jordan Demeulenaere
Browse files

Implement Modifier.container() using the Modifier Node API

This CL is a simple refactor of the container() modifier into the
Modifier Node API so that it can be delegated to by other Modifier
Nodes.

Bug: 400688335
Test: existing tests
Flag: com.android.systemui.scene_container
Change-Id: Iae71414833d1f3cca559ea3930b1fdeb508da9cb
parent 68d52650
Loading
Loading
Loading
Loading
+25 −7
Original line number Diff line number Diff line
@@ -22,7 +22,6 @@ import androidx.compose.runtime.mutableStateListOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.drawWithContent
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.graphics.Path
import androidx.compose.ui.graphics.drawscope.ContentDrawScope
@@ -32,7 +31,6 @@ import androidx.compose.ui.graphics.drawscope.translate
import androidx.compose.ui.graphics.layer.GraphicsLayer
import androidx.compose.ui.graphics.layer.drawLayer
import androidx.compose.ui.layout.LayoutCoordinates
import androidx.compose.ui.layout.onPlaced
import androidx.compose.ui.layout.positionInWindow
import androidx.compose.ui.modifier.ModifierLocalModifierNode
import androidx.compose.ui.node.DrawModifierNode
@@ -50,11 +48,7 @@ import androidx.compose.ui.util.fastForEach
 * The elements redirected to this container will be drawn above the content of this composable.
 */
fun Modifier.container(state: ContainerState): Modifier {
    return onPlaced { state.lastOffsetInWindow = it.positionInWindow() }
        .drawWithContent {
            drawContent()
            state.drawInOverlay(this)
        }
    return this then ContainerElement(state)
}

/**
@@ -105,6 +99,30 @@ internal interface LayerRenderer {
    fun drawInOverlay(drawScope: DrawScope)
}

private data class ContainerElement(private val state: ContainerState) :
    ModifierNodeElement<ContainerNode>() {
    override fun create(): ContainerNode {
        return ContainerNode(state)
    }

    override fun update(node: ContainerNode) {
        node.state = state
    }
}

/** A node implementing [container] that can be delegated to. */
class ContainerNode(var state: ContainerState) :
    Modifier.Node(), LayoutAwareModifierNode, DrawModifierNode {
    override fun onPlaced(coordinates: LayoutCoordinates) {
        state.lastOffsetInWindow = coordinates.positionInWindow()
    }

    override fun ContentDrawScope.draw() {
        drawContent()
        state.drawInOverlay(this)
    }
}

private data class DrawInContainerElement(
    var state: ContainerState,
    var enabled: () -> Boolean,