Scoop API

scoop:scoop.ui:0.1.0

src/accessibility/collection_actions.scoop

Selected production source · src/accessibility/collection_actions.scoop

1package scoop.ui
3import scoop.platform.time.MonotonicTimeSource
4import scoop.window.accessibility.*
6/** Retains an actual row only while an explicit item operation needs it. */
7internal class SemanticItemRetention(val entry: LazyItemEntry) : Closeable {
8 private var closed = false
9 init {
10 entry.semanticPins += 1
11 }
12 public override fun close() {
13 if (closed) return
14 closed = true
15 entry.semanticPins -= 1
16 if (entry.semanticPins == 0 && entry.node.active && entry.node.root.isOpen)
17 entry.node.parent?.request(UiInvalidation.Layout)
18 }
21private class ItemOperation {
22 var retention: SemanticItemRetention? = None
25internal 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 -> None
29 }
30 val focus: ((SemanticItemKey, Boolean) -> SemanticActionResult)? = when (handle.owner.lazyFocus
31 ) {
32 case Some(_) ->
33 Some { key, value ->
34 performSemanticItem(
35 handle,
36 key,
37 if (value) 5 else 6
38 )
39 }
40 case None -> None
41 }
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 -> None
54 }
55 case None -> None
56 },
57 when (handle.collectionItems?.grid ?: None) {
58 case Some(_) -> Some(semanticGridActions(handle))
59 case None -> None
60 },
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 -> None
67 },
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 -> None
74 }
75 )
78internal fun itemViewKey(key: SemanticItemKey): ViewKey = when (key) {
79 case Number(val value) -> ViewKey.Number(value)
80 case Text(val value) -> ViewKey.Text(value)
83private fun performSemanticItem(
84 handle: SemanticsHandle,
85 key: SemanticItemKey,
86 operation: Int
87): SemanticActionResult {
88 val request = ItemOperation()
89 try {
90 val result = handle
91 .controller
92 .perform(handle) {
93 val items = handle.owner.lazyItems ?:
94 return@perform SemanticActionResult.Unavailable
95 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.Unavailable
99 val entry = prepareUi<LazyItemEntry> {
100 items.prepare(handle.owner, index, MonotonicTimeSource.now())
101 }
102 if (!handle.active || !entry.node.active)
103 return@perform SemanticActionResult.Unavailable
104 request.retention = Some(SemanticItemRetention(entry))
105 if (operation == 3)
106 SemanticActionResult.Completed
107 else
108 handle.controller.revealItem(handle, entry)
109 }
110 if (!handle.active) return SemanticActionResult.Unavailable
111 when (request.retention) {
112 case Some(val value) ->
113 if (!value.entry.node.active) return SemanticActionResult.Unavailable
114 case None -> {}
115 }
116 return result
117 } catch (ended: UiLayoutEnded) {
118 return SemanticActionResult.Unavailable
119 } finally {
120 request.retention?.close()
121 }
124private fun focusSemanticItem(
125 handle: SemanticsHandle,
126 key: ViewKey,
127 focused: Boolean
128): SemanticActionResult {
129 val owner = handle.owner.lazyFocus ?: return SemanticActionResult.Unsupported
130 if (!owner.active) return SemanticActionResult.Unavailable
131 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.Completed
141 }
142 when (entry) {
143 case Some(val value) ->
144 for (target in focusOrder(value.node))
145 if (target.handle.isFocusActive) return SemanticActionResult.Completed
146 case None -> {}
147 }
148 if (owner.node.root.focus.itemKey(owner) == Some(key)) return SemanticActionResult.Busy
149 handle.controller.cancelReveal()
150 return if (owner.requestFocus(key)) SemanticActionResult.Busy else SemanticActionResult.Rejected