Scoop API

scoop:scoop.window:0.1.0

src/accessibility/collection.scoop

Selected production source · src/accessibility/collection.scoop

1package scoop.window.accessibility
3import scoop.window.requireWindow
4import scoop.collections.HashSet
6/** Stable identity within one collection, independent of its current position. */
7public 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 == right
11 case(Text(val left), Text(val right)) -> left == right
12 else -> false
13 }
14 public override fun hash(): Long = when (this) {
15 case Number(val value) -> value.hash()
16 case Text(val value) -> value.hash()
17 }
20/** Zero-based position in the current sibling set, independently of flattened row positions. */
21public struct SemanticItemPosition(val index: Long, val count: Long)
23/** Unknown text differs from known empty text; absent states are not advertised. */
24public 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 = false
43private 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 }
58/** Explicit item operations, separate from immutable metadata queries. */
59public 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)? = None
69/** An immutable selection snapshot whose size is proportional to the selection. */
70public class SemanticCollectionSelection public constructor(
71 public val keys: Array<SemanticItemKey>,
72 public val multiple: Boolean = false,
73 public val required: Boolean = false
74) {
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)
86public struct SemanticItemSearch(val name: Boolean = false, val automationId: Boolean = false)
88public enum SemanticItemQuery {
89 Name(String), AutomationId(String)
92/** Reads a single immutable data snapshot without constructing controls. */
93public interface SemanticCollectionSource {
94 public val count: Long
95 public val search: SemanticItemSearch
96 public fun keyAt(index: Long): SemanticItemKey
97 public fun indexOfKey(key: SemanticItemKey): Long?
98 public fun describe(index: Long): SemanticItemProperties?
101public 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 = true
110public enum SemanticItemResult {
111 Found(SemanticItem), NotFound, Unsupported, Unavailable
114/** Retains only the source; explicit queries do not allocate an entire item tree. */
115public class SemanticCollectionSnapshot public constructor(
116 private val source: SemanticCollectionSource,
117 public val selection: SemanticCollectionSelection? = None,
118 public val grid: SemanticGrid? = None
119) {
120 public val count: Long = source.count
121 public val search: SemanticItemSearch = source.search
122 internal val hasSelection: Boolean
123 get() = when (selection) {
124 case Some(_) -> true
125 case None -> false
126 }
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.NotFound
142 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.NotFound
152 return described(key, index)
153 }
155 /** Revalidates identity without requiring a currently available description. */
156 internal fun contains(key: SemanticItemKey): Boolean = indexOf(key) != None
158 private fun indexOf(key: SemanticItemKey): Long? {
159 val index = source.indexOfKey(key) ?: return None
160 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.Unavailable
167 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 None
181 else
182 when (selection) {
183 case Some(val value) -> Some(value.contains(key))
184 case None -> properties.selected
185 }
186 val actual = if (selected == properties.selected)
187 properties
188 else
189 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.busy
204 )
205 return SemanticItemResult
206 .Found(
207 SemanticItem(
208 key,
209 index,
210 count,
211 actual,
212 hasCells = (grid?.hasCells ?: None)?.invoke(key) ?: true
213 )
214 )
215 }
217 public fun cellForKey(row: SemanticItemKey, column: SemanticItemKey): SemanticItemResult {
218 if (indexOf(row) == None) return SemanticItemResult.NotFound
219 val table = grid ?: return SemanticItemResult.Unsupported
220 val index = table.indexOf(column) ?: return SemanticItemResult.NotFound
221 if (!(table.hasCells?.invoke(row) ?: true)) return SemanticItemResult.NotFound
222 val span = table.resolveSpan(row, index)
223 val properties = table.describe(row, span.column) ?: return SemanticItemResult.Unavailable
224 checkItemProperties(properties)
225 return SemanticItemResult
226 .Found(
227 SemanticItem(
228 table.columns[span.column].key,
229 span.column,
230 table.columns.size,
231 properties,
232 span.columnSpan
233 )
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 None
240 val table = grid ?: return None
241 if (column < 0L || column >= table.columns.size) return None
242 if (!(table.hasCells?.invoke(row) ?: true)) return None
243 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.name
250 case AutomationId(_) -> search.automationId
251 }
252 if (!supported) return SemanticItemResult.Unsupported
253 val start = when (after) {
254 case None -> 0L
255 case Some(val key) -> (indexOf(key) ?: return SemanticItemResult.Unavailable) + 1L
256 }
257 for (index in start..<count) {
258 val item = when (itemAt(index)) {
259 case Found(val value) -> value
260 case NotFound -> return SemanticItemResult.NotFound
261 case Unavailable -> return SemanticItemResult.Unavailable
262 case Unsupported -> return SemanticItemResult.Unsupported
263 }
264 val matches = when (query) {
265 case Name(val expected) ->
266 (item.properties.name ?: return SemanticItemResult.Unavailable) == expected
267 case AutomationId(val expected) ->
268 (item.properties.automationId ?: return SemanticItemResult.Unavailable) ==
269 expected
270 }
271 if (matches) return SemanticItemResult.Found(item)
272 }
273 return SemanticItemResult.NotFound
274 }