scoop:scoop.surface:0.1.0
src/shared/input/scope.scoop
Selected production source · src/shared/input/scope.scoop
1
package scoop.surface3
import scoop.window.TextCommandAvailability5
/** A logical route node, with optional area geometry and independently replaceable handlers. */6
public class InputScope internal constructor(7
internal val owner: InputRouter,8
public val area: InputArea?,9
internal var ancestor: InputScope?,10
handler: (RoutedEventContext, InputEvent) -> Unit11
): Closeable {12
internal var closed = false13
internal var accepts = true14
internal var defaultActions: Array<InputDefaultAction> = []15
internal var eventHandlers: Array<InputEventHandler> = []16
internal var finishedHandlers: Array<InputFinishedHandler> = []17
private var receive = handler18
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? = None23
public var textCommandAvailability: TextCommandAvailability?24
get() {25
owner.checkThread()26
return textCommands27
}28
set(value) {29
checkOpen()30
if (textCommands != value) {31
textCommands = value32
owner.dispatch {}33
}34
}35
public val isClosed: Boolean get() {36
owner.checkThread()37
return closed38
}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 true45
if (current.closed || !current.accepts)46
return false47
cursor = current.ancestor48
}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).value61
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 value71
}72
/** Only the current domain root decides a new mouse or contact press. */73
public var beforePress: (InputPress) -> Boolean74
get() {75
owner.checkThread()76
return press77
}78
set(value) {79
checkOpen()80
press = value81
}82
public var parent: InputScope?83
get() {84
owner.checkThread()85
return ancestor86
}87
set(value) {88
checkOpen()89
owner.reparent(this, value)90
}91
public var enabled: Boolean92
get() {93
owner.checkThread()94
return accepts95
}96
set(value) {97
checkOpen()98
if (accepts != value) {99
accepts = value100
owner.changed()101
}102
}103
public var handler: (RoutedEventContext, InputEvent) -> Unit104
get() {105
owner.checkThread();106
return receive107
}108
set(value) {109
checkOpen();110
receive = value111
}112
public var captureHandler: (RoutedEventContext, InputEvent) -> Unit113
get() {114
owner.checkThread();115
return capture116
}117
set(value) {118
checkOpen();119
capture = value120
}121
public var defaultAction: (RoutedEventContext, InputEvent) -> Unit122
get() {123
owner.checkThread();124
return action125
}126
set(value) {127
checkOpen();128
action = value129
}130
/** Uncancellable cleanup after a routed event, including a prevented release. */131
public var onEventFinished: (InputEvent) -> Unit132
get() {133
owner.checkThread()134
return finished135
}136
set(value) {137
checkOpen()138
finished = value139
}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
return149
closed = true150
retireDefaultActions()151
owner.retireActivity(this)152
owner.retirePointerObservers(this)153
owner.detached(this)154
}155
}157
internal fun sameScope(left: InputScope?, right: InputScope?): Boolean =158
when (left) {159
case Some(val a) -> when (right) {160
case Some(val b) -> a === b161
case None -> false162
}163
case None -> when (right) {164
case Some(_) -> false165
case None -> true166
}167
}169
internal fun containsScope(path: Array<InputScope>, scope: InputScope): Boolean {170
for (candidate in path)171
if (candidate === scope)172
return true173
return false174
}176
internal fun samePath(left: Array<InputScope>, right: Array<InputScope>): Boolean {177
if (left.size != right.size)178
return false179
for (index in 0L ..< left.size)180
if (left[index] !== right[index])181
return false182
return true183
}