scoop:scoop.ui:0.1.0
src/runtime/node.scoop
Selected production source · src/runtime/node.scoop
1
package scoop.ui3
import scoop.collections.HashMap5
internal enum NodeBody {6
Empty, Component(ComponentInstance), Element(ElementInstance), Portal(PortalInstance),7
Lazy(LazyInstance, LazyChildren), Direction8
}10
internal class UpdateTurn12
/** Shared only by the dependency scan preceding layout and scene callbacks. */13
internal class FrameInvalidationBatch15
internal class MountedNode(16
val root: UiRoot,17
val parent: MountedNode?,18
var view: View19
): StateInvalidationTarget {20
val depth: Long = (parent?.depth ?: -1L) + 1L21
var layoutDirection: LayoutDirection =22
view.directionOverride ?: parent?.layoutDirection ?: LayoutDirection.Ltr23
val scope = ComponentScope(this)24
val paintLayers = PaintLayers()25
val focusTargets = ArrayList<FocusTarget>()26
val semanticHandles = ArrayList<SemanticsHandle>()27
var semanticRoute: SemanticsHandle? = None28
var lazySemantics: SemanticsHandle? = None29
var semanticItem: SemanticsHandle? = None30
var focusScope: FocusScope? = None31
var navigation: KeyNavigation? = None32
var lazyFocus: LazyFocus? = None33
var children: Array<MountedNode> = []34
var active = true35
var mounted = false36
var retired = false37
var queued = false38
var updated: UpdateTurn? = None39
val dependencies = HashMap<StateSignalId, StateSubscription>()40
var inputChanged = true41
var stateDirty = false42
var layoutVersion = 0L43
var sceneVersion = 0L44
private var layoutBatch: FrameInvalidationBatch? = None45
private var sceneBatch: FrameInvalidationBatch? = None46
val frameInputs = NodeFrameInputs(this)47
var layoutRound: LayoutRound? = None48
var currentLayout: NodeLayout? = None49
val layoutRequests = ArrayList<NodeLayout>()50
private var framesAllowed = true51
val localFrameEnabled: Boolean get() = framesAllowed52
var hiddenByLayout = false53
var layoutConcealed: Boolean = parent?.layoutConcealed ?: false54
private var participates = true55
val participatesInFrame: Boolean get() = participates56
var frameEnabled: Boolean = parent?.frameEnabled ?: true58
val lazyItems: LazyChildren? =59
when (view.body) {60
case Lazy(_, val source, val capacity) -> Some(LazyChildren(source, capacity))61
else -> None62
}63
val body: NodeBody = createBody(this, view)65
fun setFrameEnabled(value: Boolean) {66
if (framesAllowed == value)67
return68
framesAllowed = value69
invalidateScene()70
inheritFrameParticipation(parent?.frameEnabled ?: true)71
}72
fun setFrameParticipation(value: Boolean) {73
participates = value74
inheritFrameParticipation(parent?.frameEnabled ?: true)75
}76
private fun inheritFrameParticipation(parentEnabled: Boolean) {77
val next = framesAllowed && participates && parentEnabled78
if (next == frameEnabled)79
return80
frameEnabled = next81
sceneVersion += 1L82
if (!next)83
paintLayers.close()84
scope.updateFrameParticipation()85
for (child in children)86
child.inheritFrameParticipation(next)87
}89
fun setLayoutHidden(value: Boolean) {90
if (hiddenByLayout == value) return91
hiddenByLayout = value92
inheritLayoutVisibility(parent?.layoutConcealed ?: false)93
scope.setLayoutInputHidden(value)94
root.semantics?.contentChanged()95
}96
private fun inheritLayoutVisibility(parentHidden: Boolean) {97
val next = hiddenByLayout || parentHidden98
if (layoutConcealed == next) return99
layoutConcealed = next100
when (body) {101
case Portal(val portal) -> portal.focusSession?.updateLayoutVisibility()102
else -> {}103
}104
for (child in children)105
child.inheritLayoutVisibility(next)106
}108
fun clearPaintSubtree() {109
when (currentLayout) {110
case Some(val value) -> value.sceneCache = None111
case None -> {}112
}113
var failure: Throwable? = None114
failure = cleanupUi(failure) { paintLayers.close() }115
for (child in children)116
failure = cleanupUi(failure) { child.clearPaintSubtree() }117
throwUi(failure)118
}120
fun invalidateLayout(batch: FrameInvalidationBatch? = None) {121
if (sameFrameBatch(layoutBatch, batch))122
return123
layoutBatch = batch124
layoutVersion += 1L125
if (!sameFrameBatch(sceneBatch, batch)) {126
sceneBatch = batch127
sceneVersion += 1L128
}129
if (mounted)130
parent?.invalidateLayout(batch)131
}133
fun invalidateScene(batch: FrameInvalidationBatch? = None) {134
if (sameFrameBatch(sceneBatch, batch))135
return136
sceneBatch = batch137
sceneVersion += 1L138
if (mounted)139
parent?.invalidateScene(batch)140
}142
fun request(value: UiInvalidation) {143
when (value) {144
case None -> return145
case Scene -> invalidateScene()146
case Layout -> invalidateLayout()147
}148
if (active && root.isOpen && frameEnabled)149
root.requestFrame()150
}152
fun invalidate() {153
if (active && root.isOpen) {154
stateDirty = true155
if (!queued) {156
queued = true157
root.enqueue(this)158
}159
}160
}161
override fun stateChanged(invalidation: UiInvalidation) {162
request(invalidation)163
invalidate()164
}165
fun replace(next: View) {166
when ((body, next.body)) {167
case(Empty, Empty) -> {}168
case(Direction, Direction(_, _)) -> {}169
case(Component(val instance), Component(val description)) -> {170
if (!instance.replace(description))171
inputChanged = true172
}173
case(Element(val instance), Element(val description, _)) -> {174
if (!instance.replace(description))175
inputChanged = true176
}177
case(Portal(val instance), Portal(_, val open, val options)) -> instance.replace(178
open,179
options180
)181
case(Lazy(val instance, _), Lazy(val description, _, _)) -> {182
if (!instance.replace(description))183
inputChanged = true184
}185
else -> throw IllegalStateException(Some("mismatched mounted view shape"))186
}187
view = next188
}189
fun rebuild(turn: UpdateTurn) {190
if (!active || !root.isOpen)191
return192
updated = Some(turn)193
val previousDirection = layoutDirection194
layoutDirection = view.directionOverride ?: parent?.layoutDirection ?: LayoutDirection.Ltr195
val shouldUpdate = inputChanged || stateDirty || layoutDirection != previousDirection196
inputChanged = false197
stateDirty = false198
if (layoutDirection != previousDirection)199
request(UiInvalidation.Layout)200
when (body) {201
case Empty -> {}202
case Direction -> when (view.body) {203
case Direction(val content, _) -> {204
if (children.size == 1L && children[0L].view === content)205
children[0L].refreshDirection(turn)206
else207
reconcile([content], turn)208
}209
else -> throw IllegalStateException(210
Some("direction description was replaced by another shape")211
)212
}213
case Portal(val instance) -> when (view.body) {214
case Portal(val content, _, _) -> instance.prepare(this, content, turn)215
else -> throw IllegalStateException(216
Some("portal description was replaced by another shape")217
)218
}219
case Component(val instance) -> if (shouldUpdate)220
rebuildComponent(instance, turn, false)221
case Element(val instance) -> {222
when (view.body) {223
case Element(_, val descriptions) -> reconcile(descriptions, turn)224
else -> throw IllegalStateException(225
Some("element description was replaced by another shape")226
)227
}228
if (shouldUpdate && active && root.isOpen && mounted)229
request(updateInputs { instance.update() })230
}231
case Lazy(val instance, val items) -> {232
when (view.body) {233
case Lazy(_, val source, val capacity) -> items.replace(234
this,235
source,236
capacity,237
turn238
)239
else -> throw IllegalStateException(240
Some("lazy description was replaced by another shape")241
)242
}243
if (layoutDirection != previousDirection)244
for (child in children)245
child.refreshDirection(turn)246
if (shouldUpdate && active && root.isOpen && mounted)247
request(updateInputs { instance.update() })248
}249
}250
}251
fun mountPending() {252
if (!active || !root.isOpen)253
return254
if (!mounted) {255
when (body) {256
case Empty -> {}257
case Direction -> {}258
case Component(val instance) -> instance.value.onMount()259
case Element(val instance) -> instance.onMount()260
case Portal(val instance) -> instance.mount(this)261
case Lazy(val instance, _) -> instance.onMount()262
}263
mounted = true264
if (active && root.isOpen) {265
when (body) {266
case Element(val instance) -> {267
updateInputs { instance.update() }268
}269
case Lazy(val instance, val items) -> {270
items.mount(this)271
updateInputs { instance.update() }272
}273
else -> {}274
}275
}276
}277
for (child in children)278
child.mountPending()279
}280
fun deactivate() {281
if (!active)282
return283
active = false284
queued = false285
var failure: Throwable? = None286
failure =287
cleanupUi(failure) {288
when (body) {289
case Portal(val instance) -> instance.retireInput()290
else -> {}291
}292
}293
failure = cleanupUi(failure) { scope.deactivate() }294
failure = cleanupUi(failure) { frameInputs.close() }295
for (subscription in dependencies.values)296
subscription.close()297
dependencies.clear()298
for (child in children)299
failure = cleanupUi(failure) { child.deactivate() }300
throwUi(failure)301
}302
fun unmount() {303
if (retired)304
return305
retired = true306
deactivate()307
var failure: Throwable? = None308
for (child in children)309
failure = cleanupUi(failure) { child.unmount() }310
children = []311
failure = cleanupUi(failure) { throwUi(scope.retireInteraction()) }312
if (mounted) {313
failure =314
cleanupUi(failure) {315
when (body) {316
case Empty -> {}317
case Direction -> {}318
case Component(val instance) -> instance.value.onUnmount()319
case Element(val instance) -> instance.onUnmount()320
case Portal(_) -> {}321
case Lazy(val instance, _) -> instance.onUnmount()322
}323
}324
}325
failure = cleanupUi(failure) { throwUi(scope.release()) }326
failure = cleanupUi(failure) { paintLayers.close() }327
failure = cleanupUi(failure) { releaseInstance(body) }328
failure = cleanupUi(failure) { currentLayout?.close() }329
currentLayout = None330
for (result in layoutRequests)331
failure = cleanupUi(failure) { result.close() }332
layoutRequests.clear()333
throwUi(failure)334
}335
}337
internal fun sameTurn(previous: UpdateTurn?, current: UpdateTurn): Boolean =338
when (previous) {339
case Some(val value) -> value === current340
case None -> false341
}343
private fun sameFrameBatch(344
previous: FrameInvalidationBatch?,345
current: FrameInvalidationBatch?346
): Boolean =347
when ((previous, current)) {348
case(Some(val a), Some(val b)) -> a === b349
else -> false350
}