Scoop API

scoop:scoop.ui.component:0.1.0

src/virtual/measurements.scoop

Selected production source · src/virtual/measurements.scoop

1package scoop.ui.component
3import scoop.collections.HashMap
4import scoop.ui.*
5import scoop.ui.style.Orientation
7/** Measured sizes are estimates after a source replacement, never a reason to skip item layout. */
8internal class VirtualMeasurements {
9 private var source: ItemSource? = None
10 private var index: ItemExtentIndex? = None
11 private var orientation = Orientation.Vertical
12 private var cross = 0.0
13 private var direction = LayoutDirection.Ltr
14 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.Ltr
23 ): ItemExtentIndex {
24 val previous = index
25 val sameShape =
26 previous?.mode ==
27 Some(mode) &&
28 orientation ==
29 axis &&
30 cross ==
31 breadth &&
32 direction ==
33 readingDirection
34 val sameSource =
35 when (source) {
36 case Some(val value) -> value === next
37 case None -> false
38 }
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 = axis
57 cross = breadth
58 direction = readingDirection
59 known = retained
60 return fresh
61 }
62 fun record(at: Long, extent: Double) {
63 val current = index!!
64 if (!current.variable)
65 return
66 current.record(at, extent)
67 known.put(source!!.keyAt(at), extent)
68 }
71internal fun virtualIndex(source: ItemSource, count: Long, key: ViewKey): Long? {
72 val at = source.indexOfKey(key) ?: return None
73 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)