Scoop API

scoop:scoop.ui.component:0.1.0

src/toast/component.scoop

Selected production source · src/toast/component.scoop

1package scoop.ui.component
3import scoop.graphics.*
4import scoop.surface.*
5import scoop.time.Duration
6import scoop.ui.*
7import scoop.ui.style.*
9public class Toaster public constructor(
10 private val props: Props<ToasterProps>,
11 scope: ComponentScope
12): Component(scope) {
13 private val themes = ThemeAccess(scope)
14 private val control = ToasterControl(scope, { props.value })
15 init {
16 themes.mount(props.value.theme)
17 }
18 public override fun render(): View {
19 val input = props.value
20 input.state.entries.value
21 val theme = themes.read(input.theme)
22 val owner = control
23 owner.interacting.value
24 val views = ArrayList<View>()
25 for (at in 0L ..< toastPositions.size)
26 views.add(toastRegion(owner, toastPositions[at], -1L - at))
27 for (entry in owner.visible.value)
28 views.add(
29 component(
30 ToastItemInput(owner, entry, input, theme),
31 { value, mount -> ToastItem(value, mount) },
32 ViewKey.Number(entry.sequence)
33 )
34 )
35 return element(owner, { value, mount -> ToasterHost(value, mount) }, views.toArray())
36 }
37 public override fun onUnmount() {
38 control.retire()
39 }
42internal class ToasterHost(
43 private val props: Props<ToasterControl>,
44 scope: ComponentScope
45): Element<Unit>(scope) {
46 public override fun update(): UiInvalidation {
47 props.value.refresh()
48 return UiInvalidation.Layout
49 }
50 public override fun measure(context: MeasureContext, constraints: Constraints): LayoutResult<
51 Unit
52 > {
53 for (at in 0L ..< context.childCount) {
54 context.measureChild(at, Constraints());
55 context.placeChild(at, Point(0.0, 0.0))
56 }
57 return LayoutResult(LayoutMetrics(constraints.constrain(Size(0.0, 0.0))), Unit)
58 }
59 public override fun scene(context: SceneContext, layout: Unit) {
60 context.children()
61 }
64internal class ToastItemInput(
65 val host: ToasterControl,
66 val entry: ToastEntry,
67 val props: ToasterProps,
68 val theme: Theme
69) {
70 val density: Double get() = theme.tokens.typography.body.fontSize / 14.0
71 val position: ToastPosition get() = entry.data.value.position ?: props.position
72 val expanded: Boolean get() = host.expanded(position)
73 val front: Boolean get() {
74 val group = host.group(position)
75 return group.size > 0L && group[0L] === entry
76 }
79private class ToastItem(private val props: Props<ToastItemInput>, scope: ComponentScope): Component(
80 scope
81) {
82 public override fun render(): View {
83 val input = props.value
84 val entry = input.entry
85 val transition = toastTransition(input)
86 return portal(
87 binding(
88 { entry.closing.value == None && entry.admitted.value },
89 {
90 value -> if (!value)
91 entry.model.beginClose(entry, ToastCloseReason.Explicit)
92 }
93 ),
94 element(input, { value, mount -> ToastSurface(value, mount) }, [toastContents(input)])
95 ) {
96 placement = OverlayPlacement.Viewport(viewportPadding = 0.0)
97 mode = OverlayMode.NonModal
98 handle = Some(entry.layer)
99 focus = Some(OverlayFocusOptions(autoFocus = false))
100 dismiss = OverlayDismiss()
101 this.transition = PresenceOptions(transition, transition)
102 onClosed = { _ -> entry.model.complete(entry) }
103 }
104 }
107internal fun toastTransition(input: ToastItemInput): AnimationSpec? =
108 input.theme.tokens.motion.resolve(
109 input.props.transition.resolve(
110 Some(
111 AnimationSpec.Tween(
112 Duration.milliseconds(500L),
113 Easing.cubicBezier(0.22, 1.0, 0.36, 1.0)
114 )
115 )
116 )
117 )