Scoop API

scoop:scoop.ui.component:0.1.0

src/toast/content.scoop

Selected production source · src/toast/content.scoop

1package scoop.ui.component
3import scoop.graphics.*
4import scoop.graphics.text.TextAlignment
5import scoop.math.*
6import scoop.ui.*
7import scoop.ui.style.*
8import scoop.ui.component.column as toastColumn
10internal fun toastContents(input: ToastItemInput): View {
11 val entry = input.entry
12 val data = entry.data.value
13 val colors = input.theme.tokens.palette
14 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.theme
28 }
29 else -> toastIcon(
30 data.kind,
31 16.0 * input.density,
32 if (data.kind == ToastKind.Error)
33 colors.destructive.background
34 else
35 colors.popover.foreground
36 )
37 }
38 val text =
39 toastColumn {
40 spacing = 4.0 * input.density
41 alignment = CrossAlignment.Stretch
42 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.semantics
63 ),
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.closeSemantics
82 ),
83 { current, mount -> Button(current, mount) }
84 )
85 else
86 empty()
87 return element(input, { value, mount -> ToastPanel(value, mount) }, [icon, text, action, close])
90/** Layout uses one set of columns and wraps the action below on narrow viewports. */
91private class ToastPanel(private val props: Props<ToastItemInput>, scope: ComponentScope): Element<
92 Unit
93>(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.Layout
99 }
100 public override fun measure(context: MeasureContext, constraints: Constraints): LayoutResult<
101 Unit
102 > {
103 val input = props.value;
104 val data = input.entry.data.value;
105 val d = input.density
106 val width = constraints.maxWidth ?: input.props.width * d
107 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.closeButton
112 val closeSpace =
113 if (hasClose)
114 close.size.width + gap
115 else
116 0.0
117 val hasIcon = panelPresent(data.icon) || data.kind != ToastKind.Default
118 val iconWidth =
119 if (hasIcon)
120 min(16.0 * d, max(0.0, inner - closeSpace))
121 else
122 0.0
123 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 + gap
131 else
132 0.0
133 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 else
139 preferredAction
140 val hasAction = panelPresent(data.action)
141 val actionSpace =
142 if (hasAction)
143 action.size.width + gap
144 else
145 0.0
146 val below = hasAction && inner - iconSpace - closeSpace - actionSpace < 96.0 * d
147 val textWidth =
148 max(
149 0.0,
150 inner -
151 iconSpace -
152 closeSpace -
153 if (below)
154 0.0
155 else
156 actionSpace
157 )
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.height
174 else
175 0.0
176 )
177 )
178 case None -> None
179 }
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.0
189 else
190 action.size.height
191 )
192 )
193 val natural =
194 padding *
195 2.0 +
196 rowHeight +
197 if (below)
198 gap + action.size.height
199 else
200 0.0
201 val size = constraints.constrain(Size(width, natural))
202 if (input.front || input.expanded) {
203 val rtl = context.layoutDirection == LayoutDirection.Rtl
204 val x =
205 {
206 start: Double,
207 child: Double -> if (rtl)
208 width - padding - start - child
209 else
210 padding + start
211 }
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 iconSpace
229 else
230 max(0.0, inner - closeSpace - action.size.width),
231 action.size.width
232 ),
233 padding +
234 if (below)
235 rowHeight + gap
236 else
237 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.palette
252 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 }
263private fun toastRunAction(entry: ToastEntry, host: ToasterControl) {
264 val action = entry.data.value.action ?: return
265 if (entry.closing.value != None || !action.enabled || !host.read().enabled)
266 return
267 if (action.dismiss)
268 entry.model.close(entry.id, ToastCloseReason.Action)
269 action.onClick()