Rename render to draw

I want rendering to be a different step which aggregates canvases together into the final output.
This commit is contained in:
Jake Wharton
2023-02-21 22:18:22 -05:00
committed by Jake Wharton
parent 1cf9e2e2a0
commit 5383798ea6
3 changed files with 29 additions and 29 deletions

View File

@ -16,30 +16,30 @@ public fun <T> Static(
items: Flow<T>, items: Flow<T>,
content: @Composable (T) -> Unit, content: @Composable (T) -> Unit,
) { ) {
class Item(val value: T, var rendered: Boolean) class Item(val value: T, var drawn: Boolean)
// Keep list of items which have not yet been rendered. // Keep list of items which have not yet been drawn.
val pending = remember { mutableStateListOf<Item>() } val pending = remember { mutableStateListOf<Item>() }
LaunchedEffect(items) { LaunchedEffect(items) {
items.collect { items.collect {
pending.add(Item(it, rendered = false)) pending.add(Item(it, drawn = false))
} }
} }
ComposeNode<StaticNode, MosaicNodeApplier>( ComposeNode<StaticNode, MosaicNodeApplier>(
factory = { factory = {
StaticNode { StaticNode {
pending.removeAll { it.rendered } pending.removeAll { it.drawn }
} }
}, },
update = {}, update = {},
content = { content = {
for (item in pending) { for (item in pending) {
Row { Row {
// Render item and mark it as having been included in render. // Draw item and mark it as such.
content(item.value) content(item.value)
item.rendered = true item.drawn = true
} }
} }
}, },
@ -47,7 +47,7 @@ public fun <T> Static(
} }
internal class StaticNode( internal class StaticNode(
private val postRender: () -> Unit, private val onPostDraw: () -> Unit,
) : ContainerNode() { ) : ContainerNode() {
// Delegate container column for static content. // Delegate container column for static content.
private val box = LinearNode(isRow = false) private val box = LinearNode(isRow = false)
@ -63,25 +63,25 @@ internal class StaticNode(
// Not visible. // Not visible.
} }
override fun renderTo(canvas: TextCanvas) { override fun drawTo(canvas: TextCanvas) {
// No content. // No content.
} }
override fun renderStatics(): List<TextCanvas> { override fun drawStatics(): List<TextCanvas> {
val statics = mutableListOf<TextCanvas>() val statics = mutableListOf<TextCanvas>()
// Render contents of static node to a separate display. // Draw contents of static node to a separate node hierarchy.
val static = box.render() val static = box.draw()
// Add display canvas to static canvases if it is not empty. // Add display canvas to static canvases if it is not empty.
if (static.width > 0 && static.height > 0) { if (static.width > 0 && static.height > 0) {
statics.add(static) statics.add(static)
} }
// Propagate any static content of the display. // Propagate any static content of this static node.
statics.addAll(box.renderStatics()) statics.addAll(box.drawStatics())
postRender() onPostDraw()
return statics return statics
} }

View File

@ -51,8 +51,8 @@ public suspend fun runMosaic(body: suspend MosaicScope.() -> Unit): Unit = corou
hasFrameWaiters = false hasFrameWaiters = false
clock.sendFrame(0L) // Frame time value is not used by Compose runtime. clock.sendFrame(0L) // Frame time value is not used by Compose runtime.
val canvas = rootNode.render() val canvas = rootNode.draw()
val statics = rootNode.renderStatics() val statics = rootNode.drawStatics()
output.display(canvas, statics) output.display(canvas, statics)
displaySignal?.complete(Unit) displaySignal?.complete(Unit)

View File

@ -9,26 +9,26 @@ internal sealed class MosaicNode {
var height = 0 var height = 0
// These two values are set by a call to `layout` on the parent node. // These two values are set by a call to `layout` on the parent node.
/** Pixels right relative to parent at which this node will render. */ /** Pixels right relative to parent at which this node will draw. */
var x = 0 var x = 0
/** Pixels down relative to parent at which this node will render. */ /** Pixels down relative to parent at which this node will draw. */
var y = 0 var y = 0
/** Measure this node (and any children) and update [width] and [height]. */ /** Measure this node (and any children) and update [width] and [height]. */
abstract fun measure() abstract fun measure()
/** Layout any children nodes and update their [x] and [y] relative to this node. */ /** Layout any children nodes and update their [x] and [y] relative to this node. */
abstract fun layout() abstract fun layout()
abstract fun renderTo(canvas: TextCanvas) abstract fun drawTo(canvas: TextCanvas)
fun render(): TextCanvas { fun draw(): TextCanvas {
measure() measure()
layout() layout()
val canvas = TextSurface(width, height) val canvas = TextSurface(width, height)
renderTo(canvas) drawTo(canvas)
return canvas return canvas
} }
abstract fun renderStatics(): List<TextCanvas> abstract fun drawStatics(): List<TextCanvas>
} }
internal class TextNode(initialValue: String = "") : MosaicNode() { internal class TextNode(initialValue: String = "") : MosaicNode() {
@ -56,13 +56,13 @@ internal class TextNode(initialValue: String = "") : MosaicNode() {
// No children. // No children.
} }
override fun renderTo(canvas: TextCanvas) { override fun drawTo(canvas: TextCanvas) {
value.split('\n').forEachIndexed { index, line -> value.split('\n').forEachIndexed { index, line ->
canvas.write(index, 0, line, foreground, background, style) canvas.write(index, 0, line, foreground, background, style)
} }
} }
override fun renderStatics() = emptyList<TextCanvas>() override fun drawStatics() = emptyList<TextCanvas>()
override fun toString() = "Text(\"$value\", x=$x, y=$y, width=$width, height=$height)" override fun toString() = "Text(\"$value\", x=$x, y=$y, width=$width, height=$height)"
} }
@ -134,22 +134,22 @@ internal class LinearNode(var isRow: Boolean = true) : ContainerNode() {
} }
} }
override fun renderTo(canvas: TextCanvas) { override fun drawTo(canvas: TextCanvas) {
for (child in children) { for (child in children) {
if (child.width != 0 && child.height != 0) { if (child.width != 0 && child.height != 0) {
val left = child.x val left = child.x
val top = child.y val top = child.y
val right = left + child.width - 1 val right = left + child.width - 1
val bottom = top + child.height - 1 val bottom = top + child.height - 1
child.renderTo(canvas[top..bottom, left..right]) child.drawTo(canvas[top..bottom, left..right])
} else { } else {
child.renderTo(canvas.empty()) child.drawTo(canvas.empty())
} }
} }
} }
override fun renderStatics(): List<TextCanvas> { override fun drawStatics(): List<TextCanvas> {
return children.flatMap(MosaicNode::renderStatics) return children.flatMap(MosaicNode::drawStatics)
} }
override fun toString() = children.joinToString(prefix = "Box(", postfix = ")") override fun toString() = children.joinToString(prefix = "Box(", postfix = ")")