scoop:scoop.ui.component:0.1.0
src/virtual/layout.scoop
Selected production source · src/virtual/layout.scoop
1
package scoop.ui.component3
import scoop.graphics.*4
import scoop.ui.*5
import scoop.ui.style.Orientation7
internal fun virtualViewport(constraints: Constraints, width: Double?, height: Double?): Size {8
val minimum = constraints.constrain(Size(0.0, 0.0))9
return Size(10
scrollExtent(width, minimum.width, constraints.maxWidth, true)!!,11
scrollExtent(height, minimum.height, constraints.maxHeight, true)!!12
)13
}15
internal class ListGeometry(16
val index: ItemExtentIndex,17
private val orientation: Orientation,18
private val overscan: Double,19
private val viewport: Size,20
direction: LayoutDirection = LayoutDirection.Ltr,21
endPadding: Double = 0.022
) {23
var padding = endPadding24
private val vertical = orientation == Orientation.Vertical25
val reversed = !vertical && direction == LayoutDirection.Rtl26
val total: Double get() {27
val value = index.total + padding28
requireComponent(value.isFinite(), "virtual content extent overflow")29
return value30
}31
private val visible =32
if (vertical)33
viewport.height34
else35
viewport.width36
init {37
nonnegative(overscan, "invalid virtual overscan")38
nonnegative(endPadding, "invalid virtual end padding")39
}40
val content: Size get() =41
if (vertical)42
Size(viewport.width, total)43
else44
Size(total, viewport.height)45
val itemConstraints: Constraints get() =46
when (index.mode) {47
case Fixed(val extent) -> Constraints.tight(48
if (vertical)49
Size(viewport.width, extent)50
else51
Size(extent, viewport.height)52
)53
case Variable(_) -> if (vertical)54
Constraints(minWidth = viewport.width, maxWidth = Some(viewport.width))55
else56
Constraints(minHeight = viewport.height, maxHeight = Some(viewport.height))57
}58
fun range(offset: Double): VirtualRange = range(offset, overscan)59
fun visibleRange(offset: Double): VirtualRange = range(offset, 0.0)60
private fun range(offset: Double, margin: Double): VirtualRange {61
nonnegative(offset, "invalid virtual offset")62
if (index.count == 0L || viewport.width == 0.0 || viewport.height == 0.0)63
return VirtualRange(0L, 0L)64
val at = upperBound(offset, total)65
val start = index.indexAt(lowerBound(at - margin, 0.0))66
val endOfViewport = at + upperBound(visible, total - at)67
val endPosition = endOfViewport + upperBound(margin, total - endOfViewport)68
val end = index.endBefore(endPosition)69
return VirtualRange(70
start,71
if (end >= start)72
end73
else74
start75
)76
}77
fun point(position: Double): Point =78
if (vertical)79
Point(0.0, position)80
else81
Point(position, 0.0)82
val maximum: Double get() = lowerBound(total - visible, 0.0)83
fun fillViewportFrom(offset: Double) {84
nonnegative(offset, "invalid virtual fill offset")85
padding = lowerBound(offset + visible - index.total, 0.0)86
}87
fun logicalOffset(offset: Point): Double {88
val physical = scrollbarAxis(orientation, offset)89
return if (reversed)90
maximum - physical91
else92
physical93
}94
fun scrollOffset(logical: Double): Point =95
point(96
if (reversed)97
maximum - logical98
else99
logical100
)101
fun origin(item: Long): Point =102
point(103
if (reversed)104
lowerBound(total, visible) - index.prefix(item) - index.extent(item)105
else106
index.prefix(item)107
)108
fun alignedOffset(109
snapshot: ScrollSnapshot,110
item: Long,111
alignment: ScrollAlignment,112
margin: Double = 0.0,113
previousPeek: Double = 0.0114
): Point {115
if (alignment == ScrollAlignment.Start) {116
return scrollOffset(upperBound(startOffset(item, margin, previousPeek), maximum))117
}118
val physical =119
if (reversed)120
when (alignment) {121
case Start -> ScrollAlignment.End122
case End -> ScrollAlignment.Start123
else -> alignment124
}125
else126
alignment127
val target = bounds(item)128
val expanded =129
if (vertical)130
Rect(target.x, target.y - margin, target.width, target.height + 2.0 * margin)131
else132
Rect(target.x - margin, target.y, target.width + 2.0 * margin, target.height)133
return snapshot.revealedOffset(134
expanded,135
if (vertical)136
ScrollAlignment.Nearest137
else138
physical,139
if (vertical)140
physical141
else142
ScrollAlignment.Nearest143
)144
}145
fun startOffset(item: Long, margin: Double, previousPeek: Double): Double {146
val peek =147
if (item > 0L)148
upperBound(previousPeek, index.extent(item - 1L))149
else150
0.0151
return lowerBound(index.prefix(item) - margin - peek, 0.0)152
}153
fun bounds(item: Long): Rect {154
val origin = origin(item)155
return if (vertical)156
Rect(origin.x, origin.y, viewport.width, index.extent(item))157
else158
Rect(origin.x, origin.y, index.extent(item), viewport.height)159
}160
}