scoop:scoop.ui:0.1.0
src/focus/target.scoop
Selected production source · src/focus/target.scoop
1
package scoop.ui3
import scoop.graphics.Rect4
import scoop.surface.InputArea6
/** One registration; its input area remains owned by ComponentScope. */7
public class FocusTarget internal constructor(8
internal val node: MountedNode,9
internal val area: InputArea,10
initial: FocusOptions11
): Closeable {12
private val localHandle = FocusHandle()13
private var current = initial14
private var reference = initial.handle ?: localHandle15
private var closed = false17
init {18
reference.bind(this)19
area.focusVisibility = initial.visibility20
}22
internal val active: Boolean get() =23
!closed && node.active && node.root.isOpen && !area.isClosed24
internal val available: Boolean get() = active && area.scope.isFocusAllowed25
internal val reachable: Boolean get() =26
available && (area.canFocus || node.root.focus.canReveal(this))27
internal val tabCandidate: Boolean get() = current.tabStop && reachable28
internal val acceptsTab: Boolean get() =29
tabCandidate && (reference.navigation?.acceptsTab(this) ?: true)30
public val isClosed: Boolean get() {31
node.root.checkThread()32
return closed || area.isClosed33
}34
public val handle: FocusHandle get() {35
node.root.checkThread()36
return reference37
}38
public var options: FocusOptions39
get() {40
node.root.checkThread()41
return current42
}43
set(value) {44
node.root.checkThread()45
checkFocusMutation()46
if (!active)47
throw IllegalStateException(Some("focus target is no longer mounted"))48
val next = value.handle ?: localHandle49
next.checkBinding(this)50
if (next !== reference) {51
node.root.focus.cancel(this)52
reference.unbind(this)53
reference = next54
next.bind(this)55
}56
current = value57
area.focusVisibility = value.visibility58
}59
/** Reveals a local subregion through the submitted scroll path without moving focus. */60
public fun reveal(rect: Rect): Boolean {61
node.root.checkThread()62
checkFocusMutation()63
checkScrollRect(rect)64
if (!active || rect.width == 0.0 || rect.height == 0.0)65
return false66
return node.root.focus.reveal(this, rect)67
}68
public override fun close() {69
node.root.checkThread()70
checkFocusMutation()71
if (closed)72
return73
closed = true74
node.root.focus.cancel(this)75
reference.unbind(this)76
node.root.semantics?.changed()77
var index = 0L78
while (index < node.focusTargets.size) {79
if (node.focusTargets[index] === this) {80
node.focusTargets.removeAt(index)81
return82
}83
index += 1L84
}85
}86
}