scoop:scoop.window:0.1.0
src/accessibility/relations.scoop
Selected production source · src/accessibility/relations.scoop
1
package scoop.window.accessibility3
import scoop.collections.HashSet4
import scoop.collections.contains5
import scoop.collections.toList6
import scoop.window.*8
internal fun checkSemanticRelations(source: AccessibilityNode, next: SemanticRelations) {9
for (ids in next.references)10
checkSemanticTargets(source, ids)11
when (next.activeDescendant) {12
case Some(Item(val id, _)) -> {13
val target = source.provider.lookup(id)!!14
requireWindow(15
target.role ==16
SemanticRole.List ||17
target.role ==18
SemanticRole.Table ||19
target.role ==20
SemanticRole.Tree,21
"active item requires a collection target"22
)23
}25
else -> {}26
}27
}29
internal fun refreshSemanticItemReferences(collection: AccessibilityNode) {30
for (source in collection.referrers.values.toList()) {31
when (source.relationValues.activeDescendant) {32
case Some(Item(val id, val key)) -> {33
if (id == collection.id && !(collection.collectionValue?.contains(key) ?: false)) {34
val previous = source.relationValues35
val next = SemanticRelations(36
previous.labelledBy,37
previous.describedBy,38
previous.errorMessages,39
previous.controls,40
None,41
previous.rowHeaders,42
previous.columnHeaders43
)44
installSemanticReferences(source, next, source.content)45
source.provider.changed(source)46
}47
}49
else -> {}50
}51
}52
}54
internal fun checkSemanticTargets(source: AccessibilityNode, ids: Array<SemanticNodeId>) {55
val seen = HashSet<SemanticNodeId>()56
for (id in ids) {57
val target = source.provider.lookup(id)58
requireWindow(59
(target?.active ?: false) && id != source.id,60
"semantic relation requires another active node in the same provider"61
)62
requireWindow(seen.add(id), "duplicate semantic relation target")63
}64
}66
/** Maintains only real references, so retirement and text updates visit their dependents. */67
internal fun installSemanticReferences(68
source: AccessibilityNode,69
next: SemanticRelations,70
content: Array<SemanticNodeId>,71
textChildren: Array<SemanticTextChild> = source.textChildValues72
) {73
val previous = source.relationValues74
for (ids in previous.references)75
for (id in ids) {76
val target = source.provider.lookup(id) ?: continue77
target.referrers.remove(source.id.number)78
source.provider.changed(target)79
}80
for (id in source.content) {81
val target = source.provider.lookup(id) ?: continue82
target.referrers.remove(source.id.number)83
source.provider.changed(target)84
}85
for (child in source.textChildValues) {86
val target = source.provider.lookup(child.node) ?: continue87
target.referrers.remove(source.id.number)88
}89
source.relationValues = next90
source.content = content91
source.textChildValues = textChildren92
for (ids in next.references)93
for (id in ids) {94
val target = source.provider.lookup(id) ?: continue95
target.referrers[source.id.number] = source96
source.provider.changed(target)97
}98
for (id in content) {99
val target = source.provider.lookup(id) ?: continue100
target.referrers[source.id.number] = source101
source.provider.changed(target)102
}103
for (child in textChildren) {104
val target = source.provider.lookup(child.node) ?: continue105
target.referrers[source.id.number] = source106
}107
}109
internal fun retireSemanticRelations(node: AccessibilityNode) {110
for (source in node.referrers.values.toList()) {111
if (!source.active) continue112
val old = source.relationValues113
val next = SemanticRelations(114
withoutSemanticId(old.labelledBy, node.id),115
withoutSemanticId(old.describedBy, node.id),116
withoutSemanticId(old.errorMessages, node.id),117
withoutSemanticId(old.controls, node.id),118
when (old.activeDescendant) {119
case Some(val value) -> if (value.node == node.id) None else old.activeDescendant120
case None -> None121
},122
withoutSemanticId(old.rowHeaders, node.id),123
withoutSemanticId(old.columnHeaders, node.id)124
)125
val content = withoutSemanticId(source.content, node.id)126
val nameChanged = source.properties.name ==127
None &&128
(129
!sameSemanticIds(old.labelledBy, next.labelledBy) ||130
!sameSemanticIds(source.content, content)131
)132
installSemanticReferences(133
source,134
next,135
content,136
withoutSemanticTextChild(source.textChildValues, node.id)137
)138
if (nameChanged) source.invalidateContent()139
semanticTextChanged(source, nameChanged)140
source.provider.changed(source)141
}142
installSemanticReferences(node, SemanticRelations(), [], [])143
node.referrers.clear()144
}146
private fun withoutSemanticId(ids: Array<SemanticNodeId>, removed: SemanticNodeId): Array<147
SemanticNodeId148
> {149
val result = ArrayList<SemanticNodeId>()150
for (id in ids)151
if (id != removed) result.add(id)152
return result.toArray()153
}155
internal fun semanticTextChanged(node: AccessibilityNode, nameChanged: Boolean) {156
node.textDirty = true157
if (!nameChanged) return158
node.nameDirty = true159
val pending = ArrayList<AccessibilityNode>()160
val visited = HashSet<SemanticNodeId>()161
pending.add(node)162
while (pending.size > 0L) {163
val target = pending.removeAt(pending.size - 1L)164
if (!visited.add(target.id)) continue165
for (source in target.referrers.values) {166
if (!source.active) continue167
val name = source.properties.name ==168
None &&169
(170
source.relationValues.labelledBy.contains(target.id) ||171
source.content.contains(target.id)172
)173
val description = source.properties.description ==174
None &&175
source.relationValues.describedBy.contains(target.id)176
val error = source.properties.error ==177
None &&178
source.relationValues.errorMessages.contains(target.id)179
if (name || description || error) {180
source.textDirty = true181
source.provider.changed(source)182
}183
if (name) {184
source.nameDirty = true185
source.invalidateContent()186
pending.add(source)187
}188
}189
}190
}192
internal fun resolveSemanticText(node: AccessibilityNode) {193
if (!node.textDirty) return194
resolveSemanticName(node)195
node.resolvedDescription = semanticTextOr(196
node,197
node.properties.description,198
node.relationValues.describedBy199
)200
node.resolvedError = semanticTextOr(201
node,202
node.properties.error,203
node.relationValues.errorMessages204
)205
node.textDirty = false206
}208
private fun resolveSemanticName(node: AccessibilityNode): String? {209
if (!node.nameDirty) return node.resolvedName210
requireWindow(!node.resolvingName, "semantic labels would create a name cycle")211
node.resolvingName = true212
try {213
var name = semanticTextOr(node, node.properties.name, node.relationValues.labelledBy)214
if (name == None) name = semanticTextOr(node, None, node.content)215
node.resolvedName = name216
node.nameDirty = false217
return name218
} finally {219
node.resolvingName = false220
}221
}223
private fun semanticTextOr(224
source: AccessibilityNode,225
explicit: String?,226
ids: Array<SemanticNodeId>227
): String? {228
when (explicit) {229
case Some(_) -> return explicit230
case None -> {}231
}232
var known = false233
var text = ""234
for (id in ids) {235
val target = source.provider.lookup(id) ?: continue236
if (!target.active) continue237
var value = resolveSemanticName(target)238
if (value == None && target.role == SemanticRole.Text && !target.properties.protectedText)239
value = target.properties.value240
when (value) {241
case Some(val content) -> {242
known = true243
if (content.byteLength > 0L) {244
if (text.byteLength > 0L) text += " "245
text += content246
}247
}249
case None -> {}250
}251
}252
return if (known) Some(text) else None253
}