scoop:scoop.ui.component:0.1.0
src/tree/handle.scoop
Selected production source · src/tree/handle.scoop
1
package scoop.ui.component3
import scoop.platform.concurrent.currentThreadId4
import scoop.ui.*5
import scoop.window.accessibility.SemanticNodeId7
public class TreeReadingSnapshot internal constructor(8
public val visible: VirtualRange,9
public val anchor: VirtualReadingAnchor?,10
public val stickyKeys: Array<ViewKey>,11
public val bodyHeight: Double,12
public val offset: Double,13
public val atEnd: Boolean14
)16
internal interface TreeTarget {17
val active: Boolean18
val pending: Boolean19
fun scroll(key: ViewKey, column: String?, alignment: ScrollAlignment, expand: Boolean): Boolean20
fun focus(key: ViewKey, column: String?, expand: Boolean): Boolean21
}23
/** Requests use the mounted tree's existing scroll and focus resources. */24
public class TreeHandle public constructor() {25
private val thread = currentThreadId()26
private var target: TreeTarget? = None27
private val snapshot = state<TreeReadingSnapshot?>(None)28
internal val semanticTarget = SemanticsReference()29
public val semanticId: SemanticNodeId?30
get() {31
checkThread();32
return semanticTarget.id33
}34
public val isAttached: Boolean35
get() {36
checkThread();37
return target?.active ?: false38
}39
public val isPending: Boolean40
get() {41
checkThread();42
return target?.pending ?: false43
}44
public val reading: TreeReadingSnapshot?45
get() {46
checkThread();47
return snapshot.value48
}49
public fun scrollToItem(50
key: ViewKey,51
alignment: ScrollAlignment = ScrollAlignment.Nearest,52
expandAncestors: Boolean = false53
): Boolean {54
snapshot.checkMutation()55
requireComponent(key != ViewKey.Position, "tree positioning requires an explicit key")56
return target?.scroll(key, None, alignment, expandAncestors) ?: false57
}58
public fun scrollToCell(59
key: ViewKey,60
column: String,61
alignment: ScrollAlignment = ScrollAlignment.Nearest,62
expandAncestors: Boolean = false63
): Boolean {64
snapshot.checkMutation()65
requireComponent(66
key != ViewKey.Position && column != "",67
"tree cell positioning requires a key and column"68
)69
return target?.scroll(key, Some(column), alignment, expandAncestors) ?: false70
}71
public fun focusItem(key: ViewKey, expandAncestors: Boolean = false): Boolean {72
snapshot.checkMutation()73
requireComponent(key != ViewKey.Position, "tree positioning requires an explicit key")74
return target?.focus(key, None, expandAncestors) ?: false75
}76
public fun focusCell(key: ViewKey, column: String, expandAncestors: Boolean = false): Boolean {77
snapshot.checkMutation()78
requireComponent(79
key != ViewKey.Position && column != "",80
"tree cell positioning requires a key and column"81
)82
return target?.focus(key, Some(column), expandAncestors) ?: false83
}84
private fun checkThread() {85
requireComponent(currentThreadId() == thread, "tree handle used from another thread")86
}87
internal fun bind(next: TreeTarget) {88
checkThread()89
when (target) {90
case Some(val old) ->91
requireComponent(old === next || !old.active, "tree handle already has a target");92
case None -> {}93
}94
target = Some(next)95
}96
internal fun unbind(owner: TreeTarget) {97
checkThread()98
when (target) {99
case Some(val current) ->100
if (current === owner) {101
target = None;102
snapshot.value = None103
};104
case None -> {}105
}106
}107
internal fun publish(owner: TreeTarget, value: TreeReadingSnapshot) {108
checkThread()109
val current = target ?: return110
if (current !== owner || !owner.active) return111
when (snapshot.value) {112
case Some(val old) ->113
if (old.visible ==114
value.visible &&115
old.anchor ==116
value.anchor &&117
treeKeysEqual(old.stickyKeys, value.stickyKeys) &&118
old.bodyHeight ==119
value.bodyHeight &&120
old.offset ==121
value.offset &&122
old.atEnd ==123
value.atEnd124
)125
return126
case None -> {}127
}128
snapshot.value = Some(value)129
}130
}