scoop:scoop.surface:0.1.0
src/shared/input/boundary.scoop
Selected production source · src/shared/input/boundary.scoop
1
package scoop.surface3
public enum InputBoundaryMode {4
AllInput, FocusOnly5
}7
/** Explicit input barrier; a retired scope stays blocking until this registration closes. */8
public class InputBoundary public constructor(9
public val input: InputScope,10
mode: InputBoundaryMode = InputBoundaryMode.AllInput,11
enabled: Boolean = true12
): Closeable {13
internal var closed = false14
private val owner = input.owner15
internal var currentMode = mode16
internal var accepts = enabled17
public val mode: InputBoundaryMode get() {18
owner.checkThread()19
return currentMode20
}21
public val enabled: Boolean get() {22
owner.checkThread()23
return accepts24
}25
init {26
input.checkOpen()27
try {28
owner.installBoundary(this)29
} catch (error: Throwable) {30
closed = true31
throwFailure(cleanupFailure(Some(error)) { owner.removeBoundary(this) })32
throw error33
}34
}35
public val isClosed: Boolean get() {36
owner.checkThread()37
return closed38
}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
return50
val previous = owner.currentBoundary51
val affected = accepts || enabled52
currentMode = mode53
accepts = enabled54
if (affected)55
owner.boundaryChanged(previous)56
}57
public override fun close() {58
owner.checkThread()59
if (closed)60
return61
closed = true62
owner.removeBoundary(this)63
}64
}66
internal fun sameBoundary(left: InputBoundary?, right: InputBoundary?): Boolean =67
when ((left, right)) {68
case(Some(val a), Some(val b)) -> a === b69
case(None, None) -> true70
else -> false71
}73
internal val InputRouter.currentFocusBoundary: InputBoundary? get() {74
val root = inputRoot75
var index = boundaries.size76
while (index > 0L) {77
index -= 1L78
val boundary = boundaries[index]79
if (!boundary.accepts)80
continue81
var cursor: InputScope? = Some(boundary.input)82
while (true) {83
val current = cursor ?: break84
if (current === root)85
return Some(boundary)86
cursor = current.ancestor87
}88
}89
return None90
}92
internal val InputRouter.currentBoundary: InputBoundary? get() {93
var index = boundaries.size94
while (index > 0L) {95
index -= 1L96
val boundary = boundaries[index]97
if (boundary.accepts && boundary.currentMode == InputBoundaryMode.AllInput)98
return Some(boundary)99
}100
return None101
}103
internal val InputRouter.inputRoot: InputScope get() = currentBoundary?.input ?: rootScope105
internal val InputRouter.focusRoot: InputScope get() = currentFocusBoundary?.input ?: rootScope107
internal fun InputRouter.allowsFocus(scope: InputScope): Boolean =108
allows(scope) && withinBoundary(scope, focusRoot)110
internal 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 = currentBoundary115
boundaries.add(value)116
if (value.accepts)117
boundaryChanged(previous)118
}120
internal fun InputRouter.removeBoundary(value: InputBoundary) {121
val previous = currentBoundary122
for (index in 0L ..< boundaries.size) {123
if (boundaries[index] === value) {124
boundaries.removeAt(index)125
if (value.accepts)126
boundaryChanged(previous)127
return128
}129
}130
}132
private fun InputRouter.boundaryChanged(previous: InputBoundary?) {133
if (!sameBoundary(previous, currentBoundary)) {134
boundaryRevision += 1uL135
pendingBoundaryChange = true136
}137
changed()138
}140
internal fun InputRouter.allows(scope: InputScope): Boolean {141
if (closed)142
return false143
return withinBoundary(scope, inputRoot)144
}146
private fun withinBoundary(scope: InputScope, boundary: InputScope): Boolean {147
var reached = false148
var cursor: InputScope? = Some(scope)149
while (true) {150
val current = cursor ?: return reached151
if (current.closed || !current.accepts)152
return false153
if (current === boundary)154
reached = true155
cursor = current.ancestor156
}157
}