scoop:scoop.window:0.1.0
src/accessibility/node.scoop
Selected production source · src/accessibility/node.scoop
1
package scoop.window.accessibility3
import scoop.collections.HashMap4
import scoop.window.*5
import scoop.window.accessibility.native.NativeSemanticHitTest6
import scoop.window.accessibility.native.NativeSemanticTextGeometry8
/** A semantic resource whose role and identity last until it is explicitly retired. */9
public class AccessibilityNode internal constructor(10
internal val provider: AccessibilityProvider,11
public val id: SemanticNodeId,12
public val role: SemanticRole,13
internal var parentNode: AccessibilityNode?14
) : Closeable {15
internal val children = ArrayList<AccessibilityNode>()16
internal var active = true17
internal var properties = SemanticProperties()18
internal var actions = SemanticActions()19
internal var relationValues = SemanticRelations()20
internal var content: Array<SemanticNodeId> = []21
internal var textChildValues: Array<SemanticTextChild> = []22
public var textChildren: Array<SemanticTextChild>23
get() {24
checkOpen();25
return textChildValues26
}27
set(value) {28
checkOpen()29
if (sameSemanticTextChildren(textChildValues, value)) return30
checkSemanticTextChildren(this, value)31
installSemanticReferences(this, relationValues, content, value)32
provider.changed(this)33
}34
internal val referrers = HashMap<Long, AccessibilityNode>()35
internal var nameDirty = true36
internal var resolvingName = false37
internal var textDirty = true38
internal var resolvedName: String? = None39
internal var resolvedDescription: String? = None40
internal var resolvedError: String? = None41
internal var revision = 0UL42
internal var textRevision = 0UL43
internal var placement: SemanticGeometry? = None44
internal var order = 0L45
internal var hit: SemanticHitSnapshot? = None46
internal var textGeometry: SemanticTextSnapshot? = None47
internal var sceneOrder = 0L48
internal var activation: LogicalPosition? = None49
internal var collectionValue: SemanticCollectionSnapshot? = None50
internal var collectionRevision = 0UL51
internal var publishedCollection: SemanticCollectionSnapshot? = None52
internal var itemActions = SemanticCollectionActions()53
internal var itemKey: SemanticItemKey? = None54
internal var hasPublished = false55
internal var publishedMessage: String? = None56
internal var documentValue: SemanticTextDocument? = None57
internal var documentEpoch = 1UL58
internal var documentRevision = 0UL59
public var textDocument: SemanticTextDocument?60
get() {61
checkOpen();62
return documentValue63
}64
set(value) {65
checkOpen()66
when (value) {67
case Some(val document) ->68
requireWindow(69
!document.isClosed &&70
!properties.protectedText &&71
(role == SemanticRole.Text || role == SemanticRole.TextField),72
"text document requires public text"73
)74
case None -> {}75
}76
val same = when ((documentValue, value)) {77
case(Some(val before), Some(val after)) -> before === after78
case(None, None) -> true79
else -> false80
}81
val revision = when (value) {82
case Some(val document) -> document.revision;83
case None -> 0UL84
}85
if (same) {86
if (documentRevision != revision) {87
documentRevision = revision;88
provider.changed(this)89
}90
return91
}92
requireWindow(documentEpoch < ULong.MAX_VALUE, "text document identity exhausted")93
documentEpoch += 1UL94
documentValue = value95
documentRevision = revision96
provider.changed(this)97
}98
internal val announcementText: String?99
get() = when (val value = properties.value) {100
case Some(_) -> value101
case None -> resolvedName102
}104
/** Operations on logical items; ordinary queries never invoke these callbacks. */105
public var collectionActions: SemanticCollectionActions106
get() {107
checkOpen()108
return itemActions109
}110
set(value) {111
checkOpen()112
requireWindow(113
role ==114
SemanticRole.List ||115
role ==116
SemanticRole.Table ||117
role ==118
SemanticRole.Tree,119
"semantic collections require a list, table or tree role"120
)121
itemActions = value122
provider.changed(this)123
}125
/** Publishes a data source independently of the currently mounted row controls. */126
public var collection: SemanticCollectionSnapshot?127
get() {128
checkOpen()129
return collectionValue130
}131
set(value) {132
checkOpen()133
requireWindow(134
role ==135
SemanticRole.List ||136
role ==137
SemanticRole.Table ||138
role ==139
SemanticRole.Tree,140
"semantic collections require a list, table or tree role"141
)142
when ((collectionValue, value)) {143
case(Some(val before), Some(val after)) -> if (before === after) return144
case(None, None) -> return145
else -> {}146
}147
when (value) {148
case Some(val snapshot) ->149
when (snapshot.grid) {150
case Some(val grid) -> {151
requireWindow(152
role == SemanticRole.Table || role == SemanticRole.Tree,153
"semantic grid requires a table or tree role"154
)155
for (column in grid.columns)156
when (column.header) {157
case Some(val id) -> checkSemanticTargets(this, [id])158
case None -> {}159
}160
}162
case None -> {}163
}164
case None -> {}165
}166
requireWindow(167
!(value?.hasSelection ?: false) || properties.selectionMode == None,168
"collection and static selection modes cannot be combined"169
)170
collectionValue = value171
refreshSemanticItemReferences(this)172
if (collectionRevision == ULong.MAX_VALUE)173
throw IllegalStateException(Some("semantic collection revision exhausted"))174
collectionRevision += 1UL175
provider.changed(this)176
}178
/** Associates a mounted row group with its logical item, without owning that item. */179
public var collectionItem: SemanticItemKey?180
get() {181
checkOpen()182
return itemKey183
}184
set(value) {185
checkOpen()186
requireWindow(187
role ==188
SemanticRole.Group &&189
(190
parentNode?.role ==191
Some(SemanticRole.List) ||192
parentNode?.role ==193
Some(SemanticRole.Table) ||194
parentNode?.role ==195
Some(SemanticRole.Tree)196
),197
"a collection item requires a group directly inside a collection"198
)199
if (itemKey == value) return200
itemKey = value201
provider.changed(this)202
}204
public val isClosed: Boolean205
get() {206
provider.checkThread()207
return !active208
}210
/** Coalesces explicit announcements until the next ordinary publication. */211
public fun announce(text: String, priority: SemanticLive = SemanticLive.Polite) {212
checkOpen()213
provider.announce(this, text, priority)214
}216
/** Changes when content or a label dependency invalidates captured layout geometry. */217
public val contentRevision: ULong218
get() {219
checkOpen()220
return revision221
}223
/** Current references, with retired targets removed by the provider. */224
public val relations: SemanticRelations225
get() {226
checkOpen()227
return relationValues228
}230
/** Fallback content names; callers separately decide which labels should be ignored. */231
public var contentLabels: Array<SemanticNodeId>232
get() {233
checkOpen()234
return content235
}236
set(value) {237
checkOpen()238
if (sameSemanticIds(content, value)) return239
checkSemanticTargets(this, value)240
installSemanticReferences(this, relationValues, value)241
val affectsName = properties.name == None242
if (affectsName) invalidateContent()243
semanticTextChanged(this, affectsName)244
provider.changed(this)245
}247
/** Stages matching, already accepted layout geometry; None invalidates an old layout. */248
public var geometry: SemanticGeometry?249
get() {250
checkOpen()251
return placement252
}253
set(value) {254
checkOpen()255
if (placement == value)256
when ((hit, textGeometry)) {257
case(None, None) -> return258
else -> {}259
}260
value?.check()261
invalidateGeometry()262
placement = value263
provider.geometryChanged(this)264
}266
/** Reads the matching native shape only; it does not run input handlers. */267
public fun containsPoint(point: LogicalPosition): Boolean {268
checkOpen()269
if (!point.x.isFinite() || !point.y.isFinite()) return false270
val snapshot = hit ?: return false271
@Unsafe272
{273
return snapshot.native.contains(snapshot.native.context, point.x, point.y) == 1274
}275
}277
/** A point verified against the renderer's current input routing, not a bounds estimate. */278
public var activationPoint: LogicalPosition?279
get() {280
checkOpen();281
return activation282
}283
set(value) {284
checkOpen()285
if (activation == value) return286
when (value) {287
case Some(val point) -> {288
val visible = (placement?.visibleBounds ?: None) ?:289
throw IllegalArgumentException(Some("activation needs visible geometry"))290
requireWindow(291
point.x.isFinite() &&292
point.y.isFinite() &&293
point.x >=294
visible.origin.x &&295
point.y >=296
visible.origin.y &&297
point.x <298
visible.origin.x +299
visible.size.width &&300
point.y <301
visible.origin.y +302
visible.size.height,303
"activation point is outside visible geometry"304
)305
val snapshot = hit ?:306
throw IllegalArgumentException(Some("activation needs exact hit geometry"))307
@Unsafe308
{309
requireWindow(310
snapshot.native.contains(snapshot.native.context, point.x, point.y) ==311
1,312
"activation point is outside the accepted shape"313
)314
}315
}317
case None -> {}318
}319
activation = value320
provider.geometryChanged(this)321
}323
/** Retains matching accepted geometry and its native hit test in actual scene order. */324
@Unsafe325
public fun setGeometry(326
geometry: SemanticGeometry,327
hitTest: NativeSemanticHitTest,328
order: Long = 0L329
) {330
checkOpen()331
geometry.check()332
val next = SemanticHitSnapshot(hitTest)333
invalidateGeometry()334
placement = Some(geometry)335
hit = Some(next)336
sceneOrder = order337
provider.geometryChanged(this)338
}340
/** Retains native text queries matching this node's known text and accepted geometry. */341
@Unsafe342
public fun setTextGeometry(geometry: NativeSemanticTextGeometry?) {343
checkOpen()344
val next = when (geometry) {345
case Some(val value) -> {346
requireWindow(347
(role == SemanticRole.Text || role == SemanticRole.TextField) &&348
properties.value !=349
None &&350
!properties.protectedText &&351
placement !=352
None,353
"text geometry requires known unprotected text and accepted node geometry"354
)355
Some(SemanticTextSnapshot(value))356
}358
case None -> {359
when (textGeometry) {360
case None -> return361
case Some(_) -> {}362
}363
None364
}365
}366
textGeometry?.close()367
textGeometry = next368
provider.geometryChanged(this)369
}371
/** Replaces current facts and callbacks without changing the native role or identity. */372
public fun update(373
properties: SemanticProperties,374
actions: SemanticActions = SemanticActions(),375
relations: SemanticRelations = SemanticRelations()376
) {377
checkOpen()378
properties.check()379
requireWindow(380
!properties.multiline || role == SemanticRole.TextField,381
"multiline requires a text field"382
)383
requireWindow(384
properties.selectionMode ==385
None ||386
role ==387
SemanticRole.Group ||388
role ==389
SemanticRole.List ||390
role ==391
SemanticRole.RadioGroup ||392
role ==393
SemanticRole.TabGroup ||394
role ==395
SemanticRole.Table ||396
role ==397
SemanticRole.Tree,398
"selection mode requires a selection container role"399
)400
requireWindow(401
properties.selectionMode == None || !(collectionValue?.hasSelection ?: false),402
"collection and static selection modes cannot be combined"403
)404
if (properties.protectedText) textDocument = None405
requireWindow(406
properties.table == None || role == SemanticRole.Table || role == SemanticRole.Tree,407
"table dimensions require a table or tree role"408
)409
requireWindow(410
properties.cell ==411
None ||412
role ==413
SemanticRole.Cell ||414
role ==415
SemanticRole.RowHeader ||416
role ==417
SemanticRole.ColumnHeader,418
"cell coordinates require a cell or header role"419
)420
requireWindow(421
properties.headingLevel == None || role == SemanticRole.Heading,422
"heading level requires the heading role"423
)424
requireWindow(425
properties.range ==426
None ||427
role ==428
SemanticRole.Slider ||429
role ==430
SemanticRole.ProgressBar ||431
role ==432
SemanticRole.Separator,433
"numeric range requires a numeric control role"434
)435
requireWindow(436
properties.scroll == None || role == SemanticRole.ScrollBar,437
"scroll ranges require the scrollbar role"438
)439
requireWindow(440
properties.checked ==441
None ||442
role ==443
SemanticRole.Checkbox ||444
role ==445
SemanticRole.Switch ||446
role ==447
SemanticRole.ToggleButton ||448
role ==449
SemanticRole.RadioButton ||450
role ==451
SemanticRole.MenuItem ||452
role ==453
SemanticRole.TreeItem,454
"checked state requires a checkable role"455
)456
requireWindow(457
properties.checked !=458
Some(SemanticChecked.Mixed) ||459
role ==460
SemanticRole.Checkbox ||461
role ==462
SemanticRole.MenuItem ||463
role ==464
SemanticRole.TreeItem,465
"mixed state requires the checkbox role"466
)467
requireWindow(468
properties.selection ==469
None ||470
role ==471
SemanticRole.Text ||472
role ==473
SemanticRole.TextField,474
"text selection requires a text role"475
)476
if (this.properties ==477
properties &&478
this.actions.capabilities ==479
actions.capabilities &&480
this.relationValues ==481
relations482
) {483
this.actions = actions484
return485
}486
if (this.relationValues != relations) checkSemanticRelations(this, relations)487
val nameChanged = this.properties.name !=488
properties.name ||489
(490
properties.name ==491
None &&492
(493
!sameSemanticIds(494
this.relationValues.labelledBy,495
relations.labelledBy496
) ||497
(498
role ==499
SemanticRole.Text &&500
(501
this.properties.value !=502
properties.value ||503
this.properties.protectedText !=504
properties.protectedText505
)506
)507
)508
)509
if (nameChanged || this.properties.value != properties.value) invalidateContent()510
if (this.relationValues != relations) installSemanticReferences(this, relations, content)511
if ((this.properties.name == None) != (properties.name == None))512
for (id in relations.labelledBy) {513
val target = provider.lookup(id) ?: continue514
provider.changed(target)515
}516
if (this.properties.value !=517
properties.value ||518
this.properties.protectedText !=519
properties.protectedText520
) {521
textRevision += 1UL522
installSemanticReferences(this, relationValues, content, [])523
}524
this.properties = properties525
this.actions = actions526
semanticTextChanged(this, nameChanged)527
provider.changed(this)528
}530
internal fun invalidateContent() {531
revision += 1UL532
invalidateGeometry()533
}535
internal fun invalidateGeometry() {536
activation = None537
placement = None538
hit?.close()539
hit = None540
textGeometry?.close()541
textGeometry = None542
sceneOrder = 0L543
}545
/** Changes reading order; geometry and creation identity do not determine parenthood. */546
public fun reparent(parent: AccessibilityNode?, order: Long = 0L) {547
checkOpen()548
provider.checkParent(parent)549
var ancestor = parent550
while (true) {551
val current = ancestor ?: break552
requireWindow(current !== this, "semantic parent would create a cycle")553
ancestor = current.parentNode554
}555
val sameParent = when (val current = parentNode) {556
case Some(val value) ->557
when (parent) {558
case Some(val next) -> value === next559
case None -> false560
}561
case None ->562
when (parent) {563
case Some(_) -> false564
case None -> true565
}566
}567
if (sameParent && this.order == order) return568
if (!sameParent) itemKey = None569
val previousAncestors = ArrayList<AccessibilityNode>()570
var before = parentNode571
while (true) {572
val ancestor = before ?: break573
if (ancestor.textChildValues.size > 0L) previousAncestors.add(ancestor)574
before = ancestor.parentNode575
}576
provider.removeChild(this)577
parentNode = parent578
this.order = order579
provider.addChild(this)580
for (source in previousAncestors) {581
val retained = ArrayList<SemanticTextChild>()582
for (child in source.textChildValues) {583
var parent = provider.lookup(child.node)?.parentNode ?: None584
while (true) {585
val ancestor = parent ?: break586
if (ancestor === source) {587
retained.add(child);588
break589
}590
parent = ancestor.parentNode591
}592
}593
source.textChildren = retained.toArray()594
}595
provider.changed(this)596
}598
internal fun checkOpen() {599
provider.checkOpen()600
if (!active) throw IllegalStateException(Some("semantic node is retired"))601
}603
internal fun canAct(): Boolean {604
if (properties.ignored) return false605
var current: AccessibilityNode? = Some(this)606
while (true) {607
val node = current ?: return true608
if (!node.active || node.properties.hidden || !node.properties.enabled) return false609
current = node.parentNode610
}611
}613
internal val announcementBusy: Boolean614
get() {615
var current: AccessibilityNode? = Some(this)616
while (true) {617
val node = current ?: return false618
if (node.properties.busy) return true619
current = node.parentNode620
}621
}623
internal fun canAnnounce(): Boolean {624
if (properties.ignored || properties.protectedText) return false625
var current: AccessibilityNode? = Some(this)626
while (true) {627
val node = current ?: return true628
if (!node.active || node.properties.hidden || node.properties.protectedText)629
return false630
current = node.parentNode631
}632
}634
/** Retires this node and its descendants. Retired native references cannot be reused. */635
public override fun close() {636
provider.checkThread()637
if (active) {638
provider.checkOpen()639
provider.retire(this)640
}641
}642
}