scoop:scoop.window:0.1.0
src/accessibility/bridge_collection.scoop
Selected production source · src/accessibility/bridge_collection.scoop
1
package scoop.window.accessibility3
import scoop.window.*5
/** Queries remain read-only; explicit operations enter the existing owner action boundary. */6
internal class NativeCollectionQueries(7
private val application: ApplicationOwner,8
provider: AccessibilityProvider9
) : Closeable {10
private val callback = collectionCallback(application, provider)12
fun attach(pointer: Ptr<Unit>) {13
@Unsafe14
{15
nativeCollectionCallback(pointer, callback.function, callback.context)16
}17
}19
fun publish(pointer: Ptr<Unit>, node: AccessibilityNode) {20
@Unsafe21
{22
when (node.relationValues.activeDescendant) {23
case Some(Item(val collection, val key)) ->24
withCollectionKey(key) { kind, number, bytes, length ->25
@Unsafe26
{27
checkNativeStatus(28
nativeCollectionActive(29
pointer,30
node.id.number,31
collection.number,32
kind,33
number,34
bytes,35
length36
),37
"publish active collection item"38
)39
}40
}41
else -> {}42
}43
checkNativeStatus(44
nativeCollectionSource(45
pointer,46
node.id.number,47
node.collectionValue?.count ?: -1L,48
node.collectionRevision,49
collectionActionBits(node.itemActions) +50
gridShapeFlags(node.collectionValue?.grid ?: None)51
),52
"publish semantic collection"53
)54
publishCollectionDetails(pointer, node)55
when (val key = node.itemKey) {56
case Some(val value) -> {57
withCollectionKey(value) { kind, number, bytes, length ->58
@Unsafe59
{60
checkNativeStatus(61
nativeCollectionRow(62
pointer,63
node.id.number,64
kind,65
number,66
bytes,67
length68
),69
"publish semantic collection row"70
)71
}72
}73
}75
case None -> {}76
}77
when (node.properties.cell) {78
case Some(val cell) ->79
when (cell.columnKey) {80
case Some(val key) ->81
withCollectionKey(key) { kind, number, bytes, length ->82
@Unsafe83
{84
checkNativeStatus(85
nativeCollectionCell(86
pointer,87
node.id.number,88
kind,89
number,90
bytes,91
length92
),93
"publish semantic cell column"94
)95
}96
}97
case None -> {}98
}99
case None -> {}100
}101
}102
}104
public override fun close() {105
val owned = callback106
application107
.mailbox108
.postControl {109
@Unsafe110
{111
releaseForeignCallback(owned)112
}113
}114
}115
}117
@Extern(name = "scoop_window_accessibility_collection_active")118
private fun nativeCollectionActive(119
provider: Ptr<Unit>,120
owner: Long,121
collection: Long,122
kind: Int,123
number: Long,124
bytes: Ptr<UInt8>?,125
length: Long126
): Long128
private class CollectionQueryStatus {129
var value = 4130
}132
private fun gridShapeFlags(grid: SemanticGrid?): Int = (133
when (grid?.span ?: None) {134
case Some(_) -> 128135
case None -> 0136
}137
) +138
(139
when (grid?.hasCells ?: None) {140
case Some(_) -> 512141
case None -> 0142
}143
)145
private fun collectionActionBits(actions: SemanticCollectionActions): Int = (146
when (actions.realize) {147
case Some(_) -> 1;148
case None -> 0149
}150
) +151
(152
when (actions.reveal) {153
case Some(_) -> 2;154
case None -> 0155
}156
) +157
(158
when (actions.focus) {159
case Some(_) -> 4;160
case None -> 0161
}162
) +163
(164
when (actions.setSelected) {165
case Some(_) -> 8;166
case None -> 0167
}168
) +169
gridActionBits(actions.grid) +170
(171
when (actions.setExpanded) {172
case Some(_) -> 256173
case None -> 0174
}175
) +176
(177
when (actions.setChecked) {178
case Some(_) -> 1024179
case None -> 0180
}181
)183
private fun gridActionBits(actions: SemanticGridActions?): Int {184
val grid = actions ?: return 0185
var bits = 0186
when (grid.realize) {187
case Some(_) -> bits += 16188
case None -> {}189
}190
when (grid.reveal) {191
case Some(_) -> bits += 32192
case None -> {}193
}194
when (grid.focus) {195
case Some(_) -> bits += 64196
case None -> {}197
}198
return bits199
}201
private fun collectionCallback(application: ApplicationOwner, provider: AccessibilityProvider):202
ForeignCallback<(Ptr<Unit>, Long, Int, Long, Int, Long, Ptr<UInt8>?, Long, Ptr<Unit>?) -> Int> {203
@Unsafe204
{205
return foreignCallback<206
(Ptr<Unit>, Long, Int, Long, Int, Long, Ptr<UInt8>?, Long, Ptr<Unit>?) -> Int207
>(208
{209
owner: Long,210
operation: Int,211
index: Long,212
kind: Int,213
number: Long,214
bytes: Ptr<UInt8>?,215
length: Long,216
reply: Ptr<Unit>?217
->218
queryCollection(219
application,220
provider,221
owner,222
operation,223
index,224
kind,225
number,226
bytes,227
length,228
reply229
)230
},231
0,232
ForeignCallbackMode.Reusable233
)234
}235
}237
private fun queryCollection(238
application: ApplicationOwner,239
provider: AccessibilityProvider,240
owner: Long,241
operation: Int,242
index: Long,243
kind: Int,244
number: Long,245
bytes: Ptr<UInt8>?,246
length: Long,247
reply: Ptr<Unit>?248
): Int {249
if ((operation >= 3 && operation <= 8) ||250
(operation >= 12 && operation <= 15) ||251
operation ==252
17 ||253
operation ==254
18 ||255
operation ==256
19 ||257
operation ==258
20259
) {260
val result = application261
.performAccessibility {262
val key = collectionKey(kind, number, bytes, length) ?:263
return@performAccessibility SemanticActionResult.InvalidArgument264
provider.performCollection(owner, key, operation, index)265
}266
return actionCode(result)267
}268
if (operation == 2 || operation == 16) {269
try {270
val source = provider.collectionSource(owner, true) ?: return 4271
val key = collectionKey(kind, number, bytes, length) ?: return 4272
if (operation == 16) {273
val span = source.cellSpanForIndex(key, index) ?: return 2274
val target = reply ?: return 4275
val grid = source.grid!!276
writeCollectionItem(277
target,278
SemanticItem(279
grid.columns[span.column].key,280
span.column,281
grid.columns.size,282
SemanticItemProperties(),283
span.columnSpan284
)285
)286
return 1287
}288
return if (source.contains(key)) 1 else 2289
} catch (error: Throwable) {290
application.failAccessibility(error)291
return 4292
}293
}294
val status = CollectionQueryStatus()295
val result = application296
.performAccessibility {297
val source = provider.collectionSource(owner, false)298
when (source) {299
case Some(val value) -> {300
val item = if (operation == 9 || operation == 10) {301
queryCollectionText(302
value,303
operation,304
index,305
kind,306
number,307
bytes,308
length,309
reply310
)311
} else if (operation == 11) {312
val key = collectionKey(kind, number, bytes, length)313
when ((key, value.grid)) {314
case(Some(val row), Some(val grid)) ->315
if (index >= 0L && index < grid.columns.size)316
value.cellForKey(row, grid.columns[index].key)317
else318
SemanticItemResult.NotFound319
else -> SemanticItemResult.Unavailable320
}321
} else if (operation == 0)322
value.itemAt(index)323
else {324
val key = collectionKey(kind, number, bytes, length)325
when (key) {326
case Some(val actual) -> value.itemForKey(actual)327
case None -> SemanticItemResult.Unavailable328
}329
}330
status.value = when (item) {331
case Found(val found) ->332
when (reply) {333
case Some(val target) -> {334
writeCollectionItem(target, found);335
1336
}338
case None -> 4339
}340
case NotFound -> 2341
case Unavailable -> 3342
case Unsupported -> 5343
}344
}346
case None -> {}347
}348
SemanticActionResult.Completed349
}350
return if (result == SemanticActionResult.Completed) status.value else 0351
}353
private fun publishCollectionDetails(pointer: Ptr<Unit>, node: AccessibilityNode) {354
when (node.properties.selectionMode) {355
case Some(val mode) ->356
@Unsafe357
{358
checkNativeStatus(359
nativeCollectionSelection(360
pointer,361
node.id.number,362
if (mode == SemanticSelectionMode.Multiple) 1 else 0,363
if (node.properties.required) 1 else 0364
),365
"publish static selection"366
)367
}368
case None -> {}369
}370
val source = node.collectionValue ?: return371
when (source.selection) {372
case Some(val selected) -> {373
@Unsafe374
{375
checkNativeStatus(376
nativeCollectionSelection(377
pointer,378
node.id.number,379
if (selected.multiple) 1 else 0,380
if (selected.required) 1 else 0381
),382
"publish collection selection"383
)384
}385
for (key in selected.keys) {386
when (source.itemForKey(key)) {387
case Found(val item) -> {388
if (!item.properties.selectable) continue389
}391
else -> continue392
}393
withCollectionKey(key) { kind, number, bytes, length ->394
@Unsafe395
{396
checkNativeStatus(397
nativeCollectionSelected(398
pointer,399
node.id.number,400
kind,401
number,402
bytes,403
length404
),405
"publish selected collection item"406
)407
}408
}409
}410
}412
case None -> {}413
}414
when (source.grid) {415
case Some(val grid) ->416
for (index in 0L..<grid.columns.size) {417
val column = grid.columns[index]418
withCollectionKey(column.key) { kind, number, bytes, length ->419
@Unsafe420
{421
checkNativeStatus(422
nativeCollectionColumn(423
pointer,424
node.id.number,425
index,426
kind,427
number,428
bytes,429
length,430
column.header?.number ?: 0L431
),432
"publish semantic column"433
)434
}435
}436
when (column.name) {437
case Some(val name) ->438
@Unsafe439
{440
name441
.withUtf8Bytes { bytes, length ->442
@Unsafe443
{444
checkNativeStatus(445
nativeCollectionColumnName(446
pointer,447
node.id.number,448
index,449
bytes,450
length451
),452
"publish semantic column name"453
)454
}455
}456
}457
case None -> {}458
}459
}460
case None -> {}461
}462
}464
@Extern(name = "scoop_window_accessibility_collection_selection")465
private fun nativeCollectionSelection(466
provider: Ptr<Unit>,467
owner: Long,468
kind: Int,469
required: Int470
): Long472
@Extern(name = "scoop_window_accessibility_collection_selected")473
private fun nativeCollectionSelected(474
provider: Ptr<Unit>,475
owner: Long,476
kind: Int,477
number: Long,478
text: Ptr<UInt8>?,479
length: Long480
): Long482
@Extern(name = "scoop_window_accessibility_collection_column")483
private fun nativeCollectionColumn(484
provider: Ptr<Unit>,485
owner: Long,486
index: Long,487
kind: Int,488
number: Long,489
text: Ptr<UInt8>?,490
length: Long,491
header: Long492
): Long494
@Extern(name = "scoop_window_accessibility_collection_column_name")495
private fun nativeCollectionColumnName(496
provider: Ptr<Unit>,497
owner: Long,498
index: Long,499
text: Ptr<UInt8>,500
length: Long501
): Long503
@Extern(name = "scoop_window_accessibility_collection_cell")504
private fun nativeCollectionCell(505
provider: Ptr<Unit>,506
owner: Long,507
kind: Int,508
number: Long,509
text: Ptr<UInt8>?,510
length: Long511
): Long513
private fun collectionKey(514
kind: Int,515
number: Long,516
bytes: Ptr<UInt8>?,517
length: Long518
): SemanticItemKey? {519
if (kind == 0) return Some(SemanticItemKey.Number(number))520
if (kind != 1 || length < 0L) return None521
when (bytes) {522
case Some(val text) -> {523
@Unsafe524
{525
return Some(SemanticItemKey.Text(String.fromUtf8(text, length)))526
}527
}529
case None -> return if (length == 0L) Some(SemanticItemKey.Text("")) else None530
}531
}533
private fun withCollectionKey(key: SemanticItemKey, block: (Int, Long, Ptr<UInt8>?, Long) -> Unit) {534
when (key) {535
case Number(val number) -> block(0, number, None, 0L)537
case Text(val text) -> {538
@Unsafe539
{540
text.withUtf8Bytes { bytes, length -> block(1, 0L, Some(bytes), length) }541
}542
}543
}544
}546
private fun writeCollectionItem(reply: Ptr<Unit>, item: SemanticItem) {547
val props = item.properties548
@Unsafe549
{550
nativeCollectionItem(551
reply,552
item.index,553
item.count,554
item.span,555
if (props.enabled) 1 else 0,556
when (props.selected) {557
case None -> -1;558
case Some(val value) -> if (value) 1 else 0559
},560
when (props.expanded) {561
case None -> -1;562
case Some(val value) -> if (value) 1 else 0563
},564
if (props.focusable) 1 else 0,565
props.hierarchyLevel ?: -1L,566
if (item.hasCells) 1 else 0,567
when (props.checked) {568
case Some(Off) -> 0569
case Some(On) -> 1570
case Some(Mixed) -> 2571
case None -> -1572
},573
if (props.busy) 1 else 0,574
props.siblings?.index ?: -1L,575
props.siblings?.count ?: -1L576
)577
}578
withCollectionKey(item.key) { kind, number, bytes, length ->579
@Unsafe580
{581
nativeCollectionItemKey(reply, kind, number, bytes, length)582
}583
}584
when (props.parentKey) {585
case Some(val parent) ->586
withCollectionKey(parent) { kind, number, bytes, length ->587
@Unsafe {588
nativeCollectionItemParent(reply, kind, number, bytes, length)589
}590
}591
case None -> {}592
}593
collectionText(reply, 0, props.name)594
collectionText(reply, 1, props.description)595
collectionText(reply, 2, props.automationId)596
collectionText(reply, 3, props.value)597
}599
private fun collectionText(reply: Ptr<Unit>, field: Int, value: String?) {600
when (value) {601
case Some(val text) -> {602
@Unsafe603
{604
text605
.withUtf8Bytes { bytes, length ->606
@Unsafe607
{608
nativeCollectionItemText(reply, field, bytes, length)609
}610
}611
}612
}614
case None -> {}615
}616
}618
@Extern(name = "scoop_window_accessibility_collection_callback")619
private fun nativeCollectionCallback(620
provider: Ptr<Unit>,621
callback: FunPtr<(Ptr<Unit>, Long, Int, Long, Int, Long, Ptr<UInt8>?, Long, Ptr<Unit>?) -> Int>,622
context: Ptr<Unit>623
)625
@Extern(name = "scoop_window_accessibility_collection")626
private fun nativeCollectionSource(627
provider: Ptr<Unit>,628
number: Long,629
count: Long,630
revision: ULong,631
actions: Int632
): Long634
@Extern(name = "scoop_window_accessibility_collection_row")635
private fun nativeCollectionRow(636
provider: Ptr<Unit>,637
node: Long,638
kind: Int,639
number: Long,640
text: Ptr<UInt8>?,641
length: Long642
): Long644
@Extern(name = "scoop_window_accessibility_collection_item")645
private fun nativeCollectionItem(646
reply: Ptr<Unit>,647
index: Long,648
count: Long,649
span: Long,650
enabled: Int,651
selected: Int,652
expanded: Int,653
focusable: Int,654
hierarchyLevel: Long,655
hasCells: Int,656
checked: Int,657
busy: Int,658
siblingIndex: Long,659
siblingCount: Long660
)662
@Extern(name = "scoop_window_accessibility_collection_item_key")663
private fun nativeCollectionItemKey(664
reply: Ptr<Unit>,665
kind: Int,666
number: Long,667
text: Ptr<UInt8>?,668
length: Long669
)671
@Extern(name = "scoop_window_accessibility_collection_item_parent")672
private fun nativeCollectionItemParent(673
reply: Ptr<Unit>,674
kind: Int,675
number: Long,676
text: Ptr<UInt8>?,677
length: Long678
)680
@Extern(name = "scoop_window_accessibility_collection_item_text")681
private fun nativeCollectionItemText(reply: Ptr<Unit>, field: Int, text: Ptr<UInt8>, length: Long)683
@CLayout684
private struct NativeCollectionSearch(val bytes: Ptr<UInt8>?, val length: Long)686
@Extern(name = "scoop_window_accessibility_collection_search")687
private fun nativeCollectionSearch(reply: Ptr<Unit>): NativeCollectionSearch689
private fun queryCollectionText(690
source: SemanticCollectionSnapshot,691
operation: Int,692
hasAfter: Long,693
kind: Int,694
number: Long,695
bytes: Ptr<UInt8>?,696
length: Long,697
reply: Ptr<Unit>?698
): SemanticItemResult {699
val target = reply ?: return SemanticItemResult.Unavailable700
@Unsafe701
{702
val query = nativeCollectionSearch(target)703
if (query.length < 0L) return SemanticItemResult.Unavailable704
val text = when (query.bytes) {705
case Some(val value) -> String.fromUtf8(value, query.length)706
case None -> if (query.length == 0L) "" else return SemanticItemResult.Unavailable707
}708
val after = if (hasAfter == 0L) None else collectionKey(kind, number, bytes, length)709
if (hasAfter != 0L && after == None) return SemanticItemResult.Unavailable710
return source711
.find(712
if (operation == 9)713
SemanticItemQuery.Name(text)714
else715
SemanticItemQuery.AutomationId(text),716
after717
)718
}719
}