Scoop API

scoop:scoop.ui.style:0.1.0

src/theme/scrollbar_geometry.scoop

Selected production source · src/theme/scrollbar_geometry.scoop

1package scoop.ui.style
3import scoop.graphics.*
5internal fun makeScrollbarGeometry(
6 bounds: Rect,
7 visual: ScrollbarVisual,
8 metrics: ScrollbarMetrics,
9 arrowWidth: Double
10): ScrollbarGeometry {
11 themeBounds(bounds)
12 val position = themeUnit(visual.position)
13 val fraction = themeUnit(visual.visibleFraction)
14 themeUnit(visual.opacity)
15 val vertical = visual.orientation == Orientation.Vertical
16 val length =
17 if (vertical)
18 bounds.height
19 else
20 bounds.width
21 val cross =
22 if (vertical)
23 bounds.width
24 else
25 bounds.height
26 val thickness = themeMin(cross, metrics.thickness)
27 val strip =
28 if (vertical)
29 Rect(bounds.x + (cross - thickness) / 2.0, bounds.y, thickness, length)
30 else
31 Rect(bounds.x, bounds.y + (cross - thickness) / 2.0, length, thickness)
32 val buttonLength = themeMin(metrics.buttonLength ?: 0.0, length / 2.0)
33 val main = axisRect(strip, vertical, buttonLength, themeMax(0.0, length - buttonLength * 2.0))
34 val track = roundTheme(insetTheme(main, Insets.all(metrics.trackInset)), metrics.cornerRadius)
35 val lane = insetTheme(track.bounds, Insets.all(metrics.thumbInset))
36 val laneLength =
37 if (vertical)
38 lane.height
39 else
40 lane.width
41 val thumbLength =
42 themeMin(laneLength, themeMax(metrics.minimumThumbLength, laneLength * fraction))
43 val travel = themeMax(0.0, laneLength - thumbLength)
44 val thumb =
45 roundTheme(axisRect(lane, vertical, travel * position, thumbLength), metrics.cornerRadius)
46 val decrement =
47 when (metrics.buttonLength) {
48 case Some(_) -> Some(
49 scrollbarButton(
50 axisRect(strip, vertical, 0.0, buttonLength),
51 vertical,
52 false,
53 metrics.cornerRadius,
54 arrowWidth
55 )
56 )
57 case None -> None
58 }
59 val increment =
60 when (metrics.buttonLength) {
61 case Some(_) -> Some(
62 scrollbarButton(
63 axisRect(strip, vertical, length - buttonLength, buttonLength),
64 vertical,
65 true,
66 metrics.cornerRadius,
67 arrowWidth
68 )
69 )
70 case None -> None
71 }
72 return ScrollbarGeometry(
73 bounds,
74 visual.orientation,
75 track,
76 thumb,
77 decrement,
78 increment,
79 Point(lane.x, lane.y),
80 travel,
81 thumbLength,
82 position,
83 fraction
84 )
87private fun axisRect(bounds: Rect, vertical: Boolean, start: Double, length: Double): Rect =
88 if (vertical)
89 Rect(bounds.x, bounds.y + start, bounds.width, length)
90 else
91 Rect(bounds.x + start, bounds.y, length, bounds.height)
93private fun scrollbarButton(
94 bounds: Rect,
95 vertical: Boolean,
96 increment: Boolean,
97 radius: Double,
98 width: Double
99): ScrollbarButtonGeometry {
100 val cx = bounds.x + bounds.width / 2.0
101 val cy = bounds.y + bounds.height / 2.0
102 val size = themeMin(bounds.width, bounds.height) * 0.22
103 val direction =
104 if (increment)
105 1.0
106 else
107 -1.0
108 val arrow =
109 if (vertical)
110 StrokeSegment(
111 Point(cx - size, cy - size * direction / 2.0),
112 Point(cx, cy + size * direction / 2.0),
113 Point(cx + size, cy - size * direction / 2.0),
114 themeMin(width, size)
115 )
116 else
117 StrokeSegment(
118 Point(cx - size * direction / 2.0, cy - size),
119 Point(cx + size * direction / 2.0, cy),
120 Point(cx - size * direction / 2.0, cy + size),
121 themeMin(width, size)
122 )
123 return ScrollbarButtonGeometry(roundTheme(bounds, radius), arrow)