Scoop API

scoop:scoop.ui:0.1.0

src/focus/lazy.scoop

Selected production source · src/focus/lazy.scoop

1package scoop.ui
3import scoop.surface.*
4import scoop.window.*
6public enum ItemFocusEdge {
7 First, Last
8}
10/** Resolves one keyed item through its ordinary mounted focus targets. */
11public class LazyFocus internal constructor(
12 internal val node: MountedNode,
13 internal val viewport: ScrollViewport,
14 private val items: LazyChildren,
15 initial: LazyFocusOptions
16): Closeable {
17 private val input = node.scope.input
18 private var current = initial
19 private var closed = false
20 private var recent: ViewKey? = None
21 private var registration: Closeable? = None
22 private var observation: Closeable? = None
24 init {
25 checkLazyFocusOptions(initial)
26 registration = Some(input.addDefaultAction { context, event -> navigate(context, event) })
27 try {
28 observation = Some(input.observeActivity { remember() })
29 } catch (failure: Throwable) {
30 registration?.close()
31 throw failure
32 }
33 }
35 internal val active: Boolean get() =
36 !closed && node.active && node.root.isOpen && viewport.active && !input.isClosed
37 internal val available: Boolean get() = active && node.scope.forChildren().isFocusAllowed
38 internal val usesItems: Boolean get() = active && current.navigation != ItemNavigation.None
39 internal val count: Long get() = items.count
40 public var options: LazyFocusOptions
41 get() {
42 node.root.checkThread()
43 return current
44 }
45 set(value) {
46 node.root.checkThread()
47 checkFocusMutation()
48 if (!active)
49 throw IllegalStateException(Some("item focus is no longer mounted"))
50 checkLazyFocusOptions(value)
51 if (value != current) {
52 node.root.focus.cancel(this)
53 current = value
54 }
55 }
56 public val isClosed: Boolean get() {
57 node.root.checkThread()
58 return closed || !viewport.active || input.isClosed
59 }
60 public val isPending: Boolean get() {
61 node.root.checkThread()
62 return active && node.root.focus.isPending(this)
63 }
64 /** The item still requiring preparation; exact target reveal continues after this becomes None. */
65 public val pendingItem: ViewKey? get() {
66 node.root.checkThread()
67 return if (active)
68 node.root.focus.itemToPrepare(this)
69 else
70 None
71 }
72 public fun requestFocus(key: ViewKey, edge: ItemFocusEdge = ItemFocusEdge.First): Boolean {
73 node.root.checkThread()
74 checkFocusMutation()
75 requireUi(key != ViewKey.Position, "item focus requires an explicit key")
76 return node.root.focus.request(this, key, edge)
77 }
78 /** Selects a real descendant after the requested item has an accepted layout. */
79 public fun requestFocus(key: ViewKey, target: () -> FocusHandle?): Boolean {
80 node.root.checkThread()
81 checkFocusMutation()
82 requireUi(key != ViewKey.Position, "item focus requires an explicit key")
83 return node.root.focus.request(this, key, ItemFocusEdge.First, Some(target))
84 }
85 public fun cancelRequest() {
86 node.root.checkThread()
87 checkFocusMutation()
88 node.root.focus.cancel(this)
89 }
90 internal fun pendingIndex(): Long? {
91 val key = pendingItem ?: return None
92 return items.indexOf(key)
93 }
94 internal fun entry(key: ViewKey): LazyItemEntry? = items.entry(key)
95 internal fun keyAt(index: Long): ViewKey = items.keyAt(index)
96 internal fun indexOf(key: ViewKey): Long? = items.indexOf(key)
97 internal fun focusedEntry(): LazyItemEntry? = items.focusedEntry()
98 internal fun entryContaining(node: MountedNode): LazyItemEntry? = items.entryContaining(node)
99 internal fun preferredKey(): ViewKey? {
100 when (focusedEntry()) {
101 case Some(val entry) -> return Some(entry.key)
102 case None -> {}
103 }
104 when (recent) {
105 case Some(val key) -> if (items.indexOf(key) != None)
106 return Some(key)
107 case None -> {}
108 }
109 return if (count > 0L)
110 Some(items.keyAt(0L))
111 else
112 None
113 }
114 private fun primary(item: LazyItemEntry): FocusTarget? {
115 for (target in focusOrder(item.node))
116 if (target.acceptsTab)
117 return Some(target)
118 return None
119 }
120 private fun remember() {
121 if (!active)
122 return
123 when (focusedEntry()) {
124 case Some(val entry) -> recent = Some(entry.key)
125 case None -> {}
126 }
127 }
128 private fun navigate(context: RoutedEventContext, event: InputEvent) {
129 if (!available)
130 return
131 val key =
132 when (event) {
133 case Keyboard(val value) -> value
134 else -> return
135 }
136 val modifiers = key.modifiers
137 if (
138 key.state !=
139 ElementState.Pressed ||
140 key.synthetic ||
141 modifiers.control ||
142 modifiers.alt ||
143 modifiers.shift ||
144 modifiers.superKey ||
145 modifiers.function
146 )
147 return
148 when (current.navigation) {
149 case Roving(val direction, val loop) -> {
150 val step =
151 navigationStep(
152 current.layout,
153 direction ?: node.layoutDirection,
154 key.logicalKey
155 ) ?:
156 return
157 val entry = focusedEntry() ?: return
158 val target = primary(entry) ?: return
159 if (!target.area.hasFocus)
160 return
161 context.consume()
162 node.root.focus.navigate(this, entry, step, loop)
163 }
164 else -> {}
165 }
166 }
167 internal fun sourceChanged() {
168 when (recent) {
169 case Some(val key) -> if (items.indexOf(key) == None)
170 recent = None
171 case None -> {}
172 }
173 val key = node.root.focus.itemKey(this) ?: return
174 if (items.indexOf(key) == None)
175 node.root.focus.cancel(this)
176 }
177 public override fun close() {
178 node.root.checkThread()
179 checkFocusMutation()
180 if (closed)
181 return
182 closed = true
183 node.root.focus.cancel(this)
184 node.lazyFocus = None
185 var failure = cleanupUi(None) { registration?.close() }
186 registration = None
187 failure = cleanupUi(failure) { observation?.close() }
188 observation = None
189 throwUi(failure)
190 }