scoop:scoop.ui.component:0.1.0
src/toast/content.scoop
Selected production source · src/toast/content.scoop
1
package scoop.ui.component3
import scoop.graphics.*4
import scoop.graphics.text.TextAlignment5
import scoop.math.*6
import scoop.ui.*7
import scoop.ui.style.*8
import scoop.ui.component.column as toastColumn10
internal fun toastContents(input: ToastItemInput): View {11
val entry = input.entry12
val data = entry.data.value13
val colors = input.theme.tokens.palette14
val titleStyle = input.theme.tokens.typography.action.copy { color = colors.popover.foreground }15
val descriptionStyle =16
input.theme.tokens.typography.body.copy {17
color =18
componentForeground(input.theme, colors.muted.foreground, colors.popover.background)19
}20
val icon =21
data.icon ?:22
when (data.kind) {23
case Default -> empty()24
case Loading -> spinner {25
size = Some(16.0 * input.density);26
color = Some(colors.popover.foreground);27
theme = input.props.theme28
}29
else -> toastIcon(30
data.kind,31
16.0 * input.density,32
if (data.kind == ToastKind.Error)33
colors.destructive.background34
else35
colors.popover.foreground36
)37
}38
val text =39
toastColumn {40
spacing = 4.0 * input.density41
alignment = CrossAlignment.Stretch42
add(panelSlot(panelCaption(data.title, titleStyle, input.props.fonts), 0L))43
when (data.description) {44
case Some(val value) -> add(45
panelSlot(panelCaption(value, descriptionStyle, input.props.fonts), 1L)46
)47
case None -> {}48
}49
}50
val action =51
when (data.action) {52
case Some(val value) -> component(53
ButtonProps(54
navigationLabel(value.content, input.props.fonts, TextAlignment.Center),55
{ toastRunAction(entry, input.host) },56
value.enabled && input.props.enabled,57
value.style,58
input.props.theme,59
value.focus,60
variant = value.variant,61
size = value.size,62
semantics = value.semantics63
),64
{ current, mount -> Button(current, mount) }65
)66
case None -> empty()67
}68
val close =69
if (data.dismiss.closeButton)70
component(71
ButtonProps(72
ButtonContent.Resolved(73
{ appearance -> modalCloseIcon(appearance.iconSize, appearance.text.color) }74
),75
{ entry.model.close(entry.id, ToastCloseReason.Explicit) },76
input.props.enabled,77
theme = input.props.theme,78
focus = data.closeFocus,79
variant = ButtonVariant.Ghost,80
size = ButtonSize.IconSm,81
semantics = data.closeSemantics82
),83
{ current, mount -> Button(current, mount) }84
)85
else86
empty()87
return element(input, { value, mount -> ToastPanel(value, mount) }, [icon, text, action, close])88
}90
/** Layout uses one set of columns and wraps the action below on narrow viewports. */91
private class ToastPanel(private val props: Props<ToastItemInput>, scope: ComponentScope): Element<92
Unit93
>(scope) {94
private val painter = scope.resource { StylePainter() }95
private val semantics = ToastSemantics(scope) { props.value }96
public override fun update(): UiInvalidation {97
semantics.update()98
return UiInvalidation.Layout99
}100
public override fun measure(context: MeasureContext, constraints: Constraints): LayoutResult<101
Unit102
> {103
val input = props.value;104
val data = input.entry.data.value;105
val d = input.density106
val width = constraints.maxWidth ?: input.props.width * d107
val padding = min(16.0 * d, width / 2.0)108
val inner = max(0.0, width - padding * 2.0)109
val gap = min(12.0 * d, inner)110
val close = context.measureChild(3L, Constraints(maxWidth = Some(min(32.0 * d, inner))))111
val hasClose = data.dismiss.closeButton112
val closeSpace =113
if (hasClose)114
close.size.width + gap115
else116
0.0117
val hasIcon = panelPresent(data.icon) || data.kind != ToastKind.Default118
val iconWidth =119
if (hasIcon)120
min(16.0 * d, max(0.0, inner - closeSpace))121
else122
0.0123
val icon =124
context.measureChild(125
0L,126
Constraints(maxWidth = Some(iconWidth), maxHeight = Some(16.0 * d))127
)128
val iconSpace =129
if (hasIcon && iconWidth > 0.0)130
icon.size.width + gap131
else132
0.0133
val preferredAction = context.measureChild(2L, Constraints())134
val actionWidth = max(0.0, inner - iconSpace)135
val action =136
if (preferredAction.size.width > actionWidth)137
context.measureChild(2L, Constraints(maxWidth = Some(actionWidth)))138
else139
preferredAction140
val hasAction = panelPresent(data.action)141
val actionSpace =142
if (hasAction)143
action.size.width + gap144
else145
0.0146
val below = hasAction && inner - iconSpace - closeSpace - actionSpace < 96.0 * d147
val textWidth =148
max(149
0.0,150
inner -151
iconSpace -152
closeSpace -153
if (below)154
0.0155
else156
actionSpace157
)158
val text =159
context.measureChild(160
1L,161
Constraints(162
textWidth,163
Some(textWidth),164
maxHeight =165
when (constraints.maxHeight) {166
case Some(val value) -> Some(167
max(168
0.0,169
value -170
padding *171
2.0 -172
if (below)173
gap + action.size.height174
else175
0.0176
)177
)178
case None -> None179
}180
)181
)182
val rowHeight =183
max(184
max(icon.size.height, text.size.height),185
max(186
close.size.height,187
if (below)188
0.0189
else190
action.size.height191
)192
)193
val natural =194
padding *195
2.0 +196
rowHeight +197
if (below)198
gap + action.size.height199
else200
0.0201
val size = constraints.constrain(Size(width, natural))202
if (input.front || input.expanded) {203
val rtl = context.layoutDirection == LayoutDirection.Rtl204
val x =205
{206
start: Double,207
child: Double -> if (rtl)208
width - padding - start - child209
else210
padding + start211
}212
if (iconWidth > 0.0)213
context.placeChild(214
0L,215
Point(216
x(0.0, icon.size.width),217
padding + min(4.0 * d, max(0.0, rowHeight - icon.size.height) / 2.0)218
)219
)220
if (textWidth > 0.0)221
context.placeChild(1L, Point(x(iconSpace, text.size.width), padding))222
if (hasAction)223
context.placeChild(224
2L,225
Point(226
x(227
if (below)228
iconSpace229
else230
max(0.0, inner - closeSpace - action.size.width),231
action.size.width232
),233
padding +234
if (below)235
rowHeight + gap236
else237
max(0.0, (rowHeight - action.size.height) / 2.0)238
)239
)240
if (hasClose)241
context.placeChild(242
3L,243
Point(x(max(0.0, inner - close.size.width), close.size.width), padding)244
)245
}246
return LayoutResult(LayoutMetrics(size), Unit)247
}248
public override fun scene(context: SceneContext, layout: Unit) {249
semantics.scene(context)250
val input = props.value;251
val colors = input.theme.tokens.palette252
val visual =253
styleOverride {254
background(Some(Paint(colors.popover.background)))255
border(Some(Border.all(1.0, componentPanelBorder(input.theme, colors.popover))))256
corners(Corners.all(16.0 * input.density))257
boxShadows(input.theme.tokens.effects.floating)258
}.then(input.props.style).then(input.entry.data.value.style).resolve(style())259
painter.scene(context, visual) { local -> local.children() }260
}261
}263
private fun toastRunAction(entry: ToastEntry, host: ToasterControl) {264
val action = entry.data.value.action ?: return265
if (entry.closing.value != None || !action.enabled || !host.read().enabled)266
return267
if (action.dismiss)268
entry.model.close(entry.id, ToastCloseReason.Action)269
action.onClick()270
}