scoop:scoop.ui.component:0.1.0
src/toast/state.scoop
Selected production source · src/toast/state.scoop
1
package scoop.ui.component3
import scoop.graphics.text.StyledText4
import scoop.platform.time.MonotonicTimeSource5
import scoop.ui.*7
internal 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 = 0L14
var height = 0.015
var card: ToastCardControl? = None16
}18
public class ToastState public constructor() {19
internal val entries = state<Array<ToastEntry>>([])20
private var next = 0L21
private var owner: ToasterControl? = None22
public val count: Long get() = entries.value.size23
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 false29
return entry.closing.value == None30
}31
internal fun find(id: ToastId): ToastEntry? {32
for (entry in entries.value)33
if (entry.id === id)34
return Some(entry)35
return None36
}37
public fun show(props: ToastProps): ToastId {38
val old = entries.value39
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
else46
entry47
}48
next += 1L49
owner?.refresh()50
return entry.id51
}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 false60
if (entry.closing.value != None)61
return false62
entry.data.value = props63
entry.clock.reset(toastDuration(props))64
owner?.refresh()65
return true66
}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 false82
if (entry.closing.value != None)83
return false84
beginClose(entry, reason)85
if (entry.layer.isOpen)86
entry.layer.requestClose()87
else88
complete(entry)89
return true90
}91
internal fun beginClose(entry: ToastEntry, reason: ToastCloseReason) {92
if (entry.closing.value != None)93
return94
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 ?: return100
if (!panelPresent(find(entry.id)))101
return102
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.value112
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 ?: return122
if (current !== value)123
return124
owner = None125
val now = MonotonicTimeSource.now()126
val records = entries.value127
for (entry in records) {128
entry.clock.pause(now)129
if (entry.admitted.value)130
entry.admitted.value = false131
}132
var failure: Throwable? = None133
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 error145
case None -> {}146
}147
}148
}