Scoop API

scoop:scoop.ui.component:0.1.0

src/actions/appearance.scoop

Selected production source · src/actions/appearance.scoop

1package scoop.ui.component
3import scoop.graphics.*
4import scoop.ui.style.*
6private fun outlinedButton(shape: ActionShape, variant: ButtonVariant): Boolean = when (shape) {
7 case Button(_) -> variant == ButtonVariant.Outline
8 else -> false
9}
11internal fun actionSurfaceKind(
12 shape: ActionShape,
13 variant: ButtonVariant,
14 kind: SurfaceKind
15): SurfaceKind = if (outlinedButton(shape, variant)) SurfaceKind.OutlinedAction else kind
17internal fun actionTextColor(
18 theme: Theme,
19 shape: ActionShape,
20 variant: ButtonVariant,
21 visual: SurfaceVisual
22): Color {
23 when (shape) {
24 case Tab(val variant, val selected) ->
25 return tabTextColor(
26 theme,
27 variant,
28 selected,
29 visual.interaction
30 )
32 case Toggle(_, _, _) -> {
33 if (theme.preferences.highContrast)
34 return actionContrastPair(theme, shape, variant, visual.interaction).foreground
35 }
37 else -> {}
38 }
39 val colors = theme.tokens.palette
40 if (variant == ButtonVariant.Default) return theme.surfaceTextColor(visual)
41 if (outlinedButton(shape, variant))
42 return theme.surfaceTextColor(SurfaceVisual(SurfaceKind.OutlinedAction, visual.interaction))
43 if (theme.preferences.highContrast)
44 return actionContrastPair(theme, shape, variant, visual.interaction).foreground
45 if (!visual.interaction.enabled && theme.tokens.disabledOpacity >= 1.0)
46 return colors.disabledText
47 return when (variant) {
48 case Default -> theme.surfaceTextColor(visual)
49 case Secondary -> colors.secondary.foreground
50 case Destructive -> colors.destructive.background
51 case Link -> colors.primary.background
52 else ->
53 if (shape ==
54 ActionShape.Badge &&
55 visual.interaction.enabled &&
56 visual.interaction.hovered
57 )
58 colors.muted.foreground
59 else
60 colors.background.foreground
61 }
64internal fun actionOptions(
65 theme: Theme,
66 shape: ActionShape,
67 variant: ButtonVariant,
68 style: ButtonStyle,
69 visual: InteractionVisual
70): SurfaceOptions {
71 val size = when (shape) {
72 case Tab(val tabVariant, val selected) ->
73 return tabOptions(
74 theme,
75 tabVariant,
76 selected,
77 style,
78 visual
79 )
80 case Button(val size) -> Some(size)
81 case Badge -> None
82 case Toggle(val toggleVariant, val size, val selected) ->
83 return toggleOptions(
84 theme,
85 toggleVariant,
86 size,
87 selected,
88 style,
89 visual
90 )
91 }
92 val base = theme.surfaceMetrics(SurfaceVisual(actionSurfaceKind(shape, variant, style.kind)))
93 val colors = theme.tokens.palette
94 val dark = theme.tokens.mode == ThemeMode.Dark
95 val badge = shape == ActionShape.Badge
96 val density = theme.tokens.typography.action.fontSize / 14.0
97 val preset = actionSizeOptions(theme, size, base, density)
98 var normal = Color.Transparent
99 var hover = Color.Transparent
100 var border = Color.Transparent
101 when (variant) {
102 case Default -> {}
104 case Outline -> {
105 hover = colors.muted.background
106 border = colors.border
107 }
109 case Secondary -> {
110 normal = colors.secondary.background
111 hover = if (badge)
112 normal.withAlpha(normal.alpha * 0.8)
113 else
114 secondaryHover(normal, colors.background.foreground)
115 }
117 case Ghost ->
118 hover = colors
119 .muted
120 .background
121 .withAlpha(
122 colors.muted.background.alpha * if (dark) 0.5 else 1.0
123 )
125 case Destructive -> {
126 normal = colors
127 .destructive
128 .background
129 .withAlpha(
130 if (dark) 0.2 else 0.1
131 )
132 hover = colors
133 .destructive
134 .background
135 .withAlpha(
136 if (dark) 0.3 else 0.2
137 )
138 }
140 case Link -> {}
141 }
142 val destructive = if (variant == ButtonVariant.Destructive)
143 Some(
144 OutlineStyle(
145 componentTint(
146 theme,
147 colors.destructive.background.withAlpha(1.0),
148 if (dark) 0.4 else 0.2
149 ),
150 theme.tokens.validation.width,
151 theme.tokens.validation.outset,
152 Some(componentTint(theme, colors.destructive.background.withAlpha(1.0), 0.4))
153 )
154 )
155 else
156 None
157 val focus: OutlineStyle? = if (style.surface.focus == None) destructive else style.surface.focus
158 if (visual.invalid)
159 border = (style.surface.validation ?: theme.tokens.validation).borderColor ?: border
160 else if (visual.enabled && visual.focusVisible)
161 border = when (focus) {
162 case Some(val value) -> value.borderColor ?: border
163 case None -> colors.ring
164 }
165 val disabled = if (theme.tokens.disabledOpacity >= 1.0)
166 mixThemeColor(normal, colors.muted.background, 0.5)
167 else
168 normal
169 val pressed = when (variant) {
170 case Default -> normal
171 case Link -> normal
172 case Destructive ->
173 colors
174 .destructive
175 .background
176 .withAlpha(
177 if (dark) 0.4 else 0.3
178 )
179 else -> mixThemeColor(hover, colors.background.foreground, 0.08)
180 }
181 val sampled = StateColors(normal, hover, pressed, disabled).resolve(visual)
182 val active = visual.enabled && (visual.hovered || visual.pressed)
183 val contrastFill = when (variant) {
184 case Link -> false
185 case Ghost -> active
186 case Outline -> !badge || active
187 else -> true
188 }
189 val fill = if (theme.preferences.highContrast && variant != ButtonVariant.Default)
190 if (contrastFill)
191 actionContrastPair(theme, shape, variant, visual).background
192 else
193 Color.Transparent
194 else
195 componentBackground(theme, sampled)
196 val resolvedBorder = border
197 val variantStyle = styleOverride {
198 if (variant != ButtonVariant.Default && !outlinedButton(shape, variant)) {
199 this.background(Some(Paint(fill)))
200 this
201 .border(
202 Some(
203 Border
204 .Physical(
205 BorderEdges(
206 BorderEdge(base.borderInsets.left, resolvedBorder),
207 BorderEdge(base.borderInsets.top, resolvedBorder),
208 BorderEdge(base.borderInsets.right, resolvedBorder),
209 BorderEdge(base.borderInsets.bottom, resolvedBorder)
210 )
211 )
212 )
213 )
214 }
215 }
216 val resolved = style
217 .states
218 .apply(preset.style.then(variantStyle).then(style.surface.style), visual)
219 return style
220 .surface
221 .copy {
222 if (this.suggestedSize == None) this.suggestedSize = preset.suggestedSize
223 if (this.padding == None) this.padding = preset.padding
224 this.style = resolved
225 this.focus = focus
226 }
229private fun actionSizeOptions(
230 theme: Theme,
231 size: ButtonSize?,
232 base: SurfaceMetrics,
233 density: Double
234): SurfaceOptions = surfaceOptions {
235 when (size) {
236 case None -> {
237 this.suggestedSize = Some(Size(0.0, 20.0 * density))
238 this.padding = Some(Insets(8.0 * density, density, 8.0 * density, density))
239 style { this.corners(Corners.all(1000.0)) }
240 }
242 case Some(val size) -> {
243 val ratio = when (size) {
244 case Xs -> 0.75
245 case IconXs -> 0.75
246 case Sm -> 0.875
247 case IconSm -> 0.875
248 case Lg -> 1.125
249 case IconLg -> 1.125
250 else -> 1.0
251 }
252 val height = base.suggestedSize.height * ratio
253 val paddingRatio = if (size == ButtonSize.Sm) 0.75 else ratio
254 this.suggestedSize = Some(
255 Size(
256 if (isIconAction(ActionShape.Button(size)))
257 height
258 else
259 base.suggestedSize.width,
260 height
261 )
262 )
263 if (isIconAction(ActionShape.Button(size)))
264 this.padding = Some(Insets.all(base.padding.top * paddingRatio))
265 else if (size == ButtonSize.Xs)
266 this.padding = Some(
267 Insets(
268 base.padding.left * 0.8,
269 base.padding.top * paddingRatio,
270 base.padding.right * 0.8,
271 base.padding.bottom * paddingRatio
272 )
273 )
274 else
275 this.padding = Some(
276 Insets(
277 base.padding.left,
278 base.padding.top * paddingRatio,
279 base.padding.right,
280 base.padding.bottom * paddingRatio
281 )
282 )
283 if (ratio < 1.0) style { this.corners(Corners.all(theme.tokens.corners.medium)) }
284 }
285 }