Scoop API

scoop:scoop.ui.style.material:0.1.0

src/theme.scoop

Selected production source · src/theme.scoop

1package scoop.ui.style.material
3import scoop.graphics.*
4import scoop.graphics.text.*
5import scoop.surface.AnimationSpec
6import scoop.surface.Easing
7import scoop.time.Duration
8import scoop.ui.style.*
10/** Material Design 3 colors, pill buttons and independent circular marker state layers. */
11public 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 = families
21 this.fontSize = 14.0
22 this.lineHeight = Some(20.0 / 14.0)
23 this.letterSpacing = 0.25
24 this.color = colors.background.foreground
25 },
26 caption = textStyle {
27 this.families = families
28 this.fontSize = 12.0
29 this.lineHeight = Some(16.0 / 12.0)
30 this.letterSpacing = 0.4
31 this.color = colors.muted.foreground
32 },
33 action = textStyle {
34 this.families = families
35 this.fontSize = 14.0
36 this.lineHeight = Some(20.0 / 14.0)
37 this.color = colors.background.foreground
38 this.weight = 500
39 this.letterSpacing = 0.1
40 }
41 ) {
42 input {
43 this.fontSize = 16.0
44 this.lineHeight = Some(24.0 / 16.0)
45 this.letterSpacing = 0.5
46 }
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 = mode
72 this.textField = TextFieldTreatment.Outlined
73 motion {
74 this.hover = materialTween(100L)
75 this.press = materialTween(100L)
76 this.selection = materialTween(200L)
77 }
78 this.effects = effects
79 }
80 val primary = colors.primary.background
81 val onPrimary = colors.primary.foreground
82 val onSurface = colors.background.foreground
83 val disabledContainer = onSurface.withAlpha(onSurface.alpha * 0.12)
84 val disabled = colors.disabledText
85 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), disabledContainer
92 )
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.outset
98 ),
99 fill = StateColors(
100 Color.Transparent, primary.withAlpha(primary.alpha * 0.08),
101 primary.withAlpha(primary.alpha * 0.12), Color.Transparent
102 ),
103 border = StateColors(colors.input, colors.input, colors.input, disabledContainer),
104 foreground = StateColors(primary, primary, primary, disabled),
105 focus = focus
106 )
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.0
111 ),
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.outset
121 ),
122 fill = accent, border = transparent, foreground = onAccent, focus = focus
123 ) { 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.outset
128 ),
129 fill = StateColors.solid(colors.popover.background),
130 border = transparent, foreground = text, focus = focus
131 ) { 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.Transparent
139 ),
140 StateColors(
141 Color.Transparent, primary.withAlpha(primary.alpha * 0.08),
142 onSurface.withAlpha(onSurface.alpha * 0.12), Color.Transparent
143 )
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 = focus
151 ) { this.stateLayer = Some(layer) }
152 val radioAppearance = markerAppearance(
153 empty = transparent, selected = transparent, border = markerEdges,
154 selectedBorder = selected, mark = selected, focus = focus
155 ) { this.stateLayer = Some(layer) }
156 val neutral = colors.muted.foreground
157 return shapeTheme(
158 tokens = tokens,
159 surfaces = surfaceParts(
160 plain = plain, action = action, outlinedAction = outlinedAction, elevated = elevated
161 ),
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.0
166 ),
167 appearance = checkAppearance
168 ),
169 radio = radioPart(
170 metrics = RadioMetrics(Size(20.0, 20.0), Size(20.0, 20.0), 2.0, 5.0, 15.0),
171 appearance = radioAppearance
172 ),
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.Transparent
179 ),
180 thumb = StateColors(
181 neutral.withAlpha(neutral.alpha * 0.38),
182 neutral.withAlpha(neutral.alpha * 0.5),
183 neutral.withAlpha(neutral.alpha * 0.5), disabled
184 ),
185 button = transparent, arrow = text
186 ) { this.arrowWidth = 2.0 },
187 configure = configure
188 )
191private fun materialTween(milliseconds: Long): AnimationSpec? = Some(
192 AnimationSpec.Tween(Duration.milliseconds(milliseconds), Easing.cubicBezier(0.2, 0.0, 0.0, 1.0))