scoop:scoop.ui.component:0.1.0
src/slider/geometry.scoop
Selected production source · src/slider/geometry.scoop
1
package scoop.ui.component3
import scoop.graphics.*4
import scoop.math.*5
import scoop.ui.*6
import scoop.ui.style.*8
internal 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: Double20
) {21
fun coordinate(point: Point): Double =22
if (orientation == Orientation.Horizontal)23
point.x24
else25
point.y26
fun center(index: Long): Double {27
val thumb = thumbs[index]28
return if (orientation == Orientation.Horizontal)29
thumb.x + thumb.width / 2.030
else31
thumb.y + thumb.height / 2.032
}33
fun position(point: Point, grab: Double): Double {34
val fraction = (coordinate(point) - grab * thumbSize - start) / travel35
return if (orientation == Orientation.Vertical || direction == LayoutDirection.Rtl)36
1.0 - fraction37
else38
fraction39
}40
}42
internal fun sliderGeometry(43
input: SliderProps,44
values: Array<Double>,45
theme: Theme,46
size: Size,47
direction: LayoutDirection48
): SliderGeometry {49
val horizontal = input.orientation == Orientation.Horizontal50
val density = theme.tokens.typography.body.fontSize / 14.051
val main =52
if (horizontal)53
size.width54
else55
size.height56
val cross =57
if (horizontal)58
size.height59
else60
size.width61
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.064
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 - fraction74
else75
fraction76
)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
else83
Rect((cross - thumb) / 2.0, positions[index] - start, thumb, thumb)84
}85
var low = 0.086
var high = 0.087
if (values.size > 1L) {88
low = min(positions[0L], positions[values.size - 1L])89
high = max(positions[0L], positions[values.size - 1L])90
} else91
if (values.size == 1L) {92
val minimumAtEnd =93
(!horizontal || direction == LayoutDirection.Rtl) != input.range.reversed94
low =95
if (minimumAtEnd)96
positions[0L]97
else98
0.099
high =100
if (minimumAtEnd)101
main102
else103
positions[0L]104
}105
val track =106
if (horizontal)107
Rect(0.0, (cross - thickness) / 2.0, main, thickness)108
else109
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
else114
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
travel132
)133
}135
internal fun sameSliderValues(a: Array<Double>, b: Array<Double>): Boolean {136
if (a === b)137
return true138
if (a.size != b.size)139
return false140
for (index in 0L ..< a.size)141
if (a[index] != b[index])142
return false143
return true144
}146
internal 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.reversed158
internal fun nearestSliderThumb(159
shape: SliderGeometry,160
range: RangeSpec,161
values: Array<Double>,162
point: Point,163
focused: Long?164
): Long? {165
var best: Long? = None166
var distance = 0.0167
for (index in 0L ..< values.size) {168
val limits = range.thumbLimits(values, index)169
if (limits.minimum == limits.maximum)170
continue171
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-16175
val tied = abs(next - distance) <= tolerance176
if (best == None || (next < distance && !tied) || (tied && focused == Some(index))) {177
best = Some(index)178
distance = next179
}180
}181
return best182
}