scoop:scoop.ui.component:0.1.0
src/virtual/handle.scoop
Selected production source · src/virtual/handle.scoop
1
package scoop.ui.component3
import scoop.platform.concurrent.currentThreadId4
import scoop.ui.*5
import scoop.window.accessibility.SemanticNodeId7
/** A stable reference to one mounted list; the associated ScrollState owns its position. */8
public class VirtualListHandle public constructor() {9
private val thread = currentThreadId()10
private var target: VirtualListInk? = None11
private val snapshot = state<VirtualReadingSnapshot?>(None)12
internal val semanticTarget = SemanticsReference()13
/** The actual mounted collection, independent of its scroll area and row instances. */14
public val semanticId: SemanticNodeId? get() {15
checkThread()16
return semanticTarget.id17
}19
private fun checkThread() {20
if (currentThreadId() != thread)21
throw IllegalStateException(Some("virtual list handle used from another thread"))22
}23
public val isAttached: Boolean get() {24
checkThread()25
return target?.active ?: false26
}27
public val isPending: Boolean get() {28
checkThread()29
return (target?.scrollPending ?: false) || (target?.focusPending ?: false)30
}31
public val reading: VirtualReadingSnapshot? get() {32
checkThread()33
return snapshot.value34
}35
public fun restore(anchor: VirtualReadingAnchor): Boolean {36
checkThread()37
requireComponent(38
anchor.key != ViewKey.Position,39
"reading restoration requires an explicit key"40
)41
nonnegative(anchor.offset, "invalid reading offset")42
return target?.restore(anchor) ?: false43
}44
public fun scrollToItem(45
key: ViewKey,46
alignment: ScrollAlignment = ScrollAlignment.Nearest,47
margin: Double = 0.0,48
previousItemPeek: Double = 0.049
): Boolean {50
checkThread()51
requireComponent(52
key != ViewKey.Position,53
"virtual item positioning requires an explicit key"54
)55
nonnegative(margin, "invalid virtual positioning margin")56
nonnegative(previousItemPeek, "invalid virtual previous item peek")57
return target?.requestItem(key, alignment, margin, previousItemPeek) ?: false58
}59
public fun focusItem(key: ViewKey, edge: ItemFocusEdge = ItemFocusEdge.First): Boolean {60
snapshot.checkMutation()61
requireComponent(key != ViewKey.Position, "virtual item focus requires an explicit key")62
return target?.focusItem(key, edge) ?: false63
}64
/** Reads an actual row descendant after its accepted layout; None ends the request. */65
public fun focusItem(key: ViewKey, target: () -> FocusHandle?): Boolean {66
snapshot.checkMutation()67
requireComponent(key != ViewKey.Position, "virtual item focus requires an explicit key")68
return this.target?.focusItem(key, target) ?: false69
}70
internal fun bind(value: VirtualListInk) {71
checkThread()72
when (target) {73
case Some(val current) -> requireComponent(74
current === value || !current.active,75
"virtual list handle already has a target"76
)77
case None -> {}78
}79
target = Some(value)80
replaceSnapshot(None)81
}82
internal fun unbind(value: VirtualListInk) {83
checkThread()84
when (target) {85
case Some(val current) -> if (current === value) {86
target = None87
replaceSnapshot(None)88
}89
case None -> {}90
}91
}92
internal fun publish(owner: VirtualListInk, value: VirtualReadingSnapshot) {93
checkThread()94
when (target) {95
case Some(val current) -> if (current === owner && current.active)96
replaceSnapshot(Some(value))97
case None -> {}98
}99
}100
private fun replaceSnapshot(value: VirtualReadingSnapshot?) {101
if (snapshot.value != value)102
snapshot.value = value103
}104
}106
internal enum VirtualPosition {107
Item(val alignment: ScrollAlignment, val margin: Double, val previousPeek: Double),108
Reading(val offset: Double)109
}111
internal class ItemScrollRequest(112
val key: ViewKey,113
val position: VirtualPosition,114
val revision: ULong,115
val source: ItemSource116
)