Scoop API

scoop:scoop.ui.component:0.1.0

src/calendar/appearance.scoop

Selected production source · src/calendar/appearance.scoop

1package scoop.ui.component
3import scoop.graphics.*
4import scoop.graphics.text.*
5import scoop.ui.*
6import scoop.ui.style.*
7import scoop.ui.style.Theme
9internal class CalendarAppearance(
10 val input: CalendarProps,
11 val theme: Theme,
12 direction: LayoutDirection
13) {
14 val density = theme.tokens.typography.body.fontSize / 14.0
15 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 direction
30 )
31 val ring = metrics.focusOutset
32 val cell =
33 lowerBound(
34 input.options.cellSize * density,
35 lowerBound(metrics.minimumSize.width, metrics.suggestedSize.width)
36 ) +
37 ring *
38 2.0
39 val body =
40 theme.tokens.typography.body.copy {
41 this.color = theme.tokens.palette.background.foreground
42 this.weight = 400
43 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.foreground
50 }
52 private fun makeStyle(selected: Boolean, outside: Boolean, today: Boolean): ButtonStyle {
53 val input = this.input
54 val theme = this.theme
55 val density = this.density
56 val palette = theme.tokens.palette
57 val color =
58 if (selected)
59 palette.primary.foreground
60 else
61 if (outside)
62 palette.muted.foreground
63 else
64 palette.background.foreground
65 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 else
86 if (today)
87 this.background(Some(Paint(palette.muted.background)))
88 }.then(input.dayStyle.surface.style)
89 this.focus = input.dayStyle.surface.focus
90 this.validation = input.dayStyle.surface.validation
91 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.states
109 this.transition = input.dayStyle.transition
110 }
111 return base
112 }
113 fun style(day: CalendarDay): ButtonStyle =
114 if (day.rangeStart || day.rangeEnd || (day.selected && !day.inRange))
115 selected
116 else
117 if (day.data.outside)
118 outside
119 else
120 if (day.today)
121 today
122 else
123 normal
126internal class CalendarAppearanceCache {
127 private var cached: CalendarAppearance? = None
128 private var direction = LayoutDirection.Ltr
129 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 current
140 )
141 return value
142 case None -> {}
143 }
144 val result = CalendarAppearance(input, theme, current)
145 cached = Some(result)
146 direction = current
147 return result
148 }
151internal 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 )