Scoop API

scoop:scoop.ui:0.1.0

src/accessibility/grid.scoop

Selected production source · src/accessibility/grid.scoop

1package scoop.ui
3import scoop.platform.time.MonotonicTimeSource
4import scoop.window.accessibility.*
6/** Exists only for an actual mounted row with lazy columns. */
7internal class SemanticGridColumns(
8 private val table: SemanticsHandle,
9 val row: ViewKey,
10 val node: MountedNode
11) : Closeable {
12 val items = node.lazyItems ?:
13 throw IllegalArgumentException(Some("grid columns require a lazy element"))
14 private var closed = false
15 val active: Boolean get() = !closed && node.active && table.active
16 init {
17 table.checkOpen()
18 requireUi(
19 table.role == SemanticRole.Table || table.role == SemanticRole.Tree,
20 "grid columns require a table or tree collection"
21 )
22 val rows = table.owner.lazyItems ?:
23 throw IllegalArgumentException(Some("grid columns require an ancestor lazy table"))
24 val entry = rows.entryContaining(node) ?:
25 throw IllegalArgumentException(
26 Some("grid columns must belong to an actual table row")
27 )
28 requireUi(entry.key == row, "grid column row key differs from its actual row")
29 requireUi(!(table.gridColumns[row]?.active ?: false), "row already has grid columns")
30 table.gridColumns[row] = this
31 }
32 public override fun close() {
33 if (closed) return
34 closed = true
35 when (table.gridColumns[row]) {
36 case Some(val current) -> if (current === this) table.gridColumns.remove(row)
37 case None -> {}
38 }
39 }
42internal fun semanticGridActions(table: SemanticsHandle): SemanticGridActions = SemanticGridActions(
43 Some { row, column -> performSemanticCell(table, row, column, 12) },
44 when (table.collectionViewport) {
45 case Some(_) -> Some { row, column -> performSemanticCell(table, row, column, 13) }
46 case None -> None
47 },
48 Some { row, column, focused ->
49 performSemanticCell(
50 table,
51 row,
52 column,
53 if (focused) 14 else 15
54 )
55 }
58private class CellOperation {
59 var row: SemanticItemRetention? = None
60 var column: SemanticItemRetention? = None
63private fun performSemanticCell(
64 table: SemanticsHandle,
65 row: SemanticItemKey,
66 column: SemanticItemKey,
67 operation: Int
68): SemanticActionResult {
69 val retained = CellOperation()
70 try {
71 val result = table
72 .controller
73 .perform(table) {
74 val rows = table.owner.lazyItems ?: return@perform SemanticActionResult.Unavailable
75 val rowKey = itemViewKey(row)
76 val index = rows.indexOf(rowKey) ?: return@perform SemanticActionResult.Unavailable
77 if (operation == 15) {
78 val entry = rows.entry(rowKey) ?: return@perform SemanticActionResult.Completed
79 val cell = findSemanticCell(entry.node, column) ?:
80 return@perform SemanticActionResult.Completed
81 return@perform focusSemanticCell(cell, false)
82 }
83 val entry = prepareUi<LazyItemEntry> {
84 rows.prepare(table.owner, index, MonotonicTimeSource.now())
85 }
86 if (!table.active || !entry.node.active)
87 return@perform SemanticActionResult.Unavailable
88 retained.row = Some(SemanticItemRetention(entry))
89 val binding = table.gridColumns[rowKey]
90 var cellNode = entry.node
91 var columnEntry: LazyItemEntry? = None
92 when (binding) {
93 case Some(val bound) -> {
94 if (!bound.active) return@perform SemanticActionResult.Unavailable
95 val at = bound.items.indexOf(itemViewKey(column)) ?:
96 return@perform SemanticActionResult.Unavailable
97 val child = prepareUi<LazyItemEntry> {
98 bound.items.prepare(bound.node, at, MonotonicTimeSource.now())
99 }
100 if (!child.node.active) return@perform SemanticActionResult.Unavailable
101 retained.column = Some(SemanticItemRetention(child))
102 cellNode = child.node
103 columnEntry = Some(child)
104 }
106 case None -> {}
107 }
108 val cell = findSemanticCell(cellNode, column) ?:
109 return@perform SemanticActionResult.Unavailable
110 return@perform when (operation) {
111 case 12 -> SemanticActionResult.Completed
112 case 13 -> table.controller.revealCell(table, entry, cell, columnEntry, column)
114 case 14 -> {
115 table.controller.cancelReveal()
116 focusSemanticCell(cell, true)
117 }
119 else -> SemanticActionResult.Unsupported
120 }
121 }
122 if (!table.active ||
123 !(retained.row?.entry?.node?.active ?: true) ||
124 !(retained.column?.entry?.node?.active ?: true)
125 )
126 return SemanticActionResult.Unavailable
127 return result
128 } catch (ended: UiLayoutEnded) {
129 return SemanticActionResult.Unavailable
130 } finally {
131 retained.column?.close()
132 retained.row?.close()
133 }
136private fun findSemanticCell(node: MountedNode, column: SemanticItemKey): SemanticsHandle? {
137 if (!node.active) return None
138 for (handle in node.semanticHandles)
139 if (handle.active && (handle.properties.cell?.columnKey ?: None) == Some(column))
140 return Some(handle)
141 for (handle in node.semanticHandles)
142 if (handle.active && (handle.role == SemanticRole.Table || handle.role == SemanticRole.Tree)
143 )
144 return None
145 for (child in node.children) {
146 val found = findSemanticCell(child, column)
147 when (found) {
148 case Some(_) -> return found
149 case None -> {}
150 }
151 }
152 return None
155private fun focusSemanticCell(cell: SemanticsHandle, focused: Boolean): SemanticActionResult {
156 if (!cell.active) return SemanticActionResult.Unavailable
157 if (cell.merged || !cell.owner.scope.input.isInputAllowed) return SemanticActionResult.Rejected
158 var ancestor: SemanticsHandle? = Some(cell)
159 while (true) {
160 val handle = ancestor ?: break
161 if (!handle.active || handle.properties.hidden || !handle.properties.enabled)
162 return SemanticActionResult.Rejected
163 ancestor = handle.parent
164 }
165 when (cell.actions.focus) {
166 case Some(val action) -> return action(focused)
167 case None -> {}
168 }
169 if (!focused) {
170 for (target in focusOrder(cell.owner))
171 if (target.handle.hasFocus || target.handle.isPending) target.handle.clearFocus()
172 return SemanticActionResult.Completed
173 }
174 for (target in focusOrder(cell.owner))
175 if (target.handle.isFocusActive) return SemanticActionResult.Completed
176 val target = cell.focus ?: return SemanticActionResult.Unsupported
177 if (!target.handle.requestFocus()) return SemanticActionResult.Rejected
178 return if (target.handle.isFocusActive)
179 SemanticActionResult.Completed
180 else
181 SemanticActionResult.Busy