Scoop API

scoop:scoop.window:0.1.0

src/accessibility/bridge_collection.scoop

Selected production source · src/accessibility/bridge_collection.scoop

1package scoop.window.accessibility
3import scoop.window.*
5/** Queries remain read-only; explicit operations enter the existing owner action boundary. */
6internal class NativeCollectionQueries(
7 private val application: ApplicationOwner,
8 provider: AccessibilityProvider
9) : Closeable {
10 private val callback = collectionCallback(application, provider)
12 fun attach(pointer: Ptr<Unit>) {
13 @Unsafe
14 {
15 nativeCollectionCallback(pointer, callback.function, callback.context)
16 }
17 }
19 fun publish(pointer: Ptr<Unit>, node: AccessibilityNode) {
20 @Unsafe
21 {
22 when (node.relationValues.activeDescendant) {
23 case Some(Item(val collection, val key)) ->
24 withCollectionKey(key) { kind, number, bytes, length ->
25 @Unsafe
26 {
27 checkNativeStatus(
28 nativeCollectionActive(
29 pointer,
30 node.id.number,
31 collection.number,
32 kind,
33 number,
34 bytes,
35 length
36 ),
37 "publish active collection item"
38 )
39 }
40 }
41 else -> {}
42 }
43 checkNativeStatus(
44 nativeCollectionSource(
45 pointer,
46 node.id.number,
47 node.collectionValue?.count ?: -1L,
48 node.collectionRevision,
49 collectionActionBits(node.itemActions) +
50 gridShapeFlags(node.collectionValue?.grid ?: None)
51 ),
52 "publish semantic collection"
53 )
54 publishCollectionDetails(pointer, node)
55 when (val key = node.itemKey) {
56 case Some(val value) -> {
57 withCollectionKey(value) { kind, number, bytes, length ->
58 @Unsafe
59 {
60 checkNativeStatus(
61 nativeCollectionRow(
62 pointer,
63 node.id.number,
64 kind,
65 number,
66 bytes,
67 length
68 ),
69 "publish semantic collection row"
70 )
71 }
72 }
73 }
75 case None -> {}
76 }
77 when (node.properties.cell) {
78 case Some(val cell) ->
79 when (cell.columnKey) {
80 case Some(val key) ->
81 withCollectionKey(key) { kind, number, bytes, length ->
82 @Unsafe
83 {
84 checkNativeStatus(
85 nativeCollectionCell(
86 pointer,
87 node.id.number,
88 kind,
89 number,
90 bytes,
91 length
92 ),
93 "publish semantic cell column"
94 )
95 }
96 }
97 case None -> {}
98 }
99 case None -> {}
100 }
101 }
102 }
104 public override fun close() {
105 val owned = callback
106 application
107 .mailbox
108 .postControl {
109 @Unsafe
110 {
111 releaseForeignCallback(owned)
112 }
113 }
114 }
117@Extern(name = "scoop_window_accessibility_collection_active")
118private fun nativeCollectionActive(
119 provider: Ptr<Unit>,
120 owner: Long,
121 collection: Long,
122 kind: Int,
123 number: Long,
124 bytes: Ptr<UInt8>?,
125 length: Long
126): Long
128private class CollectionQueryStatus {
129 var value = 4
132private fun gridShapeFlags(grid: SemanticGrid?): Int = (
133 when (grid?.span ?: None) {
134 case Some(_) -> 128
135 case None -> 0
136 }
137) +
138 (
139 when (grid?.hasCells ?: None) {
140 case Some(_) -> 512
141 case None -> 0
142 }
143 )
145private fun collectionActionBits(actions: SemanticCollectionActions): Int = (
146 when (actions.realize) {
147 case Some(_) -> 1;
148 case None -> 0
149 }
150) +
151 (
152 when (actions.reveal) {
153 case Some(_) -> 2;
154 case None -> 0
155 }
156 ) +
157 (
158 when (actions.focus) {
159 case Some(_) -> 4;
160 case None -> 0
161 }
162 ) +
163 (
164 when (actions.setSelected) {
165 case Some(_) -> 8;
166 case None -> 0
167 }
168 ) +
169 gridActionBits(actions.grid) +
170 (
171 when (actions.setExpanded) {
172 case Some(_) -> 256
173 case None -> 0
174 }
175 ) +
176 (
177 when (actions.setChecked) {
178 case Some(_) -> 1024
179 case None -> 0
180 }
181 )
183private fun gridActionBits(actions: SemanticGridActions?): Int {
184 val grid = actions ?: return 0
185 var bits = 0
186 when (grid.realize) {
187 case Some(_) -> bits += 16
188 case None -> {}
189 }
190 when (grid.reveal) {
191 case Some(_) -> bits += 32
192 case None -> {}
193 }
194 when (grid.focus) {
195 case Some(_) -> bits += 64
196 case None -> {}
197 }
198 return bits
201private fun collectionCallback(application: ApplicationOwner, provider: AccessibilityProvider):
202ForeignCallback<(Ptr<Unit>, Long, Int, Long, Int, Long, Ptr<UInt8>?, Long, Ptr<Unit>?) -> Int> {
203 @Unsafe
204 {
205 return foreignCallback<
206 (Ptr<Unit>, Long, Int, Long, Int, Long, Ptr<UInt8>?, Long, Ptr<Unit>?) -> Int
207 >(
208 {
209 owner: Long,
210 operation: Int,
211 index: Long,
212 kind: Int,
213 number: Long,
214 bytes: Ptr<UInt8>?,
215 length: Long,
216 reply: Ptr<Unit>?
217 ->
218 queryCollection(
219 application,
220 provider,
221 owner,
222 operation,
223 index,
224 kind,
225 number,
226 bytes,
227 length,
228 reply
229 )
230 },
231 0,
232 ForeignCallbackMode.Reusable
233 )
234 }
237private fun queryCollection(
238 application: ApplicationOwner,
239 provider: AccessibilityProvider,
240 owner: Long,
241 operation: Int,
242 index: Long,
243 kind: Int,
244 number: Long,
245 bytes: Ptr<UInt8>?,
246 length: Long,
247 reply: Ptr<Unit>?
248): Int {
249 if ((operation >= 3 && operation <= 8) ||
250 (operation >= 12 && operation <= 15) ||
251 operation ==
252 17 ||
253 operation ==
254 18 ||
255 operation ==
256 19 ||
257 operation ==
258 20
259 ) {
260 val result = application
261 .performAccessibility {
262 val key = collectionKey(kind, number, bytes, length) ?:
263 return@performAccessibility SemanticActionResult.InvalidArgument
264 provider.performCollection(owner, key, operation, index)
265 }
266 return actionCode(result)
267 }
268 if (operation == 2 || operation == 16) {
269 try {
270 val source = provider.collectionSource(owner, true) ?: return 4
271 val key = collectionKey(kind, number, bytes, length) ?: return 4
272 if (operation == 16) {
273 val span = source.cellSpanForIndex(key, index) ?: return 2
274 val target = reply ?: return 4
275 val grid = source.grid!!
276 writeCollectionItem(
277 target,
278 SemanticItem(
279 grid.columns[span.column].key,
280 span.column,
281 grid.columns.size,
282 SemanticItemProperties(),
283 span.columnSpan
284 )
285 )
286 return 1
287 }
288 return if (source.contains(key)) 1 else 2
289 } catch (error: Throwable) {
290 application.failAccessibility(error)
291 return 4
292 }
293 }
294 val status = CollectionQueryStatus()
295 val result = application
296 .performAccessibility {
297 val source = provider.collectionSource(owner, false)
298 when (source) {
299 case Some(val value) -> {
300 val item = if (operation == 9 || operation == 10) {
301 queryCollectionText(
302 value,
303 operation,
304 index,
305 kind,
306 number,
307 bytes,
308 length,
309 reply
310 )
311 } else if (operation == 11) {
312 val key = collectionKey(kind, number, bytes, length)
313 when ((key, value.grid)) {
314 case(Some(val row), Some(val grid)) ->
315 if (index >= 0L && index < grid.columns.size)
316 value.cellForKey(row, grid.columns[index].key)
317 else
318 SemanticItemResult.NotFound
319 else -> SemanticItemResult.Unavailable
320 }
321 } else if (operation == 0)
322 value.itemAt(index)
323 else {
324 val key = collectionKey(kind, number, bytes, length)
325 when (key) {
326 case Some(val actual) -> value.itemForKey(actual)
327 case None -> SemanticItemResult.Unavailable
328 }
329 }
330 status.value = when (item) {
331 case Found(val found) ->
332 when (reply) {
333 case Some(val target) -> {
334 writeCollectionItem(target, found);
335 1
336 }
338 case None -> 4
339 }
340 case NotFound -> 2
341 case Unavailable -> 3
342 case Unsupported -> 5
343 }
344 }
346 case None -> {}
347 }
348 SemanticActionResult.Completed
349 }
350 return if (result == SemanticActionResult.Completed) status.value else 0
353private fun publishCollectionDetails(pointer: Ptr<Unit>, node: AccessibilityNode) {
354 when (node.properties.selectionMode) {
355 case Some(val mode) ->
356 @Unsafe
357 {
358 checkNativeStatus(
359 nativeCollectionSelection(
360 pointer,
361 node.id.number,
362 if (mode == SemanticSelectionMode.Multiple) 1 else 0,
363 if (node.properties.required) 1 else 0
364 ),
365 "publish static selection"
366 )
367 }
368 case None -> {}
369 }
370 val source = node.collectionValue ?: return
371 when (source.selection) {
372 case Some(val selected) -> {
373 @Unsafe
374 {
375 checkNativeStatus(
376 nativeCollectionSelection(
377 pointer,
378 node.id.number,
379 if (selected.multiple) 1 else 0,
380 if (selected.required) 1 else 0
381 ),
382 "publish collection selection"
383 )
384 }
385 for (key in selected.keys) {
386 when (source.itemForKey(key)) {
387 case Found(val item) -> {
388 if (!item.properties.selectable) continue
389 }
391 else -> continue
392 }
393 withCollectionKey(key) { kind, number, bytes, length ->
394 @Unsafe
395 {
396 checkNativeStatus(
397 nativeCollectionSelected(
398 pointer,
399 node.id.number,
400 kind,
401 number,
402 bytes,
403 length
404 ),
405 "publish selected collection item"
406 )
407 }
408 }
409 }
410 }
412 case None -> {}
413 }
414 when (source.grid) {
415 case Some(val grid) ->
416 for (index in 0L..<grid.columns.size) {
417 val column = grid.columns[index]
418 withCollectionKey(column.key) { kind, number, bytes, length ->
419 @Unsafe
420 {
421 checkNativeStatus(
422 nativeCollectionColumn(
423 pointer,
424 node.id.number,
425 index,
426 kind,
427 number,
428 bytes,
429 length,
430 column.header?.number ?: 0L
431 ),
432 "publish semantic column"
433 )
434 }
435 }
436 when (column.name) {
437 case Some(val name) ->
438 @Unsafe
439 {
440 name
441 .withUtf8Bytes { bytes, length ->
442 @Unsafe
443 {
444 checkNativeStatus(
445 nativeCollectionColumnName(
446 pointer,
447 node.id.number,
448 index,
449 bytes,
450 length
451 ),
452 "publish semantic column name"
453 )
454 }
455 }
456 }
457 case None -> {}
458 }
459 }
460 case None -> {}
461 }
464@Extern(name = "scoop_window_accessibility_collection_selection")
465private fun nativeCollectionSelection(
466 provider: Ptr<Unit>,
467 owner: Long,
468 kind: Int,
469 required: Int
470): Long
472@Extern(name = "scoop_window_accessibility_collection_selected")
473private fun nativeCollectionSelected(
474 provider: Ptr<Unit>,
475 owner: Long,
476 kind: Int,
477 number: Long,
478 text: Ptr<UInt8>?,
479 length: Long
480): Long
482@Extern(name = "scoop_window_accessibility_collection_column")
483private fun nativeCollectionColumn(
484 provider: Ptr<Unit>,
485 owner: Long,
486 index: Long,
487 kind: Int,
488 number: Long,
489 text: Ptr<UInt8>?,
490 length: Long,
491 header: Long
492): Long
494@Extern(name = "scoop_window_accessibility_collection_column_name")
495private fun nativeCollectionColumnName(
496 provider: Ptr<Unit>,
497 owner: Long,
498 index: Long,
499 text: Ptr<UInt8>,
500 length: Long
501): Long
503@Extern(name = "scoop_window_accessibility_collection_cell")
504private fun nativeCollectionCell(
505 provider: Ptr<Unit>,
506 owner: Long,
507 kind: Int,
508 number: Long,
509 text: Ptr<UInt8>?,
510 length: Long
511): Long
513private fun collectionKey(
514 kind: Int,
515 number: Long,
516 bytes: Ptr<UInt8>?,
517 length: Long
518): SemanticItemKey? {
519 if (kind == 0) return Some(SemanticItemKey.Number(number))
520 if (kind != 1 || length < 0L) return None
521 when (bytes) {
522 case Some(val text) -> {
523 @Unsafe
524 {
525 return Some(SemanticItemKey.Text(String.fromUtf8(text, length)))
526 }
527 }
529 case None -> return if (length == 0L) Some(SemanticItemKey.Text("")) else None
530 }
533private fun withCollectionKey(key: SemanticItemKey, block: (Int, Long, Ptr<UInt8>?, Long) -> Unit) {
534 when (key) {
535 case Number(val number) -> block(0, number, None, 0L)
537 case Text(val text) -> {
538 @Unsafe
539 {
540 text.withUtf8Bytes { bytes, length -> block(1, 0L, Some(bytes), length) }
541 }
542 }
543 }
546private fun writeCollectionItem(reply: Ptr<Unit>, item: SemanticItem) {
547 val props = item.properties
548 @Unsafe
549 {
550 nativeCollectionItem(
551 reply,
552 item.index,
553 item.count,
554 item.span,
555 if (props.enabled) 1 else 0,
556 when (props.selected) {
557 case None -> -1;
558 case Some(val value) -> if (value) 1 else 0
559 },
560 when (props.expanded) {
561 case None -> -1;
562 case Some(val value) -> if (value) 1 else 0
563 },
564 if (props.focusable) 1 else 0,
565 props.hierarchyLevel ?: -1L,
566 if (item.hasCells) 1 else 0,
567 when (props.checked) {
568 case Some(Off) -> 0
569 case Some(On) -> 1
570 case Some(Mixed) -> 2
571 case None -> -1
572 },
573 if (props.busy) 1 else 0,
574 props.siblings?.index ?: -1L,
575 props.siblings?.count ?: -1L
576 )
577 }
578 withCollectionKey(item.key) { kind, number, bytes, length ->
579 @Unsafe
580 {
581 nativeCollectionItemKey(reply, kind, number, bytes, length)
582 }
583 }
584 when (props.parentKey) {
585 case Some(val parent) ->
586 withCollectionKey(parent) { kind, number, bytes, length ->
587 @Unsafe {
588 nativeCollectionItemParent(reply, kind, number, bytes, length)
589 }
590 }
591 case None -> {}
592 }
593 collectionText(reply, 0, props.name)
594 collectionText(reply, 1, props.description)
595 collectionText(reply, 2, props.automationId)
596 collectionText(reply, 3, props.value)
599private fun collectionText(reply: Ptr<Unit>, field: Int, value: String?) {
600 when (value) {
601 case Some(val text) -> {
602 @Unsafe
603 {
604 text
605 .withUtf8Bytes { bytes, length ->
606 @Unsafe
607 {
608 nativeCollectionItemText(reply, field, bytes, length)
609 }
610 }
611 }
612 }
614 case None -> {}
615 }
618@Extern(name = "scoop_window_accessibility_collection_callback")
619private fun nativeCollectionCallback(
620 provider: Ptr<Unit>,
621 callback: FunPtr<(Ptr<Unit>, Long, Int, Long, Int, Long, Ptr<UInt8>?, Long, Ptr<Unit>?) -> Int>,
622 context: Ptr<Unit>
625@Extern(name = "scoop_window_accessibility_collection")
626private fun nativeCollectionSource(
627 provider: Ptr<Unit>,
628 number: Long,
629 count: Long,
630 revision: ULong,
631 actions: Int
632): Long
634@Extern(name = "scoop_window_accessibility_collection_row")
635private fun nativeCollectionRow(
636 provider: Ptr<Unit>,
637 node: Long,
638 kind: Int,
639 number: Long,
640 text: Ptr<UInt8>?,
641 length: Long
642): Long
644@Extern(name = "scoop_window_accessibility_collection_item")
645private fun nativeCollectionItem(
646 reply: Ptr<Unit>,
647 index: Long,
648 count: Long,
649 span: Long,
650 enabled: Int,
651 selected: Int,
652 expanded: Int,
653 focusable: Int,
654 hierarchyLevel: Long,
655 hasCells: Int,
656 checked: Int,
657 busy: Int,
658 siblingIndex: Long,
659 siblingCount: Long
662@Extern(name = "scoop_window_accessibility_collection_item_key")
663private fun nativeCollectionItemKey(
664 reply: Ptr<Unit>,
665 kind: Int,
666 number: Long,
667 text: Ptr<UInt8>?,
668 length: Long
671@Extern(name = "scoop_window_accessibility_collection_item_parent")
672private fun nativeCollectionItemParent(
673 reply: Ptr<Unit>,
674 kind: Int,
675 number: Long,
676 text: Ptr<UInt8>?,
677 length: Long
680@Extern(name = "scoop_window_accessibility_collection_item_text")
681private fun nativeCollectionItemText(reply: Ptr<Unit>, field: Int, text: Ptr<UInt8>, length: Long)
683@CLayout
684private struct NativeCollectionSearch(val bytes: Ptr<UInt8>?, val length: Long)
686@Extern(name = "scoop_window_accessibility_collection_search")
687private fun nativeCollectionSearch(reply: Ptr<Unit>): NativeCollectionSearch
689private fun queryCollectionText(
690 source: SemanticCollectionSnapshot,
691 operation: Int,
692 hasAfter: Long,
693 kind: Int,
694 number: Long,
695 bytes: Ptr<UInt8>?,
696 length: Long,
697 reply: Ptr<Unit>?
698): SemanticItemResult {
699 val target = reply ?: return SemanticItemResult.Unavailable
700 @Unsafe
701 {
702 val query = nativeCollectionSearch(target)
703 if (query.length < 0L) return SemanticItemResult.Unavailable
704 val text = when (query.bytes) {
705 case Some(val value) -> String.fromUtf8(value, query.length)
706 case None -> if (query.length == 0L) "" else return SemanticItemResult.Unavailable
707 }
708 val after = if (hasAfter == 0L) None else collectionKey(kind, number, bytes, length)
709 if (hasAfter != 0L && after == None) return SemanticItemResult.Unavailable
710 return source
711 .find(
712 if (operation == 9)
713 SemanticItemQuery.Name(text)
714 else
715 SemanticItemQuery.AutomationId(text),
716 after
717 )
718 }