Scoop API

scoop:scoop.ui.component:0.1.0

src/questionnaire/choice.scoop

Selected production source · src/questionnaire/choice.scoop

1package scoop.ui.component
3import scoop.graphics.*
4import scoop.graphics.text.*
5import scoop.ui.*
6import scoop.ui.style.*
7import scoop.window.accessibility.*
9internal fun questionnaireChoice(
10 input: QuestionnaireProps,
11 theme: Theme,
12 appearance: TextStyle,
13 question: Question,
14 member: QuestionnaireMember,
15 title: String,
16 description: String,
17 focus: FocusOptions,
18 owner: QuestionnaireControl,
19 direction: LayoutDirection
20): View {
21 val d = appearance.fontSize / 14.0
22 val p = theme.tokens.palette
23 val invalid = panelPresent(input.state.issue(question.key))
24 val body =
25 makeRow {
26 this.item(
27 element(
28 QuestionnaireIndicatorProps(
29 theme,
30 member.selected,
31 question.kind == QuestionKind.Multiple,
32 d
33 ),
34 { value, mount -> QuestionnaireIndicator(value, mount) }
35 )
36 ) { this.shrink = 0.0 }
37 this.item(
38 makeColumn {
39 this.add(
40 questionnaireLabel(
41 title,
42 appearance,
43 p.background.foreground,
44 14.0,
45 500,
46 input.fonts,
47 direction
48 )
49 )
50 if (description != "")
51 this.add(
52 questionnaireLabel(
53 description,
54 appearance,
55 p.muted.foreground,
56 12.0,
57 400,
58 input.fonts,
59 direction
60 )
61 )
62 this.alignment = CrossAlignment.Stretch
63 this.spacing = 2.0 * d
64 }
65 ) { this.grow = 1.0 }
66 if (member.shortcut != "")
67 this.item(
68 makeFrame(
69 questionnaireLabel(
70 member.shortcut,
71 appearance,
72 p.muted.foreground,
73 10.0,
74 500,
75 input.fonts,
76 direction,
77 TextAlignment.Center
78 )
79 ) {
80 this.width = Some(20.0 * d);
81 this.height = Some(20.0 * d)
82 this.alignment = Alignment(AxisAlignment.Center, AxisAlignment.Center)
83 this.style =
84 styleOverride {
85 this.border(Some(Border.all(1.0 * d, p.border)))
86 this.corners(Corners.all(4.0 * d))
87 }
88 }
89 ) { this.shrink = 0.0 }
90 this.alignment = CrossAlignment.Center
91 this.spacing = 10.0 * d
92 }
93 val style =
94 conversationStyle(
95 if (member.selected)
96 p.muted.background
97 else
98 Color.Transparent,
99 p.background.foreground,
100 Insets(12.0 * d, 10.0 * d, 12.0 * d, 10.0 * d),
101 8.0 * d,
102 if (invalid)
103 p.destructive.background
104 else
105 if (member.selected)
106 mixThemeColor(p.border, p.primary.background, 0.4)
107 else
108 p.input,
109 mixThemeColor(p.background.background, p.muted.background, 0.5),
110 input.focusOutline
111 ).copy { this.surface { this.minimumSize = Some(Size(0.0, 44.0 * d)) } }
112 return input.part(
113 QuestionnairePart.Choice,
114 conversationSurface(
115 ConversationSurfaceProps(
116 ButtonContent.Custom(body),
117 theme,
118 appearance,
119 style,
120 Some({ owner.select(question.key, member.key) }),
121 input.enabled && !input.busy && member.enabled,
122 focus,
123 input.fonts,
124 semantics =
125 ComponentSemantics(
126 name = Some(title),
127 description =
128 if (description == "")
129 None
130 else
131 Some(description)
132 ),
133 semanticRole =
134 Some(
135 if (question.kind == QuestionKind.Multiple)
136 SemanticRole.Checkbox
137 else
138 SemanticRole.RadioButton
139 ),
140 semanticState =
141 Some(
142 SemanticProperties(
143 name = Some(title),
144 description =
145 if (description == "")
146 None
147 else
148 Some(description),
149 enabled =
150 input.enabled && !input.busy && question.enabled && member.enabled,
151 checked =
152 Some(
153 if (member.selected)
154 SemanticChecked.On
155 else
156 SemanticChecked.Off
157 ),
158 required = question.required,
159 invalid = invalid,
160 error =
161 when (input.state.issue(question.key)) {
162 case Some(val issue) -> Some(input.strings.issue(issue))
163 case None -> None
164 }
165 )
166 ),
167 semanticActions =
168 Some(
169 if (question.kind == QuestionKind.Multiple)
170 SemanticActions(
171 toggle = Some { owner.selectSemantically(question, member.key) }
172 )
173 else
174 SemanticActions(
175 invoke = Some { owner.selectSemantically(question, member.key) }
176 )
177 )
178 )
179 )
180 )
183internal class QuestionnaireIndicatorProps(
184 val theme: Theme,
185 val selected: Boolean,
186 val multiple: Boolean,
187 val density: Double
190/** The whole card owns Press and focus; the marker is paint only. */
191internal class QuestionnaireIndicator(
192 private val props: Props<QuestionnaireIndicatorProps>,
193 scope: ComponentScope
194): Element<Unit>(scope) {
195 private val painters = scope.resource { ThemePainterCache() }
196 public override fun measure(context: MeasureContext, constraints: Constraints): LayoutResult<
197 Unit
198 > {
199 val side = 16.0 * props.value.density
200 return LayoutResult(LayoutMetrics(constraints.constrain(Size(side, side))), Unit)
201 }
202 public override fun scene(context: SceneContext, measured: Unit) {
203 val input = props.value
204 val bounds = Rect(0.0, 0.0, context.size.width, context.size.height)
205 val painter = painters.get(input.theme)
206 if (input.multiple) {
207 val visual =
208 CheckboxVisual(
209 if (input.selected)
210 CheckMark.On
211 else
212 CheckMark.Off
213 )
214 val geometry =
215 input.theme.checkboxGeometry(
216 bounds,
217 visual,
218 checkboxOptions(),
219 context.layoutDirection
220 )
221 context.draw { paint -> painter.drawCheckbox(paint.canvas, geometry, visual) }
222 } else {
223 val visual = RadioVisual(input.selected)
224 val geometry = input.theme.radioGeometry(bounds, visual, radioOptions())
225 context.draw { paint -> painter.drawRadio(paint.canvas, geometry, visual) }
226 }
227 }