scoop:scoop.window:0.1.0
src/accessibility/bridge.scoop
Selected production source · src/accessibility/bridge.scoop
1
package scoop.window.accessibility3
import scoop.window.*4
import scoop.window.accessibility.native.NativeSemanticHitTest5
import scoop.window.accessibility.native.NativeSemanticTextGeometry7
/** Attaches before or after window configuration; closes before native destruction. */8
public fun Window.createAccessibilityProvider(): AccessibilityProvider {9
owner.requireOpen()10
when (owner.accessibility) {11
case Some(_) ->12
throw IllegalStateException(13
Some("window already has an accessibility provider")14
)15
case None -> {}16
}17
val provider = AccessibilityProvider(owner)18
val application = owner.application19
@Unsafe20
{21
val callback = foreignCallback<22
(Ptr<Unit>, Long, Int, Ptr<UInt8>?, Long, Long, Long, ULong, Double) -> Int23
>(24
{25
number: Long,26
action: Int,27
bytes: Ptr<UInt8>?,28
length: Long,29
anchor: Long,30
cursor: Long,31
revision: ULong,32
position: Double33
->34
actionCode(35
application36
.performAccessibility {37
val value = if (action == 2 || action == 6)38
actionText(bytes, length)39
else40
None41
provider42
.perform(43
number,44
action,45
value,46
Utf8Selection(anchor, cursor),47
revision,48
position49
)50
}51
)52
},53
0,54
ForeignCallbackMode.Reusable55
)56
val collections = NativeCollectionQueries(application, provider)57
try {58
val pointer = created(59
nativeAccessibilityCreate(owner.pointer!!, callback.function, callback.context),60
"create accessibility provider"61
)62
collections.attach(pointer)63
provider64
.bind(65
NativeAccessibilityProjection(application, pointer, callback, collections)66
)67
owner.accessibility = Some(provider)68
return provider69
} catch (error: Throwable) {70
collections.close()71
releaseForeignCallback(callback)72
throw error73
}74
}75
}77
private fun actionText(bytes: Ptr<UInt8>?, length: Long): String? {78
when (bytes) {79
case Some(val text) -> {80
@Unsafe81
{82
return Some(String.fromUtf8(text, length))83
}84
}86
case None -> return if (length == 0L) Some("") else None87
}88
}90
private class NativeAccessibilityProjection(91
private val application: ApplicationOwner,92
private val pointer: Ptr<Unit>,93
private val callback: ForeignCallback<94
(Ptr<Unit>, Long, Int, Ptr<UInt8>?, Long, Long, Long, ULong, Double) -> Int95
>,96
private val collections: NativeCollectionQueries97
) : AccessibilityProjection {98
private val geometryPublisher = platformAccessibilityGeometryPublisher()100
public override fun publish(101
changed: Collection<AccessibilityNode>,102
geometry: Collection<AccessibilityNode>,103
removed: Array<Long>,104
focused: Long105
) {106
@Unsafe107
{108
checkNativeStatus(nativeAccessibilityBegin(pointer), "begin accessibility publication")109
var committed = false110
try {111
for (number in removed)112
checkNativeStatus(113
nativeAccessibilityRemove(pointer, number),114
"remove accessibility node"115
)116
for (node in changed)117
publishNode(node)118
if (geometry.size > 0L) {119
val batch = geometryPublisher120
when (batch) {121
case Some(val stage) -> stage(pointer, geometry)122
case None ->123
for (node in geometry)124
publishNode(node)125
}126
}127
checkNativeStatus(128
nativeAccessibilityCommit(pointer, focused),129
"commit accessibility publication"130
)131
committed = true132
} finally {133
if (!committed) nativeAccessibilityAbort(pointer)134
}135
}136
}138
private fun publishNode(node: AccessibilityNode) {139
@Unsafe140
{141
checkNativeStatus(142
nativeAccessibilityPut(pointer, describe(node)),143
"publish accessibility node"144
)145
collections.publish(pointer, node)146
when (val hit = node.hit) {147
case Some(val value) ->148
checkNativeStatus(149
nativeAccessibilityHit(pointer, node.id.number, value.native),150
"publish accessibility hit geometry"151
)152
case None -> {}153
}154
when (val geometry = node.textGeometry) {155
case Some(val value) ->156
checkNativeStatus(157
nativeAccessibilityTextGeometry(158
pointer,159
node.id.number,160
value.native161
),162
"publish accessibility text geometry"163
)164
case None -> {}165
}166
text(node.id.number, 0, node.resolvedName)167
text(node.id.number, 1, node.resolvedDescription)168
text(node.id.number, 2, node.properties.automationId)169
text(node.id.number, 3, node.properties.value)170
text(node.id.number, 4, node.resolvedError)171
text(node.id.number, 5, node.properties.url)172
checkNativeStatus(173
nativeAccessibilityDocument(174
pointer,175
node.id.number,176
when (node.documentValue) {177
case Some(val value) -> value.pointer;178
case None -> None179
},180
node.documentRevision,181
node.documentEpoch182
),183
"publish semantic text document"184
)185
for (child in node.textChildValues)186
checkNativeStatus(187
nativeAccessibilityTextChild(188
pointer,189
node.id.number,190
child.node.number,191
child.range.start,192
child.range.end193
),194
"publish semantic text child"195
)196
for (id in node.relationValues.labelledBy)197
relation(node.id.number, 0, id.number)198
for (id in node.relationValues.controls)199
relation(node.id.number, 1, id.number)200
for (id in node.relationValues.describedBy)201
relation(node.id.number, 3, id.number)202
for (id in node.relationValues.errorMessages)203
relation(node.id.number, 4, id.number)204
for (id in node.relationValues.rowHeaders)205
relation(node.id.number, 5, id.number)206
for (id in node.relationValues.columnHeaders)207
relation(node.id.number, 6, id.number)208
when (node.relationValues.activeDescendant) {209
case Some(Node(val id)) -> relation(node.id.number, 7, id.number)210
else -> {}211
}212
for (source in node.referrers.values)213
if (source.active &&214
source.properties.name ==215
None &&216
source.relationValues.labelledBy.size ==217
1L &&218
source.relationValues.labelledBy[0L] ==219
node.id220
)221
relation(node.id.number, 2, source.id.number)222
}223
}225
private fun relation(number: Long, field: Int, target: Long) {226
@Unsafe227
{228
checkNativeStatus(229
nativeAccessibilityRelation(pointer, number, field, target),230
"publish accessibility relation"231
)232
}233
}235
private fun text(number: Long, field: Int, value: String?) {236
@Unsafe237
{238
when (value) {239
case Some(val string) ->240
string241
.withUtf8Bytes { bytes, length ->242
@Unsafe243
{244
checkNativeStatus(245
nativeAccessibilityText(246
pointer,247
number,248
field,249
Some(bytes),250
length251
),252
"publish accessibility text"253
)254
}255
}256
case None ->257
checkNativeStatus(258
nativeAccessibilityText(pointer, number, field, None, -1L),259
"clear accessibility text"260
)261
}262
}263
}265
public override fun retire(number: Long) {266
@Unsafe267
{268
nativeAccessibilityRetire(pointer, number)269
}270
}272
public override fun announce(number: Long, text: String, priority: SemanticLive) {273
@Unsafe274
{275
text276
.withUtf8Bytes { bytes, length ->277
@Unsafe278
{279
nativeAccessibilityAnnounce(280
pointer,281
number,282
bytes,283
length,284
if (priority == SemanticLive.Assertive) 2 else 1285
)286
}287
}288
}289
}291
public override fun close() {292
@Unsafe293
{294
nativeAccessibilityClose(pointer)295
}296
val owned = callback297
collections.close()298
application299
.mailbox300
.postControl {301
@Unsafe302
{303
releaseForeignCallback(owned)304
}305
}306
}307
}309
private fun describe(node: AccessibilityNode): NativeSemanticNode {310
val value = node.properties311
val orientation = when (value.scroll) {312
case Some(val scroll) -> Some(scroll.orientation);313
case None -> value.orientation314
}315
val bounds = node.placement?.bounds ?:316
LogicalRect(LogicalPosition(0.0, 0.0), LogicalSize(0.0, 0.0))317
val visible = when (val geometry = node.placement) {318
case Some(val placed) -> placed.visibleBounds319
case None -> None320
}321
val visibleRect = visible ?: LogicalRect(LogicalPosition(0.0, 0.0), LogicalSize(0.0, 0.0))322
return NativeSemanticNode(323
node.id.number,324
node.parentNode?.id?.number ?: 0L,325
node.order,326
node.sceneOrder,327
node.revision,328
node.textRevision,329
value.selection?.anchor ?: -1L,330
value.selection?.cursor ?: -1L,331
when (node.role) {332
case Group -> 0;333
case Text -> 1;334
case Button -> 2;335
case Checkbox -> 3;336
case TextField -> 4;337
case ScrollArea -> 5;338
case ScrollBar -> 6339
case Switch -> 7340
case ToggleButton -> 8341
case RadioButton -> 9342
case RadioGroup -> 10343
case Image -> 11344
case List -> 12345
case Link -> 13346
case Heading -> 14347
case Slider -> 15348
case ProgressBar -> 16349
case Menu -> 17350
case MenuItem -> 18351
case TabGroup -> 19352
case Tab -> 20353
case Dialog -> 21354
case Table -> 22355
case Cell -> 23356
case RowHeader -> 24357
case ColumnHeader -> 25358
case Disclosure -> 26359
case Separator -> 27360
case Row -> 28361
case Tree -> 29362
case TreeItem -> 30363
},364
flag(value.enabled) +365
2 *366
flag(value.readOnly) +367
4 *368
flag(value.protectedText) +369
8 *370
flag(value.hidden) +371
16 *372
flag(value.name != None) +373
32 *374
flag(value.ignored) +375
64 *376
flag(value.required) +377
128 *378
flag(value.selection != None && value.cursorAffinity == SemanticTextAffinity.Upstream) +379
256 *380
flag(value.invalid) +381
512 *382
flag(value.busy) +383
1024 *384
flag(value.modal) +385
2048 *386
flag(value.multiline) +387
4096 *388
flag(value.scroll != None),389
node.actions.capabilities,390
when (value.checked) {391
case None -> -1;392
case Some(Off) -> 0;393
case Some(On) -> 1;394
case Some(Mixed) -> 2395
},396
flag(node.placement != None),397
flag(visibleRect.size.width > 0.0 && visibleRect.size.height > 0.0),398
when (orientation) {399
case Some(Horizontal) -> 1;400
case Some(Vertical) -> 2;401
case None -> 0402
},403
bounds.origin.x,404
bounds.origin.y,405
bounds.size.width,406
bounds.size.height,407
visibleRect.origin.x,408
visibleRect.origin.y,409
visibleRect.size.width,410
visibleRect.size.height,411
value.scroll?.offset ?: 0.0,412
value.scroll?.viewport ?: 0.0,413
value.scroll?.content ?: 0.0,414
optionalFlag(value.selected),415
optionalFlag(value.expanded),416
value.headingLevel ?: 0,417
flag(value.range != None),418
when (value.live) {419
case Off -> 0;420
case Polite -> 1;421
case Assertive -> 2422
},423
value.range?.minimum ?: 0.0,424
value.range?.maximum ?: 0.0,425
value.range?.value ?: 0.0,426
value.range?.step ?: ((value.scroll?.lineStep ?: None) ?: 0.0),427
value.table?.rows ?: -1L,428
value.table?.columns ?: -1L,429
value.cell?.row ?: -1L,430
value.cell?.column ?: -1L,431
value.cell?.rowSpan ?: 0L,432
value.cell?.columnSpan ?: 0L,433
flag(node.activation != None),434
node.activation?.x ?: 0.0,435
node.activation?.y ?: 0.0436
)437
}439
private fun optionalFlag(value: Boolean?): Int = when (value) {440
case Some(val actual) -> if (actual) 1 else 0441
case None -> -1442
}444
@CLayout445
private struct NativeSemanticNode(446
val number: Long,447
val parent: Long,448
val order: Long,449
val sceneOrder: Long,450
val contentRevision: ULong,451
val textRevision: ULong,452
val anchor: Long,453
val cursor: Long,454
val role: Int,455
val flags: Int,456
val actions: Int,457
val checked: Int,458
val geometry: Int,459
val visible: Int,460
val scrollOrientation: Int,461
val x: Double,462
val y: Double,463
val width: Double,464
val height: Double,465
val visibleX: Double,466
val visibleY: Double,467
val visibleWidth: Double,468
val visibleHeight: Double,469
val scrollOffset: Double,470
val scrollViewport: Double,471
val scrollContent: Double,472
val selected: Int,473
val expanded: Int,474
val headingLevel: Int,475
val hasRange: Int,476
val live: Int,477
val minimum: Double,478
val maximum: Double,479
val numberValue: Double,480
val numberStep: Double,481
val tableRows: Long,482
val tableColumns: Long,483
val cellRow: Long,484
val cellColumn: Long,485
val rowSpan: Long,486
val columnSpan: Long,487
val activation: Int,488
val activationX: Double,489
val activationY: Double490
)492
@Extern(name = "scoop_window_accessibility_create")493
private fun nativeAccessibilityCreate(494
window: Ptr<Unit>,495
callback: FunPtr<(Ptr<Unit>, Long, Int, Ptr<UInt8>?, Long, Long, Long, ULong, Double) -> Int>,496
context: Ptr<Unit>497
): NativeCreateResult499
@Extern(name = "scoop_window_accessibility_begin")500
private fun nativeAccessibilityBegin(provider: Ptr<Unit>): Long502
@Extern(name = "scoop_window_accessibility_put")503
private fun nativeAccessibilityPut(provider: Ptr<Unit>, value: NativeSemanticNode): Long505
@Extern(name = "scoop_window_accessibility_document")506
private fun nativeAccessibilityDocument(507
provider: Ptr<Unit>,508
node: Long,509
document: Ptr<Unit>?,510
revision: ULong,511
epoch: ULong512
): Long514
@Extern(name = "scoop_window_accessibility_text")515
private fun nativeAccessibilityText(516
provider: Ptr<Unit>,517
number: Long,518
field: Int,519
text: Ptr<UInt8>?,520
length: Long521
): Long523
@Extern(name = "scoop_window_accessibility_relation")524
private fun nativeAccessibilityRelation(525
provider: Ptr<Unit>,526
number: Long,527
field: Int,528
target: Long529
): Long531
@Extern(name = "scoop_window_accessibility_hit")532
private fun nativeAccessibilityHit(533
provider: Ptr<Unit>,534
number: Long,535
geometry: NativeSemanticHitTest536
): Long538
@Extern(name = "scoop_window_accessibility_remove")539
private fun nativeAccessibilityRemove(provider: Ptr<Unit>, number: Long): Long541
@Extern(name = "scoop_window_accessibility_text_geometry")542
private fun nativeAccessibilityTextGeometry(543
provider: Ptr<Unit>,544
number: Long,545
geometry: NativeSemanticTextGeometry546
): Long548
@Extern(name = "scoop_window_accessibility_commit")549
private fun nativeAccessibilityCommit(provider: Ptr<Unit>, focused: Long): Long551
@Extern(name = "scoop_window_accessibility_abort")552
private fun nativeAccessibilityAbort(provider: Ptr<Unit>)554
@Extern(name = "scoop_window_accessibility_retire")555
private fun nativeAccessibilityRetire(provider: Ptr<Unit>, number: Long)557
@Extern(name = "scoop_window_accessibility_close")558
private fun nativeAccessibilityClose(provider: Ptr<Unit>)560
@Extern(name = "scoop_window_accessibility_announce")561
private fun nativeAccessibilityAnnounce(562
provider: Ptr<Unit>,563
number: Long,564
text: Ptr<UInt8>,565
length: Long,566
priority: Int567
)569
@Extern(name = "scoop_window_accessibility_text_child")570
private fun nativeAccessibilityTextChild(571
provider: Ptr<Unit>,572
node: Long,573
child: Long,574
start: Long,575
end: Long576
): Long