scoop:scoop.ui:0.1.0
src/focus/scope.scoop
Selected production source · src/focus/scope.scoop
1
package scoop.ui3
import scoop.surface.addDefaultAction5
/** Groups logical Tab order and optionally loops at its boundary. */6
public class FocusScope internal constructor(7
internal val node: MountedNode,8
initial: FocusScopeOptions9
): Closeable {10
private var current = initial11
private var closed = false12
private val input = node.scope.input13
private val windowRoot =14
when (node.parent) {15
case Some(_) -> false16
case None -> true17
}18
private val registration =19
input.addDefaultAction {20
context,21
event -> if (active)22
node.root.focus.tab(node, current.loop || windowRoot, context, event)23
}25
internal val active: Boolean get() =26
!closed && node.active && node.root.isOpen && !input.isClosed27
internal val available: Boolean get() = active && input.isFocusAllowed28
public val isClosed: Boolean get() {29
node.root.checkThread()30
return closed || input.isClosed31
}32
public val isPending: Boolean get() {33
node.root.checkThread()34
return active && node.root.focus.isPending(this)35
}36
public var options: FocusScopeOptions37
get() {38
node.root.checkThread()39
return current40
}41
set(value) {42
node.root.checkThread()43
checkFocusMutation()44
if (!active)45
throw IllegalStateException(Some("focus scope is no longer mounted"))46
current = value47
}48
public fun requestFocus(): Boolean {49
node.root.checkThread()50
checkFocusMutation()51
return node.root.focus.request(this)52
}53
public override fun close() {54
node.root.checkThread()55
checkFocusMutation()56
if (closed)57
return58
closed = true59
node.root.focus.cancel(this)60
registration.close()61
node.focusScope = None62
}63
}