Scoop API

scoop:scoop.ui:0.1.0

src/accessibility/reveal.scoop

Selected production source · src/accessibility/reveal.scoop

1package scoop.ui
3import scoop.graphics.Rect
4import scoop.window.WindowState
5import scoop.window.accessibility.*
7/** The scroll path borrows the same accepted boundaries owned by FocusController. */
8internal class SemanticRevealPlacement(
9 val generation: ULong,
10 val path: Array<ScrollReveal>,
11 val bounds: Rect,
12 val visible: Boolean
15private class PendingSemanticReveal(
16 val handle: SemanticsHandle,
17 private var initial: SemanticRevealPlacement?,
18 val collection: SemanticsHandle? = None,
19 val retention: SemanticItemRetention? = None,
20 val region: (() -> Rect?)? = None,
21 val horizontal: ScrollAlignment = ScrollAlignment.Nearest,
22 val vertical: ScrollAlignment = ScrollAlignment.Nearest,
23 val columnRetention: SemanticItemRetention? = None,
24 val column: SemanticItemKey? = None
25) {
26 val attempt = RevealAttempt()
27 val hasRegion: Boolean get() =
28 when (region) {
29 case Some(_) -> true
30 case None -> false
31 }
32 val awaitingScene: Boolean get() =
33 when (initial) {
34 case Some(_) -> false
35 case None -> true
36 }
37 init {
38 when (initial) {
39 case Some(val scene) -> attempt.watch(scene.path)
40 case None -> {}
41 }
42 when (collection) {
43 case Some(val parent) -> {
44 when (parent.revealPlacement) {
45 case Some(val scene) -> attempt.watch(scene.path)
46 case None -> {}
47 }
48 when (parent.collectionViewport) {
49 case Some(val viewport) -> attempt.watch(viewport)
50 case None -> {}
51 }
52 }
53 case None -> {}
54 }
55 }
56 fun begin(scene: SemanticRevealPlacement) {
57 initial = Some(scene)
58 attempt.watch(scene.path)
59 }
60 fun matches(placement: SemanticRevealPlacement): Boolean {
61 val before = initial ?: return false
62 if (
63 placement.generation !=
64 before.generation ||
65 placement.path.size !=
66 before.path.size ||
67 !attempt.isCurrent
68 )
69 return false
70 for (index in 0L ..< before.path.size) {
71 val previous = before.path[index]
72 val after = placement.path[index]
73 if (
74 previous.viewport !==
75 after.viewport ||
76 previous.state !==
77 after.state ||
78 previous.layout.binding !=
79 after.layout.binding
80 )
81 return false
82 }
83 return true
84 }
87/** Advances semantic scrolling on the root's existing accepted-frame path. */
88internal class SemanticRevealController(private val root: UiRoot) {
89 private var pending: PendingSemanticReveal? = None
91 fun request(handle: SemanticsHandle): SemanticActionResult {
92 when (val result = eligibility(handle, None, None)) {
93 case Some(val value) -> return value
94 case None -> {}
95 }
96 val placement = placement(handle) ?: return SemanticActionResult.Unavailable
97 val value =
98 when (val previous = pending) {
99 case Some(val current) -> if (
100 current.handle === handle && current.matches(placement)
101 )
102 current
103 else
104 PendingSemanticReveal(handle, Some(placement))
105 case None -> PendingSemanticReveal(handle, Some(placement))
106 }
107 install(value)
108 return advance(value, placement)
109 }
111 fun requestRegion(
112 handle: SemanticsHandle,
113 region: () -> Rect?,
114 horizontal: ScrollAlignment,
115 vertical: ScrollAlignment
116 ): SemanticActionResult {
117 when (val result = eligibility(handle, None, None)) {
118 case Some(val value) -> return value;
119 case None -> {}
120 }
121 val scene = placement(handle, true) ?: return SemanticActionResult.Unavailable
122 if (region() == None)
123 return SemanticActionResult.Unavailable
124 val value =
125 PendingSemanticReveal(
126 handle,
127 Some(scene),
128 region = Some(region),
129 horizontal = horizontal,
130 vertical = vertical
131 )
132 install(value)
133 return advance(value, scene)
134 }
136 fun requestItem(parent: SemanticsHandle, entry: LazyItemEntry): SemanticActionResult {
137 val handle = entry.node.semanticItem ?: return SemanticActionResult.Unavailable
138 return requestCollection(parent, entry, handle, None, None)
139 }
141 fun requestCell(
142 parent: SemanticsHandle,
143 entry: LazyItemEntry,
144 handle: SemanticsHandle,
145 columnEntry: LazyItemEntry?,
146 column: SemanticItemKey
147 ): SemanticActionResult = requestCollection(parent, entry, handle, columnEntry, Some(column))
149 private fun requestCollection(
150 parent: SemanticsHandle,
151 entry: LazyItemEntry,
152 handle: SemanticsHandle,
153 columnEntry: LazyItemEntry?,
154 column: SemanticItemKey?
155 ): SemanticActionResult {
156 val viewport = parent.collectionViewport ?: return SemanticActionResult.Unsupported
157 if (!viewport.active)
158 return SemanticActionResult.Unavailable
159 when (val result = eligibility(handle, Some(parent), Some(entry), column)) {
160 case Some(val value) -> return value
161 case None -> {}
162 }
163 val scene = placement(handle)
164 val value =
165 when (pending) {
166 case Some(val current) -> if (
167 current.handle ===
168 handle &&
169 sameSemantics(current.collection, Some(parent)) &&
170 current.attempt.isCurrent &&
171 when (scene) {
172 case Some(val accepted) -> current.awaitingScene ||
173 current.matches(accepted)
174 case None -> current.awaitingScene
175 }
176 )
177 current
178 else
179 PendingSemanticReveal(
180 handle,
181 scene,
182 Some(parent),
183 Some(SemanticItemRetention(entry)),
184 columnRetention =
185 when (columnEntry) {
186 case Some(val item) -> Some(SemanticItemRetention(item))
187 case None -> None
188 },
189 column = column
190 )
191 case None -> PendingSemanticReveal(
192 handle,
193 scene,
194 Some(parent),
195 Some(SemanticItemRetention(entry)),
196 columnRetention =
197 when (columnEntry) {
198 case Some(val item) -> Some(SemanticItemRetention(item))
199 case None -> None
200 },
201 column = column
202 )
203 }
204 install(value)
205 when (scene) {
206 case Some(val current) -> {
207 if (value.awaitingScene)
208 value.begin(current)
209 return advance(value, current)
210 }
211 case None -> {
212 parent.owner.request(UiInvalidation.Layout)
213 return SemanticActionResult.Busy
214 }
215 }
216 }
218 private fun install(value: PendingSemanticReveal) {
219 when (pending) {
220 case Some(val current) -> if (current === value)
221 return
222 case None -> {}
223 }
224 cancel()
225 pending = Some(value)
226 }
228 fun cancel() {
229 val previous = pending
230 pending = None
231 when (previous) {
232 case Some(val value) -> {
233 value.columnRetention?.close()
234 value.retention?.close()
235 }
236 case None -> {}
237 }
238 }
240 fun cancelCollection(parent: SemanticsHandle) {
241 when (pending) {
242 case Some(val value) -> if (sameSemantics(value.collection, Some(parent)))
243 cancel()
244 case None -> {}
245 }
246 }
248 fun complete() {
249 val value = pending ?: return
250 if (!eligible(value)) {
251 cancel()
252 return
253 }
254 when (placement(value.handle, value.hasRegion)) {
255 case Some(val scene) -> {
256 if (value.awaitingScene)
257 value.begin(scene)
258 if (value.matches(scene))
259 advance(value, scene)
260 else
261 cancel()
262 }
263 case None -> cancel()
264 }
265 }
267 fun check() {
268 val value = pending ?: return
269 if (!eligible(value)) {
270 cancel()
271 return
272 }
273 if (value.awaitingScene)
274 return
275 when (current(value)) {
276 case Some(_) -> {}
277 case None -> cancel()
278 }
279 }
281 fun discarded() {
282 check()
283 when (pending) {
284 case Some(_) -> root.requestFrame()
285 case None -> {}
286 }
287 }
289 fun retire(handle: SemanticsHandle) {
290 when (pending) {
291 case Some(val value) -> if (
292 value.handle === handle || sameSemantics(value.collection, Some(handle))
293 )
294 cancel()
295 case None -> {}
296 }
297 }
299 fun close() {
300 cancel()
301 }
303 private fun advance(
304 value: PendingSemanticReveal,
305 scene: SemanticRevealPlacement
306 ): SemanticActionResult {
307 val bounds =
308 when (val region = value.region) {
309 case Some(val read) -> when (read()) {
310 case Some(val rect) -> rect
311 case None -> {
312 finish(value)
313 return SemanticActionResult.Unavailable
314 }
315 }
316 case None -> scene.bounds
317 }
318 val visible =
319 scene.visible && (!value.hasRegion || intersectReveal(bounds, scene.bounds) != None)
320 val progress =
321 value.attempt.advance(scene.path, bounds, visible, value.horizontal, value.vertical)
322 // Scroll intent observers may retire the target or replace the request synchronously.
323 when (current(value)) {
324 case None -> {
325 finish(value)
326 return SemanticActionResult.Unavailable
327 }
328 case Some(_) -> {}
329 }
330 return when (progress) {
331 case Ready -> {
332 finish(value)
333 SemanticActionResult.Completed
334 }
335 case Moved -> SemanticActionResult.Busy
336 case Waiting -> SemanticActionResult.Busy
337 case Unavailable -> {
338 finish(value)
339 SemanticActionResult.Unavailable
340 }
341 case Cancelled -> {
342 finish(value)
343 SemanticActionResult.Unavailable
344 }
345 }
346 }
348 private fun finish(value: PendingSemanticReveal) {
349 when (pending) {
350 case Some(val current) -> if (current === value)
351 cancel()
352 case None -> {}
353 }
354 }
356 private fun eligible(value: PendingSemanticReveal): Boolean =
357 value.attempt.isCurrent &&
358 eligibility(value.handle, value.collection, value.retention?.entry, value.column) ==
359 None
361 private fun current(value: PendingSemanticReveal): SemanticRevealPlacement? {
362 if (!eligible(value))
363 return None
364 when (pending) {
365 case Some(val current) -> if (current !== value)
366 return None
367 case None -> return None
368 }
369 when (value.handle.actions.reveal) {
370 case Some(_) -> if (!value.hasRegion)
371 return None
372 case None -> {}
373 }
374 val scene = placement(value.handle, value.hasRegion) ?: return None
375 return if (value.matches(scene))
376 Some(scene)
377 else
378 None
379 }
381 private fun placement(
382 handle: SemanticsHandle,
383 text: Boolean = false
384 ): SemanticRevealPlacement? {
385 val scene = handle.revealPlacement ?: return None
386 if (scene.generation != handle.generation || handle.resource.geometry == None)
387 return None
388 if (text) {
389 val path = (handle.textBinding?.path ?: None) ?: return None
390 return Some(
391 SemanticRevealPlacement(scene.generation, path, scene.bounds, scene.visible)
392 )
393 }
394 return Some(scene)
395 }
397 private fun eligibility(
398 handle: SemanticsHandle,
399 collection: SemanticsHandle?,
400 entry: LazyItemEntry?,
401 column: SemanticItemKey? = None
402 ): SemanticActionResult? {
403 if (!root.isOpen || !handle.active || root.window.state != WindowState.Open)
404 return Some(SemanticActionResult.Unavailable)
405 if (!root.window.isVisible || root.window.isOccluded)
406 return Some(SemanticActionResult.Unavailable)
407 if (handle.merged || !handle.owner.scope.input.isInputAllowed)
408 return Some(SemanticActionResult.Rejected)
409 when (collection) {
410 case Some(val parent) -> {
411 val item = entry ?: return Some(SemanticActionResult.Unavailable)
412 if (
413 !parent.active ||
414 !item.node.active ||
415 !withinFocusScope(handle.owner, item.node)
416 )
417 return Some(SemanticActionResult.Unavailable)
418 if (column == None && !sameSemantics(item.node.semanticItem, Some(handle)))
419 return Some(SemanticActionResult.Unavailable)
420 val source =
421 parent.resource.collection ?: return Some(SemanticActionResult.Unavailable)
422 when (source.itemForKey(semanticItemKey(item.key))) {
423 case Found(val value) -> {
424 if (!value.properties.enabled)
425 return Some(SemanticActionResult.Rejected)
426 }
427 else -> return Some(SemanticActionResult.Unavailable)
428 }
429 when (column) {
430 case Some(val key) -> when (source.cellForKey(semanticItemKey(item.key), key)) {
431 case Found(val value) -> {
432 if (value.key != key)
433 return Some(SemanticActionResult.Unavailable)
434 if (!value.properties.enabled)
435 return Some(SemanticActionResult.Rejected)
436 }
437 else -> return Some(SemanticActionResult.Unavailable)
438 }
439 case None -> {}
440 }
441 }
442 case None -> if (handle.properties.ignored)
443 return Some(SemanticActionResult.Rejected)
444 }
445 var cursor: SemanticsHandle? = Some(handle)
446 while (true) {
447 val node = cursor ?: return None
448 if (!node.active || node.properties.hidden || !node.properties.enabled)
449 return Some(SemanticActionResult.Rejected)
450 cursor = node.parent
451 }
452 }