Scoop API

scoop:scoop.ui:0.1.0

src/accessibility/scene.scoop

Selected production source · src/accessibility/scene.scoop

1package scoop.ui
3import scoop.collections.HashMap
4import scoop.graphics.*
5import scoop.graphics.native.*
6import scoop.window.LogicalRect
7import scoop.window.LogicalPosition
8import scoop.window.LogicalSize
9import scoop.window.accessibility.*
10import scoop.window.accessibility.native.NativeSemanticHitTest
11import scoop.window.accessibility.native.NativeSemanticTextGeometry
12import scoop.ui.text.NativeTextSource
14private class SemanticPlacement(
15 val generation: ULong,
16 private val geometry: SemanticGeometry,
17 private val order: Long,
18 private val hit: NativeGeometry,
19 val reveal: SemanticRevealPlacement
20): Closeable {
21 private var closed = false
22 init {
23 @Unsafe
24 {
25 hit.retain(hit.context)
26 }
27 }
28 fun apply(node: AccessibilityNode) {
29 @Unsafe
30 {
31 node.setGeometry(
32 geometry,
33 NativeSemanticHitTest(hit.context, hit.retain, hit.release, hit.contains),
34 order
35 )
36 }
37 }
38 public override fun close() {
39 if (closed)
40 return
41 closed = true
42 @Unsafe
43 {
44 hit.release(hit.context)
45 }
46 }
49/** A candidate owns immutable native snapshots until matching accepted geometry is installed. */
50internal class SemanticScene: Closeable {
51 private val placements = HashMap<SemanticNodeId, SemanticPlacement>()
52 private val text = HashMap<SemanticNodeId, SemanticTextPlacement>()
54 fun captureText(
55 binding: SemanticTextBinding,
56 canvas: Canvas,
57 bounds: Rect,
58 source: NativeTextSource,
59 path: Array<ScrollReveal>
60 ) {
61 val handle = binding.handle
62 requireUi(!text.containsKey(handle.id), "semantic text was already placed this frame")
63 @Unsafe
64 {
65 captureGeometry(canvas, bounds).use {
66 geometry -> @Unsafe
67 {
68 geometry.withNativeGeometry {
69 scene -> @Unsafe
70 {
71 source.withGeometry(scene) {
72 native ->
73 val placement =
74 SemanticTextPlacement(binding, handle.generation, native, path)
75 try {
76 text[handle.id] = placement
77 } catch (error: Throwable) {
78 placement.close()
79 throw error
80 }
81 }
82 }
83 }
84 }
85 }
86 }
87 }
89 fun capture(handle: SemanticsHandle, canvas: Canvas, rect: Rect, path: Array<ScrollReveal>) {
90 check(handle)
91 @Unsafe
92 {
93 captureGeometry(canvas, rect).use { shape -> append(handle, shape, path) }
94 }
95 }
97 fun capture(handle: SemanticsHandle, canvas: Canvas, shape: Path, path: Array<ScrollReveal>) {
98 check(handle)
99 @Unsafe
100 {
101 captureGeometry(canvas, shape).use { value -> append(handle, value, path) }
102 }
103 }
105 private fun check(handle: SemanticsHandle) {
106 requireUi(!placements.containsKey(handle.id), "semantic node was already placed this frame")
107 }
109 private fun append(handle: SemanticsHandle, shape: CanvasGeometry, path: Array<ScrollReveal>) {
110 val visible =
111 when (val value = shape.visibleBounds) {
112 case Some(val bounds) -> Some(semanticRect(bounds))
113 case None -> None
114 }
115 val rect = shape.bounds
116 val bounds = SemanticGeometry(semanticRect(rect), visible)
117 val revealed =
118 when (visible) {
119 case Some(val value) -> value.size.width > 0.0 && value.size.height > 0.0
120 case None -> false
121 }
122 @Unsafe
123 {
124 shape.withNativeGeometry {
125 hit ->
126 val placement =
127 SemanticPlacement(
128 handle.generation,
129 bounds,
130 placements.size,
131 hit,
132 SemanticRevealPlacement(handle.generation, path, rect, revealed)
133 )
134 try {
135 placements[handle.id] = placement
136 } catch (error: Throwable) {
137 placement.close()
138 throw error
139 }
140 }
141 }
142 }
144 fun apply(handle: SemanticsHandle) {
145 when (handle.textBinding) {
146 case Some(val binding) -> binding.path = None;
147 case None -> {}
148 }
149 when (val placement = placements[handle.id]) {
150 case Some(val value) -> if (value.generation == handle.generation) {
151 value.apply(handle.resource)
152 handle.revealPlacement = Some(value.reveal)
153 text[handle.id]?.apply(handle)
154 return
155 }
156 case None -> {}
157 }
158 handle.resource.geometry = None
159 handle.revealPlacement = None
160 }
162 public override fun close() {
163 for (placement in placements.values)
164 placement.close()
165 placements.clear()
166 for (placement in text.values)
167 placement.close()
168 text.clear()
169 }
172private class SemanticTextPlacement(
173 private val binding: SemanticTextBinding,
174 private val generation: ULong,
175 private val native: NativeSemanticTextGeometry,
176 private val path: Array<ScrollReveal>
177): Closeable {
178 private var closed = false
179 init {
180 @Unsafe
181 {
182 native.retain(native.context)
183 }
184 }
185 fun apply(handle: SemanticsHandle) {
186 if (binding.active && generation == handle.generation)
187 @Unsafe
188 {
189 handle.resource.setTextGeometry(Some(native))
190 binding.path = Some(path)
191 }
192 }
193 public override fun close() {
194 if (closed)
195 return
196 closed = true
197 @Unsafe
198 {
199 native.release(native.context)
200 }
201 }
204private fun semanticRect(rect: Rect): LogicalRect =
205 LogicalRect(LogicalPosition(rect.x, rect.y), LogicalSize(rect.width, rect.height))