Scoop API

scoop:scoop.ui.component:0.1.0

src/content/alert.scoop

Selected production source · src/content/alert.scoop

1package scoop.ui.component
3import scoop.graphics.*
4import scoop.graphics.text.*
5import scoop.ui.*
6import scoop.ui.component.column as panelColumn
7import scoop.ui.component.row as panelRow
8import scoop.ui.style.*
10public enum AlertVariant {
11 Default, Destructive
14public 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()
26public class Alert public constructor(
27 private val props: Props<AlertProps>,
28 scope: ComponentScope
29): 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.value
38 val theme = themes.read(input.theme)
39 val colors = theme.tokens.palette
40 val density = theme.tokens.typography.body.fontSize / 14.0
41 val destructive = input.variant == AlertVariant.Destructive
42 val titleStyle =
43 titles.resolve(
44 theme.tokens.typography.action,
45 componentForeground(
46 theme,
47 if (destructive)
48 colors.destructive.background
49 else
50 colors.card.foreground,
51 colors.card.background
52 ),
53 None
54 )
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.9
63 )
64 else
65 colors.muted.foreground,
66 colors.card.background
67 ),
68 None
69 )
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 * density
86 this.alignment = CrossAlignment.Stretch
87 }
88 val body =
89 panelAside(
90 Some(text),
91 input.action,
92 8.0 * density,
93 theme.tokens.typography.body.fontSize * 8.0
94 )
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 * density
109 this.alignment = CrossAlignment.Start
110 }
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 }
130public class AlertBuilder internal constructor(): PanelBuilder() {
131 public var icon: View? = None
132 public var action: View? = None
133 public var variant: AlertVariant = AlertVariant.Default
134 public fun icon(content: View): Unit {
135 icon = Some(content)
136 }
137 public fun action(content: View): Unit {
138 action = Some(content)
139 }
142public fun alert(content: View, configure: AlertBuilder.() -> Unit = {}): View =
143 makeAlert(content, configure)
145internal 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.style
159 ),
160 { current, mount -> Alert(current, scope = mount) },
161 builder.key
162 )