Scoop API

scoop:scoop.ui.component:0.1.0

src/date_picker/component.scoop

Selected production source · src/date_picker/component.scoop

1package scoop.ui.component
3import scoop.graphics.*
4import scoop.graphics.text.*
5import scoop.ui.*
6import scoop.ui.style.*
7import scoop.window.accessibility.*
8import scoop.ui.component.column as dateColumn
9import scoop.ui.component.row as dateRow
10import scoop.ui.component.button as dateButton
12public class DatePicker public constructor(
13 private val props: Props<DatePickerProps>,
14 scope: ComponentScope
15): Component(scope) {
16 private val control = DatePickerControl(props, scope)
17 private val themes = ThemeAccess(scope)
18 public override fun onMount() {
19 themes.mount(props.value.theme);
20 control.mount()
21 }
22 public override fun render(): View {
23 val control = this.control
24 val input = props.value
25 val theme = themes.read(input.theme)
26 val selected = calendarRead(input.selection)
27 val open = control.open.value
28 val popupSemantics =
29 input.popupSemantics.copy {
30 reference = Some(input.popupSemantics.reference ?: control.popupReference)
31 }
32 val semantics =
33 input.semantics.copy {
34 relations = semanticControls(relations, (popupSemantics.reference?.id ?: None))
35 }
36 val d = theme.tokens.typography.body.fontSize / 14.0
37 val focusVisual = InteractionVisual(focusVisible = true)
38 val buttonRing =
39 theme.surfaceMetrics(
40 SurfaceVisual(input.buttonStyle.kind, focusVisual),
41 actionOptions(
42 theme,
43 ActionShape.Button(ButtonSize.Icon),
44 ButtonVariant.Outline,
45 input.buttonStyle,
46 focusVisual
47 ),
48 scope.layoutDirection
49 ).focusOutset
50 val inputRing =
51 when (input.draft) {
52 case Some(_) -> theme.surfaceMetrics(
53 SurfaceVisual(SurfaceKind.Plain, focusVisual),
54 input.inputStyle.options(focusVisual),
55 scope.layoutDirection
56 ).focusOutset
57 case None -> 0.0
58 }
59 val triggerRing = lowerBound(buttonRing, inputRing)
60 val calendarProps =
61 CalendarProps(
62 control.selection(),
63 calendarFirst(selected) ?: (input.calendar.today ?: input.initialPage),
64 input.calendar,
65 enabled = input.mutable,
66 handle = Some(control.calendar),
67 fonts = input.fonts,
68 theme = input.theme,
69 day = input.day
70 )
71 val appearance = CalendarAppearance(calendarProps, theme, scope.layoutDirection)
72 val columns =
73 if (input.calendar.weekNumbers == CalendarWeekNumbers.None)
74 7.0
75 else
76 8.0
77 val width =
78 (columns * appearance.cell + 16.0 * d) *
79 input.calendar.monthCount.toDouble() +
80 input.calendar.monthGap *
81 d *
82 (input.calendar.monthCount - 1L).toDouble() +
83 20.0 *
84 d
85 val popupContent =
86 dateColumn {
87 if (input.presets.size > 0L)
88 this.add(
89 dateColumn {
90 for (index in 0L ..< input.presets.size) {
91 val preset = input.presets[index]
92 this.add(
93 dateButton(
94 preset.label,
95 {
96 control.acceptValue(preset.value);
97 Unit
98 }
99 ) {
100 this.variant = ButtonVariant.Ghost
101 this.enabled =
102 input.mutable &&
103 calendarValueAllowed(
104 preset.value,
105 input.calendar.rules
106 )
107 this.fonts = input.fonts
108 this.theme = input.theme
109 this.key = ViewKey.Number(index)
110 }
111 )
112 }
113 this.spacing = 2.0 * d
114 this.alignment = CrossAlignment.Stretch
115 }
116 )
117 this.add(
118 component(
119 calendarProps,
120 { props, scope -> Calendar(props, scope) },
121 ViewKey.Text("calendar")
122 )
123 )
124 when (input.footer) {
125 case Some(val content) -> this.add(content);
126 case None -> {}
127 }
128 this.spacing = 8.0 * d
129 this.alignment = CrossAlignment.Stretch
130 }
131 val popup =
132 component(
133 PopoverProps(
134 control.open,
135 popupContent,
136 anchor = Some(control.anchor),
137 width = width / d,
138 alignment = OverlayAlignment.Start,
139 mode = input.mode,
140 focus =
141 OverlayFocusOptions(autoFocus = false, restoreTarget = Some(control.focus)),
142 handle = Some(control.layer),
143 enabled = input.mutable,
144 transition = input.transition,
145 onCloseRequest = { reason -> control.closeRequest(reason) },
146 onClosed = { reason -> control.closed(reason) },
147 fonts = input.fonts,
148 theme = input.theme,
149 style = input.popupStyle,
150 semantics = popupSemantics
151 ),
152 { props, scope -> Popover(props, scope) }
153 )
154 val trigger =
155 when (input.draft) {
156 case None -> {
157 val text = control.text()
158 popupSemanticButton(
159 ButtonProps(
160 ButtonContent.Resolved {
161 style -> dateRow {
162 this.add(dateCalendarIcon(style.iconSize, style.text.color))
163 this.add(
164 calendarLabel(
165 if (text.byteLength == 0L)
166 input.placeholder
167 else
168 text,
169 style.text,
170 input.fonts
171 )
172 )
173 this.spacing = style.spacing
174 this.alignment = CrossAlignment.Center
175 }
176 },
177 { control.toggle() },
178 input.mutable,
179 input.buttonStyle,
180 input.theme,
181 input.focus.copy { this.handle = Some(control.focus) },
182 input.invalid,
183 ButtonVariant.Outline,
184 labelTarget = input.labelTarget,
185 semantics = semantics
186 ),
187 open,
188 { next -> control.expand(next) }
189 )
190 }
191 case Some(val draft) -> dateColumn {
192 this.add(
193 dateRow {
194 this.item(
195 component(
196 InputProps(
197 draft.editor,
198 input.placeholder,
199 input.enabled,
200 input.readOnly,
201 input.invalid || draft.error != None,
202 fonts = input.fonts,
203 style = input.inputStyle,
204 theme = input.theme,
205 focus =
206 input.focus.copy { this.handle = Some(control.focus) },
207 onEdited = { _ -> control.edited() },
208 onSubmit =
209 {
210 _ ->
211 control.commit();
212 Unit
213 },
214 labelTarget = input.labelTarget,
215 semantics =
216 semantics.copy {
217 error =
218 when (val supplied = input.semantics.error) {
219 case Some(_) -> supplied
220 case None -> draft.error
221 }
222 },
223 expansion =
224 Some(
225 TextFieldExpansion(
226 open,
227 { next -> control.expand(next) }
228 )
229 )
230 ),
231 { props, scope -> Input(props, scope) },
232 ViewKey.Text("input")
233 )
234 ) { this.grow = 1.0 }
235 this.item(
236 popupSemanticButton(
237 ButtonProps(
238 ButtonContent.Resolved {
239 style -> dateCalendarIcon(
240 style.iconSize,
241 style.text.color
242 )
243 },
244 { control.toggle() },
245 enabled = input.mutable,
246 variant = ButtonVariant.Outline,
247 size = ButtonSize.Icon,
248 style = input.buttonStyle,
249 theme = input.theme,
250 semantics =
251 input.calendarButtonSemantics.copy {
252 relations =
253 semanticControls(
254 relations,
255 (popupSemantics.reference?.id ?: None)
256 )
257 }
258 ),
259 open,
260 { next -> control.expand(next) }
261 )
262 ) { this.shrink = 0.0 }
263 this.spacing = lowerBound(6.0 * d, buttonRing + inputRing)
264 this.alignment = CrossAlignment.Center
265 }
266 )
267 when (draft.error) {
268 case Some(val error) -> this.add(
269 calendarLabel(
270 error,
271 theme.tokens.typography.body.copy {
272 this.color = theme.tokens.palette.destructive.background
273 },
274 input.fonts
275 )
276 )
277 case None -> {}
278 }
279 this.spacing = lowerBound(6.0 * d, inputRing)
280 this.alignment = CrossAlignment.Stretch
281 }
282 }
283 return element(
284 DatePickerRootInput(control, selected, open),
285 { props, scope -> DatePickerRoot(props, scope) },
286 [panelFrame(trigger, Insets.all(triggerRing)), popup]
287 )
288 }