Scoop API

scoop:scoop.ui.component:0.1.0

src/interaction/range.scoop

Selected production source · src/interaction/range.scoop

1package scoop.ui.component
3import scoop.math.*
5/** Numeric policy shared by range controls; no input, rendering or clock ownership. */
6public class RangeSpec public constructor(
7 public val minimum: Double,
8 public val maximum: Double,
9 public val step: Double? = None,
10 public val minimumGap: Double = 0.0,
11 public val reversed: Boolean = false
12) {
13 private val extent = maximum - minimum
14 init {
15 requireComponent(
16 minimum.isFinite() && maximum.isFinite() && extent.isFinite() && extent >= 0.0,
17 "invalid range bounds"
18 )
19 nonnegative(minimumGap, "invalid range minimum gap")
20 when (step) {
21 case Some(val value) -> requireComponent(
22 value.isFinite() && value > 0.0,
23 "range step must be finite and positive"
24 )
25 case None -> {}
26 }
27 }
28 private val steps: Long? =
29 when (step) {
30 case Some(val value) -> Some(rangeStepCount(extent / value))
31 case None -> None
32 }
33 private val gapSteps: Long =
34 when (step) {
35 case Some(val value) -> rangeGapSteps(minimumGap / value)
36 case None -> 0L
37 }
38 public val lastValue: Double get() =
39 when (steps) {
40 case Some(val value) -> fromStep(value)
41 case None -> maximum
42 }
43 public fun clamp(value: Double): Double {
44 requireComponent(value.isFinite(), "range value must be finite")
45 return when (steps) {
46 case Some(_) -> fromStep(stepIndex(value))
47 case None -> bound(value, minimum, maximum)
48 }
49 }
50 /** Position is normalized to 0..1 and can be mapped onto any committed track. */
51 public fun valueAt(position: Double): Double {
52 requireComponent(position.isFinite(), "range position must be finite")
53 val fraction = bound(position, 0.0, 1.0)
54 return clamp(
55 minimum +
56 extent *
57 (
58 if (reversed)
59 1.0 - fraction
60 else
61 fraction
62 )
63 )
64 }
65 public fun positionOf(value: Double): Double {
66 val selected = clamp(value)
67 val fraction =
68 if (extent == 0.0)
69 0.0
70 else
71 (selected - minimum) / extent
72 return if (reversed)
73 1.0 - fraction
74 else
75 fraction
76 }
77 /** Moves one stable thumb index without sorting or changing its neighbors. */
78 public fun move(values: Array<Double>, index: Long, value: Double): Array<Double> {
79 validate(values)
80 requireComponent(index >= 0L && index < values.size, "range thumb index is out of bounds")
81 requireComponent(value.isFinite(), "range value must be finite")
82 return moveValidated(values, index, value)
83 }
84 internal fun moveValidated(values: Array<Double>, index: Long, value: Double): Array<Double> {
85 val limits = thumbLimits(values, index)
86 val selected = bound(clamp(value), limits.minimum, limits.maximum)
87 return Array<Double>(values.size) {
88 position -> if (position == index)
89 selected
90 else
91 values[position]
92 }
93 }
94 internal fun thumbLimits(values: Array<Double>, index: Long): ThumbLimits =
95 when (steps) {
96 case Some(val last) -> ThumbLimits(
97 fromStep(
98 if (index == 0L)
99 0L
100 else
101 stepIndex(values[index - 1L]) + gapSteps
102 ),
103 fromStep(
104 if (index + 1L == values.size)
105 last
106 else
107 stepIndex(values[index + 1L]) - gapSteps
108 )
109 )
110 case None -> ThumbLimits(
111 if (index == 0L)
112 minimum
113 else
114 values[index - 1L] + minimumGap,
115 if (index + 1L == values.size)
116 maximum
117 else
118 values[index + 1L] - minimumGap
119 )
120 }
121 public fun validate(values: Array<Double>) {
122 val count =
123 if (values.size == 0L)
124 0L
125 else
126 values.size - 1L
127 val required =
128 when (steps) {
129 case Some(val last) -> count.toDouble() * gapSteps.toDouble() <= last.toDouble()
130 case None -> count.toDouble() * minimumGap <= extent
131 }
132 requireComponent(required, "range cannot contain all thumbs at the minimum gap")
133 for (index in 0L ..< values.size) {
134 val value = values[index]
135 requireComponent(
136 value.isFinite() && value >= minimum && value <= maximum,
137 "range thumb is outside its bounds"
138 )
139 when (steps) {
140 case Some(_) -> {
141 val number = stepIndex(value)
142 val canonical = fromStep(number)
143 requireComponent(
144 abs(value - canonical) <= step!! * 1.0e-10,
145 "range thumb is not on a discrete step"
146 )
147 if (index > 0L)
148 requireComponent(
149 number - stepIndex(values[index - 1L]) >= gapSteps,
150 "range thumbs violate their minimum gap"
151 )
152 }
153 case None -> if (index > 0L) {
154 val previous = values[index - 1L]
155 requireComponent(
156 value >=
157 previous &&
158 (minimumGap == 0.0 || value > previous) &&
159 (
160 value -
161 previous >=
162 minimumGap ||
163 value ==
164 previous +
165 minimumGap ||
166 previous ==
167 value -
168 minimumGap
169 ),
170 "range thumbs violate their minimum gap"
171 )
172 }
173 }
174 }
175 }
176 private fun stepIndex(value: Double): Long {
177 val last = steps!!
178 val limited = bound(value, minimum, maximum)
179 val quotient = (limited - minimum) / step!!
180 val lower = floor(quotient)
181 val rounded =
182 if (quotient - lower >= 0.5)
183 lower + 1.0
184 else
185 lower
186 return min(rounded.toLong(), last)
187 }
188 private fun fromStep(index: Long): Double = min(maximum, minimum + index.toDouble() * step!!)
191private fun bound(value: Double, low: Double, high: Double): Double = min(max(value, low), high)
193private fun checkedSteps(value: Double): Long {
194 requireComponent(
195 value.isFinite() && value <= 9007199254740991.0,
196 "range step count exceeds exact integer precision"
197 )
198 return value.toLong()
201private fun rangeStepCount(quotient: Double): Long {
202 val lower = floor(quotient)
203 val fraction = quotient - lower
204 // Only compensate for the last arithmetic rounding, not an extra endpoint step.
205 val tolerance = min(1.0e-8, quotient * 8.881784197001252e-16)
206 val value =
207 if (fraction > 0.0 && 1.0 - fraction <= tolerance)
208 lower + 1.0
209 else
210 lower
211 return checkedSteps(value)
214private fun rangeGapSteps(quotient: Double): Long {
215 val lower = floor(quotient)
216 val tolerance = min(1.0e-8, quotient * 8.881784197001252e-16)
217 return checkedSteps(
218 if (quotient - lower <= tolerance)
219 lower
220 else
221 ceil(quotient)
222 )
225internal struct ThumbLimits(val minimum: Double, val maximum: Double)