Scoop API

scoop:scoop.ui.component:0.1.0

src/questionnaire/questionnaire.scoop

Selected production source · src/questionnaire/questionnaire.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.style.style as baseStyle
10public class Questionnaire(
11 private val props: Props<QuestionnaireProps>,
12 scope: ComponentScope
13): Component(scope) {
14 private val themes = ThemeAccess(scope)
15 private val handles = ChoiceHandles()
16 private val editorFocus = FocusHandle()
17 private val text = ActionTextStyle()
18 private val control = QuestionnaireControl(props, scope)
19 public override fun onMount() {
20 themes.mount(props.value.theme)
21 }
22 public override fun onUnmount() {
23 control.close()
24 }
25 public override fun render(): View {
26 val input = props.value
27 val theme = themes.read(input.theme)
28 val appearance =
29 text.resolve(
30 theme.tokens.typography.body,
31 theme.tokens.palette.background.foreground,
32 None
33 )
34 val d = appearance.fontSize / 14.0
35 val state = input.state
36 val current = state.current
37 val members = ArrayList<QuestionnaireMember>()
38 val choiceViews = ArrayList<View>()
39 val keys = ArrayList<ViewKey>()
40 val options = ArrayList<FocusOptions>()
41 when (current) {
42 case Some(val question) -> {
43 for (choice in question.choices) {
44 keys.add(ViewKey.Text(question.key.value + ":choice:" + choice.key.value))
45 options.add(input.choiceFocus(question.key, Some(choice.key)))
46 }
47 if (question.kind != QuestionKind.Freeform && panelPresent(question.freeform)) {
48 keys.add(ViewKey.Text(question.key.value + ":freeform"))
49 options.add(input.choiceFocus(question.key, None))
50 }
51 }
52 case None -> {}
53 }
54 val focus = handles.resolve(keys.toArray(), options.toArray())
55 var shortcutIndex = 0L
56 when (current) {
57 case Some(val question) -> {
58 for (index in 0L ..< question.choices.size) {
59 val choice = question.choices[index]
60 val shortcut =
61 if (choice.enabled && input.shortcuts)
62 questionShortcut(shortcutIndex)
63 else
64 ""
65 if (choice.enabled)
66 shortcutIndex += 1L
67 val member =
68 QuestionnaireMember(
69 Some(choice.key),
70 focus[index],
71 choice.enabled,
72 state.isSelected(question.key, choice.key),
73 shortcut
74 )
75 members.add(member)
76 choiceViews.add(
77 questionnaireChoice(
78 input,
79 theme,
80 appearance,
81 question,
82 member,
83 choice.title,
84 choice.description,
85 choiceFocus(options[index], focus[index]),
86 control,
87 scope.layoutDirection
88 )
89 )
90 }
91 if (focus.size > question.choices.size) {
92 val index = question.choices.size
93 val member =
94 QuestionnaireMember(
95 None,
96 focus[index],
97 true,
98 state.usesText(question.key),
99 ""
100 )
101 members.add(member)
102 choiceViews.add(
103 questionnaireChoice(
104 input,
105 theme,
106 appearance,
107 question,
108 member,
109 question.freeform!!.title,
110 "",
111 choiceFocus(options[index], focus[index]),
112 control,
113 scope.layoutDirection
114 )
115 )
116 }
117 }
118 case None -> {}
119 }
120 val resolved = members.toArray()
121 val activeInput = input.inputFocus.handle ?: editorFocus
122 val editor =
123 when (current) {
124 case Some(val question) -> state.editor(question.key);
125 case None -> None
126 }
127 val showsInput =
128 when (current) {
129 case Some(val question) -> question.kind ==
130 QuestionKind.Freeform ||
131 state.usesText(question.key)
132 case None -> false
133 }
134 val snapshot =
135 QuestionnaireScene(
136 state,
137 state.activeKey,
138 state.focusRevision,
139 control.deliveryRevision,
140 resolved,
141 editor,
142 activeInput,
143 showsInput,
144 editor?.isComposing ?: false
145 )
146 val progress = state.progress
147 val progressView =
148 questionnaireLabel(
149 input.strings.progress(progress.position, progress.total),
150 appearance,
151 theme.tokens.palette.muted.foreground,
152 12.0,
153 500,
154 input.fonts,
155 scope.layoutDirection
156 )
157 val body =
158 when (current) {
159 case Some(val question) -> item(
160 question,
161 input,
162 theme,
163 appearance,
164 resolved,
165 focus,
166 keys.toArray(),
167 choiceViews.toArray(),
168 activeInput,
169 showsInput
170 )
171 case None -> empty()
172 }
173 val direction = scope.layoutDirection
174 val owner = control
175 val content =
176 makeColumn {
177 this.add(panelSlot(input.part(QuestionnairePart.Progress, progressView), 0L))
178 this.add(
179 panelFrame(
180 body,
181 key = ViewKey.Text("question:" + (state.activeKey?.value ?: "empty"))
182 )
183 )
184 this.add(
185 panelSlot(
186 input.part(
187 QuestionnairePart.Actions,
188 questionnaireActions(input, owner, theme, direction, d)
189 ),
190 2L
191 )
192 )
193 this.spacing = 16.0 * d
194 this.alignment = CrossAlignment.Stretch
195 }
196 return styled(
197 element(
198 QuestionnaireEventsProps(control, snapshot),
199 { value, mount -> QuestionnaireEvents(value, mount) },
200 [content]
201 )
202 ) { this.style = input.style.resolve(baseStyle()) }
203 }
204 private fun item(
205 question: Question,
206 input: QuestionnaireProps,
207 theme: Theme,
208 appearance: TextStyle,
209 members: Array<QuestionnaireMember>,
210 focus: Array<FocusHandle>,
211 keys: Array<ViewKey>,
212 choices: Array<View>,
213 inputHandle: FocusHandle,
214 showsInput: Boolean
215 ): View {
216 val state = input.state
217 val d = appearance.fontSize / 14.0
218 val p = theme.tokens.palette
219 val enabled = input.enabled && !input.busy
220 val owner = control
221 val direction = scope.layoutDirection
222 var preferred: FocusHandle? = None
223 for (member in members)
224 if (member.enabled && member.selected)
225 preferred = Some(member.focus)
226 val choicesView =
227 choiceLayout(
228 choices,
229 keys,
230 focus,
231 FlexAxis.Vertical,
232 8.0 * d,
233 true,
234 enabled,
235 styleOverride(),
236 preferred,
237 {
238 handle -> if (question.kind == QuestionKind.Single)
239 for (member in members)
240 if (member.focus === handle)
241 owner.select(question.key, member.key)
242 },
243 fitCross = false,
244 roving = question.kind == QuestionKind.Single
245 )
246 val editor =
247 if (showsInput)
248 makeInput(state.editor(question.key)!!) {
249 this.placeholder = question.freeform!!.placeholder
250 this.enabled = enabled
251 this.invalid = panelPresent(state.issue(question.key))
252 this.focus = choiceFocus(input.inputFocus, inputHandle)
253 this.fonts = input.fonts
254 this.theme = input.theme
255 this.style { this.surface { this.focus = input.focusOutline } }
256 this.onEdited = { _ -> owner.edited(question.key) }
257 this.semantics =
258 ComponentSemantics(
259 name = Some(question.freeform!!.title),
260 description = Some(question.title),
261 error =
262 when (state.issue(question.key)) {
263 case Some(val issue) -> Some(input.strings.issue(issue))
264 case None -> None
265 }
266 )
267 }
268 else
269 empty()
270 val item =
271 makeColumn {
272 this.add(
273 input.part(
274 QuestionnairePart.Title,
275 questionnaireLabel(
276 question.title,
277 appearance,
278 p.background.foreground,
279 16.0,
280 500,
281 input.fonts,
282 direction
283 )
284 )
285 )
286 if (question.description != "")
287 this.add(
288 input.part(
289 QuestionnairePart.Description,
290 questionnaireLabel(
291 question.description,
292 appearance,
293 p.muted.foreground,
294 14.0,
295 400,
296 input.fonts,
297 direction
298 )
299 )
300 )
301 if (!question.required) {
302 val caption =
303 when (state.answer(question.key)) {
304 case Skipped -> input.strings.skipped;
305 else -> input.strings.optional
306 }
307 this.add(
308 questionnaireLabel(
309 caption,
310 appearance,
311 p.muted.foreground,
312 12.0,
313 500,
314 input.fonts,
315 direction
316 )
317 )
318 }
319 if (choices.size > 0L)
320 this.add(panelSlot(input.part(QuestionnairePart.Choices, choicesView), 10L))
321 if (showsInput) {
322 val ring =
323 theme.surfaceMetrics(
324 SurfaceVisual(SurfaceKind.Plain),
325 surfaceOptions { this.focus = input.focusOutline },
326 direction
327 ).focusOutset
328 this.add(
329 panelSlot(
330 input.part(
331 QuestionnairePart.Input,
332 panelFrame(editor, Insets.all(ring))
333 ),
334 11L
335 )
336 )
337 }
338 when (state.issue(question.key)) {
339 case Some(val issue) -> this.add(
340 panelSlot(
341 input.part(
342 QuestionnairePart.Error,
343 questionnaireLabel(
344 input.strings.issue(issue),
345 appearance,
346 p.destructive.background,
347 14.0,
348 400,
349 input.fonts,
350 direction
351 )
352 ),
353 12L
354 )
355 )
356 case None -> {}
357 }
358 this.spacing = 12.0 * d
359 this.alignment = CrossAlignment.Stretch
360 }
361 return semanticContent(
362 input.part(QuestionnairePart.Item, item),
363 if (question.kind == QuestionKind.Single)
364 SemanticRole.RadioGroup
365 else
366 SemanticRole.Group,
367 SemanticProperties(
368 name = Some(question.title),
369 description = Some(question.description),
370 enabled = enabled && question.enabled,
371 busy = input.busy,
372 required = question.required,
373 invalid = state.issue(question.key) != None,
374 error =
375 when (state.issue(question.key)) {
376 case Some(val issue) -> Some(input.strings.issue(issue))
377 case None -> None
378 },
379 selectionMode =
380 if (question.kind == QuestionKind.Single)
381 Some(SemanticSelectionMode.Single)
382 else
383 None
384 ),
385 mergeLabels = false
386 )
387 }
390internal fun questionnaireLabel(
391 value: String,
392 base: TextStyle,
393 color: Color,
394 size: Double,
395 weight: Int,
396 fonts: FontCollection?,
397 direction: LayoutDirection,
398 align: TextAlignment = TextAlignment.Start
399): View =
400 conversationCaption(
401 ButtonContent.Label(
402 LabelProps(
403 LabelText.Plain(value, None),
404 settings =
405 Some(
406 paragraphStyle {
407 this.alignment = align;
408 this.direction = direction.textDirection()
409 }
410 )
411 )
412 ),
413 base.copy {
414 this.color = color;
415 this.fontSize = size * base.fontSize / 14.0;
416 this.weight = weight
417 },
418 fonts,
419 direction
420 )