Scoop API

scoop:scoop.surface:0.1.0

src/shared/input/boundary.scoop

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

1package scoop.surface
3public enum InputBoundaryMode {
4 AllInput, FocusOnly
5}
7/** Explicit input barrier; a retired scope stays blocking until this registration closes. */
8public class InputBoundary public constructor(
9 public val input: InputScope,
10 mode: InputBoundaryMode = InputBoundaryMode.AllInput,
11 enabled: Boolean = true
12): Closeable {
13 internal var closed = false
14 private val owner = input.owner
15 internal var currentMode = mode
16 internal var accepts = enabled
17 public val mode: InputBoundaryMode get() {
18 owner.checkThread()
19 return currentMode
20 }
21 public val enabled: Boolean get() {
22 owner.checkThread()
23 return accepts
24 }
25 init {
26 input.checkOpen()
27 try {
28 owner.installBoundary(this)
29 } catch (error: Throwable) {
30 closed = true
31 throwFailure(cleanupFailure(Some(error)) { owner.removeBoundary(this) })
32 throw error
33 }
34 }
35 public val isClosed: Boolean get() {
36 owner.checkThread()
37 return closed
38 }
39 public val isCurrent: Boolean get() {
40 owner.checkThread()
41 return !closed && sameBoundary(owner.currentFocusBoundary, Some(this))
42 }
43 /** Changes effective input policy without moving this registration above its children. */
44 public fun update(mode: InputBoundaryMode, enabled: Boolean) {
45 owner.checkOpen()
46 if (closed)
47 throw IllegalStateException(Some("input boundary is closed"))
48 if (currentMode == mode && accepts == enabled)
49 return
50 val previous = owner.currentBoundary
51 val affected = accepts || enabled
52 currentMode = mode
53 accepts = enabled
54 if (affected)
55 owner.boundaryChanged(previous)
56 }
57 public override fun close() {
58 owner.checkThread()
59 if (closed)
60 return
61 closed = true
62 owner.removeBoundary(this)
63 }
66internal fun sameBoundary(left: InputBoundary?, right: InputBoundary?): Boolean =
67 when ((left, right)) {
68 case(Some(val a), Some(val b)) -> a === b
69 case(None, None) -> true
70 else -> false
71 }
73internal val InputRouter.currentFocusBoundary: InputBoundary? get() {
74 val root = inputRoot
75 var index = boundaries.size
76 while (index > 0L) {
77 index -= 1L
78 val boundary = boundaries[index]
79 if (!boundary.accepts)
80 continue
81 var cursor: InputScope? = Some(boundary.input)
82 while (true) {
83 val current = cursor ?: break
84 if (current === root)
85 return Some(boundary)
86 cursor = current.ancestor
87 }
88 }
89 return None
92internal val InputRouter.currentBoundary: InputBoundary? get() {
93 var index = boundaries.size
94 while (index > 0L) {
95 index -= 1L
96 val boundary = boundaries[index]
97 if (boundary.accepts && boundary.currentMode == InputBoundaryMode.AllInput)
98 return Some(boundary)
99 }
100 return None
103internal val InputRouter.inputRoot: InputScope get() = currentBoundary?.input ?: rootScope
105internal val InputRouter.focusRoot: InputScope get() = currentFocusBoundary?.input ?: rootScope
107internal fun InputRouter.allowsFocus(scope: InputScope): Boolean =
108 allows(scope) && withinBoundary(scope, focusRoot)
110internal fun InputRouter.installBoundary(value: InputBoundary) {
111 checkOpen()
112 for (current in boundaries)
113 requireInput(current.input !== value.input, "input scope already has a boundary")
114 val previous = currentBoundary
115 boundaries.add(value)
116 if (value.accepts)
117 boundaryChanged(previous)
120internal fun InputRouter.removeBoundary(value: InputBoundary) {
121 val previous = currentBoundary
122 for (index in 0L ..< boundaries.size) {
123 if (boundaries[index] === value) {
124 boundaries.removeAt(index)
125 if (value.accepts)
126 boundaryChanged(previous)
127 return
128 }
129 }
132private fun InputRouter.boundaryChanged(previous: InputBoundary?) {
133 if (!sameBoundary(previous, currentBoundary)) {
134 boundaryRevision += 1uL
135 pendingBoundaryChange = true
136 }
137 changed()
140internal fun InputRouter.allows(scope: InputScope): Boolean {
141 if (closed)
142 return false
143 return withinBoundary(scope, inputRoot)
146private fun withinBoundary(scope: InputScope, boundary: InputScope): Boolean {
147 var reached = false
148 var cursor: InputScope? = Some(scope)
149 while (true) {
150 val current = cursor ?: return reached
151 if (current.closed || !current.accepts)
152 return false
153 if (current === boundary)
154 reached = true
155 cursor = current.ancestor
156 }