Scoop API

scoop:scoop.ui.component:0.1.0

src/tree/sticky.scoop

Selected production source · src/tree/sticky.scoop

1package scoop.ui.component
3import scoop.ui.*
5internal class TreePinnedRow(val index: Long, val y: Double, val top: Double, val bottom: Double)
7internal class TreePinning(val rows: Array<TreePinnedRow>, val height: Double)
9/** The next slot follows the branch at its reading edge; each subtree pushes its own row out. */
10internal fun <T> treePinning(
11 source: TreeProjection<T>,
12 extents: ItemExtentIndex,
13 offset: Double,
14 height: Double,
15 options: TreeSticky
16): TreePinning {
17 if (!options.enabled || options.maxRows == 0L || height <= 0.0 || source.count == 0L)
18 return TreePinning([], 0.0)
19 val limit = height * options.maxHeightRatio
20 val result = ArrayList<TreePinnedRow>()
21 var top = 0.0
22 var visible = 0.0
23 var last: Long? = None
24 while (result.size < options.maxRows) {
25 val at = extents.indexAt(offset + top)
26 if (at >= source.count) break
27 val path = ArrayList<Long>()
28 var cursor: Long? = Some(at)
29 while (cursor != None) {
30 val current = cursor!!
31 path.add(current)
32 cursor = source.rowAt(current).parent
33 }
34 var chosen: Long? = None
35 var inside = last == None
36 var index = path.size
37 while (index > 0L) {
38 index -= 1L
39 val row = path[index]
40 if (last == Some(row)) {
41 inside = true;
42 continue
43 }
44 if (inside &&
45 extents.prefix(row) -
46 offset <
47 top &&
48 source.rowAt(row).subtreeEnd >
49 row +
50 1L
51 ) {
52 chosen = Some(row)
53 break
54 }
55 }
56 val row = chosen ?: break
57 val extent = extents.extent(row)
58 if (extent <= 0.0 || top + extent > limit) break
59 val end = extents.prefix(source.rowAt(row).subtreeEnd) - offset
60 val y = upperBound(top, end - extent)
61 val bottom = upperBound(y + extent, height)
62 if (bottom <= top) break
63 result.add(TreePinnedRow(row, y, top, bottom))
64 visible = lowerBound(visible, bottom)
65 top += extent
66 last = Some(row)
67 }
68 return TreePinning(result.toArray(), visible)
71/** Candidate slot sums keep an anchor stable across changed row heights without frame feedback. */
72internal fun <T> treeAnchoredOffset(
73 source: TreeProjection<T>,
74 extents: ItemExtentIndex,
75 at: Long,
76 inside: Double,
77 viewport: Double,
78 options: TreeSticky,
79 previous: Double,
80 previousHeight: Double
81): Double {
82 val target = extents.prefix(at) + upperBound(inside, extents.extent(at))
83 val maximum = lowerBound(extents.total - viewport, 0.0)
84 val candidates = ArrayList<Double>()
85 candidates.add(0.0)
86 candidates.add(previousHeight)
87 val path = ArrayList<Long>()
88 var parent = source.rowAt(at).parent
89 while (parent != None) {
90 path.add(parent!!);
91 parent = source.rowAt(parent!!).parent
92 }
93 var height = 0.0
94 var kept = 0L
95 var index = path.size
96 while (index > 0L && kept < options.maxRows && options.enabled) {
97 index -= 1L
98 height += extents.extent(path[index])
99 if (height > viewport * options.maxHeightRatio) break
100 candidates.add(height)
101 kept += 1L
102 }
103 var best = upperBound(lowerBound(target - previousHeight, 0.0), maximum)
104 var error = Double.MAX_VALUE
105 for (height in candidates) {
106 val offset = upperBound(lowerBound(target - height, 0.0), maximum)
107 val actual = treePinning(source, extents, offset, viewport, options).height
108 val delta = target - offset - actual
109 val distance = if (delta < 0.0) -delta else delta
110 val travel = if (offset < previous) previous - offset else offset - previous
111 val oldTravel = if (best < previous) previous - best else best - previous
112 if (distance < error || (distance == error && travel < oldTravel)) {
113 best = offset;
114 error = distance
115 }
116 }
117 return best