scoop:scoop.ui.style.material:0.1.0
src/theme.scoop
Selected production source · src/theme.scoop
1
package scoop.ui.style.material3
import scoop.graphics.*4
import scoop.graphics.text.*5
import scoop.surface.AnimationSpec6
import scoop.surface.Easing7
import scoop.time.Duration8
import scoop.ui.style.*10
/** Material Design 3 colors, pill buttons and independent circular marker state layers. */11
public fun materialTheme(12
mode: ThemeMode = ThemeMode.Light,13
palette: ThemePalette? = None,14
configure: ShapeThemeBuilder.() -> Unit = {}15
): ShapeTheme {16
val colors = palette ?: materialPalette(mode)17
val families = ["Roboto", "Arial", "sans-serif"]18
val fonts = themeTypography(19
body = textStyle {20
this.families = families21
this.fontSize = 14.022
this.lineHeight = Some(20.0 / 14.0)23
this.letterSpacing = 0.2524
this.color = colors.background.foreground25
},26
caption = textStyle {27
this.families = families28
this.fontSize = 12.029
this.lineHeight = Some(16.0 / 12.0)30
this.letterSpacing = 0.431
this.color = colors.muted.foreground32
},33
action = textStyle {34
this.families = families35
this.fontSize = 14.036
this.lineHeight = Some(20.0 / 14.0)37
this.color = colors.background.foreground38
this.weight = 50039
this.letterSpacing = 0.140
}41
) {42
input {43
this.fontSize = 16.044
this.lineHeight = Some(24.0 / 16.0)45
this.letterSpacing = 0.546
}47
}48
val keyShadow = colors.shadow.withAlpha(colors.shadow.alpha * 0.3)49
val ambientShadow = colors.shadow.withAlpha(colors.shadow.alpha * 0.15)50
val effects = themeEffects {51
// CSS blur radii become Gaussian sigmas in the shared painter.52
this.raised = [53
BoxShadow(Point(0.0, 1.0), 1.0, 1.0, keyShadow),54
BoxShadow(Point(0.0, 1.0), 1.5, 1.5, ambientShadow)55
]56
this.floating = [57
BoxShadow(Point(0.0, 1.0), 1.0, 1.0, keyShadow),58
BoxShadow(Point(0.0, 2.0), 3.0, 3.0, ambientShadow)59
]60
}61
val tokens = themeTokens(62
palette = colors,63
typography = fonts,64
spacing = ThemeSpacing(4.0, 12.0, 24.0),65
corners = ThemeCorners(4.0, 12.0, 28.0),66
validation = OutlineStyle(67
colors.destructive.background.withAlpha(0.72),68
2.0, 0.0, Some(colors.destructive.background)69
)70
) {71
this.mode = mode72
this.textField = TextFieldTreatment.Outlined73
motion {74
this.hover = materialTween(100L)75
this.press = materialTween(100L)76
this.selection = materialTween(200L)77
}78
this.effects = effects79
}80
val primary = colors.primary.background81
val onPrimary = colors.primary.foreground82
val onSurface = colors.background.foreground83
val disabledContainer = onSurface.withAlpha(onSurface.alpha * 0.12)84
val disabled = colors.disabledText85
val transparent = StateColors.solid(Color.Transparent)86
val onAccent = StateColors(onPrimary, onPrimary, onPrimary, disabled)87
val text = StateColors(onSurface, onSurface, onSurface, disabled)88
val accent = StateColors(89
primary,90
mixThemeColor(primary, onPrimary, 0.08),91
mixThemeColor(primary, onPrimary, 0.12), disabledContainer92
)93
val focus = OutlineStyle(colors.ring, 3.0, 5.0)94
val outlinedAction = surfacePart(95
metrics = SurfaceMetrics(96
Size(64.0, 40.0), Size(64.0, 40.0),97
Insets(23.0, 9.0, 23.0, 9.0), Insets.all(1.0), CornerRadii.all(9999.0), focus.outset98
),99
fill = StateColors(100
Color.Transparent, primary.withAlpha(primary.alpha * 0.08),101
primary.withAlpha(primary.alpha * 0.12), Color.Transparent102
),103
border = StateColors(colors.input, colors.input, colors.input, disabledContainer),104
foreground = StateColors(primary, primary, primary, disabled),105
focus = focus106
)107
val plain = surfacePart(108
metrics = SurfaceMetrics(109
Size(160.0, 56.0), Size(64.0, 56.0),110
Insets.all(15.0), Insets.all(1.0), CornerRadii.all(4.0), 0.0111
),112
fill = transparent,113
border = StateColors(colors.input, onSurface, onSurface, disabledContainer),114
foreground = text,115
focus = OutlineStyle(primary, 2.0, 0.0, Some(primary))116
)117
val action = surfacePart(118
metrics = SurfaceMetrics(119
Size(64.0, 40.0), Size(64.0, 40.0),120
Insets(24.0, 10.0, 24.0, 10.0), Insets.all(0.0), CornerRadii.all(9999.0), focus.outset121
),122
fill = accent, border = transparent, foreground = onAccent, focus = focus123
) { this.hoverShadows = Some(effects.raised) }124
val elevated = surfacePart(125
metrics = SurfaceMetrics(126
Size(120.0, 72.0), Size(0.0, 0.0),127
Insets.all(16.0), Insets.all(0.0), CornerRadii.all(12.0), focus.outset128
),129
fill = StateColors.solid(colors.popover.background),130
border = transparent, foreground = text, focus = focus131
) { this.shadows = effects.floating }132
val markerEdges = StateColors(colors.muted.foreground, onSurface, onSurface, disabled)133
val selected = StateColors(primary, primary, primary, disabled)134
val layer = MarkerStateLayer(135
40.0,136
StateColors(137
Color.Transparent, onSurface.withAlpha(onSurface.alpha * 0.08),138
primary.withAlpha(primary.alpha * 0.12), Color.Transparent139
),140
StateColors(141
Color.Transparent, primary.withAlpha(primary.alpha * 0.08),142
onSurface.withAlpha(onSurface.alpha * 0.12), Color.Transparent143
)144
)145
val checkAppearance = markerAppearance(146
empty = transparent, selected = selected, border = markerEdges,147
// A translucent selected fill must not be darkened again by an overlapping border.148
selectedBorder = transparent,149
mark = StateColors(onPrimary, onPrimary, onPrimary, colors.background.background),150
focus = focus151
) { this.stateLayer = Some(layer) }152
val radioAppearance = markerAppearance(153
empty = transparent, selected = transparent, border = markerEdges,154
selectedBorder = selected, mark = selected, focus = focus155
) { this.stateLayer = Some(layer) }156
val neutral = colors.muted.foreground157
return shapeTheme(158
tokens = tokens,159
surfaces = surfaceParts(160
plain = plain, action = action, outlinedAction = outlinedAction, elevated = elevated161
),162
checkbox = checkboxPart(163
metrics = CheckboxMetrics(164
Size(18.0, 18.0), Size(18.0, 18.0),165
Insets.all(2.0), CornerRadii.all(2.0), 4.0, 2.0, 16.0166
),167
appearance = checkAppearance168
),169
radio = radioPart(170
metrics = RadioMetrics(Size(20.0, 20.0), Size(20.0, 20.0), 2.0, 5.0, 15.0),171
appearance = radioAppearance172
),173
scrollbar = scrollbarPart(174
metrics = ScrollbarMetrics(14.0, 32.0, 1.0, 3.0, 8.0, None),175
track = StateColors(176
onSurface.withAlpha(onSurface.alpha * 0.04),177
onSurface.withAlpha(onSurface.alpha * 0.06),178
onSurface.withAlpha(onSurface.alpha * 0.08), Color.Transparent179
),180
thumb = StateColors(181
neutral.withAlpha(neutral.alpha * 0.38),182
neutral.withAlpha(neutral.alpha * 0.5),183
neutral.withAlpha(neutral.alpha * 0.5), disabled184
),185
button = transparent, arrow = text186
) { this.arrowWidth = 2.0 },187
configure = configure188
)189
}191
private fun materialTween(milliseconds: Long): AnimationSpec? = Some(192
AnimationSpec.Tween(Duration.milliseconds(milliseconds), Easing.cubicBezier(0.2, 0.0, 0.0, 1.0))193
)