scoop:scoop.ui:0.1.0
src/accessibility/collection_actions.scoop
Selected production source · src/accessibility/collection_actions.scoop
1
package scoop.ui3
import scoop.platform.time.MonotonicTimeSource4
import scoop.window.accessibility.*6
/** Retains an actual row only while an explicit item operation needs it. */7
internal class SemanticItemRetention(val entry: LazyItemEntry) : Closeable {8
private var closed = false9
init {10
entry.semanticPins += 111
}12
public override fun close() {13
if (closed) return14
closed = true15
entry.semanticPins -= 116
if (entry.semanticPins == 0 && entry.node.active && entry.node.root.isOpen)17
entry.node.parent?.request(UiInvalidation.Layout)18
}19
}21
private class ItemOperation {22
var retention: SemanticItemRetention? = None23
}25
internal fun semanticCollectionActions(handle: SemanticsHandle): SemanticCollectionActions {26
val reveal: ((SemanticItemKey) -> SemanticActionResult)? = when (handle.collectionViewport) {27
case Some(_) -> Some { key -> performSemanticItem(handle, key, 4) }28
case None -> None29
}30
val focus: ((SemanticItemKey, Boolean) -> SemanticActionResult)? = when (handle.owner.lazyFocus31
) {32
case Some(_) ->33
Some { key, value ->34
performSemanticItem(35
handle,36
key,37
if (value) 5 else 638
)39
}40
case None -> None41
}42
return SemanticCollectionActions(43
Some { key -> performSemanticItem(handle, key, 3) },44
reveal,45
focus,46
when (handle.collectionItems) {47
case Some(val items) ->48
when (items.setSelected) {49
case Some(val select) ->50
Some { key, selected ->51
handle.controller.perform(handle) { select(itemViewKey(key), selected) }52
}53
case None -> None54
}55
case None -> None56
},57
when (handle.collectionItems?.grid ?: None) {58
case Some(_) -> Some(semanticGridActions(handle))59
case None -> None60
},61
when (handle.collectionItems?.setExpanded ?: None) {62
case Some(val expand) ->63
Some { key, expanded ->64
handle.controller.perform(handle) { expand(itemViewKey(key), expanded) }65
}66
case None -> None67
},68
when (handle.collectionItems?.setChecked ?: None) {69
case Some(val check) ->70
Some { key, checked ->71
handle.controller.perform(handle) { check(itemViewKey(key), checked) }72
}73
case None -> None74
}75
)76
}78
internal fun itemViewKey(key: SemanticItemKey): ViewKey = when (key) {79
case Number(val value) -> ViewKey.Number(value)80
case Text(val value) -> ViewKey.Text(value)81
}83
private fun performSemanticItem(84
handle: SemanticsHandle,85
key: SemanticItemKey,86
operation: Int87
): SemanticActionResult {88
val request = ItemOperation()89
try {90
val result = handle91
.controller92
.perform(handle) {93
val items = handle.owner.lazyItems ?:94
return@perform SemanticActionResult.Unavailable95
val actual = itemViewKey(key)96
if (operation == 5 || operation == 6)97
return@perform focusSemanticItem(handle, actual, operation == 5)98
val index = items.indexOf(actual) ?: return@perform SemanticActionResult.Unavailable99
val entry = prepareUi<LazyItemEntry> {100
items.prepare(handle.owner, index, MonotonicTimeSource.now())101
}102
if (!handle.active || !entry.node.active)103
return@perform SemanticActionResult.Unavailable104
request.retention = Some(SemanticItemRetention(entry))105
if (operation == 3)106
SemanticActionResult.Completed107
else108
handle.controller.revealItem(handle, entry)109
}110
if (!handle.active) return SemanticActionResult.Unavailable111
when (request.retention) {112
case Some(val value) ->113
if (!value.entry.node.active) return SemanticActionResult.Unavailable114
case None -> {}115
}116
return result117
} catch (ended: UiLayoutEnded) {118
return SemanticActionResult.Unavailable119
} finally {120
request.retention?.close()121
}122
}124
private fun focusSemanticItem(125
handle: SemanticsHandle,126
key: ViewKey,127
focused: Boolean128
): SemanticActionResult {129
val owner = handle.owner.lazyFocus ?: return SemanticActionResult.Unsupported130
if (!owner.active) return SemanticActionResult.Unavailable131
val entry = owner.entry(key)132
if (!focused) {133
if (owner.node.root.focus.itemKey(owner) == Some(key)) owner.cancelRequest()134
when (entry) {135
case Some(val value) ->136
for (target in focusOrder(value.node))137
if (target.handle.hasFocus) target.handle.clearFocus()138
case None -> {}139
}140
return SemanticActionResult.Completed141
}142
when (entry) {143
case Some(val value) ->144
for (target in focusOrder(value.node))145
if (target.handle.isFocusActive) return SemanticActionResult.Completed146
case None -> {}147
}148
if (owner.node.root.focus.itemKey(owner) == Some(key)) return SemanticActionResult.Busy149
handle.controller.cancelReveal()150
return if (owner.requestFocus(key)) SemanticActionResult.Busy else SemanticActionResult.Rejected151
}