scoop:scoop.ui:0.1.0
src/layout/measure.scoop
Selected production source · src/layout/measure.scoop
1
package scoop.ui3
import scoop.graphics.Point4
import scoop.graphics.Size5
import scoop.platform.time.MonotonicInstant7
private class ChildMeasure(val node: MountedNode) {8
var measuring = false9
var skipped = false10
var hidden = false11
var constraints: Constraints? = None12
var result: NodeLayout? = None13
var origin: Point? = None14
}16
/** One synchronous measure callback; children can be remeasured before their single placement. */17
public class MeasureContext internal constructor(18
private val node: MountedNode,19
children: Array<MountedNode>,20
private val mark: MonotonicInstant21
) {22
private val root = node.root23
public val layoutDirection: LayoutDirection24
get() {25
checkActive();26
return node.layoutDirection27
}28
private var active = true29
internal var usesLayoutInput = false30
private val entries = Array<ChildMeasure>(children.size) { index ->31
ChildMeasure(children[index])32
}33
public val time: MonotonicInstant34
get() {35
checkActive();36
observeFrameTime()37
return mark38
}39
public val childCount: Long40
get() {41
checkActive();42
return entries.size43
}44
private fun checkActive() {45
root.checkThread()46
if (!active) throw IllegalStateException(Some("measure context has ended"))47
}48
public fun <T> input(): T {49
checkActive()50
usesLayoutInput = true51
return readLayoutInput<T>(node)52
}53
public fun <T> measureChild(54
index: Long,55
constraints: Constraints,56
input: LayoutInput<T>57
): LayoutMetrics {58
checkActive()59
checkLayoutInputAccess(node)60
return withLayoutInput<T, LayoutMetrics>(input) { measureChild(index, constraints) }61
}62
public fun measureChild(index: Long, constraints: Constraints): LayoutMetrics {63
checkActive()64
val entry = entries[index]65
if (entry.skipped) throw IllegalStateException(Some("child was skipped for this layout"))66
if (entry.origin != None) throw IllegalStateException(Some("child was already placed"))67
if (entry.measuring) throw IllegalStateException(Some("child measurement is reentrant"))68
when (entry.result) {69
case Some(val result) ->70
if (entry.constraints ==71
Some(constraints) &&72
result.version ==73
entry.node.layoutVersion &&74
result.matchesEnvironment(currentLayoutEnvironment())75
)76
return result.metrics77
case None -> {}78
}79
entry.result = None80
entry.constraints = Some(constraints)81
entry.measuring = true82
try {83
val result = entry.node.measure(constraints, mark)84
usesLayoutInput = usesLayoutInput || result.usesLayoutInput85
entry.result = Some(result)86
return result.metrics87
} catch (error: Throwable) {88
// A failed trial can still influence the parent's chosen layout.89
usesLayoutInput = true90
throw error91
} finally {92
entry.measuring = false93
}94
}95
public fun placeChild(index: Long, origin: Point) {96
checkActive()97
requireUi(origin.x.isFinite() && origin.y.isFinite(), "non-finite child origin")98
val entry = entries[index]99
when (entry.result) {100
case None ->101
throw IllegalStateException(102
Some("child must be measured before placement")103
)104
case Some(_) -> {}105
}106
if (entry.origin != None) throw IllegalStateException(Some("child was already placed"))107
entry.origin = Some(origin)108
}109
/** Skips measurement and placement. Hidden subtrees also leave input and accessibility on submission. */110
public fun skipChild(index: Long, hidden: Boolean = false) {111
checkActive()112
val entry = entries[index]113
if (entry.skipped || entry.measuring || entry.constraints != None || entry.origin != None)114
throw IllegalStateException(Some("child was already measured or skipped"))115
entry.skipped = true116
entry.hidden = hidden117
}118
internal fun finish(): Array<ChildLayout> {119
for (entry in entries) {120
when (entry.result) {121
case None ->122
if (!entry.skipped)123
throw IllegalStateException(124
Some("every child must be measured or explicitly skipped")125
)126
case Some(_) -> {}127
}128
}129
return Array<ChildLayout>(entries.size) { index ->130
val entry = entries[index]131
ChildLayout(entry.node, entry.result, entry.origin, entry.hidden)132
}133
}134
internal fun end() {135
active = false136
}137
}139
internal fun MountedNode.measure(constraints: Constraints, time: MonotonicInstant): NodeLayout {140
if (!root.isOpen) throw UiLayoutEnded()141
constraints.check()142
val store = root.layouts143
val environment = currentLayoutEnvironment()144
if (!sameLayoutRound(layoutRound, store.round)) store.touch(this)145
for (previous in layoutRequests)146
if (previous.version ==147
layoutVersion &&148
previous.direction ==149
layoutDirection &&150
previous.constraints ==151
constraints &&152
previous.matchesEnvironment(environment)153
)154
return previous155
val result = when (body) {156
case Empty ->157
layoutResult(158
constraints,159
LayoutMetrics(Size(constraints.minWidth, constraints.minHeight)),160
[],161
LayoutBody.Empty162
)163
case Portal(val instance) ->164
layoutResult(165
constraints,166
LayoutMetrics(Size(constraints.minWidth, constraints.minHeight)),167
[],168
LayoutBody.Portal(instance)169
)170
case Direction -> transparentLayout(constraints, time)171
case Component(_) -> transparentLayout(constraints, time)172
case Element(val instance) -> measureElement(instance, constraints, time)173
case Lazy(val instance, val items) -> measureLazy(instance, items, constraints, time)174
}175
store.add(result)176
return result177
}179
private fun MountedNode.layoutResult(180
constraints: Constraints,181
metrics: LayoutMetrics,182
children: Array<ChildLayout>,183
body: LayoutBody,184
inputs: FrameInputs = FrameInputs(),185
usesLayoutInput: Boolean = false186
): NodeLayout = NodeLayout(187
this,188
constraints,189
layoutVersion,190
layoutDirection,191
currentLayoutEnvironment(),192
root.layouts.round,193
metrics,194
children,195
body,196
inputs,197
usesLayoutInput198
)200
private fun MountedNode.transparentLayout(201
constraints: Constraints,202
time: MonotonicInstant203
): NodeLayout {204
val child = children[0L]205
val result = child.measure(constraints, time)206
return layoutResult(207
constraints,208
result.metrics,209
[ChildLayout(child, Some(result), Some(Point(0.0, 0.0)))],210
LayoutBody.Transparent,211
usesLayoutInput = result.usesLayoutInput212
)213
}215
private fun MountedNode.measureElement(216
instance: ElementInstance,217
constraints: Constraints,218
time: MonotonicInstant219
): NodeLayout {220
val context = MeasureContext(this, children, time)221
val reads = FrameReads(this, time)222
var result: ElementLayout? = None223
try {224
val measured = evaluateUi<ElementLayout>(None, Some(reads), Some(this)) {225
instance.measure(context, constraints)226
}227
result = Some(measured)228
checkLayoutMetrics(measured.metrics, constraints)229
return layoutResult(230
constraints,231
measured.metrics,232
context.finish(),233
LayoutBody.Element(measured),234
reads.finish(),235
context.usesLayoutInput236
)237
} catch (error: Throwable) {238
val failed = result239
throwUi(cleanupUi(Some(error)) { failed?.close() })240
throw error241
} finally {242
context.end()243
}244
}246
private fun MountedNode.measureLazy(247
instance: LazyInstance,248
items: LazyChildren,249
constraints: Constraints,250
time: MonotonicInstant251
): NodeLayout {252
val context = LazyMeasureContext(this, items, time)253
val reads = FrameReads(this, time)254
var result: ElementLayout? = None255
try {256
val measured = evaluateUi<ElementLayout>(None, Some(reads), Some(this)) {257
instance.measure(context, constraints)258
}259
result = Some(measured)260
checkLayoutMetrics(measured.metrics, constraints)261
val selected = context.finish()262
val children = Array<ChildLayout>(selected.size) { index ->263
val child = selected[index]264
ChildLayout(child.entry.node, Some(child.result), child.origin)265
}266
return layoutResult(267
constraints,268
measured.metrics,269
children,270
LayoutBody.Lazy(measured, selected),271
reads.finish(),272
context.usesLayoutInput273
)274
} catch (error: Throwable) {275
val failed = result276
throwUi(cleanupUi(Some(error)) { failed?.close() })277
throw error278
} finally {279
context.end()280
}281
}