Scoop API

scoop:scoop.ui:0.1.0

src/focus/navigation.scoop

Selected production source · src/focus/navigation.scoop

1package scoop.ui
3import scoop.surface.*
4import scoop.window.*
6/** Composes member navigation with the existing routed defaults. */
7public class KeyNavigation internal constructor(
8 internal val node: MountedNode,
9 initial: NavigationOptions
10): Closeable {
11 private val input = node.scope.input
12 private var current = initial
13 private var closed = false
14 private var registration: Closeable? = None
16 init {
17 validate(initial)
18 bind()
19 registration = Some(input.addDefaultAction { context, event -> navigate(context, event) })
20 }
22 internal val active: Boolean get() =
23 !closed && node.active && node.root.isOpen && !input.isClosed
24 public val isClosed: Boolean get() {
25 node.root.checkThread()
26 return closed || input.isClosed
27 }
28 public var options: NavigationOptions
29 get() {
30 node.root.checkThread()
31 return current
32 }
33 set(value) {
34 node.root.checkThread()
35 checkFocusMutation()
36 if (!active)
37 throw IllegalStateException(Some("key navigation is no longer mounted"))
38 validate(value)
39 unbind()
40 current = value
41 bind()
42 }
44 internal fun checkMember(member: MountedNode) {
45 if (active)
46 requireUi(withinFocusScope(member, node), "navigation member belongs to another scope")
47 }
48 private fun validate(value: NavigationOptions) {
49 var preferredFound = false
50 for (index in 0L ..< value.items.size) {
51 val item = value.items[index]
52 when (value.preferredTab) {
53 case Some(val handle) -> if (handle === item.handle)
54 preferredFound = true
55 case None -> preferredFound = true
56 }
57 item.handle.checkThread()
58 when (item.handle.navigation) {
59 case Some(val group) -> requireUi(
60 group === this || !group.active,
61 "focus handle already belongs to a navigation group"
62 )
63 case None -> {}
64 }
65 when (item.handle.target) {
66 case Some(val target) -> if (target.active)
67 checkMember(target.node)
68 case None -> {}
69 }
70 for (previous in 0L ..< index) {
71 val other = value.items[previous]
72 requireUi(other.handle !== item.handle, "duplicate navigation handle")
73 requireUi(
74 value.layout !=
75 NavigationLayout.Grid ||
76 other.row !=
77 item.row ||
78 other.column !=
79 item.column,
80 "duplicate navigation grid position"
81 )
82 }
83 }
84 when (value.preferredTab) {
85 case Some(_) -> requireUi(
86 preferredFound,
87 "preferred Tab handle is not a navigation member"
88 )
89 case None -> {}
90 }
91 }
92 private fun bind() {
93 for (item in current.items)
94 item.handle.navigation = Some(this)
95 }
96 private fun unbind() {
97 for (item in current.items) {
98 when (item.handle.navigation) {
99 case Some(val group) -> if (group === this)
100 item.handle.navigation = None
101 case None -> {}
102 }
103 }
104 }
106 internal fun acceptsTab(target: FocusTarget): Boolean {
107 if (!active || !current.roving || !input.isEnabledInHierarchy)
108 return true
109 val member = preferredTab() ?: return false
110 return member === target
111 }
112 private fun preferredTab(): FocusTarget? {
113 var first: FocusTarget? = None
114 var recent: FocusTarget? = None
115 var distance: ULong? = None
116 val now = node.root.surface.focusRevision
117 for (item in current.items) {
118 val target = item.handle.target ?: continue
119 if (!target.tabCandidate)
120 continue
121 if (target.area.hasFocus)
122 return Some(target)
123 when (first) {
124 case None -> first = Some(target)
125 case Some(_) -> {}
126 }
127 when (target.area.focusRevision) {
128 case Some(val revision) -> {
129 val age = now - revision
130 val newer =
131 when (distance) {
132 case Some(val previous) -> age < previous
133 case None -> true
134 }
135 if (newer) {
136 recent = Some(target)
137 distance = Some(age)
138 }
139 }
140 case None -> {}
141 }
142 }
143 when (current.preferredTab) {
144 case Some(val handle) -> when (handle.target) {
145 case Some(val target) -> if (target.tabCandidate)
146 return Some(target)
147 case None -> {}
148 }
149 case None -> {}
150 }
151 return when (recent) {
152 case Some(_) -> recent
153 case None -> first
154 }
155 }
156 private fun member(index: Long): FocusTarget? {
157 val target = current.items[index].handle.target ?: return None
158 return if (target.reachable)
159 Some(target)
160 else
161 None
162 }
163 private fun focusedIndex(): Long? {
164 for (index in 0L ..< current.items.size) {
165 when (current.items[index].handle.target) {
166 case Some(val target) -> if (target.active && target.area.hasFocus)
167 return Some(index)
168 case None -> {}
169 }
170 }
171 return None
172 }
173 private fun navigate(context: RoutedEventContext, event: InputEvent) {
174 if (!active || !input.isEnabledInHierarchy)
175 return
176 val key =
177 when (event) {
178 case Keyboard(val value) -> value
179 else -> return
180 }
181 if (!navigationKeyAllowed(key))
182 return
183 val step = navigationStep(current, key.logicalKey, node.layoutDirection) ?: return
184 val index = focusedIndex() ?: return
185 val next =
186 navigationDestination(current.items, index, step, current.loop) {
187 candidate -> when (member(candidate)) {
188 case Some(_) -> true
189 case None -> false
190 }
191 }
192 context.consume()
193 when (next) {
194 case Some(val position) -> {
195 val target = member(position) ?: return
196 val callback = current.onNavigate
197 if (target.handle.requestFocus(FocusOrigin.Keyboard) && active)
198 callback(target.handle)
199 }
200 case None -> {}
201 }
202 }
203 public override fun close() {
204 node.root.checkThread()
205 checkFocusMutation()
206 if (closed)
207 return
208 closed = true
209 unbind()
210 registration?.close()
211 registration = None
212 node.navigation = None
213 }