Scoop API

scoop:scoop.ui.component:0.1.0

src/scroll/layout.scoop

Selected production source · src/scroll/layout.scoop

1package scoop.ui.component
3import scoop.graphics.*
4import scoop.ui.*
5import scoop.ui.style.*
7/** Resolves definite viewport axes before the single content measurement. */
8internal 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.0
22 else
23 lowerBound((x ?: minimum.width) - gutter.width, 0.0),
24 if (axes.horizontal)
25 None
26 else
27 when (x) {
28 case Some(val value) -> Some(lowerBound(value - gutter.width, 0.0))
29 case None -> None
30 },
31 if (axes.vertical)
32 0.0
33 else
34 lowerBound((y ?: minimum.height) - gutter.height, 0.0),
35 if (axes.vertical)
36 None
37 else
38 when (y) {
39 case Some(val value) -> Some(lowerBound(value - gutter.height, 0.0))
40 case None -> None
41 }
42 )
43 fun size(content: Size): Size =
44 parent.constrain(
45 Size(x ?: content.width + gutter.width, y ?: content.height + gutter.height)
46 )
49internal fun scrollExtent(
50 explicit: Double?,
51 minimum: Double,
52 maximum: Double?,
53 scrollable: Boolean
54): 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 -> maximum
62 }
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 None
74 }
75 }
78internal fun showScrollbar(mode: ScrollbarVisibility, range: Double): Boolean =
79 when (mode) {
80 case Hidden -> false
81 case Always -> true
82 case Auto -> range > 0.0
83 }
85internal fun scrollbarPart(geometry: ScrollbarGeometry, point: Point): ScrollbarRegion? {
86 if (!geometry.bounds.contains(point))
87 return None
88 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)
107private fun scrollbarAxisContains(orientation: Orientation, rect: Rect, point: Point): Boolean =
108 if (orientation == Orientation.Horizontal)
109 point.x >= rect.x && point.x < rect.x + rect.width
110 else
111 point.y >= rect.y && point.y < rect.y + rect.height
113internal fun scrollbarAxis(orientation: Orientation, point: Point): Double =
114 if (orientation == Orientation.Horizontal)
115 point.x
116 else
117 point.y
119internal fun scrollbarThumbOffset(
120 geometry: ScrollbarGeometry,
121 point: Point,
122 grab: Double,
123 maximum: Double
124): Double {
125 if (geometry.travel <= 0.0)
126 return geometry.position * maximum
127 val position =
128 (
129 scrollbarAxis(geometry.orientation, point) -
130 scrollbarAxis(geometry.orientation, geometry.travelStart) -
131 grab *
132 geometry.thumbLength
133 ) /
134 geometry.travel
135 return upperBound(lowerBound(position, 0.0), 1.0) * maximum