scoop:scoop.ui.component:0.1.0
src/virtual/measurements.scoop
Selected production source · src/virtual/measurements.scoop
1
package scoop.ui.component3
import scoop.collections.HashMap4
import scoop.ui.*5
import scoop.ui.style.Orientation7
/** Measured sizes are estimates after a source replacement, never a reason to skip item layout. */8
internal class VirtualMeasurements {9
private var source: ItemSource? = None10
private var index: ItemExtentIndex? = None11
private var orientation = Orientation.Vertical12
private var cross = 0.013
private var direction = LayoutDirection.Ltr14
private var known = HashMap<ViewKey, Double>()16
fun prepare(17
next: ItemSource,18
count: Long,19
mode: ItemExtent,20
axis: Orientation,21
breadth: Double,22
readingDirection: LayoutDirection = LayoutDirection.Ltr23
): ItemExtentIndex {24
val previous = index25
val sameShape =26
previous?.mode ==27
Some(mode) &&28
orientation ==29
axis &&30
cross ==31
breadth &&32
direction ==33
readingDirection34
val sameSource =35
when (source) {36
case Some(val value) -> value === next37
case None -> false38
}39
if (sameSource && sameShape)40
return previous!!41
val fresh = ItemExtentIndex(count, mode)42
val retained = HashMap<ViewKey, Double>()43
if (sameShape && fresh.variable) {44
for (entry in known.entries) {45
when (virtualIndex(next, count, entry.key)) {46
case Some(val at) -> {47
fresh.record(at, entry.value)48
retained.put(entry.key, entry.value)49
}50
case None -> {}51
}52
}53
}54
source = Some(next)55
index = Some(fresh)56
orientation = axis57
cross = breadth58
direction = readingDirection59
known = retained60
return fresh61
}62
fun record(at: Long, extent: Double) {63
val current = index!!64
if (!current.variable)65
return66
current.record(at, extent)67
known.put(source!!.keyAt(at), extent)68
}69
}71
internal fun virtualIndex(source: ItemSource, count: Long, key: ViewKey): Long? {72
val at = source.indexOfKey(key) ?: return None73
requireComponent(at >= 0L && at < count, "item reverse index is out of range")74
requireComponent(source.keyAt(at) == key, "item key mapping is inconsistent")75
return Some(at)76
}