Scoop API

scoop:scoop.ui.component:0.1.0

src/toast/semantics.scoop

Selected production source · src/toast/semantics.scoop

1package scoop.ui.component
3import scoop.graphics.Rect
4import scoop.surface.FrameSubmission
5import scoop.ui.*
6import scoop.window.accessibility.*
8/** Notification content and dismissal share the mounted toast and its ordinary close path. */
9internal class ToastSemantics(
10 private val scope: ComponentScope,
11 private val read: () -> ToastItemInput
12) {
13 private val handle = createComponentSemantics(scope, SemanticRole.Group)
14 private var announced = false
15 private var message: String? = None
16 init {
17 scope.routeSemanticsChildren(handle)
18 }
19 fun update() {
20 val target = handle ?: return
21 val input = read()
22 val data = input.entry.data.value
23 val title = semanticCaption(Some(data.title))
24 val description = semanticCaption(data.description)
25 val caption =
26 when ((title, description)) {
27 case(Some(val first), Some(val second)) -> Some(first + "\n" + second)
28 case(Some(_), None) -> title
29 case(None, _) -> description
30 }
31 message =
32 when (val overrideName = data.semantics.name) {
33 case Some(_) -> overrideName
34 case None -> caption
35 }
36 target.mergeLabels = true
37 target.bindReference(data.semantics.reference)
38 val dismissible = data.dismiss.closeButton || data.dismiss.escape || data.dismiss.swipe
39 target.update(
40 data.semantics.properties(enabled = input.props.enabled).copy {
41 name = this @ToastSemantics.message
42 busy = data.semantics.busy || data.kind == ToastKind.Loading
43 hidden =
44 data.semantics.hidden ||
45 input.entry.closing.value !=
46 None ||
47 !(input.front || input.expanded)
48 },
49 SemanticActions(
50 dismiss =
51 if (dismissible)
52 Some { dismiss() }
53 else
54 None
55 ),
56 data.semantics.relations
57 )
58 }
59 private fun dismiss(): SemanticActionResult {
60 val input = read()
61 if (!scope.isMounted || input.entry.closing.value != None)
62 return SemanticActionResult.Unavailable
63 if (!input.props.enabled)
64 return SemanticActionResult.Rejected
65 return if (input.entry.model.close(input.entry.id, ToastCloseReason.Explicit))
66 SemanticActionResult.Completed
67 else
68 SemanticActionResult.Rejected
69 }
70 fun scene(context: SceneContext) {
71 val target = handle ?: return
72 context.semantics(target, Rect(0.0, 0.0, context.size.width, context.size.height))
73 if (announced)
74 return
75 val input = read()
76 val data = input.entry.data.value
77 val text = message ?: return
78 context.onFrameComplete {
79 frame -> if (
80 frame.submission ==
81 FrameSubmission.Accepted &&
82 !announced &&
83 !target.isClosed &&
84 input.entry.data.value ===
85 data &&
86 input.entry.closing.value ==
87 None &&
88 (input.front || input.expanded) &&
89 !data.semantics.hidden &&
90 !data.semantics.ignored &&
91 scope.input.isInputAllowed &&
92 (target.geometry?.visibleBounds ?: None) !=
93 None
94 ) {
95 target.announce(text, data.semantics.live)
96 announced = true
97 }
98 }
99 }