Scoop API

scoop:scoop.ui.component:0.1.0

src/slider/geometry.scoop

Selected production source · src/slider/geometry.scoop

1package scoop.ui.component
3import scoop.graphics.*
4import scoop.math.*
5import scoop.ui.*
6import scoop.ui.style.*
8internal class SliderGeometry(
9 val size: Size,
10 val track: Rect,
11 val fill: Rect,
12 val thumbs: Array<Rect>,
13 val hit: Rect,
14 val range: RangeSpec,
15 val orientation: Orientation,
16 val direction: LayoutDirection,
17 val thumbSize: Double,
18 val start: Double,
19 val travel: Double
20) {
21 fun coordinate(point: Point): Double =
22 if (orientation == Orientation.Horizontal)
23 point.x
24 else
25 point.y
26 fun center(index: Long): Double {
27 val thumb = thumbs[index]
28 return if (orientation == Orientation.Horizontal)
29 thumb.x + thumb.width / 2.0
30 else
31 thumb.y + thumb.height / 2.0
32 }
33 fun position(point: Point, grab: Double): Double {
34 val fraction = (coordinate(point) - grab * thumbSize - start) / travel
35 return if (orientation == Orientation.Vertical || direction == LayoutDirection.Rtl)
36 1.0 - fraction
37 else
38 fraction
39 }
42internal fun sliderGeometry(
43 input: SliderProps,
44 values: Array<Double>,
45 theme: Theme,
46 size: Size,
47 direction: LayoutDirection
48): SliderGeometry {
49 val horizontal = input.orientation == Orientation.Horizontal
50 val density = theme.tokens.typography.body.fontSize / 14.0
51 val main =
52 if (horizontal)
53 size.width
54 else
55 size.height
56 val cross =
57 if (horizontal)
58 size.height
59 else
60 size.width
61 val thumb = min(input.style.thumbSize ?: 12.0 * density, min(main, cross))
62 val thickness = min(input.style.thickness ?: 4.0 * density, cross)
63 val start = thumb / 2.0
64 val travel = max(0.0, main - thumb)
65 val positions =
66 Array<Double>(values.size) {
67 index ->
68 val fraction = input.range.positionOf(values[index])
69 start +
70 travel *
71 (
72 if (!horizontal || direction == LayoutDirection.Rtl)
73 1.0 - fraction
74 else
75 fraction
76 )
77 }
78 val rects =
79 Array<Rect>(values.size) {
80 index -> if (horizontal)
81 Rect(positions[index] - start, (cross - thumb) / 2.0, thumb, thumb)
82 else
83 Rect((cross - thumb) / 2.0, positions[index] - start, thumb, thumb)
84 }
85 var low = 0.0
86 var high = 0.0
87 if (values.size > 1L) {
88 low = min(positions[0L], positions[values.size - 1L])
89 high = max(positions[0L], positions[values.size - 1L])
90 } else
91 if (values.size == 1L) {
92 val minimumAtEnd =
93 (!horizontal || direction == LayoutDirection.Rtl) != input.range.reversed
94 low =
95 if (minimumAtEnd)
96 positions[0L]
97 else
98 0.0
99 high =
100 if (minimumAtEnd)
101 main
102 else
103 positions[0L]
104 }
105 val track =
106 if (horizontal)
107 Rect(0.0, (cross - thickness) / 2.0, main, thickness)
108 else
109 Rect((cross - thickness) / 2.0, 0.0, thickness, main)
110 val fill =
111 if (horizontal)
112 Rect(low, track.y, high - low, track.height)
113 else
114 Rect(track.x, low, track.width, high - low)
115 val hit =
116 switchHitBounds(
117 Rect(0.0, 0.0, size.width, size.height),
118 input.style.hitInsets ?: Insets.all(8.0 * density)
119 )
120 return SliderGeometry(
121 size,
122 track,
123 fill,
124 rects,
125 hit,
126 input.range,
127 input.orientation,
128 direction,
129 thumb,
130 start,
131 travel
132 )
135internal fun sameSliderValues(a: Array<Double>, b: Array<Double>): Boolean {
136 if (a === b)
137 return true
138 if (a.size != b.size)
139 return false
140 for (index in 0L ..< a.size)
141 if (a[index] != b[index])
142 return false
143 return true
146internal fun sameSliderRange(a: RangeSpec, b: RangeSpec): Boolean =
147 a.minimum ==
148 b.minimum &&
149 a.maximum ==
150 b.maximum &&
151 a.step ==
152 b.step &&
153 a.minimumGap ==
154 b.minimumGap &&
155 a.reversed ==
156 b.reversed
158internal fun nearestSliderThumb(
159 shape: SliderGeometry,
160 range: RangeSpec,
161 values: Array<Double>,
162 point: Point,
163 focused: Long?
164): Long? {
165 var best: Long? = None
166 var distance = 0.0
167 for (index in 0L ..< values.size) {
168 val limits = range.thumbLimits(values, index)
169 if (limits.minimum == limits.maximum)
170 continue
171 val next = abs(shape.coordinate(point) - shape.center(index))
172 val tolerance =
173 max(1.0, max(abs(shape.coordinate(point)), abs(shape.center(index)))) *
174 8.881784197001252e-16
175 val tied = abs(next - distance) <= tolerance
176 if (best == None || (next < distance && !tied) || (tied && focused == Some(index))) {
177 best = Some(index)
178 distance = next
179 }
180 }
181 return best