scoop:scoop.ui.style:0.1.0
src/theme/scrollbar_geometry.scoop
Selected production source · src/theme/scrollbar_geometry.scoop
1
package scoop.ui.style3
import scoop.graphics.*5
internal fun makeScrollbarGeometry(6
bounds: Rect,7
visual: ScrollbarVisual,8
metrics: ScrollbarMetrics,9
arrowWidth: Double10
): 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.Vertical16
val length =17
if (vertical)18
bounds.height19
else20
bounds.width21
val cross =22
if (vertical)23
bounds.width24
else25
bounds.height26
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
else31
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.height39
else40
lane.width41
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
arrowWidth55
)56
)57
case None -> None58
}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
arrowWidth68
)69
)70
case None -> None71
}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
fraction84
)85
}87
private 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
else91
Rect(bounds.x + start, bounds.y, length, bounds.height)93
private fun scrollbarButton(94
bounds: Rect,95
vertical: Boolean,96
increment: Boolean,97
radius: Double,98
width: Double99
): ScrollbarButtonGeometry {100
val cx = bounds.x + bounds.width / 2.0101
val cy = bounds.y + bounds.height / 2.0102
val size = themeMin(bounds.width, bounds.height) * 0.22103
val direction =104
if (increment)105
1.0106
else107
-1.0108
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
else117
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)124
}