scoop:scoop.ui.component:0.1.0
src/interaction/range.scoop
Selected production source · src/interaction/range.scoop
1
package scoop.ui.component3
import scoop.math.*5
/** Numeric policy shared by range controls; no input, rendering or clock ownership. */6
public 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 = false12
) {13
private val extent = maximum - minimum14
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 -> None32
}33
private val gapSteps: Long =34
when (step) {35
case Some(val value) -> rangeGapSteps(minimumGap / value)36
case None -> 0L37
}38
public val lastValue: Double get() =39
when (steps) {40
case Some(val value) -> fromStep(value)41
case None -> maximum42
}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 - fraction60
else61
fraction62
)63
)64
}65
public fun positionOf(value: Double): Double {66
val selected = clamp(value)67
val fraction =68
if (extent == 0.0)69
0.070
else71
(selected - minimum) / extent72
return if (reversed)73
1.0 - fraction74
else75
fraction76
}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
selected90
else91
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
0L100
else101
stepIndex(values[index - 1L]) + gapSteps102
),103
fromStep(104
if (index + 1L == values.size)105
last106
else107
stepIndex(values[index + 1L]) - gapSteps108
)109
)110
case None -> ThumbLimits(111
if (index == 0L)112
minimum113
else114
values[index - 1L] + minimumGap,115
if (index + 1L == values.size)116
maximum117
else118
values[index + 1L] - minimumGap119
)120
}121
public fun validate(values: Array<Double>) {122
val count =123
if (values.size == 0L)124
0L125
else126
values.size - 1L127
val required =128
when (steps) {129
case Some(val last) -> count.toDouble() * gapSteps.toDouble() <= last.toDouble()130
case None -> count.toDouble() * minimumGap <= extent131
}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
minimumGap169
),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.0184
else185
lower186
return min(rounded.toLong(), last)187
}188
private fun fromStep(index: Long): Double = min(maximum, minimum + index.toDouble() * step!!)189
}191
private fun bound(value: Double, low: Double, high: Double): Double = min(max(value, low), high)193
private 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()199
}201
private fun rangeStepCount(quotient: Double): Long {202
val lower = floor(quotient)203
val fraction = quotient - lower204
// 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.0209
else210
lower211
return checkedSteps(value)212
}214
private 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
lower220
else221
ceil(quotient)222
)223
}225
internal struct ThumbLimits(val minimum: Double, val maximum: Double)