Scoop API

scoop:scoop.ui:0.1.0

src/scroll/geometry.scoop

Selected production source · src/scroll/geometry.scoop

1package scoop.ui
3import scoop.graphics.*
4import scoop.math.max
5import scoop.math.min
7public enum ScrollAxes {
8 Horizontal, Vertical, Both
10 public val horizontal: Boolean get() = this != ScrollAxes.Vertical
11 public val vertical: Boolean get() = this != ScrollAxes.Horizontal
14public enum ScrollAlignment {
15 Nearest, Start, Center, End
18/** Physical margins that make obscured content edges reachable by reveal. */
19public struct ScrollPadding(
20 val left: Double = 0.0,
21 val top: Double = 0.0,
22 val right: Double = 0.0,
23 val bottom: Double = 0.0
24) {
25 internal fun extent(): Size {
26 checkLayoutSize(Size(left, top))
27 checkLayoutSize(Size(right, bottom))
28 val size = Size(left + right, top + bottom)
29 checkLayoutSize(size)
30 return size
31 }
34public struct ScrollSnapshot(
35 val offset: Point,
36 val viewport: Size,
37 val content: Size,
38 val padding: ScrollPadding = ScrollPadding()
39) {
40 /** Content extent before the reveal margins were added. */
41 public val bodySize: Size get() =
42 Size(
43 max(0.0, content.width - padding.left - padding.right),
44 max(0.0, content.height - padding.top - padding.bottom)
45 )
46 /** Unobscured extent used by reveal and page navigation. */
47 public val visibleSize: Size get() =
48 Size(
49 max(0.0, viewport.width - padding.left - padding.right),
50 max(0.0, viewport.height - padding.top - padding.bottom)
51 )
52 public val maximum: Point get() =
53 Point(max(0.0, content.width - viewport.width), max(0.0, content.height - viewport.height))
54 public fun revealedOffset(
55 rect: Rect,
56 horizontal: ScrollAlignment = ScrollAlignment.Nearest,
57 vertical: ScrollAlignment = ScrollAlignment.Nearest
58 ): Point {
59 checkScrollRect(rect)
60 checkScrollPoint(offset)
61 checkLayoutSize(viewport)
62 checkLayoutSize(content)
63 padding.extent()
64 val visible = visibleSize
65 return clampScroll(
66 Point(
67 revealScrollAxis(offset.x, visible.width, rect.x, rect.width, horizontal),
68 revealScrollAxis(offset.y, visible.height, rect.y, rect.height, vertical)
69 ),
70 viewport,
71 content
72 )
73 }
76/** A layout correction based on a particular explicit positioning intent. */
77public struct ScrollAdjustment(val requestRevision: ULong, val offset: Point)
79internal enum ScrollRequest {
80 Position(val point: Point),
81 Reveal(val rect: Rect, val horizontal: ScrollAlignment, val vertical: ScrollAlignment)
84internal fun checkScrollPoint(point: Point) {
85 requireUi(point.x.isFinite() && point.y.isFinite(), "invalid scroll position or displacement")
88internal fun checkScrollRect(rect: Rect) {
89 checkScrollPoint(Point(rect.x, rect.y))
90 checkLayoutSize(Size(rect.width, rect.height))
91 checkScrollPoint(Point(rect.x + rect.width, rect.y + rect.height))
94internal fun clampScroll(point: Point, viewport: Size, content: Size): Point =
95 Point(
96 min(max(0.0, point.x), max(0.0, content.width - viewport.width)),
97 min(max(0.0, point.y), max(0.0, content.height - viewport.height))
98 )
100internal fun resolveScroll(
101 request: ScrollRequest,
102 previous: Point,
103 viewport: Size,
104 content: Size,
105 padding: ScrollPadding = ScrollPadding()
106): ScrollSnapshot {
107 checkLayoutSize(viewport)
108 checkLayoutSize(content)
109 val point =
110 when (request) {
111 case Position(val value) -> value
112 case Reveal(val rect, val horizontal, val vertical) ->
113 ScrollSnapshot(previous, viewport, content, padding).revealedOffset(
114 rect,
115 horizontal,
116 vertical
117 )
118 }
119 checkScrollPoint(point)
120 return ScrollSnapshot(clampScroll(point, viewport, content), viewport, content, padding)
123private fun revealScrollAxis(
124 offset: Double,
125 viewport: Double,
126 start: Double,
127 length: Double,
128 alignment: ScrollAlignment
129): Double =
130 when (alignment) {
131 case Start -> start
132 case Center -> start + (length - viewport) / 2.0
133 case End -> start + length - viewport
134 case Nearest -> {
135 val end = start + length
136 if (
137 (start >= offset && end <= offset + viewport) ||
138 (start <= offset && end >= offset + viewport)
139 )
140 offset
141 else
142 if (start < offset)
143 if (length <= viewport)
144 start
145 else
146 end - viewport
147 else
148 if (length <= viewport)
149 end - viewport
150 else
151 start
152 }
153 }