Scoop API

scoop:scoop.ui:0.1.0

src/focus/scope.scoop

Selected production source · src/focus/scope.scoop

1package scoop.ui
3import scoop.surface.addDefaultAction
5/** Groups logical Tab order and optionally loops at its boundary. */
6public class FocusScope internal constructor(
7 internal val node: MountedNode,
8 initial: FocusScopeOptions
9): Closeable {
10 private var current = initial
11 private var closed = false
12 private val input = node.scope.input
13 private val windowRoot =
14 when (node.parent) {
15 case Some(_) -> false
16 case None -> true
17 }
18 private val registration =
19 input.addDefaultAction {
20 context,
21 event -> if (active)
22 node.root.focus.tab(node, current.loop || windowRoot, context, event)
23 }
25 internal val active: Boolean get() =
26 !closed && node.active && node.root.isOpen && !input.isClosed
27 internal val available: Boolean get() = active && input.isFocusAllowed
28 public val isClosed: Boolean get() {
29 node.root.checkThread()
30 return closed || input.isClosed
31 }
32 public val isPending: Boolean get() {
33 node.root.checkThread()
34 return active && node.root.focus.isPending(this)
35 }
36 public var options: FocusScopeOptions
37 get() {
38 node.root.checkThread()
39 return current
40 }
41 set(value) {
42 node.root.checkThread()
43 checkFocusMutation()
44 if (!active)
45 throw IllegalStateException(Some("focus scope is no longer mounted"))
46 current = value
47 }
48 public fun requestFocus(): Boolean {
49 node.root.checkThread()
50 checkFocusMutation()
51 return node.root.focus.request(this)
52 }
53 public override fun close() {
54 node.root.checkThread()
55 checkFocusMutation()
56 if (closed)
57 return
58 closed = true
59 node.root.focus.cancel(this)
60 registration.close()
61 node.focusScope = None
62 }