scoop:scoop.ui.component:0.1.0
src/toast/component.scoop
Selected production source · src/toast/component.scoop
1
package scoop.ui.component3
import scoop.graphics.*4
import scoop.surface.*5
import scoop.time.Duration6
import scoop.ui.*7
import scoop.ui.style.*9
public class Toaster public constructor(10
private val props: Props<ToasterProps>,11
scope: ComponentScope12
): 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.value20
input.state.entries.value21
val theme = themes.read(input.theme)22
val owner = control23
owner.interacting.value24
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
}40
}42
internal class ToasterHost(43
private val props: Props<ToasterControl>,44
scope: ComponentScope45
): Element<Unit>(scope) {46
public override fun update(): UiInvalidation {47
props.value.refresh()48
return UiInvalidation.Layout49
}50
public override fun measure(context: MeasureContext, constraints: Constraints): LayoutResult<51
Unit52
> {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
}62
}64
internal class ToastItemInput(65
val host: ToasterControl,66
val entry: ToastEntry,67
val props: ToasterProps,68
val theme: Theme69
) {70
val density: Double get() = theme.tokens.typography.body.fontSize / 14.071
val position: ToastPosition get() = entry.data.value.position ?: props.position72
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] === entry76
}77
}79
private class ToastItem(private val props: Props<ToastItemInput>, scope: ComponentScope): Component(80
scope81
) {82
public override fun render(): View {83
val input = props.value84
val entry = input.entry85
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.NonModal98
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
}105
}107
internal 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
)