Scoop API

scoop:scoop.ui:0.1.0

src/focus/target.scoop

Selected production source · src/focus/target.scoop

1package scoop.ui
3import scoop.graphics.Rect
4import scoop.surface.InputArea
6/** One registration; its input area remains owned by ComponentScope. */
7public class FocusTarget internal constructor(
8 internal val node: MountedNode,
9 internal val area: InputArea,
10 initial: FocusOptions
11): Closeable {
12 private val localHandle = FocusHandle()
13 private var current = initial
14 private var reference = initial.handle ?: localHandle
15 private var closed = false
17 init {
18 reference.bind(this)
19 area.focusVisibility = initial.visibility
20 }
22 internal val active: Boolean get() =
23 !closed && node.active && node.root.isOpen && !area.isClosed
24 internal val available: Boolean get() = active && area.scope.isFocusAllowed
25 internal val reachable: Boolean get() =
26 available && (area.canFocus || node.root.focus.canReveal(this))
27 internal val tabCandidate: Boolean get() = current.tabStop && reachable
28 internal val acceptsTab: Boolean get() =
29 tabCandidate && (reference.navigation?.acceptsTab(this) ?: true)
30 public val isClosed: Boolean get() {
31 node.root.checkThread()
32 return closed || area.isClosed
33 }
34 public val handle: FocusHandle get() {
35 node.root.checkThread()
36 return reference
37 }
38 public var options: FocusOptions
39 get() {
40 node.root.checkThread()
41 return current
42 }
43 set(value) {
44 node.root.checkThread()
45 checkFocusMutation()
46 if (!active)
47 throw IllegalStateException(Some("focus target is no longer mounted"))
48 val next = value.handle ?: localHandle
49 next.checkBinding(this)
50 if (next !== reference) {
51 node.root.focus.cancel(this)
52 reference.unbind(this)
53 reference = next
54 next.bind(this)
55 }
56 current = value
57 area.focusVisibility = value.visibility
58 }
59 /** Reveals a local subregion through the submitted scroll path without moving focus. */
60 public fun reveal(rect: Rect): Boolean {
61 node.root.checkThread()
62 checkFocusMutation()
63 checkScrollRect(rect)
64 if (!active || rect.width == 0.0 || rect.height == 0.0)
65 return false
66 return node.root.focus.reveal(this, rect)
67 }
68 public override fun close() {
69 node.root.checkThread()
70 checkFocusMutation()
71 if (closed)
72 return
73 closed = true
74 node.root.focus.cancel(this)
75 reference.unbind(this)
76 node.root.semantics?.changed()
77 var index = 0L
78 while (index < node.focusTargets.size) {
79 if (node.focusTargets[index] === this) {
80 node.focusTargets.removeAt(index)
81 return
82 }
83 index += 1L
84 }
85 }