Scoop API

scoop:scoop.ui:0.1.0

src/runtime/node.scoop

Selected production source · src/runtime/node.scoop

1package scoop.ui
3import scoop.collections.HashMap
5internal enum NodeBody {
6 Empty, Component(ComponentInstance), Element(ElementInstance), Portal(PortalInstance),
7 Lazy(LazyInstance, LazyChildren), Direction
8}
10internal class UpdateTurn
12/** Shared only by the dependency scan preceding layout and scene callbacks. */
13internal class FrameInvalidationBatch
15internal class MountedNode(
16 val root: UiRoot,
17 val parent: MountedNode?,
18 var view: View
19): StateInvalidationTarget {
20 val depth: Long = (parent?.depth ?: -1L) + 1L
21 var layoutDirection: LayoutDirection =
22 view.directionOverride ?: parent?.layoutDirection ?: LayoutDirection.Ltr
23 val scope = ComponentScope(this)
24 val paintLayers = PaintLayers()
25 val focusTargets = ArrayList<FocusTarget>()
26 val semanticHandles = ArrayList<SemanticsHandle>()
27 var semanticRoute: SemanticsHandle? = None
28 var lazySemantics: SemanticsHandle? = None
29 var semanticItem: SemanticsHandle? = None
30 var focusScope: FocusScope? = None
31 var navigation: KeyNavigation? = None
32 var lazyFocus: LazyFocus? = None
33 var children: Array<MountedNode> = []
34 var active = true
35 var mounted = false
36 var retired = false
37 var queued = false
38 var updated: UpdateTurn? = None
39 val dependencies = HashMap<StateSignalId, StateSubscription>()
40 var inputChanged = true
41 var stateDirty = false
42 var layoutVersion = 0L
43 var sceneVersion = 0L
44 private var layoutBatch: FrameInvalidationBatch? = None
45 private var sceneBatch: FrameInvalidationBatch? = None
46 val frameInputs = NodeFrameInputs(this)
47 var layoutRound: LayoutRound? = None
48 var currentLayout: NodeLayout? = None
49 val layoutRequests = ArrayList<NodeLayout>()
50 private var framesAllowed = true
51 val localFrameEnabled: Boolean get() = framesAllowed
52 var hiddenByLayout = false
53 var layoutConcealed: Boolean = parent?.layoutConcealed ?: false
54 private var participates = true
55 val participatesInFrame: Boolean get() = participates
56 var frameEnabled: Boolean = parent?.frameEnabled ?: true
58 val lazyItems: LazyChildren? =
59 when (view.body) {
60 case Lazy(_, val source, val capacity) -> Some(LazyChildren(source, capacity))
61 else -> None
62 }
63 val body: NodeBody = createBody(this, view)
65 fun setFrameEnabled(value: Boolean) {
66 if (framesAllowed == value)
67 return
68 framesAllowed = value
69 invalidateScene()
70 inheritFrameParticipation(parent?.frameEnabled ?: true)
71 }
72 fun setFrameParticipation(value: Boolean) {
73 participates = value
74 inheritFrameParticipation(parent?.frameEnabled ?: true)
75 }
76 private fun inheritFrameParticipation(parentEnabled: Boolean) {
77 val next = framesAllowed && participates && parentEnabled
78 if (next == frameEnabled)
79 return
80 frameEnabled = next
81 sceneVersion += 1L
82 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) return
91 hiddenByLayout = value
92 inheritLayoutVisibility(parent?.layoutConcealed ?: false)
93 scope.setLayoutInputHidden(value)
94 root.semantics?.contentChanged()
95 }
96 private fun inheritLayoutVisibility(parentHidden: Boolean) {
97 val next = hiddenByLayout || parentHidden
98 if (layoutConcealed == next) return
99 layoutConcealed = next
100 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 = None
111 case None -> {}
112 }
113 var failure: Throwable? = None
114 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 return
123 layoutBatch = batch
124 layoutVersion += 1L
125 if (!sameFrameBatch(sceneBatch, batch)) {
126 sceneBatch = batch
127 sceneVersion += 1L
128 }
129 if (mounted)
130 parent?.invalidateLayout(batch)
131 }
133 fun invalidateScene(batch: FrameInvalidationBatch? = None) {
134 if (sameFrameBatch(sceneBatch, batch))
135 return
136 sceneBatch = batch
137 sceneVersion += 1L
138 if (mounted)
139 parent?.invalidateScene(batch)
140 }
142 fun request(value: UiInvalidation) {
143 when (value) {
144 case None -> return
145 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 = true
155 if (!queued) {
156 queued = true
157 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 = true
172 }
173 case(Element(val instance), Element(val description, _)) -> {
174 if (!instance.replace(description))
175 inputChanged = true
176 }
177 case(Portal(val instance), Portal(_, val open, val options)) -> instance.replace(
178 open,
179 options
180 )
181 case(Lazy(val instance, _), Lazy(val description, _, _)) -> {
182 if (!instance.replace(description))
183 inputChanged = true
184 }
185 else -> throw IllegalStateException(Some("mismatched mounted view shape"))
186 }
187 view = next
188 }
189 fun rebuild(turn: UpdateTurn) {
190 if (!active || !root.isOpen)
191 return
192 updated = Some(turn)
193 val previousDirection = layoutDirection
194 layoutDirection = view.directionOverride ?: parent?.layoutDirection ?: LayoutDirection.Ltr
195 val shouldUpdate = inputChanged || stateDirty || layoutDirection != previousDirection
196 inputChanged = false
197 stateDirty = false
198 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 else
207 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 turn
238 )
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 return
254 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 = true
264 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 return
283 active = false
284 queued = false
285 var failure: Throwable? = None
286 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 return
305 retired = true
306 deactivate()
307 var failure: Throwable? = None
308 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 = None
330 for (result in layoutRequests)
331 failure = cleanupUi(failure) { result.close() }
332 layoutRequests.clear()
333 throwUi(failure)
334 }
337internal fun sameTurn(previous: UpdateTurn?, current: UpdateTurn): Boolean =
338 when (previous) {
339 case Some(val value) -> value === current
340 case None -> false
341 }
343private fun sameFrameBatch(
344 previous: FrameInvalidationBatch?,
345 current: FrameInvalidationBatch?
346): Boolean =
347 when ((previous, current)) {
348 case(Some(val a), Some(val b)) -> a === b
349 else -> false
350 }