scoop:scoop.ui.component:0.1.0
src/content/alert.scoop
Selected production source · src/content/alert.scoop
1
package scoop.ui.component3
import scoop.graphics.*4
import scoop.graphics.text.*5
import scoop.ui.*6
import scoop.ui.component.column as panelColumn7
import scoop.ui.component.row as panelRow8
import scoop.ui.style.*10
public enum AlertVariant {11
Default, Destructive12
}14
public class AlertProps public constructor(15
public val content: View,16
public val title: PanelContent? = None,17
public val description: PanelContent? = None,18
public val icon: View? = None,19
public val action: View? = None,20
public val variant: AlertVariant = AlertVariant.Default,21
public val fonts: FontCollection? = None,22
public val theme: ThemeSource? = None,23
public val style: StyleOverride = styleOverride()24
)26
public class Alert public constructor(27
private val props: Props<AlertProps>,28
scope: ComponentScope29
): Component(scope) {30
private val themes = ThemeAccess(scope)31
private val titles = ActionTextStyle()32
private val descriptions = ActionTextStyle()33
public override fun onMount() {34
themes.mount(props.value.theme)35
}36
public override fun render(): View {37
val input = props.value38
val theme = themes.read(input.theme)39
val colors = theme.tokens.palette40
val density = theme.tokens.typography.body.fontSize / 14.041
val destructive = input.variant == AlertVariant.Destructive42
val titleStyle =43
titles.resolve(44
theme.tokens.typography.action,45
componentForeground(46
theme,47
if (destructive)48
colors.destructive.background49
else50
colors.card.foreground,51
colors.card.background52
),53
None54
)55
val descriptionStyle =56
descriptions.resolve(57
theme.tokens.typography.body,58
componentForeground(59
theme,60
if (destructive)61
colors.destructive.background.withAlpha(62
colors.destructive.background.alpha * 0.963
)64
else65
colors.muted.foreground,66
colors.card.background67
),68
None69
)70
val text =71
panelColumn {72
when (input.title) {73
case Some(val value) -> this.add(74
panelSlot(panelCaption(value, titleStyle, input.fonts), 0L)75
);76
case None -> {}77
}78
when (input.description) {79
case Some(val value) -> this.add(80
panelSlot(panelCaption(value, descriptionStyle, input.fonts), 1L)81
);82
case None -> {}83
}84
this.add(panelSlot(input.content, 2L))85
this.spacing = 2.0 * density86
this.alignment = CrossAlignment.Stretch87
}88
val body =89
panelAside(90
Some(text),91
input.action,92
8.0 * density,93
theme.tokens.typography.body.fontSize * 8.094
)95
val content =96
panelRow {97
when (input.icon) {98
case Some(val icon) -> this.item(99
panelFrame(100
icon,101
Insets(0.0, 2.0 * density, 0.0, 0.0),102
key = ViewKey.Number(0L)103
)104
) { this.shrink = 0.0 }105
case None -> {}106
}107
this.item(panelSlot(body, 1L)) { this.grow = 1.0 }108
this.spacing = 8.0 * density109
this.alignment = CrossAlignment.Start110
}111
return panelFrame(112
content,113
Insets(10.0 * density, 8.0 * density, 10.0 * density, 8.0 * density),114
styleOverride {115
this.background(Some(Paint(colors.card.background)))116
this.border(117
Some(118
Border.all(119
1.0,120
componentForeground(theme, colors.border, colors.card.background, 3.0)121
)122
)123
)124
this.corners(Corners.all(theme.tokens.corners.medium))125
}.then(input.style)126
)127
}128
}130
public class AlertBuilder internal constructor(): PanelBuilder() {131
public var icon: View? = None132
public var action: View? = None133
public var variant: AlertVariant = AlertVariant.Default134
public fun icon(content: View): Unit {135
icon = Some(content)136
}137
public fun action(content: View): Unit {138
action = Some(content)139
}140
}142
public fun alert(content: View, configure: AlertBuilder.() -> Unit = {}): View =143
makeAlert(content, configure)145
internal fun makeAlert(content: View, configure: AlertBuilder.() -> Unit): View {146
val builder = AlertBuilder()147
builder.configure()148
return component(149
AlertProps(150
content,151
builder.title,152
builder.description,153
builder.icon,154
builder.action,155
builder.variant,156
builder.fonts,157
builder.theme,158
builder.style159
),160
{ current, mount -> Alert(current, scope = mount) },161
builder.key162
)163
}