Scoop API

scoop:scoop.window:0.1.0

src/accessibility/bridge.scoop

Selected production source · src/accessibility/bridge.scoop

1package scoop.window.accessibility
3import scoop.window.*
4import scoop.window.accessibility.native.NativeSemanticHitTest
5import scoop.window.accessibility.native.NativeSemanticTextGeometry
7/** Attaches before or after window configuration; closes before native destruction. */
8public fun Window.createAccessibilityProvider(): AccessibilityProvider {
9 owner.requireOpen()
10 when (owner.accessibility) {
11 case Some(_) ->
12 throw IllegalStateException(
13 Some("window already has an accessibility provider")
14 )
15 case None -> {}
16 }
17 val provider = AccessibilityProvider(owner)
18 val application = owner.application
19 @Unsafe
20 {
21 val callback = foreignCallback<
22 (Ptr<Unit>, Long, Int, Ptr<UInt8>?, Long, Long, Long, ULong, Double) -> Int
23 >(
24 {
25 number: Long,
26 action: Int,
27 bytes: Ptr<UInt8>?,
28 length: Long,
29 anchor: Long,
30 cursor: Long,
31 revision: ULong,
32 position: Double
33 ->
34 actionCode(
35 application
36 .performAccessibility {
37 val value = if (action == 2 || action == 6)
38 actionText(bytes, length)
39 else
40 None
41 provider
42 .perform(
43 number,
44 action,
45 value,
46 Utf8Selection(anchor, cursor),
47 revision,
48 position
49 )
50 }
51 )
52 },
53 0,
54 ForeignCallbackMode.Reusable
55 )
56 val collections = NativeCollectionQueries(application, provider)
57 try {
58 val pointer = created(
59 nativeAccessibilityCreate(owner.pointer!!, callback.function, callback.context),
60 "create accessibility provider"
61 )
62 collections.attach(pointer)
63 provider
64 .bind(
65 NativeAccessibilityProjection(application, pointer, callback, collections)
66 )
67 owner.accessibility = Some(provider)
68 return provider
69 } catch (error: Throwable) {
70 collections.close()
71 releaseForeignCallback(callback)
72 throw error
73 }
74 }
77private fun actionText(bytes: Ptr<UInt8>?, length: Long): String? {
78 when (bytes) {
79 case Some(val text) -> {
80 @Unsafe
81 {
82 return Some(String.fromUtf8(text, length))
83 }
84 }
86 case None -> return if (length == 0L) Some("") else None
87 }
90private class NativeAccessibilityProjection(
91 private val application: ApplicationOwner,
92 private val pointer: Ptr<Unit>,
93 private val callback: ForeignCallback<
94 (Ptr<Unit>, Long, Int, Ptr<UInt8>?, Long, Long, Long, ULong, Double) -> Int
95 >,
96 private val collections: NativeCollectionQueries
97) : AccessibilityProjection {
98 private val geometryPublisher = platformAccessibilityGeometryPublisher()
100 public override fun publish(
101 changed: Collection<AccessibilityNode>,
102 geometry: Collection<AccessibilityNode>,
103 removed: Array<Long>,
104 focused: Long
105 ) {
106 @Unsafe
107 {
108 checkNativeStatus(nativeAccessibilityBegin(pointer), "begin accessibility publication")
109 var committed = false
110 try {
111 for (number in removed)
112 checkNativeStatus(
113 nativeAccessibilityRemove(pointer, number),
114 "remove accessibility node"
115 )
116 for (node in changed)
117 publishNode(node)
118 if (geometry.size > 0L) {
119 val batch = geometryPublisher
120 when (batch) {
121 case Some(val stage) -> stage(pointer, geometry)
122 case None ->
123 for (node in geometry)
124 publishNode(node)
125 }
126 }
127 checkNativeStatus(
128 nativeAccessibilityCommit(pointer, focused),
129 "commit accessibility publication"
130 )
131 committed = true
132 } finally {
133 if (!committed) nativeAccessibilityAbort(pointer)
134 }
135 }
136 }
138 private fun publishNode(node: AccessibilityNode) {
139 @Unsafe
140 {
141 checkNativeStatus(
142 nativeAccessibilityPut(pointer, describe(node)),
143 "publish accessibility node"
144 )
145 collections.publish(pointer, node)
146 when (val hit = node.hit) {
147 case Some(val value) ->
148 checkNativeStatus(
149 nativeAccessibilityHit(pointer, node.id.number, value.native),
150 "publish accessibility hit geometry"
151 )
152 case None -> {}
153 }
154 when (val geometry = node.textGeometry) {
155 case Some(val value) ->
156 checkNativeStatus(
157 nativeAccessibilityTextGeometry(
158 pointer,
159 node.id.number,
160 value.native
161 ),
162 "publish accessibility text geometry"
163 )
164 case None -> {}
165 }
166 text(node.id.number, 0, node.resolvedName)
167 text(node.id.number, 1, node.resolvedDescription)
168 text(node.id.number, 2, node.properties.automationId)
169 text(node.id.number, 3, node.properties.value)
170 text(node.id.number, 4, node.resolvedError)
171 text(node.id.number, 5, node.properties.url)
172 checkNativeStatus(
173 nativeAccessibilityDocument(
174 pointer,
175 node.id.number,
176 when (node.documentValue) {
177 case Some(val value) -> value.pointer;
178 case None -> None
179 },
180 node.documentRevision,
181 node.documentEpoch
182 ),
183 "publish semantic text document"
184 )
185 for (child in node.textChildValues)
186 checkNativeStatus(
187 nativeAccessibilityTextChild(
188 pointer,
189 node.id.number,
190 child.node.number,
191 child.range.start,
192 child.range.end
193 ),
194 "publish semantic text child"
195 )
196 for (id in node.relationValues.labelledBy)
197 relation(node.id.number, 0, id.number)
198 for (id in node.relationValues.controls)
199 relation(node.id.number, 1, id.number)
200 for (id in node.relationValues.describedBy)
201 relation(node.id.number, 3, id.number)
202 for (id in node.relationValues.errorMessages)
203 relation(node.id.number, 4, id.number)
204 for (id in node.relationValues.rowHeaders)
205 relation(node.id.number, 5, id.number)
206 for (id in node.relationValues.columnHeaders)
207 relation(node.id.number, 6, id.number)
208 when (node.relationValues.activeDescendant) {
209 case Some(Node(val id)) -> relation(node.id.number, 7, id.number)
210 else -> {}
211 }
212 for (source in node.referrers.values)
213 if (source.active &&
214 source.properties.name ==
215 None &&
216 source.relationValues.labelledBy.size ==
217 1L &&
218 source.relationValues.labelledBy[0L] ==
219 node.id
220 )
221 relation(node.id.number, 2, source.id.number)
222 }
223 }
225 private fun relation(number: Long, field: Int, target: Long) {
226 @Unsafe
227 {
228 checkNativeStatus(
229 nativeAccessibilityRelation(pointer, number, field, target),
230 "publish accessibility relation"
231 )
232 }
233 }
235 private fun text(number: Long, field: Int, value: String?) {
236 @Unsafe
237 {
238 when (value) {
239 case Some(val string) ->
240 string
241 .withUtf8Bytes { bytes, length ->
242 @Unsafe
243 {
244 checkNativeStatus(
245 nativeAccessibilityText(
246 pointer,
247 number,
248 field,
249 Some(bytes),
250 length
251 ),
252 "publish accessibility text"
253 )
254 }
255 }
256 case None ->
257 checkNativeStatus(
258 nativeAccessibilityText(pointer, number, field, None, -1L),
259 "clear accessibility text"
260 )
261 }
262 }
263 }
265 public override fun retire(number: Long) {
266 @Unsafe
267 {
268 nativeAccessibilityRetire(pointer, number)
269 }
270 }
272 public override fun announce(number: Long, text: String, priority: SemanticLive) {
273 @Unsafe
274 {
275 text
276 .withUtf8Bytes { bytes, length ->
277 @Unsafe
278 {
279 nativeAccessibilityAnnounce(
280 pointer,
281 number,
282 bytes,
283 length,
284 if (priority == SemanticLive.Assertive) 2 else 1
285 )
286 }
287 }
288 }
289 }
291 public override fun close() {
292 @Unsafe
293 {
294 nativeAccessibilityClose(pointer)
295 }
296 val owned = callback
297 collections.close()
298 application
299 .mailbox
300 .postControl {
301 @Unsafe
302 {
303 releaseForeignCallback(owned)
304 }
305 }
306 }
309private fun describe(node: AccessibilityNode): NativeSemanticNode {
310 val value = node.properties
311 val orientation = when (value.scroll) {
312 case Some(val scroll) -> Some(scroll.orientation);
313 case None -> value.orientation
314 }
315 val bounds = node.placement?.bounds ?:
316 LogicalRect(LogicalPosition(0.0, 0.0), LogicalSize(0.0, 0.0))
317 val visible = when (val geometry = node.placement) {
318 case Some(val placed) -> placed.visibleBounds
319 case None -> None
320 }
321 val visibleRect = visible ?: LogicalRect(LogicalPosition(0.0, 0.0), LogicalSize(0.0, 0.0))
322 return NativeSemanticNode(
323 node.id.number,
324 node.parentNode?.id?.number ?: 0L,
325 node.order,
326 node.sceneOrder,
327 node.revision,
328 node.textRevision,
329 value.selection?.anchor ?: -1L,
330 value.selection?.cursor ?: -1L,
331 when (node.role) {
332 case Group -> 0;
333 case Text -> 1;
334 case Button -> 2;
335 case Checkbox -> 3;
336 case TextField -> 4;
337 case ScrollArea -> 5;
338 case ScrollBar -> 6
339 case Switch -> 7
340 case ToggleButton -> 8
341 case RadioButton -> 9
342 case RadioGroup -> 10
343 case Image -> 11
344 case List -> 12
345 case Link -> 13
346 case Heading -> 14
347 case Slider -> 15
348 case ProgressBar -> 16
349 case Menu -> 17
350 case MenuItem -> 18
351 case TabGroup -> 19
352 case Tab -> 20
353 case Dialog -> 21
354 case Table -> 22
355 case Cell -> 23
356 case RowHeader -> 24
357 case ColumnHeader -> 25
358 case Disclosure -> 26
359 case Separator -> 27
360 case Row -> 28
361 case Tree -> 29
362 case TreeItem -> 30
363 },
364 flag(value.enabled) +
365 2 *
366 flag(value.readOnly) +
367 4 *
368 flag(value.protectedText) +
369 8 *
370 flag(value.hidden) +
371 16 *
372 flag(value.name != None) +
373 32 *
374 flag(value.ignored) +
375 64 *
376 flag(value.required) +
377 128 *
378 flag(value.selection != None && value.cursorAffinity == SemanticTextAffinity.Upstream) +
379 256 *
380 flag(value.invalid) +
381 512 *
382 flag(value.busy) +
383 1024 *
384 flag(value.modal) +
385 2048 *
386 flag(value.multiline) +
387 4096 *
388 flag(value.scroll != None),
389 node.actions.capabilities,
390 when (value.checked) {
391 case None -> -1;
392 case Some(Off) -> 0;
393 case Some(On) -> 1;
394 case Some(Mixed) -> 2
395 },
396 flag(node.placement != None),
397 flag(visibleRect.size.width > 0.0 && visibleRect.size.height > 0.0),
398 when (orientation) {
399 case Some(Horizontal) -> 1;
400 case Some(Vertical) -> 2;
401 case None -> 0
402 },
403 bounds.origin.x,
404 bounds.origin.y,
405 bounds.size.width,
406 bounds.size.height,
407 visibleRect.origin.x,
408 visibleRect.origin.y,
409 visibleRect.size.width,
410 visibleRect.size.height,
411 value.scroll?.offset ?: 0.0,
412 value.scroll?.viewport ?: 0.0,
413 value.scroll?.content ?: 0.0,
414 optionalFlag(value.selected),
415 optionalFlag(value.expanded),
416 value.headingLevel ?: 0,
417 flag(value.range != None),
418 when (value.live) {
419 case Off -> 0;
420 case Polite -> 1;
421 case Assertive -> 2
422 },
423 value.range?.minimum ?: 0.0,
424 value.range?.maximum ?: 0.0,
425 value.range?.value ?: 0.0,
426 value.range?.step ?: ((value.scroll?.lineStep ?: None) ?: 0.0),
427 value.table?.rows ?: -1L,
428 value.table?.columns ?: -1L,
429 value.cell?.row ?: -1L,
430 value.cell?.column ?: -1L,
431 value.cell?.rowSpan ?: 0L,
432 value.cell?.columnSpan ?: 0L,
433 flag(node.activation != None),
434 node.activation?.x ?: 0.0,
435 node.activation?.y ?: 0.0
436 )
439private fun optionalFlag(value: Boolean?): Int = when (value) {
440 case Some(val actual) -> if (actual) 1 else 0
441 case None -> -1
444@CLayout
445private struct NativeSemanticNode(
446 val number: Long,
447 val parent: Long,
448 val order: Long,
449 val sceneOrder: Long,
450 val contentRevision: ULong,
451 val textRevision: ULong,
452 val anchor: Long,
453 val cursor: Long,
454 val role: Int,
455 val flags: Int,
456 val actions: Int,
457 val checked: Int,
458 val geometry: Int,
459 val visible: Int,
460 val scrollOrientation: Int,
461 val x: Double,
462 val y: Double,
463 val width: Double,
464 val height: Double,
465 val visibleX: Double,
466 val visibleY: Double,
467 val visibleWidth: Double,
468 val visibleHeight: Double,
469 val scrollOffset: Double,
470 val scrollViewport: Double,
471 val scrollContent: Double,
472 val selected: Int,
473 val expanded: Int,
474 val headingLevel: Int,
475 val hasRange: Int,
476 val live: Int,
477 val minimum: Double,
478 val maximum: Double,
479 val numberValue: Double,
480 val numberStep: Double,
481 val tableRows: Long,
482 val tableColumns: Long,
483 val cellRow: Long,
484 val cellColumn: Long,
485 val rowSpan: Long,
486 val columnSpan: Long,
487 val activation: Int,
488 val activationX: Double,
489 val activationY: Double
492@Extern(name = "scoop_window_accessibility_create")
493private fun nativeAccessibilityCreate(
494 window: Ptr<Unit>,
495 callback: FunPtr<(Ptr<Unit>, Long, Int, Ptr<UInt8>?, Long, Long, Long, ULong, Double) -> Int>,
496 context: Ptr<Unit>
497): NativeCreateResult
499@Extern(name = "scoop_window_accessibility_begin")
500private fun nativeAccessibilityBegin(provider: Ptr<Unit>): Long
502@Extern(name = "scoop_window_accessibility_put")
503private fun nativeAccessibilityPut(provider: Ptr<Unit>, value: NativeSemanticNode): Long
505@Extern(name = "scoop_window_accessibility_document")
506private fun nativeAccessibilityDocument(
507 provider: Ptr<Unit>,
508 node: Long,
509 document: Ptr<Unit>?,
510 revision: ULong,
511 epoch: ULong
512): Long
514@Extern(name = "scoop_window_accessibility_text")
515private fun nativeAccessibilityText(
516 provider: Ptr<Unit>,
517 number: Long,
518 field: Int,
519 text: Ptr<UInt8>?,
520 length: Long
521): Long
523@Extern(name = "scoop_window_accessibility_relation")
524private fun nativeAccessibilityRelation(
525 provider: Ptr<Unit>,
526 number: Long,
527 field: Int,
528 target: Long
529): Long
531@Extern(name = "scoop_window_accessibility_hit")
532private fun nativeAccessibilityHit(
533 provider: Ptr<Unit>,
534 number: Long,
535 geometry: NativeSemanticHitTest
536): Long
538@Extern(name = "scoop_window_accessibility_remove")
539private fun nativeAccessibilityRemove(provider: Ptr<Unit>, number: Long): Long
541@Extern(name = "scoop_window_accessibility_text_geometry")
542private fun nativeAccessibilityTextGeometry(
543 provider: Ptr<Unit>,
544 number: Long,
545 geometry: NativeSemanticTextGeometry
546): Long
548@Extern(name = "scoop_window_accessibility_commit")
549private fun nativeAccessibilityCommit(provider: Ptr<Unit>, focused: Long): Long
551@Extern(name = "scoop_window_accessibility_abort")
552private fun nativeAccessibilityAbort(provider: Ptr<Unit>)
554@Extern(name = "scoop_window_accessibility_retire")
555private fun nativeAccessibilityRetire(provider: Ptr<Unit>, number: Long)
557@Extern(name = "scoop_window_accessibility_close")
558private fun nativeAccessibilityClose(provider: Ptr<Unit>)
560@Extern(name = "scoop_window_accessibility_announce")
561private fun nativeAccessibilityAnnounce(
562 provider: Ptr<Unit>,
563 number: Long,
564 text: Ptr<UInt8>,
565 length: Long,
566 priority: Int
569@Extern(name = "scoop_window_accessibility_text_child")
570private fun nativeAccessibilityTextChild(
571 provider: Ptr<Unit>,
572 node: Long,
573 child: Long,
574 start: Long,
575 end: Long
576): Long