-
Notifications
You must be signed in to change notification settings - Fork 56
/
nodes.kt
167 lines (142 loc) · 3.66 KB
/
nodes.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package com.jakewharton.mosaic
import androidx.compose.runtime.AbstractApplier
internal sealed class MosaicNode {
// These two values are set by a call to `measure`.
var width = 0
var height = 0
// 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. */
var x = 0
/** Pixels down relative to parent at which this node will render. */
var y = 0
/** Measure this node (and any children) and update [width] and [height]. */
abstract fun measure()
/** Layout any children nodes and update their [x] and [y] relative to this node. */
abstract fun layout()
abstract fun renderTo(canvas: TextCanvas)
fun render(): String {
measure()
layout()
val canvas = TextSurface(width, height)
renderTo(canvas)
return canvas.toString()
}
}
internal class TextNode(initialValue: String = "") : MosaicNode() {
private var sizeInvalidated = true
var value: String = initialValue
set(value) {
field = value
sizeInvalidated = true
}
var foreground: Color? = null
var background: Color? = null
var style: TextStyle? = null
override fun measure() {
if (sizeInvalidated) {
val lines = value.split('\n')
width = lines.maxOf { it.codePointCount(0, it.length) }
height = lines.size
sizeInvalidated = false
}
}
override fun layout() {
// No children.
}
override fun renderTo(canvas: TextCanvas) {
value.split('\n').forEachIndexed { index, line ->
canvas.write(index, 0, line, foreground, background, style)
}
}
override fun toString() = "Text(\"$value\", x=$x, y=$y, width=$width, height=$height)"
}
internal class BoxNode : MosaicNode() {
val children = mutableListOf<MosaicNode>()
/** If row, otherwise column. */
var isRow = true
override fun measure() {
if (isRow) {
measureRow()
} else {
measureColumn()
}
}
private fun measureRow() {
var width = 0
var height = 0
for (child in children) {
child.measure()
width += child.width
height = maxOf(height, child.height)
}
this.width = width
this.height = height
}
private fun measureColumn() {
var width = 0
var height = 0
for (child in children) {
child.measure()
width = maxOf(width, child.width)
height += child.height
}
this.width = width
this.height = height
}
override fun layout() {
if (isRow) {
layoutRow()
} else {
layoutColumn()
}
}
private fun layoutRow() {
var childX = 0
for (child in children) {
child.x = childX
child.y = 0
child.layout()
childX += child.width
}
}
private fun layoutColumn() {
var childY = 0
for (child in children) {
child.x = 0
child.y = childY
child.layout()
childY += child.height
}
}
override fun renderTo(canvas: TextCanvas) {
for (child in children) {
val left = child.x
val top = child.y
val right = left + child.width - 1
val bottom = top + child.height - 1
child.renderTo(canvas[top..bottom, left..right])
}
}
override fun toString() = children.joinToString(prefix = "Box(", postfix = ")")
}
internal class MosaicNodeApplier(root: BoxNode) : AbstractApplier<MosaicNode>(root) {
override fun insertTopDown(index: Int, instance: MosaicNode) {
// Ignored, we insert bottom-up.
}
override fun insertBottomUp(index: Int, instance: MosaicNode) {
val boxNode = current as BoxNode
boxNode.children.add(index, instance)
}
override fun remove(index: Int, count: Int) {
val boxNode = current as BoxNode
boxNode.children.remove(index, count)
}
override fun move(from: Int, to: Int, count: Int) {
val boxNode = current as BoxNode
boxNode.children.move(from, to, count)
}
override fun onClear() {
val boxNode = root as BoxNode
boxNode.children.clear()
}
}