Scoop API

scoop:scoop.ui:0.1.0

src/overlay/controller.scoop

Selected production source · src/overlay/controller.scoop

1package scoop.ui
3import scoop.graphics.Point
4import scoop.graphics.Rect
5import scoop.surface.*
7/** Frame identity contains values only; it never retains a Canvas borrow. */
8internal class OverlayFrame {
9 val unavailable = ArrayList<PortalInstance>()
12internal class OverlayController(private val root: UiRoot): Closeable {
13 private val sessions = ArrayList<PortalInstance>()
14 private val focusSessions = ArrayList<OverlayFocusSession>()
15 private val retiring = ArrayList<PortalInstance>()
16 private var stopped = false
17 private var registration: Closeable? = None
18 private var activity: Closeable? = None
19 private var geometry: Closeable? = None
20 var frame = OverlayFrame()
22 val currentModal: OverlayFocusSession? get() {
23 val boundary = root.surface.inputBoundary ?: return None
24 for (session in focusSessions) {
25 when (session.boundary) {
26 case Some(val value) -> if (value === boundary)
27 return Some(session)
28 case None -> {}
29 }
30 }
31 return None
32 }
33 val currentFocus: OverlayFocusSession? get() {
34 var index = focusSessions.size
35 while (index > 0L) {
36 index -= 1L
37 val session = focusSessions[index]
38 if (session.acceptsFocus && session.inCurrentDomain)
39 return Some(session)
40 }
41 return None
42 }
43 fun start() {
44 root.surface.input.beforePress = { press -> beforePress(press) }
45 registration =
46 Some(root.surface.input.addDefaultAction { context, event -> dismiss(context, event) })
47 activity = Some(root.window.observeActivity { _ -> scheduleIfUnavailable() })
48 geometry =
49 Some(
50 root.host.frameEvents.subscribe(root.window.id, false) {
51 _ -> scheduleIfUnavailable()
52 }
53 )
54 }
55 private val drawable: Boolean get() =
56 root.window.isVisible &&
57 !root.window.isOccluded &&
58 when (val metrics = root.window.metrics) {
59 case Some(val value) -> value.physicalSize.isDrawable
60 case None -> false
61 }
62 private fun scheduleIfUnavailable() {
63 if (root.isOpen && !drawable && (sessions.size > 0L || retiring.size > 0L))
64 root.scheduleUpdate()
65 }
66 /** A compositor may suspend frames indefinitely; completed exits still release their owners. */
67 fun flush() {
68 if (root.isOpen && !drawable)
69 completeRemovals(None)
70 }
71 fun add(value: PortalInstance) {
72 sessions.add(value)
73 value.domain = containingModal(value.node.parent)
74 }
75 fun open(value: PortalInstance, reopening: Boolean = false) {
76 if (!reopening) {
77 removeSession(value)
78 sessions.add(value)
79 }
80 when (value.focusSession) {
81 case Some(_) -> {}
82 case None -> {
83 val session = OverlayFocusSession(value, value.options, currentFocus)
84 value.focusSession = Some(session)
85 value.domain =
86 if (session.isModal)
87 Some(session)
88 else
89 containingModal(value.node.parent)
90 if (root.isOpen) {
91 focusSessions.add(session)
92 when (value.retirementRevision) {
93 case Some(val revision) -> session.beginClose(revision)
94 case None -> {}
95 }
96 } else
97 session.finish()
98 }
99 }
100 }
102 /** Logical ancestry and registration order survive a change of modal policy. */
103 fun reclassify() {
104 for (portal in sessions) {
105 val session = portal.focusSession
106 portal.domain =
107 when (session) {
108 case Some(val value) -> if (value.isModal)
109 Some(value)
110 else
111 containingModal(portal.node.parent)
112 case None -> containingModal(portal.node.parent)
113 }
114 }
115 }
117 fun remove(value: PortalInstance, pending: Boolean) {
118 removeSession(value)
119 if (pending)
120 retiring.add(value)
121 }
122 private fun removeSession(value: PortalInstance) {
123 for (index in 0L ..< sessions.size)
124 if (sessions[index] === value) {
125 sessions.removeAt(index);
126 return
127 }
128 }
129 fun beginFrame() {
130 frame = OverlayFrame()
131 }
132 fun include(value: PortalInstance) {
133 value.included = Some(frame)
134 }
135 fun paint(context: FrameContext, bounds: Rect) {
136 paintDomain(None, context, bounds)
137 for (session in focusSessions)
138 if (session.isModal)
139 paintDomain(Some(session), context, bounds)
140 for (portal in sessions) {
141 val included = portal.included
142 when (included) {
143 case Some(val value) -> if (value !== frame)
144 portal.node.clearPaintSubtree()
145 case None -> portal.node.clearPaintSubtree()
146 }
147 }
148 }
149 private fun paintDomain(domain: OverlayFocusSession?, context: FrameContext, bounds: Rect) {
150 for (portal in sessions) {
151 val included = portal.included ?: continue
152 if (!portal.isPresent || included !== frame || !sameModal(portal.domain, domain))
153 continue
154 val anchor =
155 when (portal.anchor) {
156 case Some(val handle) -> {
157 val geometry = handle.geometry(frame)
158 val usable =
159 when (geometry) {
160 case Some(val value) -> value.visible ||
161 portal.options.anchorVisibility ==
162 AnchorVisibility.KeepVisible
163 case None -> false
164 }
165 if (!usable) {
166 portal.node.clearPaintSubtree()
167 if (portal.options.anchorVisibility != AnchorVisibility.Hide)
168 frame.unavailable.add(portal)
169 continue
170 }
171 geometry?.bounds
172 }
173 case None -> None
174 }
175 val plan =
176 planOverlay(
177 portal.options.placement,
178 bounds,
179 anchor,
180 portal.node.layoutDirection
181 )
182 val child = portal.node.children[0L]
183 val result = child.measure(plan.constraints, context.time)
184 result.finishMeasurement()
185 root.semantics?.publish()
186 val metrics = result.metrics
187 val layout = plan.layout(metrics.size, portal.snapshot, metrics.overlayAnchor)
188 portal.focusSession?.place(context)
189 result.scene(context, Point(layout.bounds.x, layout.bounds.y), Some(layout))
190 portal.presented = Some(frame)
191 when (portal.focusSession) {
192 case Some(val modal) -> modal.presented = Some(frame)
193 case None -> {}
194 }
195 }
196 }
197 fun complete(completion: FrameCompletion) {
198 val completed = frame
199 val unavailable = frame.unavailable.toArray()
200 frame.unavailable.clear()
201 if (completion.submission != FrameSubmission.Accepted)
202 return
203 var failure: Throwable? = None
204 for (portal in unavailable) {
205 if (portal.isOpen)
206 failure =
207 cleanupUi(failure) { portal.forceClose(OverlayCloseReason.AnchorUnavailable) }
208 }
209 failure = cleanupUi(failure) { completeRemovals(Some(completed)) }
210 for (modal in focusSessions.toArray())
211 if (root.isOpen)
212 failure = cleanupUi(failure) { modal.complete(completed) }
213 throwUi(failure)
214 }
215 private fun completeRemovals(completed: OverlayFrame?) {
216 var failure: Throwable? = None
217 val previous = currentFocus
218 var restore: OverlayFocusSession? = None
219 val work = focusSessions.toArray()
220 var index = work.size
221 while (index > 0L) {
222 index -= 1L
223 val modal = work[index]
224 if (modal.canComplete(completed)) {
225 when (restore) {
226 case None -> if (
227 modal.canRestoreFocus &&
228 (sameModal(previous, Some(modal)) || modal.heldFocusOnClose)
229 )
230 restore = Some(modal)
231 case Some(_) -> {}
232 }
233 failure = cleanupUi(failure) { modal.finish() }
234 removeModal(modal)
235 }
236 }
237 val restoreTarget = restore
238 if (root.isOpen)
239 failure = cleanupUi(failure) { restoreTarget?.restore() }
240 for (portal in sessions.toArray())
241 if (root.isOpen)
242 failure = cleanupUi(failure) { portal.complete(completed) }
243 val pending = retiring.toArray()
244 retiring.clear()
245 for (portal in pending) {
246 failure = cleanupUi(failure) { portal.complete(completed) }
247 if (portal.hasPendingClose)
248 retiring.add(portal)
249 }
250 throwUi(failure)
251 }
252 private fun removeModal(value: OverlayFocusSession) {
253 for (index in 0L ..< focusSessions.size)
254 if (focusSessions[index] === value) {
255 focusSessions.removeAt(index)
256 return
257 }
258 }
259 fun stop() {
260 if (stopped)
261 return
262 stopped = true
263 var failure = cleanupUi(None) { registration?.close() }
264 registration = None
265 failure = cleanupUi(failure) { activity?.close() }
266 activity = None
267 failure = cleanupUi(failure) { geometry?.close() }
268 geometry = None
269 for (portal in sessions.toArray())
270 failure = cleanupUi(failure) { portal.forceClose(OverlayCloseReason.OwnerClosed) }
271 val work = focusSessions.toArray()
272 focusSessions.clear()
273 var index = work.size
274 while (index > 0L) {
275 index -= 1L
276 val modal = work[index]
277 failure = cleanupUi(failure) { modal.finish() }
278 }
279 throwUi(failure)
280 }
281 public override fun close() {
282 var failure = cleanupUi(None) { stop() }
283 val live = sessions.toArray()
284 val retired = retiring.toArray()
285 sessions.clear()
286 retiring.clear()
287 for (portal in live)
288 failure = cleanupUi(failure) { portal.completeClose() }
289 for (portal in retired)
290 failure = cleanupUi(failure) { portal.completeClose() }
291 throwUi(failure)
292 }
294 fun beforePress(press: InputPress): Boolean =
295 dismissOverlayPress(root, sessions, currentModal, press)
296 fun dismiss(context: RoutedEventContext, event: InputEvent) {
297 dismissOverlayKey(root, sessions, currentModal, context, event)
298 }