Scoop API

scoop:scoop.window:0.1.0

platform/darwin/accessibility_geometry.scoop

Selected production source · platform/darwin/accessibility_geometry.scoop

1package scoop.window
3import scoop.window.accessibility.AccessibilityNode
4import scoop.window.accessibility.native.NativeSemanticHitTest
5import scoop.window.accessibility.native.NativeSemanticTextGeometry
7internal fun platformAccessibilityGeometryPublisher(): (
8 (Ptr<Unit>, Collection<AccessibilityNode>) -> Unit
9)? = Some { pointer, nodes -> stageAccessibilityGeometry(pointer, nodes) }
11private fun stageAccessibilityGeometry(pointer: Ptr<Unit>, nodes: Collection<AccessibilityNode>) {
12 val count = nodes.size
13 val bytesPerNode = sizeOf<NativeAccessibilityGeometry>() +
14 sizeOf<NativeSemanticHitTest>() +
15 sizeOf<NativeSemanticTextGeometry>()
16 requireWindow(
17 count.toULong() <= ULong.MAX_VALUE / bytesPerNode,
18 "accessibility geometry batch is too large"
19 )
20 @Unsafe
21 {
22 val storage = allocateGeometry(count.toULong() * bytesPerNode) ?:
23 throw IllegalStateException(Some("cannot allocate accessibility geometry batch"))
24 try {
25 // The three C-layout arrays have the same alignment.
26 val records = storage.cast<NativeAccessibilityGeometry>()
27 val hits = (records + count).cast<NativeSemanticHitTest>()
28 val texts = (hits + count).cast<NativeSemanticTextGeometry>()
29 var index = 0L
30 for (node in nodes) {
31 val hit = when (val snapshot = node.hit) {
32 case Some(val value) -> {
33 val address = hits + index
34 address.store(value.native)
35 Some(address)
36 }
38 case None -> None
39 }
40 val text = when (val snapshot = node.textGeometry) {
41 case Some(val value) -> {
42 val address = texts + index
43 address.store(value.native)
44 Some(address)
45 }
47 case None -> None
48 }
49 val placement = node.placement
50 val bounds = placement?.bounds ?: emptyGeometry
51 val visible = (placement?.visibleBounds ?: None) ?: emptyGeometry
52 val activation = node.activation
53 records
54 .store(
55 index,
56 NativeAccessibilityGeometry(
57 node.id.number,
58 node.sceneOrder,
59 flag(placement != None),
60 flag(visible.size.width > 0.0 && visible.size.height > 0.0),
61 bounds.origin.x, bounds.origin.y, bounds.size.width, bounds.size.height,
62 visible.origin.x, visible.origin.y, visible.size.width, visible
63 .size
64 .height,
65 flag(activation != None), activation?.x ?: 0.0, activation?.y ?: 0.0,
66 hit, text
67 )
68 )
69 index += 1L
70 }
71 checkNativeStatus(
72 nativeAccessibilityStageGeometry(pointer, records, count),
73 "stage accessibility geometry"
74 )
75 } finally {
76 freeGeometry(storage)
77 }
78 }
81private val emptyGeometry = LogicalRect(LogicalPosition(0.0, 0.0), LogicalSize(0.0, 0.0))
83@CLayout
84private struct NativeAccessibilityGeometry(
85 val number: Long,
86 val sceneOrder: Long,
87 val geometry: Int,
88 val visible: Int,
89 val x: Double,
90 val y: Double,
91 val width: Double,
92 val height: Double,
93 val visibleX: Double,
94 val visibleY: Double,
95 val visibleWidth: Double,
96 val visibleHeight: Double,
97 val activation: Int,
98 val activationX: Double,
99 val activationY: Double,
100 val hit: Ptr<NativeSemanticHitTest>?,
101 val text: Ptr<NativeSemanticTextGeometry>?
104@Extern(name = "malloc")
105private fun allocateGeometry(bytes: ULong): Ptr<Unit>?
107@Extern(name = "free")
108private fun freeGeometry(storage: Ptr<Unit>)
110@Extern(name = "scoop_window_accessibility_stage_geometry")
111private fun nativeAccessibilityStageGeometry(
112 pointer: Ptr<Unit>,
113 records: Ptr<NativeAccessibilityGeometry>,
114 count: Long
115): Long