Scoop API

scoop:scoop.window:0.1.0

src/accessibility/provider.scoop

Selected production source · src/accessibility/provider.scoop

1package scoop.window.accessibility
3import scoop.collections.HashMap
4import scoop.window.*
6/** The platform projection owns no writable application state. */
7internal interface AccessibilityProjection : Closeable {
8 fun publish(
9 changed: Collection<AccessibilityNode>,
10 geometry: Collection<AccessibilityNode>,
11 removed: Array<Long>,
12 focused: Long
13 )
14 fun retire(number: Long)
15 fun announce(number: Long, text: String, priority: SemanticLive)
18/** Stages owner-thread semantic changes and publishes them independently of drawing. */
19public class AccessibilityProvider internal constructor(
20 internal val owner: WindowOwner
21) : Closeable {
22 private val nodes = HashMap<Long, AccessibilityNode>()
23 private val pending = HashMap<Long, AccessibilityNode>()
24 private val pendingGeometry = HashMap<Long, AccessibilityNode>()
25 private val removed = ArrayList<Long>()
26 private val announcements = HashMap<Long, SemanticAnnouncement>()
27 internal val roots = ArrayList<AccessibilityNode>()
28 private var focus: AccessibilityNode? = None
29 private var focusChanged = false
30 private var publishing = false
31 private var closed = false
32 private var projection: AccessibilityProjection? = None
34 internal fun bind(value: AccessibilityProjection) {
35 projection = Some(value)
36 }
38 public val isClosed: Boolean
39 get() {
40 checkThread()
41 return closed
42 }
44 /** The actual keyboard focus. Setting it does not request focus or run a handler. */
45 public var focused: AccessibilityNode?
46 get() {
47 checkOpen()
48 return focus
49 }
50 set(value) {
51 checkOpen()
52 checkParent(value)
53 when (val previous = focus) {
54 case Some(val old) ->
55 when (value) {
56 case Some(val next) -> if (old === next) return
57 case None -> {}
58 }
59 case None ->
60 when (value) {
61 case None -> return
62 case Some(_) -> {}
63 }
64 }
65 focus = value
66 focusChanged = true
67 }
69 public fun createNode(
70 role: SemanticRole,
71 parent: AccessibilityNode? = None
72 ): AccessibilityNode {
73 checkOpen()
74 checkParent(parent)
75 val sequence = owner.semanticIdentity
76 if (sequence.next == Long.MAX_VALUE)
77 throw IllegalStateException(Some("semantic node identity exhausted"))
78 val id = SemanticNodeId(sequence, owner.id, sequence.next)
79 sequence.next += 1L
80 val node = AccessibilityNode(this, id, role, parent)
81 nodes[id.number] = node
82 addChild(node)
83 changed(node)
84 return node
85 }
87 internal fun checkThread() {
88 owner.application.checkThread()
89 }
91 internal fun checkOpen() {
92 checkThread()
93 if (closed ||
94 (owner.phase != WindowState.Open && owner.phase != WindowState.PendingConfigure)
95 )
96 throw IllegalStateException(Some("accessibility provider is closed"))
97 if (publishing)
98 throw IllegalStateException(Some("accessibility publication is in progress"))
99 }
101 internal fun checkParent(value: AccessibilityNode?) {
102 when (value) {
103 case Some(val node) ->
104 requireWindow(
105 node.provider === this && node.active,
106 "semantic relation requires an active node in the same provider"
107 )
108 case None -> {}
109 }
110 }
112 internal fun addChild(node: AccessibilityNode) {
113 (node.parentNode?.children ?: roots).add(node)
114 }
116 internal fun removeChild(node: AccessibilityNode) {
117 val children = node.parentNode?.children ?: roots
118 for (index in 0L..<children.size) {
119 if (children[index] === node) {
120 children.removeAt(index)
121 return
122 }
123 }
124 }
126 internal fun changed(node: AccessibilityNode) {
127 if (node.active) {
128 pendingGeometry.remove(node.id.number)
129 pending[node.id.number] = node
130 }
131 }
133 internal fun geometryChanged(node: AccessibilityNode) {
134 if (node.active && !pending.containsKey(node.id.number))
135 pendingGeometry[node.id.number] = node
136 }
138 internal fun lookup(id: SemanticNodeId): AccessibilityNode? {
139 val node = nodes[id.number] ?: return None
140 return if (node.id == id) Some(node) else None
141 }
143 internal fun retire(node: AccessibilityNode) {
144 announcements.remove(node.id.number)
145 node.active = false
146 node.collectionValue = None
147 node.publishedCollection = None
148 node.documentValue = None
149 node.documentRevision = 0UL
150 node.itemActions = SemanticCollectionActions()
151 retireSemanticRelations(node)
152 node.actions = SemanticActions()
153 node.invalidateGeometry()
154 projection!!.retire(node.id.number)
155 for (child in node.children.toArray())
156 retire(child)
157 removeChild(node)
158 node.parentNode = None
159 pending.remove(node.id.number)
160 pendingGeometry.remove(node.id.number)
161 nodes.remove(node.id.number)
162 removed.add(node.id.number)
163 when (val current = focus) {
164 case Some(val value) ->
165 if (value === node) {
166 focus = None
167 focusChanged = true
168 }
169 case None -> {}
170 }
171 }
173 /** Publishes only staged changes. A native getter never observes a partial publication. */
174 public fun publish() {
175 checkOpen()
176 if (pending.size ==
177 0L &&
178 pendingGeometry.size ==
179 0L &&
180 removed.size ==
181 0L &&
182 !focusChanged &&
183 announcements.size ==
184 0L
185 )
186 return
187 publishing = true
188 try {
189 for (node in pending.values) {
190 resolveSemanticText(node)
191 val message = node.announcementText
192 if (node.hasPublished &&
193 node.properties.live !=
194 SemanticLive.Off &&
195 message !=
196 node.publishedMessage
197 ) {
198 when (message) {
199 case Some(val text) ->
200 announcements[node.id.number] = SemanticAnnouncement(
201 text,
202 node.properties.live
203 )
204 case None -> {}
205 }
206 }
207 }
208 projection!!
209 .publish(
210 pending.values,
211 pendingGeometry.values,
212 removed.toArray(),
213 focus?.id?.number ?: 0L
214 )
215 for (node in pending.values) {
216 node.publishedCollection = node.collectionValue
217 node.publishedMessage = node.announcementText
218 node.hasPublished = true
219 }
220 pending.clear()
221 pendingGeometry.clear()
222 removed.clear()
223 focusChanged = false
224 val delivered = ArrayList<Long>()
225 for (entry in announcements.entries) {
226 val node = nodes[entry.key]
227 when (node) {
228 case Some(val target) ->
229 if (!target.canAnnounce() || owner.inputBlocked) {
230 delivered.add(entry.key)
231 } else if (!target.announcementBusy) {
232 projection!!.announce(entry.key, entry.value.text, entry.value.priority)
233 delivered.add(entry.key)
234 }
235 case None -> delivered.add(entry.key)
236 }
237 }
238 for (number in delivered)
239 announcements.remove(number)
240 } finally {
241 publishing = false
242 }
243 }
245 internal fun announce(node: AccessibilityNode, text: String, priority: SemanticLive) {
246 if (priority == SemanticLive.Off || !node.canAnnounce() || owner.inputBlocked) return
247 announcements[node.id.number] = SemanticAnnouncement(text, priority)
248 changed(node)
249 }
251 internal fun collectionSource(number: Long, candidate: Boolean): SemanticCollectionSnapshot? {
252 if (closed ||
253 (owner.phase != WindowState.Open && owner.phase != WindowState.PendingConfigure) ||
254 owner.application.exiting
255 )
256 return None
257 val node = nodes[number] ?: return None
258 if (!node.active) return None
259 if (candidate) return if (publishing) node.collectionValue else None
260 return if (publishing) None else node.publishedCollection
261 }
263 internal fun performCollection(
264 number: Long,
265 key: SemanticItemKey,
266 operation: Int,
267 column: Long
268 ):
269 SemanticActionResult {
270 if (closed || owner.phase != WindowState.Open || owner.application.exiting)
271 return SemanticActionResult.Unavailable
272 if (owner.inputBlocked) return SemanticActionResult.Rejected
273 if (publishing) return SemanticActionResult.Busy
274 val node = nodes[number] ?: return SemanticActionResult.Unavailable
275 if (!node.canAct()) return SemanticActionResult.Rejected
276 val source = node.collectionValue ?: return SemanticActionResult.Unavailable
277 when (node.publishedCollection) {
278 case Some(val published) ->
279 if (source !== published) return SemanticActionResult.Unavailable
280 case None -> return SemanticActionResult.Unavailable
281 }
282 val item = when (source.itemForKey(key)) {
283 case Found(val value) -> value
284 else -> return SemanticActionResult.Unavailable
285 }
286 if (!item.properties.enabled) return SemanticActionResult.Rejected
287 if ((operation == 5 || operation == 6) && !item.properties.focusable)
288 return SemanticActionResult.Unsupported
289 if ((operation == 7 || operation == 8) && !item.properties.selectable)
290 return SemanticActionResult.Unsupported
291 if ((operation == 17 || operation == 18) && item.properties.expanded == None)
292 return SemanticActionResult.Unsupported
293 if ((operation == 19 || operation == 20) && item.properties.checked == None)
294 return SemanticActionResult.Unsupported
295 val actions = node.itemActions
296 val result = if (operation >= 12 && operation <= 15)
297 performGridCell(source, actions.grid, key, column, operation)
298 else
299 when (operation) {
300 case 3 -> actions.realize?.invoke(key) ?: SemanticActionResult.Unsupported
301 case 4 -> actions.reveal?.invoke(key) ?: SemanticActionResult.Unsupported
302 case 5 -> actions.focus?.invoke(key, true) ?: SemanticActionResult.Unsupported
303 case 6 -> actions.focus?.invoke(key, false) ?: SemanticActionResult.Unsupported
304 case 7 ->
305 when (source.selection) {
306 case Some(_) ->
307 actions.setSelected?.invoke(key, true) ?:
308 SemanticActionResult.Unsupported
309 case None -> SemanticActionResult.Unsupported
310 }
311 case 8 ->
312 when (source.selection) {
313 case Some(_) ->
314 actions.setSelected?.invoke(key, operation == 7) ?:
315 SemanticActionResult.Unsupported
316 case None -> SemanticActionResult.Unsupported
317 }
318 case 17 ->
319 actions.setExpanded?.invoke(key, true) ?: SemanticActionResult.Unsupported
320 case 18 ->
321 actions.setExpanded?.invoke(key, false) ?: SemanticActionResult.Unsupported
322 case 19 -> actions.setChecked?.invoke(key, true) ?: SemanticActionResult.Unsupported
323 case 20 ->
324 actions.setChecked?.invoke(key, false) ?: SemanticActionResult.Unsupported
325 else -> SemanticActionResult.Unsupported
326 }
327 if (!closed && owner.phase == WindowState.Open) publish()
328 return if (node.active) result else SemanticActionResult.Unavailable
329 }
331 internal fun perform(
332 number: Long,
333 action: Int,
334 value: String?,
335 selection: Utf8Selection,
336 revision: ULong,
337 position: Double
338 ): SemanticActionResult {
339 if (closed || owner.phase != WindowState.Open || owner.application.exiting)
340 return SemanticActionResult.Unavailable
341 if (owner.inputBlocked) return SemanticActionResult.Rejected
342 if (publishing) return SemanticActionResult.Busy
343 val node = nodes[number] ?: return SemanticActionResult.Unavailable
344 if (!node.canAct()) return SemanticActionResult.Rejected
345 if (action >= 8 && action <= 10) {
346 if (node.properties.scroll == None) return SemanticActionResult.Unsupported
347 if (node.properties.readOnly) return SemanticActionResult.Rejected
348 if (action == 8 && (!position.isFinite() || position < 0.0 || position > 1.0))
349 return SemanticActionResult.InvalidArgument
350 }
351 if (action == 5 || action == 6 || action == 21) {
352 if (node.contentRevision != revision) return SemanticActionResult.Unavailable
353 if (node.properties.protectedText || node.properties.value == None)
354 return SemanticActionResult.Unsupported
355 try {
356 checkTextRange(node.properties.value!!.toByteArray(), selection.range)
357 } catch (error: IllegalArgumentException) {
358 return SemanticActionResult.InvalidArgument
359 }
360 if (action == 6 && node.properties.selection?.range != Some(selection.range))
361 return SemanticActionResult.Unavailable
362 }
363 val actions = node.actions
364 if (action >= 12 && action <= 18 && node.properties.readOnly)
365 return SemanticActionResult.Rejected
366 if (action >= 12 && action <= 14) {
367 val range = node.properties.range ?: return SemanticActionResult.Unsupported
368 if (action ==
369 12 &&
370 (!position.isFinite() || position < range.minimum || position > range.maximum)
371 )
372 return SemanticActionResult.InvalidArgument
373 }
374 if ((action == 15 || action == 16) && node.properties.selected == None)
375 return SemanticActionResult.Unsupported
376 if ((action == 17 || action == 18) && node.properties.expanded == None)
377 return SemanticActionResult.Unsupported
378 val result = when (action) {
379 case 0 -> actions.invoke?.invoke() ?: SemanticActionResult.Unsupported
380 case 1 ->
381 if (node.properties.readOnly)
382 SemanticActionResult.Rejected
383 else
384 actions.toggle?.invoke() ?: SemanticActionResult.Unsupported
385 case 2 ->
386 if (node.properties.readOnly)
387 SemanticActionResult.Rejected
388 else
389 when (value) {
390 case Some(val text) ->
391 actions.setValue?.invoke(text) ?: SemanticActionResult.Unsupported
392 case None -> SemanticActionResult.InvalidArgument
393 }
394 case 3 -> actions.focus?.invoke(true) ?: SemanticActionResult.Unsupported
395 case 4 -> actions.focus?.invoke(false) ?: SemanticActionResult.Unsupported
396 case 5 -> actions.setSelection?.invoke(selection) ?: SemanticActionResult.Unsupported
397 case 6 ->
398 if (node.properties.readOnly)
399 SemanticActionResult.Rejected
400 else
401 when (value) {
402 case Some(val text) ->
403 actions.replaceText?.invoke(selection.range, text) ?:
404 SemanticActionResult.Unsupported
405 case None -> SemanticActionResult.InvalidArgument
406 }
407 case 7 -> actions.showContextMenu?.invoke() ?: SemanticActionResult.Unsupported
408 case 8 ->
409 actions.setScrollPosition?.invoke(position) ?: SemanticActionResult.Unsupported
410 case 9 -> actions.scrollBackward?.invoke() ?: SemanticActionResult.Unsupported
411 case 10 -> actions.scrollForward?.invoke() ?: SemanticActionResult.Unsupported
412 case 11 -> actions.reveal?.invoke() ?: SemanticActionResult.Unsupported
413 case 12 -> actions.setNumber?.invoke(position) ?: SemanticActionResult.Unsupported
414 case 13 -> actions.increment?.invoke() ?: SemanticActionResult.Unsupported
415 case 14 -> actions.decrement?.invoke() ?: SemanticActionResult.Unsupported
416 case 15 -> actions.setSelected?.invoke(true) ?: SemanticActionResult.Unsupported
417 case 16 -> actions.setSelected?.invoke(false) ?: SemanticActionResult.Unsupported
418 case 17 -> actions.setExpanded?.invoke(true) ?: SemanticActionResult.Unsupported
419 case 18 -> actions.setExpanded?.invoke(false) ?: SemanticActionResult.Unsupported
420 case 21 ->
421 when (val scroll = semanticTextScroll(position)) {
422 case Some(val value) ->
423 actions.revealText?.invoke(selection.range, value) ?:
424 SemanticActionResult.Unsupported
425 case None -> SemanticActionResult.InvalidArgument
426 }
427 case 19 -> actions.dismiss?.invoke() ?: SemanticActionResult.Unsupported
428 else -> SemanticActionResult.Unsupported
429 }
430 if (!closed && owner.phase == WindowState.Open) publish()
431 return result
432 }
434 public override fun close() {
435 checkThread()
436 if (closed) return
437 closed = true
438 for (node in nodes.values) {
439 node.active = false
440 node.actions = SemanticActions()
441 node.itemActions = SemanticCollectionActions()
442 node.collectionValue = None
443 node.publishedCollection = None
444 node.documentValue = None
445 node.documentRevision = 0UL
446 node.invalidateGeometry()
447 node.children.clear()
448 node.parentNode = None
449 node.relationValues = SemanticRelations()
450 node.content = []
451 node.referrers.clear()
452 node.resolvedName = None
453 node.resolvedDescription = None
454 node.resolvedError = None
455 }
456 nodes.clear()
457 pending.clear()
458 pendingGeometry.clear()
459 announcements.clear()
460 removed.clear()
461 roots.clear()
462 focus = None
463 when (val value = projection) {
464 case Some(val current) -> current.close()
465 case None -> {}
466 }
467 projection = None
468 owner.accessibility = None
469 }