Scoop API

scoop:scoop.ui.component:0.1.0

src/questionnaire/control.scoop

Selected production source · src/questionnaire/control.scoop

1package scoop.ui.component
3import scoop.graphics.*
4import scoop.surface.*
5import scoop.ui.*
6import scoop.ui.text.TextEditorState
7import scoop.window.*
8import scoop.window.accessibility.*
10internal class QuestionnaireMember(
11 val key: QuestionChoiceKey?,
12 val focus: FocusHandle,
13 val enabled: Boolean,
14 val selected: Boolean,
15 val shortcut: String
18internal class QuestionnaireScene(
19 val state: QuestionnaireState,
20 val question: QuestionKey?,
21 val revision: Long,
22 val delivery: Long,
23 val members: Array<QuestionnaireMember>,
24 val editor: TextEditorState?,
25 val inputFocus: FocusHandle,
26 val showsInput: Boolean,
27 val composing: Boolean
30internal class QuestionnaireControl(
31 val props: Props<QuestionnaireProps>,
32 private val scope: ComponentScope
33) {
34 private var accepted: QuestionnaireScene? = None
35 private var previousState = props.value.state
36 private var revision = props.value.state.focusRevision
37 private var initial = true
38 private var textRequested = false
39 private var submitting = false
40 private var deliveryPending = false
41 private val delivery = state(0L)
42 val deliveryRevision: Long get() = delivery.value
43 private var usable = false
44 val fallback = FocusHandle()
45 private val continueChord =
46 ShortcutChord(
47 ShortcutKey.Logical(LogicalKey.Named(NamedKey.Enter)),
48 ShortcutModifiers(primary = true)
49 )
50 private fun enabled(): Boolean =
51 scope.isMounted && props.value.enabled && !props.value.busy && !submitting
52 fun select(key: QuestionKey, choice: QuestionChoiceKey?) {
53 if (!enabled())
54 return
55 val state = props.value.state
56 val changed =
57 when (choice) {
58 case Some(val value) -> state.select(key, value)
59 case None -> state.selectText(key)
60 }
61 if (choice == None && state.usesText(key)) {
62 textRequested = true;
63 scope.requestFrame()
64 }
65 if (changed)
66 props.value.onAnswerChange(key)
67 }
68 fun selectSemantically(question: Question, choice: QuestionChoiceKey?): SemanticActionResult {
69 if (!scope.isMounted || props.value.state.activeKey != Some(question.key))
70 return SemanticActionResult.Unavailable
71 if (!enabled() || !question.enabled)
72 return SemanticActionResult.Rejected
73 val state = props.value.state
74 val before =
75 when (choice) {
76 case Some(val key) -> state.isSelected(question.key, key)
77 case None -> state.usesText(question.key)
78 }
79 val expected =
80 if (question.kind == QuestionKind.Multiple)
81 !before
82 else
83 true
84 select(question.key, choice)
85 val after =
86 when (choice) {
87 case Some(val key) -> state.isSelected(question.key, key)
88 case None -> state.usesText(question.key)
89 }
90 return if (after == expected)
91 SemanticActionResult.Completed
92 else
93 SemanticActionResult.Rejected
94 }
95 fun edited(key: QuestionKey) {
96 if (!enabled())
97 return
98 props.value.state.textEdited(key)
99 props.value.onAnswerChange(key)
100 }
101 fun previous() {
102 if (enabled())
103 props.value.state.previous()
104 }
105 fun skip() {
106 if (!enabled())
107 return
108 val key = props.value.state.activeKey ?: return
109 if (props.value.state.skip())
110 props.value.onAnswerChange(key)
111 }
112 fun proceed() {
113 if (!enabled() || deliveryPending || !props.value.continueEnabled)
114 return
115 val value = props.value
116 val progress = value.state.progress
117 if (progress.position < progress.total) {
118 value.state.next();
119 return
120 }
121 submitting = true
122 try {
123 when (value.state.submit()) {
124 case Some(val result) -> {
125 // A second activation may arrive before the caller's busy prop is rendered.
126 deliveryPending = true
127 delivery.value += 1L
128 value.onSubmit(result)
129 }
130 case None -> {}
131 }
132 } finally {
133 submitting = false
134 }
135 }
136 fun accept(snapshot: QuestionnaireScene, visible: Boolean) {
137 if (
138 !scope.isMounted ||
139 snapshot.state !==
140 props.value.state ||
141 snapshot.question !=
142 props.value.state.activeKey ||
143 snapshot.revision !=
144 props.value.state.focusRevision
145 )
146 return
147 accepted = Some(snapshot)
148 if (snapshot.delivery == delivery.value)
149 deliveryPending = false
150 usable = visible && enabled()
151 if (snapshot.state !== previousState) {
152 previousState = snapshot.state;
153 initial = true;
154 revision = snapshot.revision
155 }
156 val shouldFocus =
157 revision != snapshot.revision || textRequested || (initial && props.value.autoFocus)
158 initial = false
159 revision = snapshot.revision
160 val preferText = textRequested
161 textRequested = false
162 if (!usable || !shouldFocus)
163 return
164 val issue =
165 when (snapshot.question) {
166 case Some(val key) -> snapshot.state.issue(key);
167 case None -> None
168 }
169 if (
170 snapshot.showsInput &&
171 (preferText || panelPresent(issue) || snapshot.members.size == 0L)
172 ) {
173 snapshot.inputFocus.requestFocus(FocusOrigin.Keyboard);
174 return
175 }
176 for (member in snapshot.members)
177 if (member.enabled && member.selected) {
178 member.focus.requestFocus(FocusOrigin.Keyboard);
179 return
180 }
181 for (member in snapshot.members)
182 if (member.enabled) {
183 member.focus.requestFocus(FocusOrigin.Keyboard);
184 return
185 }
186 if (snapshot.showsInput)
187 snapshot.inputFocus.requestFocus(FocusOrigin.Keyboard)
188 else
189 fallback.requestFocus(FocusOrigin.Keyboard)
190 }
191 fun key(context: RoutedEventContext, event: InputEvent) {
192 if (context.defaultPrevented || !usable || !enabled())
193 return
194 val key =
195 when (event) {
196 case Keyboard(val value) -> value;
197 else -> return
198 }
199 if (key.state != ElementState.Pressed || key.synthetic)
200 return
201 val scene = accepted ?: return
202 if (scene.state !== props.value.state || scene.question != props.value.state.activeKey)
203 return
204 val editing = scene.showsInput && scene.inputFocus.hasFocus
205 val composing = editing && (scene.composing || (scene.editor?.isComposing ?: false))
206 if (continueChord.matches(key)) {
207 context.consume()
208 if (!key.isRepeat && !composing)
209 proceed()
210 return
211 }
212 if (editing || composing || !menuKeyAllowed(key) || !props.value.shortcuts)
213 return
214 val question = scene.question ?: return
215 for (member in scene.members)
216 if (
217 member.enabled &&
218 member.shortcut !=
219 "" &&
220 LogicalKey.Character(member.shortcut).matchesShortcut(key.logicalKey)
221 ) {
222 context.consume()
223 if (!key.isRepeat) {
224 member.focus.requestFocus(FocusOrigin.Keyboard);
225 select(question, member.key)
226 }
227 return
228 }
229 for (member in scene.members)
230 if (member.focus.hasFocus)
231 return
232 val forward = key.logicalKey == LogicalKey.Named(NamedKey.ArrowRight)
233 if (forward || key.logicalKey == LogicalKey.Named(NamedKey.ArrowLeft)) {
234 context.consume()
235 if (!key.isRepeat) {
236 if (forward != (scope.layoutDirection == LayoutDirection.Rtl))
237 proceed()
238 else
239 previous()
240 }
241 }
242 }
243 fun close() {
244 accepted = None;
245 usable = false;
246 textRequested = false
247 deliveryPending = false
248 }
251internal class QuestionnaireEventsProps(
252 val owner: QuestionnaireControl,
253 val scene: QuestionnaireScene
256internal class QuestionnaireEvents(
257 private val props: Props<QuestionnaireEventsProps>,
258 scope: ComponentScope
259): Element<Unit>(scope) {
260 private var presented = false
261 init {
262 scope.input.captureHandler = { context, event -> props.value.owner.key(context, event) }
263 }
264 public override fun update(): UiInvalidation {
265 val input = props.value.owner.props.value
266 scope.input.enabled = presented && input.enabled && !input.busy
267 return UiInvalidation.Layout
268 }
269 public override fun measure(context: MeasureContext, constraints: Constraints): LayoutResult<
270 Unit
271 > {
272 val child = context.measureChild(0L, constraints)
273 context.placeChild(0L, Point(0.0, 0.0))
274 return LayoutResult(child, Unit)
275 }
276 public override fun scene(context: SceneContext, measured: Unit) {
277 val input = props.value
278 val visible = context.size.width > 0.0 && context.size.height > 0.0
279 context.children()
280 context.onFrameComplete {
281 frame -> if (
282 frame.submission ==
283 FrameSubmission.Accepted &&
284 scope.isMounted &&
285 props.value ===
286 input
287 ) {
288 presented = visible
289 scope.input.enabled =
290 presented && input.owner.props.value.enabled && !input.owner.props.value.busy
291 input.owner.accept(input.scene, visible)
292 }
293 }
294 }