Scoop API

scoop:scoop.ui.component:0.1.0

src/forms/switch_style.scoop

Selected production source · src/forms/switch_style.scoop

1package scoop.ui.component
3import scoop.graphics.*
4import scoop.surface.AnimationSpec
5import scoop.ui.LayoutDirection
6import scoop.ui.style.*
8public class SwitchStyle internal constructor(
9 public val track: SurfaceOptions = surfaceOptions(),
10 public val thumb: StyleOverride = styleOverride(),
11 public val states: InteractionStyles = interactionStyles(),
12 public val hitInsets: Insets? = None,
13 public val transition: StyleValue<AnimationSpec?> = StyleValue.Inherit,
14 public val selectionTransition: StyleValue<AnimationSpec?> = StyleValue.Inherit
15) {
16 init {
17 hitInsets?.totals()
18 }
19 public fun copy(configure: SwitchStyleBuilder.() -> Unit = {}): SwitchStyle {
20 val builder = SwitchStyleBuilder(this)
21 builder.configure()
22 return builder.snapshot()
23 }
26public fun switchStyle(configure: SwitchStyleBuilder.() -> Unit = {}): SwitchStyle =
27 SwitchStyle().copy(configure)
29public class SwitchStyleBuilder internal constructor(initial: SwitchStyle) {
30 public var track: SurfaceOptions = initial.track
31 public var thumb: StyleOverride = initial.thumb
32 public var states: InteractionStyles = initial.states
33 public var hitInsets: Insets? = initial.hitInsets
34 public var transition: StyleValue<AnimationSpec?> = initial.transition
35 public var selectionTransition: StyleValue<AnimationSpec?> = initial.selectionTransition
36 public fun track(configure: SurfaceOptionsBuilder.() -> Unit): Unit {
37 track = track.copy(configure)
38 }
39 public fun thumb(configure: StyleOverrideBuilder.() -> Unit): Unit {
40 thumb = thumb.copy(configure)
41 }
42 public fun states(configure: InteractionStylesBuilder.() -> Unit): Unit {
43 states = states.copy(configure)
44 }
45 public fun transition(value: AnimationSpec?): Unit {
46 transition = StyleValue.Set(value)
47 }
48 public fun selectionTransition(value: AnimationSpec?): Unit {
49 selectionTransition = StyleValue.Set(value)
50 }
51 internal fun snapshot(): SwitchStyle =
52 SwitchStyle(track, thumb, states, hitInsets, transition, selectionTransition)
55internal fun switchOptions(
56 input: SwitchProps,
57 theme: Theme,
58 visual: InteractionVisual,
59 progress: Double
60): SurfaceOptions {
61 val colors = theme.tokens.palette
62 val density = theme.tokens.typography.body.fontSize / 14.0
63 val off =
64 if (theme.tokens.mode == ThemeMode.Dark)
65 componentTint(theme, colors.input, 0.8)
66 else
67 componentBackground(theme, colors.input)
68 val fill = mixThemeColor(off, colors.primary.background, progress)
69 val border = controlBorder(theme, input.style.track, visual, Color.Transparent)
70 val local =
71 styleOverride {
72 this.background(Some(Paint(fill)))
73 this.border(Some(Border.all(density, border)))
74 this.corners(Corners.all(1000.0))
75 this.boxShadows([])
76 }.then(input.style.track.style)
77 return input.style.track.copy {
78 if (this.suggestedSize == None)
79 this.suggestedSize =
80 Some(
81 if (input.size == SwitchSize.Sm)
82 Size(24.0 * density, 14.0 * density)
83 else
84 Size(32.0 * density, 18.4 * density)
85 )
86 if (this.minimumSize == None)
87 this.minimumSize = Some(Size(0.0, 0.0))
88 if (this.padding == None)
89 this.padding = Some(Insets())
90 this.style = input.style.states.apply(local, visual)
91 }
94internal fun switchThumbStyle(input: SwitchProps, theme: Theme, progress: Double): Style {
95 val colors = theme.tokens.palette
96 val dark = theme.tokens.mode == ThemeMode.Dark
97 val off =
98 if (theme.preferences.highContrast)
99 colors.background.background
100 else
101 if (dark)
102 colors.background.foreground
103 else
104 colors.background.background
105 val on =
106 if (dark || theme.preferences.highContrast)
107 colors.primary.foreground
108 else
109 colors.background.background
110 return input.style.thumb.resolve(
111 style {
112 this.background = Some(Paint(mixThemeColor(off, on, progress)))
113 this.corners = Corners.all(1000.0)
114 }
115 )
118internal fun switchThumbBounds(
119 inner: Rect,
120 preferred: Double,
121 progress: Double,
122 direction: LayoutDirection
123): Rect {
124 val side = upperBound(preferred, upperBound(inner.width, inner.height))
125 val position =
126 if (direction == LayoutDirection.Rtl)
127 1.0 - progress
128 else
129 progress
130 return Rect(
131 inner.x + (inner.width - side) * position,
132 inner.y + (inner.height - side) / 2.0,
133 side,
134 side
135 )
138internal fun switchHitBounds(bounds: Rect, insets: Insets): Rect =
139 if (bounds.width == 0.0 || bounds.height == 0.0)
140 bounds
141 else
142 Rect(
143 bounds.x - insets.left,
144 bounds.y - insets.top,
145 bounds.width + insets.left + insets.right,
146 bounds.height + insets.top + insets.bottom
147 )
149/** Local surfaces override their base border, so apply the shared focus/invalid priority here. */
150internal fun controlBorder(
151 theme: Theme,
152 options: SurfaceOptions,
153 visual: InteractionVisual,
154 base: Color
155): Color {
156 if (visual.invalid)
157 return (options.validation ?: theme.tokens.validation).borderColor ?: base
158 if (visual.enabled && visual.focusVisible)
159 return when (options.focus) {
160 case Some(val value) -> value.borderColor ?: base
161 case None -> theme.tokens.palette.ring
162 }
163 return base