Scoop API

scoop:scoop.ui.component:0.1.0

src/toast/state.scoop

Selected production source · src/toast/state.scoop

1package scoop.ui.component
3import scoop.graphics.text.StyledText
4import scoop.platform.time.MonotonicTimeSource
5import scoop.ui.*
7internal class ToastEntry(val model: ToastState, val id: ToastId, initial: ToastProps) {
8 val data = state(initial)
9 val closing = state<ToastCloseReason?>(None)
10 val admitted = state(false)
11 val clock = ToastClock(toastDuration(initial))
12 val layer = OverlayHandle()
13 var sequence = 0L
14 var height = 0.0
15 var card: ToastCardControl? = None
18public class ToastState public constructor() {
19 internal val entries = state<Array<ToastEntry>>([])
20 private var next = 0L
21 private var owner: ToasterControl? = None
22 public val count: Long get() = entries.value.size
23 public val isAttached: Boolean get() {
24 entries.value;
25 return panelPresent(owner)
26 }
27 public fun contains(id: ToastId): Boolean {
28 val entry = find(id) ?: return false
29 return entry.closing.value == None
30 }
31 internal fun find(id: ToastId): ToastEntry? {
32 for (entry in entries.value)
33 if (entry.id === id)
34 return Some(entry)
35 return None
36 }
37 public fun show(props: ToastProps): ToastId {
38 val old = entries.value
39 requireComponent(next < 9223372036854775807L, "toast identity overflow")
40 val entry = ToastEntry(this, ToastId(next), props)
41 entries.value =
42 Array<ToastEntry>(old.size + 1L) {
43 at -> if (at < old.size)
44 old[at]
45 else
46 entry
47 }
48 next += 1L
49 owner?.refresh()
50 return entry.id
51 }
52 public fun show(title: String, configure: ToastBuilder.() -> Unit = {}): ToastId =
53 show(toastProps(title, configure))
54 public fun show(title: StyledText, configure: ToastBuilder.() -> Unit = {}): ToastId =
55 show(toastProps(title, configure))
56 public fun show(title: View, configure: ToastBuilder.() -> Unit = {}): ToastId =
57 show(toastProps(title, configure))
58 public fun update(id: ToastId, props: ToastProps): Boolean {
59 val entry = find(id) ?: return false
60 if (entry.closing.value != None)
61 return false
62 entry.data.value = props
63 entry.clock.reset(toastDuration(props))
64 owner?.refresh()
65 return true
66 }
67 public fun update(
68 id: ToastId,
69 title: String,
70 configure: ToastBuilder.() -> Unit = {}
71 ): Boolean = update(id, toastProps(title, configure))
72 public fun update(
73 id: ToastId,
74 title: StyledText,
75 configure: ToastBuilder.() -> Unit = {}
76 ): Boolean = update(id, toastProps(title, configure))
77 public fun update(id: ToastId, title: View, configure: ToastBuilder.() -> Unit = {}): Boolean =
78 update(id, toastProps(title, configure))
79 public fun dismiss(id: ToastId): Boolean = close(id, ToastCloseReason.Explicit)
80 internal fun close(id: ToastId, reason: ToastCloseReason): Boolean {
81 val entry = find(id) ?: return false
82 if (entry.closing.value != None)
83 return false
84 beginClose(entry, reason)
85 if (entry.layer.isOpen)
86 entry.layer.requestClose()
87 else
88 complete(entry)
89 return true
90 }
91 internal fun beginClose(entry: ToastEntry, reason: ToastCloseReason) {
92 if (entry.closing.value != None)
93 return
94 entry.closing.value = Some(reason)
95 entry.clock.pause(MonotonicTimeSource.now())
96 owner?.refresh()
97 }
98 internal fun complete(entry: ToastEntry) {
99 val reason = entry.closing.value ?: return
100 if (!panelPresent(find(entry.id)))
101 return
102 val kept = ArrayList<ToastEntry>()
103 for (value in entries.value)
104 if (value !== entry)
105 kept.add(value)
106 entries.value = kept.toArray()
107 owner?.refresh()
108 entry.data.value.onClosed(entry.id, reason)
109 }
110 internal fun attach(value: ToasterControl) {
111 entries.value
112 when (owner) {
113 case Some(val current) -> requireComponent(
114 current === value,
115 "toast state already has an active toaster"
116 )
117 case None -> owner = Some(value)
118 }
119 }
120 internal fun detach(value: ToasterControl) {
121 val current = owner ?: return
122 if (current !== value)
123 return
124 owner = None
125 val now = MonotonicTimeSource.now()
126 val records = entries.value
127 for (entry in records) {
128 entry.clock.pause(now)
129 if (entry.admitted.value)
130 entry.admitted.value = false
131 }
132 var failure: Throwable? = None
133 for (entry in records)
134 try {
135 complete(entry)
136 } catch (error: Throwable) {
137 failure =
138 when (failure) {
139 case Some(val previous) -> Some(ResourceUseException(previous, error))
140 case None -> Some(error)
141 }
142 }
143 when (failure) {
144 case Some(val error) -> throw error
145 case None -> {}
146 }
147 }