scoop:scoop.ui.component:0.1.0
src/scroll/layout.scoop
Selected production source · src/scroll/layout.scoop
1
package scoop.ui.component3
import scoop.graphics.*4
import scoop.ui.*5
import scoop.ui.style.*7
/** Resolves definite viewport axes before the single content measurement. */8
internal class ScrollMeasure(9
private val parent: Constraints,10
axes: ScrollAxes,11
width: Double?,12
height: Double?,13
private val gutter: Size = Size(0.0, 0.0)14
) {15
private val minimum = parent.constrain(Size(0.0, 0.0))16
private val x = scrollExtent(width, minimum.width, parent.maxWidth, axes.horizontal)17
private val y = scrollExtent(height, minimum.height, parent.maxHeight, axes.vertical)18
val contentConstraints =19
Constraints(20
if (axes.horizontal)21
0.022
else23
lowerBound((x ?: minimum.width) - gutter.width, 0.0),24
if (axes.horizontal)25
None26
else27
when (x) {28
case Some(val value) -> Some(lowerBound(value - gutter.width, 0.0))29
case None -> None30
},31
if (axes.vertical)32
0.033
else34
lowerBound((y ?: minimum.height) - gutter.height, 0.0),35
if (axes.vertical)36
None37
else38
when (y) {39
case Some(val value) -> Some(lowerBound(value - gutter.height, 0.0))40
case None -> None41
}42
)43
fun size(content: Size): Size =44
parent.constrain(45
Size(x ?: content.width + gutter.width, y ?: content.height + gutter.height)46
)47
}49
internal fun scrollExtent(50
explicit: Double?,51
minimum: Double,52
maximum: Double?,53
scrollable: Boolean54
): Double? {55
val candidate =56
when (explicit) {57
case Some(val value) -> {58
nonnegative(value, "invalid scroll viewport extent");59
Some(value)60
}61
case None -> maximum62
}63
return when (candidate) {64
case Some(val value) -> {65
val lower = lowerBound(value, minimum)66
Some(upperBound(lower, maximum ?: lower))67
}68
case None -> {69
requireComponent(70
!scrollable,71
"scroll axis requires a bounded parent or an explicit extent"72
)73
None74
}75
}76
}78
internal fun showScrollbar(mode: ScrollbarVisibility, range: Double): Boolean =79
when (mode) {80
case Hidden -> false81
case Always -> true82
case Auto -> range > 0.083
}85
internal fun scrollbarPart(geometry: ScrollbarGeometry, point: Point): ScrollbarRegion? {86
if (!geometry.bounds.contains(point))87
return None88
if (scrollbarAxisContains(geometry.orientation, geometry.thumb.bounds, point))89
return Some(ScrollbarRegion.Thumb)90
when (geometry.decrement) {91
case Some(val button) -> if (92
scrollbarAxisContains(geometry.orientation, button.outline.bounds, point)93
)94
return Some(ScrollbarRegion.Decrement)95
case None -> {}96
}97
when (geometry.increment) {98
case Some(val button) -> if (99
scrollbarAxisContains(geometry.orientation, button.outline.bounds, point)100
)101
return Some(ScrollbarRegion.Increment)102
case None -> {}103
}104
return Some(ScrollbarRegion.Track)105
}107
private fun scrollbarAxisContains(orientation: Orientation, rect: Rect, point: Point): Boolean =108
if (orientation == Orientation.Horizontal)109
point.x >= rect.x && point.x < rect.x + rect.width110
else111
point.y >= rect.y && point.y < rect.y + rect.height113
internal fun scrollbarAxis(orientation: Orientation, point: Point): Double =114
if (orientation == Orientation.Horizontal)115
point.x116
else117
point.y119
internal fun scrollbarThumbOffset(120
geometry: ScrollbarGeometry,121
point: Point,122
grab: Double,123
maximum: Double124
): Double {125
if (geometry.travel <= 0.0)126
return geometry.position * maximum127
val position =128
(129
scrollbarAxis(geometry.orientation, point) -130
scrollbarAxis(geometry.orientation, geometry.travelStart) -131
grab *132
geometry.thumbLength133
) /134
geometry.travel135
return upperBound(lowerBound(position, 0.0), 1.0) * maximum136
}