scoop:scoop.ui.component:0.1.0
src/calendar/appearance.scoop
Selected production source · src/calendar/appearance.scoop
1
package scoop.ui.component3
import scoop.graphics.*4
import scoop.graphics.text.*5
import scoop.ui.*6
import scoop.ui.style.*7
import scoop.ui.style.Theme9
internal class CalendarAppearance(10
val input: CalendarProps,11
val theme: Theme,12
direction: LayoutDirection13
) {14
val density = theme.tokens.typography.body.fontSize / 14.015
val normal = makeStyle(false, false, false)16
val selected = makeStyle(true, false, false)17
val outside = makeStyle(false, true, false)18
val today = makeStyle(false, false, true)19
val metrics =20
theme.surfaceMetrics(21
SurfaceVisual(SurfaceKind.Action),22
actionOptions(23
theme,24
ActionShape.Button(ButtonSize.Icon),25
ButtonVariant.Ghost,26
normal,27
InteractionVisual()28
),29
direction30
)31
val ring = metrics.focusOutset32
val cell =33
lowerBound(34
input.options.cellSize * density,35
lowerBound(metrics.minimumSize.width, metrics.suggestedSize.width)36
) +37
ring *38
2.039
val body =40
theme.tokens.typography.body.copy {41
this.color = theme.tokens.palette.background.foreground42
this.weight = 40043
this.lineHeight = Some(20.0 / 14.0)44
}45
val caption = body.copy { this.weight = 500 }46
val weekday =47
body.copy {48
this.fontSize = 12.8 * this @CalendarAppearance.density;49
this.color = theme.tokens.palette.muted.foreground50
}52
private fun makeStyle(selected: Boolean, outside: Boolean, today: Boolean): ButtonStyle {53
val input = this.input54
val theme = this.theme55
val density = this.density56
val palette = theme.tokens.palette57
val color =58
if (selected)59
palette.primary.foreground60
else61
if (outside)62
palette.muted.foreground63
else64
palette.background.foreground65
val base =66
buttonStyle {67
this.text {68
this.color = Some(color);69
this.weight = Some(400)70
}71
this.surface {72
this.suggestedSize =73
Some(74
Size(input.options.cellSize * density, input.options.cellSize * density)75
)76
this.minimumSize = Some(Size(0.0, 0.0))77
this.padding = Some(Insets.all(density))78
this.style =79
styleOverride {80
this.border(None)81
this.boxShadows([])82
this.corners(Corners.all(theme.tokens.corners.medium))83
if (selected)84
this.background(Some(Paint(palette.primary.background)))85
else86
if (today)87
this.background(Some(Paint(palette.muted.background)))88
}.then(input.dayStyle.surface.style)89
this.focus = input.dayStyle.surface.focus90
this.validation = input.dayStyle.surface.validation91
when (input.dayStyle.surface.padding) {92
case Some(val value) -> this.padding = Some(value);93
case None -> {}94
}95
when (input.dayStyle.surface.suggestedSize) {96
case Some(val value) -> this.suggestedSize = Some(value);97
case None -> {}98
}99
when (input.dayStyle.surface.minimumSize) {100
case Some(val value) -> this.minimumSize = Some(value);101
case None -> {}102
}103
}104
when (input.dayStyle.text) {105
case Some(val value) -> this.text = Some(value);106
case None -> {}107
}108
this.states = input.dayStyle.states109
this.transition = input.dayStyle.transition110
}111
return base112
}113
fun style(day: CalendarDay): ButtonStyle =114
if (day.rangeStart || day.rangeEnd || (day.selected && !day.inRange))115
selected116
else117
if (day.data.outside)118
outside119
else120
if (day.today)121
today122
else123
normal124
}126
internal class CalendarAppearanceCache {127
private var cached: CalendarAppearance? = None128
private var direction = LayoutDirection.Ltr129
fun resolve(input: CalendarProps, theme: Theme, current: LayoutDirection): CalendarAppearance {130
when (cached) {131
case Some(val value) -> if (132
value.theme ===133
theme &&134
value.input.dayStyle ===135
input.dayStyle &&136
value.input.options.cellSize ==137
input.options.cellSize &&138
direction ==139
current140
)141
return value142
case None -> {}143
}144
val result = CalendarAppearance(input, theme, current)145
cached = Some(result)146
direction = current147
return result148
}149
}151
internal fun calendarLabel(text: String, style: TextStyle, fonts: FontCollection?): View =152
component(153
LabelProps(154
LabelText.Plain(text, None),155
fonts,156
Some(paragraphStyle { this.alignment = TextAlignment.Center }),157
baseStyle = Some(style)158
),159
{ props, scope -> Label(props, scope) }160
)