scoop:scoop.window:0.1.0
src/accessibility/collection.scoop
Selected production source · src/accessibility/collection.scoop
1
package scoop.window.accessibility3
import scoop.window.requireWindow4
import scoop.collections.HashSet6
/** Stable identity within one collection, independent of its current position. */7
public enum SemanticItemKey : Equals<SemanticItemKey>, Hash {8
Number(Long), Text(String)9
public override operator fun equals(other: SemanticItemKey): Boolean = when ((this, other)) {10
case(Number(val left), Number(val right)) -> left == right11
case(Text(val left), Text(val right)) -> left == right12
else -> false13
}14
public override fun hash(): Long = when (this) {15
case Number(val value) -> value.hash()16
case Text(val value) -> value.hash()17
}18
}20
/** Zero-based position in the current sibling set, independently of flattened row positions. */21
public struct SemanticItemPosition(val index: Long, val count: Long)23
/** Unknown text differs from known empty text; absent states are not advertised. */24
public struct SemanticItemProperties(25
val name: String? = None,26
val description: String? = None,27
val automationId: String? = None,28
val value: String? = None,29
val enabled: Boolean = true,30
val selected: Boolean? = None,31
val expanded: Boolean? = None,32
val focusable: Boolean = false,33
/** Zero-based hierarchy depth; absent for collections without a hierarchy. */34
val hierarchyLevel: Long? = None,35
/** Excludes this item from selection without disabling reading or focus. */36
val selectable: Boolean = true,37
val parentKey: SemanticItemKey? = None,38
val siblings: SemanticItemPosition? = None,39
val checked: SemanticChecked? = None,40
val busy: Boolean = false41
)43
private fun checkItemProperties(properties: SemanticItemProperties) {44
when (properties.hierarchyLevel) {45
case Some(val level) -> requireWindow(level >= 0L, "negative semantic hierarchy level")46
case None -> {}47
}48
when (properties.siblings) {49
case Some(val position) ->50
requireWindow(51
position.index >= 0L && position.index < position.count,52
"semantic sibling position is out of range"53
)54
case None -> {}55
}56
}58
/** Explicit item operations, separate from immutable metadata queries. */59
public struct SemanticCollectionActions(60
val realize: ((SemanticItemKey) -> SemanticActionResult)? = None,61
val reveal: ((SemanticItemKey) -> SemanticActionResult)? = None,62
val focus: ((SemanticItemKey, Boolean) -> SemanticActionResult)? = None,63
val setSelected: ((SemanticItemKey, Boolean) -> SemanticActionResult)? = None,64
val grid: SemanticGridActions? = None,65
val setExpanded: ((SemanticItemKey, Boolean) -> SemanticActionResult)? = None,66
val setChecked: ((SemanticItemKey, Boolean) -> SemanticActionResult)? = None67
)69
/** An immutable selection snapshot whose size is proportional to the selection. */70
public class SemanticCollectionSelection public constructor(71
public val keys: Array<SemanticItemKey>,72
public val multiple: Boolean = false,73
public val required: Boolean = false74
) {75
private val selected = HashSet<SemanticItemKey>()76
init {77
requireWindow(multiple || keys.size <= 1L, "single selection has multiple items")78
for (key in keys) {79
requireWindow(!selected.contains(key), "duplicate selected item key")80
selected.add(key)81
}82
}83
public fun contains(key: SemanticItemKey): Boolean = selected.contains(key)84
}86
public struct SemanticItemSearch(val name: Boolean = false, val automationId: Boolean = false)88
public enum SemanticItemQuery {89
Name(String), AutomationId(String)90
}92
/** Reads a single immutable data snapshot without constructing controls. */93
public interface SemanticCollectionSource {94
public val count: Long95
public val search: SemanticItemSearch96
public fun keyAt(index: Long): SemanticItemKey97
public fun indexOfKey(key: SemanticItemKey): Long?98
public fun describe(index: Long): SemanticItemProperties?99
}101
public struct SemanticItem(102
val key: SemanticItemKey,103
val index: Long,104
val count: Long,105
val properties: SemanticItemProperties,106
val span: Long = 1L,107
val hasCells: Boolean = true108
)110
public enum SemanticItemResult {111
Found(SemanticItem), NotFound, Unsupported, Unavailable112
}114
/** Retains only the source; explicit queries do not allocate an entire item tree. */115
public class SemanticCollectionSnapshot public constructor(116
private val source: SemanticCollectionSource,117
public val selection: SemanticCollectionSelection? = None,118
public val grid: SemanticGrid? = None119
) {120
public val count: Long = source.count121
public val search: SemanticItemSearch = source.search122
internal val hasSelection: Boolean123
get() = when (selection) {124
case Some(_) -> true125
case None -> false126
}127
init {128
requireWindow(count >= 0L, "negative semantic collection count")129
when (selection) {130
case Some(val value) ->131
for (key in value.keys)132
requireWindow(133
indexOf(key) != None,134
"selected key is not in the collection snapshot"135
)136
case None -> {}137
}138
}140
public fun itemAt(index: Long): SemanticItemResult {141
if (index < 0L || index >= count) return SemanticItemResult.NotFound142
val key = source.keyAt(index)143
requireWindow(144
source.indexOfKey(key) == Some(index),145
"semantic item key mapping is inconsistent"146
)147
return described(key, index)148
}150
public fun itemForKey(key: SemanticItemKey): SemanticItemResult {151
val index = indexOf(key) ?: return SemanticItemResult.NotFound152
return described(key, index)153
}155
/** Revalidates identity without requiring a currently available description. */156
internal fun contains(key: SemanticItemKey): Boolean = indexOf(key) != None158
private fun indexOf(key: SemanticItemKey): Long? {159
val index = source.indexOfKey(key) ?: return None160
requireWindow(index >= 0L && index < count, "semantic item index is out of range")161
requireWindow(source.keyAt(index) == key, "semantic item key mapping is inconsistent")162
return Some(index)163
}165
private fun described(key: SemanticItemKey, index: Long): SemanticItemResult {166
val properties = source.describe(index) ?: return SemanticItemResult.Unavailable167
checkItemProperties(properties)168
when (properties.parentKey) {169
case Some(val parent) -> {170
val position = indexOf(parent)171
requireWindow(172
position != None && position!! < index,173
"semantic parent must precede its child in the same collection"174
)175
}177
case None -> {}178
}179
val selected = if (!properties.selectable)180
None181
else182
when (selection) {183
case Some(val value) -> Some(value.contains(key))184
case None -> properties.selected185
}186
val actual = if (selected == properties.selected)187
properties188
else189
SemanticItemProperties(190
properties.name,191
properties.description,192
properties.automationId,193
properties.value,194
properties.enabled,195
selected,196
properties.expanded,197
properties.focusable,198
properties.hierarchyLevel,199
properties.selectable,200
properties.parentKey,201
properties.siblings,202
properties.checked,203
properties.busy204
)205
return SemanticItemResult206
.Found(207
SemanticItem(208
key,209
index,210
count,211
actual,212
hasCells = (grid?.hasCells ?: None)?.invoke(key) ?: true213
)214
)215
}217
public fun cellForKey(row: SemanticItemKey, column: SemanticItemKey): SemanticItemResult {218
if (indexOf(row) == None) return SemanticItemResult.NotFound219
val table = grid ?: return SemanticItemResult.Unsupported220
val index = table.indexOf(column) ?: return SemanticItemResult.NotFound221
if (!(table.hasCells?.invoke(row) ?: true)) return SemanticItemResult.NotFound222
val span = table.resolveSpan(row, index)223
val properties = table.describe(row, span.column) ?: return SemanticItemResult.Unavailable224
checkItemProperties(properties)225
return SemanticItemResult226
.Found(227
SemanticItem(228
table.columns[span.column].key,229
span.column,230
table.columns.size,231
properties,232
span.columnSpan233
)234
)235
}237
/** Used while publishing to retire covered columns and rows without cells. */238
internal fun cellSpanForIndex(row: SemanticItemKey, column: Long): SemanticGridSpan? {239
if (indexOf(row) == None) return None240
val table = grid ?: return None241
if (column < 0L || column >= table.columns.size) return None242
if (!(table.hasCells?.invoke(row) ?: true)) return None243
return Some(table.resolveSpan(row, column))244
}246
/** Searches after a key in this snapshot, without retaining a positional cursor. */247
public fun find(query: SemanticItemQuery, after: SemanticItemKey? = None): SemanticItemResult {248
val supported = when (query) {249
case Name(_) -> search.name250
case AutomationId(_) -> search.automationId251
}252
if (!supported) return SemanticItemResult.Unsupported253
val start = when (after) {254
case None -> 0L255
case Some(val key) -> (indexOf(key) ?: return SemanticItemResult.Unavailable) + 1L256
}257
for (index in start..<count) {258
val item = when (itemAt(index)) {259
case Found(val value) -> value260
case NotFound -> return SemanticItemResult.NotFound261
case Unavailable -> return SemanticItemResult.Unavailable262
case Unsupported -> return SemanticItemResult.Unsupported263
}264
val matches = when (query) {265
case Name(val expected) ->266
(item.properties.name ?: return SemanticItemResult.Unavailable) == expected267
case AutomationId(val expected) ->268
(item.properties.automationId ?: return SemanticItemResult.Unavailable) ==269
expected270
}271
if (matches) return SemanticItemResult.Found(item)272
}273
return SemanticItemResult.NotFound274
}275
}