Scoop API

scoop:scoop.surface:0.1.0

src/shared/input/scope.scoop

Selected production source · src/shared/input/scope.scoop

1package scoop.surface
3import scoop.window.TextCommandAvailability
5/** A logical route node, with optional area geometry and independently replaceable handlers. */
6public class InputScope internal constructor(
7 internal val owner: InputRouter,
8 public val area: InputArea?,
9 internal var ancestor: InputScope?,
10 handler: (RoutedEventContext, InputEvent) -> Unit
11): Closeable {
12 internal var closed = false
13 internal var accepts = true
14 internal var defaultActions: Array<InputDefaultAction> = []
15 internal var eventHandlers: Array<InputEventHandler> = []
16 internal var finishedHandlers: Array<InputFinishedHandler> = []
17 private var receive = handler
18 private var capture: (RoutedEventContext, InputEvent) -> Unit = { _, _ -> Unit }
19 private var action: (RoutedEventContext, InputEvent) -> Unit = { _, _ -> Unit }
20 private var finished: (InputEvent) -> Unit = { _ -> Unit }
21 private var press: (InputPress) -> Boolean = { _ -> false }
22 private var textCommands: TextCommandAvailability? = None
23 public var textCommandAvailability: TextCommandAvailability?
24 get() {
25 owner.checkThread()
26 return textCommands
27 }
28 set(value) {
29 checkOpen()
30 if (textCommands != value) {
31 textCommands = value
32 owner.dispatch {}
33 }
34 }
35 public val isClosed: Boolean get() {
36 owner.checkThread()
37 return closed
38 }
39 /** Logical availability; an area may still be waiting for a committed placement. */
40 public val isEnabledInHierarchy: Boolean get() {
41 owner.checkThread()
42 var cursor: InputScope? = Some(this)
43 while (true) {
44 val current = cursor ?: return true
45 if (current.closed || !current.accepts)
46 return false
47 cursor = current.ancestor
48 }
49 }
50 /** Logical admission to the current input domain, independent of area placement. */
51 public val isInputAllowed: Boolean get() {
52 owner.checkThread()
53 return owner.allows(this)
54 }
55 /** Logical admission to both the current focus and pointer domains. */
56 public val isFocusAllowed: Boolean get() {
57 owner.checkThread()
58 return owner.allowsFocus(this)
59 }
60 public val activity: InputActivity get() = owner.activityFor(this).value
61 public val isHovered: Boolean get() {
62 owner.checkThread()
63 return !closed && containsScope(owner.hover, this)
64 }
65 /** Notifies changes of the actual owners, including moves within this subtree. */
66 public fun observeActivity(handler: () -> Unit): Closeable {
67 checkOpen()
68 val value = InputActivityObserver(this, handler)
69 owner.activityObservers.add(value)
70 return value
71 }
72 /** Only the current domain root decides a new mouse or contact press. */
73 public var beforePress: (InputPress) -> Boolean
74 get() {
75 owner.checkThread()
76 return press
77 }
78 set(value) {
79 checkOpen()
80 press = value
81 }
82 public var parent: InputScope?
83 get() {
84 owner.checkThread()
85 return ancestor
86 }
87 set(value) {
88 checkOpen()
89 owner.reparent(this, value)
90 }
91 public var enabled: Boolean
92 get() {
93 owner.checkThread()
94 return accepts
95 }
96 set(value) {
97 checkOpen()
98 if (accepts != value) {
99 accepts = value
100 owner.changed()
101 }
102 }
103 public var handler: (RoutedEventContext, InputEvent) -> Unit
104 get() {
105 owner.checkThread();
106 return receive
107 }
108 set(value) {
109 checkOpen();
110 receive = value
111 }
112 public var captureHandler: (RoutedEventContext, InputEvent) -> Unit
113 get() {
114 owner.checkThread();
115 return capture
116 }
117 set(value) {
118 checkOpen();
119 capture = value
120 }
121 public var defaultAction: (RoutedEventContext, InputEvent) -> Unit
122 get() {
123 owner.checkThread();
124 return action
125 }
126 set(value) {
127 checkOpen();
128 action = value
129 }
130 /** Uncancellable cleanup after a routed event, including a prevented release. */
131 public var onEventFinished: (InputEvent) -> Unit
132 get() {
133 owner.checkThread()
134 return finished
135 }
136 set(value) {
137 checkOpen()
138 finished = value
139 }
140 internal fun checkOpen() {
141 owner.checkOpen()
142 if (closed)
143 throw IllegalStateException(Some("input scope is closed"))
144 }
145 public override fun close() {
146 owner.checkThread()
147 if (closed)
148 return
149 closed = true
150 retireDefaultActions()
151 owner.retireActivity(this)
152 owner.retirePointerObservers(this)
153 owner.detached(this)
154 }
157internal fun sameScope(left: InputScope?, right: InputScope?): Boolean =
158 when (left) {
159 case Some(val a) -> when (right) {
160 case Some(val b) -> a === b
161 case None -> false
162 }
163 case None -> when (right) {
164 case Some(_) -> false
165 case None -> true
166 }
167 }
169internal fun containsScope(path: Array<InputScope>, scope: InputScope): Boolean {
170 for (candidate in path)
171 if (candidate === scope)
172 return true
173 return false
176internal fun samePath(left: Array<InputScope>, right: Array<InputScope>): Boolean {
177 if (left.size != right.size)
178 return false
179 for (index in 0L ..< left.size)
180 if (left[index] !== right[index])
181 return false
182 return true