Scoop API

scoop:scoop.ui.component:0.1.0

src/slider/style.scoop

Selected production source · src/slider/style.scoop

1package scoop.ui.component
3import scoop.graphics.*
4import scoop.surface.AnimationSpec
5import scoop.ui.style.*
7public class SliderStyle internal constructor(
8 public val length: Double? = None,
9 public val thickness: Double? = None,
10 public val thumbSize: Double? = None,
11 public val hitInsets: Insets? = None,
12 public val track: StyleOverride = styleOverride(),
13 public val range: StyleOverride = styleOverride(),
14 public val thumb: StyleOverride = styleOverride(),
15 public val states: InteractionStyles = interactionStyles(),
16 public val transition: StyleValue<AnimationSpec?> = StyleValue.Inherit
17) {
18 init {
19 for (size in [length, thickness, thumbSize])
20 when (size) {
21 case Some(val value) -> nonnegative(value, "invalid slider dimension");
22 case None -> {}
23 }
24 hitInsets?.totals()
25 }
26 public fun copy(configure: SliderStyleBuilder.() -> Unit = {}): SliderStyle {
27 val builder = SliderStyleBuilder(this)
28 builder.configure()
29 return builder.snapshot()
30 }
33public fun sliderStyle(configure: SliderStyleBuilder.() -> Unit = {}): SliderStyle =
34 SliderStyle().copy(configure)
36public class SliderStyleBuilder internal constructor(initial: SliderStyle) {
37 public var length: Double? = initial.length
38 public var thickness: Double? = initial.thickness
39 public var thumbSize: Double? = initial.thumbSize
40 public var hitInsets: Insets? = initial.hitInsets
41 public var track: StyleOverride = initial.track
42 public var range: StyleOverride = initial.range
43 public var thumb: StyleOverride = initial.thumb
44 public var states: InteractionStyles = initial.states
45 public var transition: StyleValue<AnimationSpec?> = initial.transition
46 public fun track(configure: StyleOverrideBuilder.() -> Unit) {
47 track = track.copy(configure)
48 }
49 public fun range(configure: StyleOverrideBuilder.() -> Unit) {
50 range = range.copy(configure)
51 }
52 public fun thumb(configure: StyleOverrideBuilder.() -> Unit) {
53 thumb = thumb.copy(configure)
54 }
55 public fun states(configure: InteractionStylesBuilder.() -> Unit) {
56 states = states.copy(configure)
57 }
58 public fun transition(value: AnimationSpec?) {
59 transition = StyleValue.Set(value)
60 }
61 internal fun snapshot(): SliderStyle =
62 SliderStyle(
63 length,
64 thickness,
65 thumbSize,
66 hitInsets,
67 track,
68 range,
69 thumb,
70 states,
71 transition
72 )
75internal fun sliderPartStyle(color: Color, local: StyleOverride): Style =
76 local.resolve(
77 style {
78 this.background = Some(Paint(color))
79 this.corners = Corners.all(1000.0)
80 }
81 )
83internal fun sliderThumbOptions(
84 theme: Theme,
85 local: SliderStyle,
86 visual: InteractionVisual
87): SurfaceOptions {
88 val density = theme.tokens.typography.body.fontSize / 14.0
89 val amount =
90 if (visual.focusVisible)
91 1.0
92 else
93 lowerBound(visual.hoverProgress ?: 0.0, visual.pressProgress ?: 0.0)
94 val base =
95 styleOverride {
96 this.background(
97 Some(
98 Paint(
99 if (theme.preferences.highContrast)
100 theme.tokens.palette.background.background
101 else
102 Color.White
103 )
104 )
105 )
106 this.border(
107 Some(
108 Border.all(
109 density,
110 if (visual.invalid)
111 theme.tokens.validation.borderColor ?: theme.tokens.validation.color
112 else
113 theme.tokens.palette.ring
114 )
115 )
116 )
117 this.corners(Corners.all(1000.0))
118 }.then(local.thumb)
119 return surfaceOptions {
120 this.style = local.states.apply(base, visual)
121 this.padding = Some(Insets())
122 this.focus =
123 Some(
124 OutlineStyle(
125 componentTint(theme, theme.tokens.palette.ring, 0.5 * amount),
126 componentFocusWidth(theme, 3.0 * density),
127 0.0
128 )
129 )
130 }