Scoop API

scoop:scoop.ui:0.1.0

src/host/root.scoop

Selected production source · src/host/root.scoop

1package scoop.ui
3import scoop.collections.sortedBy
4import scoop.graphics.Rect
5import scoop.graphics.Size
6import scoop.math.min
7import scoop.math.max
8import scoop.graphics.Point
9import scoop.graphics.Color
10import scoop.graphics.text.FontCollection
11import scoop.surface.*
12import scoop.window.Window
13import scoop.window.WindowState
14import scoop.window.InteractionPreferences
16private class SceneCompletion(val node: MountedNode, val action: (FrameCompletion) -> Unit)
18/** Owns one root; closing it leaves the caller's native window intact. */
19public class UiWindow internal constructor(internal val root: UiRoot): Closeable {
20 public val nativeWindow: Window get() = root.window
21 public var preferences: UiPreferences
22 get() {
23 checkOpen()
24 return root.preferences.options
25 }
26 set(value) {
27 checkOpen()
28 checkFocusMutation()
29 value.check()
30 root.surface.dispatch { root.preferences.replace(value) }
31 }
32 public val interactionPreferences: InteractionPreferences get() {
33 checkOpen()
34 return root.preferences.value
35 }
36 public var defaultFonts: FontCollection
37 get() {
38 checkOpen();
39 return root.fonts.value
40 }
41 set(value) {
42 checkOpen()
43 root.fonts.replace(value)
44 root.requestFrame()
45 }
46 private fun checkOpen() {
47 root.checkThread()
48 if (!root.isOpen)
49 throw IllegalStateException(Some("UI root is closed"))
50 }
51 public val isClosed: Boolean get() {
52 root.checkThread();
53 return root.closed
54 }
55 public override fun close() {
56 root.requestClose()
57 }
60internal class UiRoot(
61 val host: UiHost,
62 val window: Window,
63 private val initial: View,
64 defaultFonts: FontCollection? = None,
65 initialPreferences: UiPreferences = uiPreferences()
66): SurfaceContent, UiUpdateRoot {
67 private var attached: SurfaceWindow? = None
68 internal var node: MountedNode? = None
69 val layouts = LayoutStore()
70 val frameDependencies = FrameDependencies()
71 val focus = FocusController(this)
72 val overlays = OverlayController(this)
73 var semantics: RootSemantics? = None
74 var drops: RootDrops? = None
75 var dragSource: DragSource? = None
76 var windowFrame: UiWindowFrameClaim? = None
77 internal var scrollInput: ScrollInputFrame? = None
78 internal val scrollEdges = ArrayList<ScrollInputEntry>()
79 internal val scrollInertias = ArrayList<ScrollInertia>()
80 private var scrollCompletion: Closeable? = None
81 private val pending = ArrayList<MountedNode>()
82 private val completions = ArrayList<SceneCompletion>()
83 override var pendingEnqueued = false
84 override var isOpen = true
85 var closed = false
86 val surface: SurfaceWindow get() =
87 attached ?: throw IllegalStateException(Some("UI root is not attached"))
89 val preferences = RootPreferences(this, initialPreferences)
90 val fonts = RootFonts(defaultFonts)
92 fun checkThread() {
93 host.checkThread()
94 }
95 fun requestFrame() {
96 if (isOpen && surface.state == SurfaceWindowState.Open)
97 surface.requestFrame()
98 }
99 fun mount(value: SurfaceWindow) {
100 attached = Some(value)
101 scrollCompletion =
102 Some(
103 value.input.addEventFinished {
104 _ ->
105 val pending = scrollInput
106 scrollInput = None
107 pending?.cancel()
108 }
109 )
110 val current = createNode(this, None, initial)
111 node = Some(current)
112 preferences.start()
113 focus.start()
114 overlays.start()
115 current.rebuild(UpdateTurn())
116 current.mountPending()
117 semantics?.publish()
118 }
119 private val acceptsUpdates: Boolean get() =
120 isOpen &&
121 surface.state ==
122 SurfaceWindowState.Open &&
123 window.state !=
124 WindowState.Closing &&
125 window.state !=
126 WindowState.Destroyed
127 fun enqueue(value: MountedNode) {
128 if (!acceptsUpdates)
129 return
130 pending.add(value)
131 host.enqueue(this)
132 }
133 fun scheduleUpdate() {
134 if (acceptsUpdates)
135 host.enqueue(this)
136 }
137 fun flush() {
138 if (!isOpen)
139 return
140 val turn = UpdateTurn()
141 val work = pending.toArray().sortedBy<MountedNode, Long> { value -> value.depth }
142 pending.clear()
143 for (value in work) {
144 value.queued = false
145 }
146 for (value in work) {
147 if (value.active && isOpen && !sameTurn(value.updated, turn)) {
148 value.rebuild(turn)
149 value.mountPending()
150 }
151 }
152 overlays.flush()
153 semantics?.publish()
154 drops?.flushLeaves()
155 dragSource?.reconcile()
156 }
157 override fun dispatchUpdate() {
158 surface.dispatch { flush() }
159 }
160 public override fun paint(frame: FrameContext) {
161 if (!isOpen)
162 return
163 for (motion in scrollInertias.toArray())
164 motion.advance()
165 val current = node ?: throw IllegalStateException(Some("UI root has not mounted"))
166 frame.canvas.clear(Color.Transparent)
167 focus.beginFrame()
168 semantics?.beginFrame()
169 overlays.beginFrame()
170 frameDependencies.check(frame.time)
171 layouts.begin()
172 var success = false
173 var failure: Throwable? = None
174 try {
175 val insets = frame.safeAreaInsets
176 val x = min(frame.size.width, insets.left)
177 val y = min(frame.size.height, insets.top)
178 val extent = Size(max(0.0, frame.size.width - x - insets.right),
179 max(0.0, frame.size.height - y - insets.bottom))
180 val bounds = Rect(x, y, extent.width, extent.height)
181 val result = current.measure(Constraints.tight(extent), frame.time)
182 result.finishMeasurement()
183 semantics?.publish()
184 frame.withState { content ->
185 content.canvas.clipRect(bounds)
186 result.scene(content, Point(x, y))
187 overlays.paint(content, bounds)
188 }
189 success = true
190 } catch (error: Throwable) {
191 if (!(error is UiLayoutEnded) || isOpen)
192 failure = Some(error)
193 }
194 val completed = success
195 failure = cleanupUi(failure) { layouts.finish(completed) }
196 throwUi(failure)
197 semantics?.publish()
198 }
200 fun addCompletion(node: MountedNode, action: (FrameCompletion) -> Unit) {
201 completions.add(SceneCompletion(node, action))
202 }
203 public override fun onFrameComplete(frame: FrameCompletion) {
204 if (isOpen)
205 focus.publishScene(frame)
206 if (isOpen)
207 overlays.complete(frame)
208 if (isOpen)
209 focus.complete(frame)
210 if (isOpen)
211 semantics?.complete(frame)
212 val work = completions.toArray()
213 completions.clear()
214 for (completion in work) {
215 if (isOpen && completion.node.active)
216 completion.action(frame)
217 }
218 }
219 fun stop() {
220 if (!isOpen)
221 return
222 isOpen = false
223 pending.clear()
224 completions.clear()
225 prepareUi {
226 var failure: Throwable? = None
227 failure = cleanupUi(failure) { scrollInput?.cancel() }
228 scrollInput = None
229 scrollEdges.clear()
230 for (motion in scrollInertias.toArray())
231 failure = cleanupUi(failure) { motion.stop() }
232 failure = cleanupUi(failure) { scrollCompletion?.close() }
233 scrollCompletion = None
234 failure = cleanupUi(failure) { preferences.close() }
235 failure = cleanupUi(failure) { focus.close() }
236 failure = cleanupUi(failure) { drops?.close() }
237 failure = cleanupUi(failure) { dragSource?.close() }
238 failure = cleanupUi(failure) { semantics?.close() }
239 failure = cleanupUi(failure) { node?.deactivate() }
240 failure = cleanupUi(failure) { overlays.stop() }
241 throwUi(failure)
242 }
243 }
244 fun requestClose() {
245 checkThread()
246 if (!isOpen)
247 return
248 surface.dispatch {
249 stop()
250 host.retire(this)
251 }
252 }
253 fun closeSurface() {
254 when (attached) {
255 case Some(val value) -> value.close()
256 case None -> close()
257 }
258 }
259 public override fun close() {
260 checkThread()
261 if (closed)
262 return
263 var failure = cleanupUi(None) { stop() }
264 closed = true
265 val current = node
266 node = None
267 when (current) {
268 case Some(val value) -> failure = cleanupUi(failure) { value.unmount() }
269 case None -> {}
270 }
271 failure = cleanupUi(failure) { layouts.close() }
272 failure = cleanupUi(failure) { frameDependencies.close() }
273 failure = cleanupUi(failure) { overlays.close() }
274 failure = cleanupUi(failure) { fonts.close() }
275 host.detached(this)
276 throwUi(failure)
277 }