scoop:scoop.ui.component:0.1.0
src/forms/switch_style.scoop
Selected production source · src/forms/switch_style.scoop
1
package scoop.ui.component3
import scoop.graphics.*4
import scoop.surface.AnimationSpec5
import scoop.ui.LayoutDirection6
import scoop.ui.style.*8
public 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.Inherit15
) {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
}24
}26
public fun switchStyle(configure: SwitchStyleBuilder.() -> Unit = {}): SwitchStyle =27
SwitchStyle().copy(configure)29
public class SwitchStyleBuilder internal constructor(initial: SwitchStyle) {30
public var track: SurfaceOptions = initial.track31
public var thumb: StyleOverride = initial.thumb32
public var states: InteractionStyles = initial.states33
public var hitInsets: Insets? = initial.hitInsets34
public var transition: StyleValue<AnimationSpec?> = initial.transition35
public var selectionTransition: StyleValue<AnimationSpec?> = initial.selectionTransition36
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)53
}55
internal fun switchOptions(56
input: SwitchProps,57
theme: Theme,58
visual: InteractionVisual,59
progress: Double60
): SurfaceOptions {61
val colors = theme.tokens.palette62
val density = theme.tokens.typography.body.fontSize / 14.063
val off =64
if (theme.tokens.mode == ThemeMode.Dark)65
componentTint(theme, colors.input, 0.8)66
else67
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
else84
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
}92
}94
internal fun switchThumbStyle(input: SwitchProps, theme: Theme, progress: Double): Style {95
val colors = theme.tokens.palette96
val dark = theme.tokens.mode == ThemeMode.Dark97
val off =98
if (theme.preferences.highContrast)99
colors.background.background100
else101
if (dark)102
colors.background.foreground103
else104
colors.background.background105
val on =106
if (dark || theme.preferences.highContrast)107
colors.primary.foreground108
else109
colors.background.background110
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
)116
}118
internal fun switchThumbBounds(119
inner: Rect,120
preferred: Double,121
progress: Double,122
direction: LayoutDirection123
): Rect {124
val side = upperBound(preferred, upperBound(inner.width, inner.height))125
val position =126
if (direction == LayoutDirection.Rtl)127
1.0 - progress128
else129
progress130
return Rect(131
inner.x + (inner.width - side) * position,132
inner.y + (inner.height - side) / 2.0,133
side,134
side135
)136
}138
internal fun switchHitBounds(bounds: Rect, insets: Insets): Rect =139
if (bounds.width == 0.0 || bounds.height == 0.0)140
bounds141
else142
Rect(143
bounds.x - insets.left,144
bounds.y - insets.top,145
bounds.width + insets.left + insets.right,146
bounds.height + insets.top + insets.bottom147
)149
/** Local surfaces override their base border, so apply the shared focus/invalid priority here. */150
internal fun controlBorder(151
theme: Theme,152
options: SurfaceOptions,153
visual: InteractionVisual,154
base: Color155
): Color {156
if (visual.invalid)157
return (options.validation ?: theme.tokens.validation).borderColor ?: base158
if (visual.enabled && visual.focusVisible)159
return when (options.focus) {160
case Some(val value) -> value.borderColor ?: base161
case None -> theme.tokens.palette.ring162
}163
return base164
}