scoop:scoop.ui.component:0.1.0
src/tree/sticky.scoop
Selected production source · src/tree/sticky.scoop
1
package scoop.ui.component3
import scoop.ui.*5
internal class TreePinnedRow(val index: Long, val y: Double, val top: Double, val bottom: Double)7
internal 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. */10
internal fun <T> treePinning(11
source: TreeProjection<T>,12
extents: ItemExtentIndex,13
offset: Double,14
height: Double,15
options: TreeSticky16
): TreePinning {17
if (!options.enabled || options.maxRows == 0L || height <= 0.0 || source.count == 0L)18
return TreePinning([], 0.0)19
val limit = height * options.maxHeightRatio20
val result = ArrayList<TreePinnedRow>()21
var top = 0.022
var visible = 0.023
var last: Long? = None24
while (result.size < options.maxRows) {25
val at = extents.indexAt(offset + top)26
if (at >= source.count) break27
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).parent33
}34
var chosen: Long? = None35
var inside = last == None36
var index = path.size37
while (index > 0L) {38
index -= 1L39
val row = path[index]40
if (last == Some(row)) {41
inside = true;42
continue43
}44
if (inside &&45
extents.prefix(row) -46
offset <47
top &&48
source.rowAt(row).subtreeEnd >49
row +50
1L51
) {52
chosen = Some(row)53
break54
}55
}56
val row = chosen ?: break57
val extent = extents.extent(row)58
if (extent <= 0.0 || top + extent > limit) break59
val end = extents.prefix(source.rowAt(row).subtreeEnd) - offset60
val y = upperBound(top, end - extent)61
val bottom = upperBound(y + extent, height)62
if (bottom <= top) break63
result.add(TreePinnedRow(row, y, top, bottom))64
visible = lowerBound(visible, bottom)65
top += extent66
last = Some(row)67
}68
return TreePinning(result.toArray(), visible)69
}71
/** Candidate slot sums keep an anchor stable across changed row heights without frame feedback. */72
internal 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: Double81
): 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).parent89
while (parent != None) {90
path.add(parent!!);91
parent = source.rowAt(parent!!).parent92
}93
var height = 0.094
var kept = 0L95
var index = path.size96
while (index > 0L && kept < options.maxRows && options.enabled) {97
index -= 1L98
height += extents.extent(path[index])99
if (height > viewport * options.maxHeightRatio) break100
candidates.add(height)101
kept += 1L102
}103
var best = upperBound(lowerBound(target - previousHeight, 0.0), maximum)104
var error = Double.MAX_VALUE105
for (height in candidates) {106
val offset = upperBound(lowerBound(target - height, 0.0), maximum)107
val actual = treePinning(source, extents, offset, viewport, options).height108
val delta = target - offset - actual109
val distance = if (delta < 0.0) -delta else delta110
val travel = if (offset < previous) previous - offset else offset - previous111
val oldTravel = if (best < previous) previous - best else best - previous112
if (distance < error || (distance == error && travel < oldTravel)) {113
best = offset;114
error = distance115
}116
}117
return best118
}