scoop:scoop.window:0.1.0
src/accessibility/provider.scoop
Selected production source · src/accessibility/provider.scoop
1
package scoop.window.accessibility3
import scoop.collections.HashMap4
import scoop.window.*6
/** The platform projection owns no writable application state. */7
internal interface AccessibilityProjection : Closeable {8
fun publish(9
changed: Collection<AccessibilityNode>,10
geometry: Collection<AccessibilityNode>,11
removed: Array<Long>,12
focused: Long13
)14
fun retire(number: Long)15
fun announce(number: Long, text: String, priority: SemanticLive)16
}18
/** Stages owner-thread semantic changes and publishes them independently of drawing. */19
public class AccessibilityProvider internal constructor(20
internal val owner: WindowOwner21
) : 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? = None29
private var focusChanged = false30
private var publishing = false31
private var closed = false32
private var projection: AccessibilityProjection? = None34
internal fun bind(value: AccessibilityProjection) {35
projection = Some(value)36
}38
public val isClosed: Boolean39
get() {40
checkThread()41
return closed42
}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 focus49
}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) return57
case None -> {}58
}59
case None ->60
when (value) {61
case None -> return62
case Some(_) -> {}63
}64
}65
focus = value66
focusChanged = true67
}69
public fun createNode(70
role: SemanticRole,71
parent: AccessibilityNode? = None72
): AccessibilityNode {73
checkOpen()74
checkParent(parent)75
val sequence = owner.semanticIdentity76
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 += 1L80
val node = AccessibilityNode(this, id, role, parent)81
nodes[id.number] = node82
addChild(node)83
changed(node)84
return node85
}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 ?: roots118
for (index in 0L..<children.size) {119
if (children[index] === node) {120
children.removeAt(index)121
return122
}123
}124
}126
internal fun changed(node: AccessibilityNode) {127
if (node.active) {128
pendingGeometry.remove(node.id.number)129
pending[node.id.number] = node130
}131
}133
internal fun geometryChanged(node: AccessibilityNode) {134
if (node.active && !pending.containsKey(node.id.number))135
pendingGeometry[node.id.number] = node136
}138
internal fun lookup(id: SemanticNodeId): AccessibilityNode? {139
val node = nodes[id.number] ?: return None140
return if (node.id == id) Some(node) else None141
}143
internal fun retire(node: AccessibilityNode) {144
announcements.remove(node.id.number)145
node.active = false146
node.collectionValue = None147
node.publishedCollection = None148
node.documentValue = None149
node.documentRevision = 0UL150
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 = None159
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 = None167
focusChanged = true168
}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
0L185
)186
return187
publishing = true188
try {189
for (node in pending.values) {190
resolveSemanticText(node)191
val message = node.announcementText192
if (node.hasPublished &&193
node.properties.live !=194
SemanticLive.Off &&195
message !=196
node.publishedMessage197
) {198
when (message) {199
case Some(val text) ->200
announcements[node.id.number] = SemanticAnnouncement(201
text,202
node.properties.live203
)204
case None -> {}205
}206
}207
}208
projection!!209
.publish(210
pending.values,211
pendingGeometry.values,212
removed.toArray(),213
focus?.id?.number ?: 0L214
)215
for (node in pending.values) {216
node.publishedCollection = node.collectionValue217
node.publishedMessage = node.announcementText218
node.hasPublished = true219
}220
pending.clear()221
pendingGeometry.clear()222
removed.clear()223
focusChanged = false224
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 = false242
}243
}245
internal fun announce(node: AccessibilityNode, text: String, priority: SemanticLive) {246
if (priority == SemanticLive.Off || !node.canAnnounce() || owner.inputBlocked) return247
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.exiting255
)256
return None257
val node = nodes[number] ?: return None258
if (!node.active) return None259
if (candidate) return if (publishing) node.collectionValue else None260
return if (publishing) None else node.publishedCollection261
}263
internal fun performCollection(264
number: Long,265
key: SemanticItemKey,266
operation: Int,267
column: Long268
):269
SemanticActionResult {270
if (closed || owner.phase != WindowState.Open || owner.application.exiting)271
return SemanticActionResult.Unavailable272
if (owner.inputBlocked) return SemanticActionResult.Rejected273
if (publishing) return SemanticActionResult.Busy274
val node = nodes[number] ?: return SemanticActionResult.Unavailable275
if (!node.canAct()) return SemanticActionResult.Rejected276
val source = node.collectionValue ?: return SemanticActionResult.Unavailable277
when (node.publishedCollection) {278
case Some(val published) ->279
if (source !== published) return SemanticActionResult.Unavailable280
case None -> return SemanticActionResult.Unavailable281
}282
val item = when (source.itemForKey(key)) {283
case Found(val value) -> value284
else -> return SemanticActionResult.Unavailable285
}286
if (!item.properties.enabled) return SemanticActionResult.Rejected287
if ((operation == 5 || operation == 6) && !item.properties.focusable)288
return SemanticActionResult.Unsupported289
if ((operation == 7 || operation == 8) && !item.properties.selectable)290
return SemanticActionResult.Unsupported291
if ((operation == 17 || operation == 18) && item.properties.expanded == None)292
return SemanticActionResult.Unsupported293
if ((operation == 19 || operation == 20) && item.properties.checked == None)294
return SemanticActionResult.Unsupported295
val actions = node.itemActions296
val result = if (operation >= 12 && operation <= 15)297
performGridCell(source, actions.grid, key, column, operation)298
else299
when (operation) {300
case 3 -> actions.realize?.invoke(key) ?: SemanticActionResult.Unsupported301
case 4 -> actions.reveal?.invoke(key) ?: SemanticActionResult.Unsupported302
case 5 -> actions.focus?.invoke(key, true) ?: SemanticActionResult.Unsupported303
case 6 -> actions.focus?.invoke(key, false) ?: SemanticActionResult.Unsupported304
case 7 ->305
when (source.selection) {306
case Some(_) ->307
actions.setSelected?.invoke(key, true) ?:308
SemanticActionResult.Unsupported309
case None -> SemanticActionResult.Unsupported310
}311
case 8 ->312
when (source.selection) {313
case Some(_) ->314
actions.setSelected?.invoke(key, operation == 7) ?:315
SemanticActionResult.Unsupported316
case None -> SemanticActionResult.Unsupported317
}318
case 17 ->319
actions.setExpanded?.invoke(key, true) ?: SemanticActionResult.Unsupported320
case 18 ->321
actions.setExpanded?.invoke(key, false) ?: SemanticActionResult.Unsupported322
case 19 -> actions.setChecked?.invoke(key, true) ?: SemanticActionResult.Unsupported323
case 20 ->324
actions.setChecked?.invoke(key, false) ?: SemanticActionResult.Unsupported325
else -> SemanticActionResult.Unsupported326
}327
if (!closed && owner.phase == WindowState.Open) publish()328
return if (node.active) result else SemanticActionResult.Unavailable329
}331
internal fun perform(332
number: Long,333
action: Int,334
value: String?,335
selection: Utf8Selection,336
revision: ULong,337
position: Double338
): SemanticActionResult {339
if (closed || owner.phase != WindowState.Open || owner.application.exiting)340
return SemanticActionResult.Unavailable341
if (owner.inputBlocked) return SemanticActionResult.Rejected342
if (publishing) return SemanticActionResult.Busy343
val node = nodes[number] ?: return SemanticActionResult.Unavailable344
if (!node.canAct()) return SemanticActionResult.Rejected345
if (action >= 8 && action <= 10) {346
if (node.properties.scroll == None) return SemanticActionResult.Unsupported347
if (node.properties.readOnly) return SemanticActionResult.Rejected348
if (action == 8 && (!position.isFinite() || position < 0.0 || position > 1.0))349
return SemanticActionResult.InvalidArgument350
}351
if (action == 5 || action == 6 || action == 21) {352
if (node.contentRevision != revision) return SemanticActionResult.Unavailable353
if (node.properties.protectedText || node.properties.value == None)354
return SemanticActionResult.Unsupported355
try {356
checkTextRange(node.properties.value!!.toByteArray(), selection.range)357
} catch (error: IllegalArgumentException) {358
return SemanticActionResult.InvalidArgument359
}360
if (action == 6 && node.properties.selection?.range != Some(selection.range))361
return SemanticActionResult.Unavailable362
}363
val actions = node.actions364
if (action >= 12 && action <= 18 && node.properties.readOnly)365
return SemanticActionResult.Rejected366
if (action >= 12 && action <= 14) {367
val range = node.properties.range ?: return SemanticActionResult.Unsupported368
if (action ==369
12 &&370
(!position.isFinite() || position < range.minimum || position > range.maximum)371
)372
return SemanticActionResult.InvalidArgument373
}374
if ((action == 15 || action == 16) && node.properties.selected == None)375
return SemanticActionResult.Unsupported376
if ((action == 17 || action == 18) && node.properties.expanded == None)377
return SemanticActionResult.Unsupported378
val result = when (action) {379
case 0 -> actions.invoke?.invoke() ?: SemanticActionResult.Unsupported380
case 1 ->381
if (node.properties.readOnly)382
SemanticActionResult.Rejected383
else384
actions.toggle?.invoke() ?: SemanticActionResult.Unsupported385
case 2 ->386
if (node.properties.readOnly)387
SemanticActionResult.Rejected388
else389
when (value) {390
case Some(val text) ->391
actions.setValue?.invoke(text) ?: SemanticActionResult.Unsupported392
case None -> SemanticActionResult.InvalidArgument393
}394
case 3 -> actions.focus?.invoke(true) ?: SemanticActionResult.Unsupported395
case 4 -> actions.focus?.invoke(false) ?: SemanticActionResult.Unsupported396
case 5 -> actions.setSelection?.invoke(selection) ?: SemanticActionResult.Unsupported397
case 6 ->398
if (node.properties.readOnly)399
SemanticActionResult.Rejected400
else401
when (value) {402
case Some(val text) ->403
actions.replaceText?.invoke(selection.range, text) ?:404
SemanticActionResult.Unsupported405
case None -> SemanticActionResult.InvalidArgument406
}407
case 7 -> actions.showContextMenu?.invoke() ?: SemanticActionResult.Unsupported408
case 8 ->409
actions.setScrollPosition?.invoke(position) ?: SemanticActionResult.Unsupported410
case 9 -> actions.scrollBackward?.invoke() ?: SemanticActionResult.Unsupported411
case 10 -> actions.scrollForward?.invoke() ?: SemanticActionResult.Unsupported412
case 11 -> actions.reveal?.invoke() ?: SemanticActionResult.Unsupported413
case 12 -> actions.setNumber?.invoke(position) ?: SemanticActionResult.Unsupported414
case 13 -> actions.increment?.invoke() ?: SemanticActionResult.Unsupported415
case 14 -> actions.decrement?.invoke() ?: SemanticActionResult.Unsupported416
case 15 -> actions.setSelected?.invoke(true) ?: SemanticActionResult.Unsupported417
case 16 -> actions.setSelected?.invoke(false) ?: SemanticActionResult.Unsupported418
case 17 -> actions.setExpanded?.invoke(true) ?: SemanticActionResult.Unsupported419
case 18 -> actions.setExpanded?.invoke(false) ?: SemanticActionResult.Unsupported420
case 21 ->421
when (val scroll = semanticTextScroll(position)) {422
case Some(val value) ->423
actions.revealText?.invoke(selection.range, value) ?:424
SemanticActionResult.Unsupported425
case None -> SemanticActionResult.InvalidArgument426
}427
case 19 -> actions.dismiss?.invoke() ?: SemanticActionResult.Unsupported428
else -> SemanticActionResult.Unsupported429
}430
if (!closed && owner.phase == WindowState.Open) publish()431
return result432
}434
public override fun close() {435
checkThread()436
if (closed) return437
closed = true438
for (node in nodes.values) {439
node.active = false440
node.actions = SemanticActions()441
node.itemActions = SemanticCollectionActions()442
node.collectionValue = None443
node.publishedCollection = None444
node.documentValue = None445
node.documentRevision = 0UL446
node.invalidateGeometry()447
node.children.clear()448
node.parentNode = None449
node.relationValues = SemanticRelations()450
node.content = []451
node.referrers.clear()452
node.resolvedName = None453
node.resolvedDescription = None454
node.resolvedError = None455
}456
nodes.clear()457
pending.clear()458
pendingGeometry.clear()459
announcements.clear()460
removed.clear()461
roots.clear()462
focus = None463
when (val value = projection) {464
case Some(val current) -> current.close()465
case None -> {}466
}467
projection = None468
owner.accessibility = None469
}470
}