scoop:scoop.ui:0.1.0
src/accessibility/collection.scoop
Selected production source · src/accessibility/collection.scoop
1
package scoop.ui3
import scoop.window.accessibility.*5
/** Data descriptions for exactly the ItemSource snapshot used by a lazy container. */6
public class ItemSemantics public constructor(7
public val source: ItemSource,8
public val describe: (Long) -> SemanticItemProperties?,9
public val search: SemanticItemSearch = SemanticItemSearch(),10
public val selection: SemanticCollectionSelection? = None,11
public val setSelected: ((ViewKey, Boolean) -> SemanticActionResult)? = None,12
public val grid: SemanticGrid? = None,13
public val setExpanded: ((ViewKey, Boolean) -> SemanticActionResult)? = None,14
public val setChecked: ((ViewKey, Boolean) -> SemanticActionResult)? = None15
)17
private class LazySemanticSource(18
private val source: ItemSource,19
private val items: ItemSemantics?20
) : SemanticCollectionSource {21
public override val count: Long get() = evaluateUi<Long>(None) { source.count }22
public override val search: SemanticItemSearch get() = items?.search ?: SemanticItemSearch()23
public override fun keyAt(index: Long): SemanticItemKey = evaluateUi<SemanticItemKey>(None) {24
semanticItemKey(source.keyAt(index))25
}26
public override fun indexOfKey(key: SemanticItemKey): Long? = evaluateUi<Long?>(None) {27
source28
.indexOfKey(29
when (key) {30
case Number(val value) -> ViewKey.Number(value)31
case Text(val value) -> ViewKey.Text(value)32
}33
)34
}35
public override fun describe(index: Long): SemanticItemProperties? = evaluateUi<36
SemanticItemProperties?37
>(None) {38
when (items) {39
case Some(val value) -> value.describe(index)40
case None -> Some(SemanticItemProperties())41
}42
}43
}45
/** Data-only semantics for an existing drawing, such as a chart, without a lazy view tree. */46
public fun SemanticsHandle.updateDataCollection(47
snapshot: SemanticCollectionSnapshot,48
actions: SemanticCollectionActions = SemanticCollectionActions()49
) {50
checkOpen()51
checkSemanticsMutation()52
when (owner.lazyItems) {53
case Some(_) ->54
throw IllegalArgumentException(55
Some("lazy elements must use updateCollection")56
);57
case None -> {}58
}59
resource.collection = Some(snapshot)60
resource.collectionActions = SemanticCollectionActions(61
when (actions.realize) {62
case Some(val action) -> Some { key -> controller.perform(this) { action(key) } };63
case None -> None64
},65
when (actions.reveal) {66
case Some(val action) -> Some { key -> controller.perform(this) { action(key) } };67
case None -> None68
},69
when (actions.focus) {70
case Some(val action) ->71
Some { key, value -> controller.perform(this) { action(key, value) } };72
case None -> None73
},74
when (actions.setSelected) {75
case Some(val action) ->76
Some { key, value -> controller.perform(this) { action(key, value) } };77
case None -> None78
},79
when (actions.grid) {80
case Some(val grid) -> Some(wrappedGridActions(this, grid))81
case None -> None82
},83
when (actions.setExpanded) {84
case Some(val action) ->85
Some { key, value -> controller.perform(this) { action(key, value) } }86
case None -> None87
},88
when (actions.setChecked) {89
case Some(val action) ->90
Some { key, value -> controller.perform(this) { action(key, value) } }91
case None -> None92
}93
)94
controller.changed()95
}97
/** Binds one lazy mount's data-level list without constructing any of its rows. */98
public fun SemanticsHandle.updateCollection(99
source: ItemSource,100
items: ItemSemantics? = None,101
viewport: ScrollViewport? = None102
) {103
checkOpen()104
checkSemanticsMutation()105
requireUi(106
role == SemanticRole.List || role == SemanticRole.Table || role == SemanticRole.Tree,107
"lazy collection requires a list, table or tree role"108
)109
val lazy = owner.lazyItems ?:110
throw IllegalArgumentException(Some("collection semantics require a lazy element"))111
requireUi(lazy.uses(source), "semantic collection source differs from the lazy source")112
when (viewport) {113
case Some(val value) ->114
requireUi(115
value.contains(owner),116
"semantic collection viewport must belong to this mount or an ancestor"117
)118
case None -> {}119
}120
when (items) {121
case Some(val value) ->122
requireUi(123
value.source === source,124
"item semantics must describe the same item source snapshot"125
)126
case None -> {}127
}128
when (owner.lazySemantics) {129
case Some(val current) ->130
requireUi(131
current === this,132
"a lazy element already has collection semantics"133
)134
case None -> owner.lazySemantics = Some(this)135
}136
val sameItems = when ((collectionItems, items)) {137
case(Some(val before), Some(val after)) -> before === after138
case(None, None) -> true139
else -> false140
}141
val sameViewport = when ((collectionViewport, viewport)) {142
case(Some(val before), Some(val after)) -> before === after143
case(None, None) -> true144
else -> false145
}146
when (collectionSource) {147
case Some(val before) -> if (before === source && sameItems && sameViewport) return148
case None -> {}149
}150
resource.collection = Some(151
SemanticCollectionSnapshot(152
LazySemanticSource(source, items),153
selection = when (items) {154
case Some(val value) -> value.selection;155
case None -> None156
},157
grid = when (items) {158
case Some(val value) -> readonlyGrid(value.grid);159
case None -> None160
}161
)162
)163
collectionSource = Some(source)164
collectionItems = items165
if (!sameViewport) controller.cancelCollectionReveal(this)166
collectionViewport = viewport167
resource.collectionActions = semanticCollectionActions(this)168
lazy.bindSemantics(this)169
controller.changed()170
}172
private fun readonlyGrid(grid: SemanticGrid?): SemanticGrid? = when (grid) {173
case Some(val value) ->174
Some(175
SemanticGrid(176
value.columns,177
{ row, column ->178
evaluateUi<SemanticItemProperties?>(None) { value.describe(row, column) }179
},180
span = when (value.span) {181
case Some(val resolve) ->182
Some { row, column ->183
evaluateUi<SemanticGridSpan>(None) { resolve(row, column) }184
}185
case None -> None186
},187
hasCells = when (value.hasCells) {188
case Some(val contains) ->189
Some { row -> evaluateUi<Boolean>(None) { contains(row) } }190
case None -> None191
}192
)193
)194
case None -> None195
}197
private fun wrappedGridActions(198
table: SemanticsHandle,199
actions: SemanticGridActions200
): SemanticGridActions = SemanticGridActions(201
when (actions.realize) {202
case Some(val action) ->203
Some { row, column -> table.controller.perform(table) { action(row, column) } }204
case None -> None205
},206
when (actions.reveal) {207
case Some(val action) ->208
Some { row, column -> table.controller.perform(table) { action(row, column) } }209
case None -> None210
},211
when (actions.focus) {212
case Some(val action) ->213
Some { row, column, focused ->214
table.controller.perform(table) { action(row, column, focused) }215
}216
case None -> None217
}218
)220
internal fun semanticItemKey(key: ViewKey): SemanticItemKey = when (key) {221
case Number(val value) -> SemanticItemKey.Number(value)222
case Text(val value) -> SemanticItemKey.Text(value)223
case Position ->224
throw IllegalArgumentException(225
Some("semantic items require explicit keys")226
)227
}229
internal fun bindLazyItemSemantics(entry: LazyItemEntry, parent: SemanticsHandle) {230
when (entry.node.semanticItem) {231
case Some(val current) ->232
if (current.active && sameSemantics(current.parent, Some(parent))) return233
case None -> {}234
}235
val row = parent.controller.create(entry.node, SemanticRole.Group, Some(parent))236
row.resource.collectionItem = Some(semanticItemKey(entry.key))237
row.update(SemanticProperties(ignored = true))238
entry.node.semanticItem = Some(row)239
entry.node.scope.routeSemanticsChildren(Some(row))240
}