scoop:scoop.ui.component:0.1.0
src/actions/appearance.scoop
Selected production source · src/actions/appearance.scoop
1
package scoop.ui.component3
import scoop.graphics.*4
import scoop.ui.style.*6
private fun outlinedButton(shape: ActionShape, variant: ButtonVariant): Boolean = when (shape) {7
case Button(_) -> variant == ButtonVariant.Outline8
else -> false9
}11
internal fun actionSurfaceKind(12
shape: ActionShape,13
variant: ButtonVariant,14
kind: SurfaceKind15
): SurfaceKind = if (outlinedButton(shape, variant)) SurfaceKind.OutlinedAction else kind17
internal fun actionTextColor(18
theme: Theme,19
shape: ActionShape,20
variant: ButtonVariant,21
visual: SurfaceVisual22
): Color {23
when (shape) {24
case Tab(val variant, val selected) ->25
return tabTextColor(26
theme,27
variant,28
selected,29
visual.interaction30
)32
case Toggle(_, _, _) -> {33
if (theme.preferences.highContrast)34
return actionContrastPair(theme, shape, variant, visual.interaction).foreground35
}37
else -> {}38
}39
val colors = theme.tokens.palette40
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).foreground45
if (!visual.interaction.enabled && theme.tokens.disabledOpacity >= 1.0)46
return colors.disabledText47
return when (variant) {48
case Default -> theme.surfaceTextColor(visual)49
case Secondary -> colors.secondary.foreground50
case Destructive -> colors.destructive.background51
case Link -> colors.primary.background52
else ->53
if (shape ==54
ActionShape.Badge &&55
visual.interaction.enabled &&56
visual.interaction.hovered57
)58
colors.muted.foreground59
else60
colors.background.foreground61
}62
}64
internal fun actionOptions(65
theme: Theme,66
shape: ActionShape,67
variant: ButtonVariant,68
style: ButtonStyle,69
visual: InteractionVisual70
): 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
visual79
)80
case Button(val size) -> Some(size)81
case Badge -> None82
case Toggle(val toggleVariant, val size, val selected) ->83
return toggleOptions(84
theme,85
toggleVariant,86
size,87
selected,88
style,89
visual90
)91
}92
val base = theme.surfaceMetrics(SurfaceVisual(actionSurfaceKind(shape, variant, style.kind)))93
val colors = theme.tokens.palette94
val dark = theme.tokens.mode == ThemeMode.Dark95
val badge = shape == ActionShape.Badge96
val density = theme.tokens.typography.action.fontSize / 14.097
val preset = actionSizeOptions(theme, size, base, density)98
var normal = Color.Transparent99
var hover = Color.Transparent100
var border = Color.Transparent101
when (variant) {102
case Default -> {}104
case Outline -> {105
hover = colors.muted.background106
border = colors.border107
}109
case Secondary -> {110
normal = colors.secondary.background111
hover = if (badge)112
normal.withAlpha(normal.alpha * 0.8)113
else114
secondaryHover(normal, colors.background.foreground)115
}117
case Ghost ->118
hover = colors119
.muted120
.background121
.withAlpha(122
colors.muted.background.alpha * if (dark) 0.5 else 1.0123
)125
case Destructive -> {126
normal = colors127
.destructive128
.background129
.withAlpha(130
if (dark) 0.2 else 0.1131
)132
hover = colors133
.destructive134
.background135
.withAlpha(136
if (dark) 0.3 else 0.2137
)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.2149
),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
else156
None157
val focus: OutlineStyle? = if (style.surface.focus == None) destructive else style.surface.focus158
if (visual.invalid)159
border = (style.surface.validation ?: theme.tokens.validation).borderColor ?: border160
else if (visual.enabled && visual.focusVisible)161
border = when (focus) {162
case Some(val value) -> value.borderColor ?: border163
case None -> colors.ring164
}165
val disabled = if (theme.tokens.disabledOpacity >= 1.0)166
mixThemeColor(normal, colors.muted.background, 0.5)167
else168
normal169
val pressed = when (variant) {170
case Default -> normal171
case Link -> normal172
case Destructive ->173
colors174
.destructive175
.background176
.withAlpha(177
if (dark) 0.4 else 0.3178
)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 -> false185
case Ghost -> active186
case Outline -> !badge || active187
else -> true188
}189
val fill = if (theme.preferences.highContrast && variant != ButtonVariant.Default)190
if (contrastFill)191
actionContrastPair(theme, shape, variant, visual).background192
else193
Color.Transparent194
else195
componentBackground(theme, sampled)196
val resolvedBorder = border197
val variantStyle = styleOverride {198
if (variant != ButtonVariant.Default && !outlinedButton(shape, variant)) {199
this.background(Some(Paint(fill)))200
this201
.border(202
Some(203
Border204
.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 = style217
.states218
.apply(preset.style.then(variantStyle).then(style.surface.style), visual)219
return style220
.surface221
.copy {222
if (this.suggestedSize == None) this.suggestedSize = preset.suggestedSize223
if (this.padding == None) this.padding = preset.padding224
this.style = resolved225
this.focus = focus226
}227
}229
private fun actionSizeOptions(230
theme: Theme,231
size: ButtonSize?,232
base: SurfaceMetrics,233
density: Double234
): 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.75245
case IconXs -> 0.75246
case Sm -> 0.875247
case IconSm -> 0.875248
case Lg -> 1.125249
case IconLg -> 1.125250
else -> 1.0251
}252
val height = base.suggestedSize.height * ratio253
val paddingRatio = if (size == ButtonSize.Sm) 0.75 else ratio254
this.suggestedSize = Some(255
Size(256
if (isIconAction(ActionShape.Button(size)))257
height258
else259
base.suggestedSize.width,260
height261
)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 * paddingRatio272
)273
)274
else275
this.padding = Some(276
Insets(277
base.padding.left,278
base.padding.top * paddingRatio,279
base.padding.right,280
base.padding.bottom * paddingRatio281
)282
)283
if (ratio < 1.0) style { this.corners(Corners.all(theme.tokens.corners.medium)) }284
}285
}286
}