Scoop API

scoop:scoop.ui.component:0.1.0

src/conversation/attachment.scoop

Selected production source · src/conversation/attachment.scoop

1package scoop.ui.component
3import scoop.graphics.*
4import scoop.graphics.Image as GraphicsImage
5import scoop.graphics.text.*
6import scoop.ui.*
7import scoop.ui.style.*
9public enum AttachmentState {
10 Idle, Uploading, Processing, Error, Done
13public enum AttachmentSize {
14 Default, Sm, Xs
17public enum AttachmentMedia {
18 Icon(ButtonContent), Image(GraphicsImage, Size)
21public class AttachmentProps public constructor(
22 public val title: ButtonContent,
23 public val description: PanelContent? = None,
24 public val content: View? = None,
25 public val media: AttachmentMedia? = None,
26 public val actions: View? = None,
27 public val state: AttachmentState = AttachmentState.Done,
28 public val size: AttachmentSize = AttachmentSize.Default,
29 public val orientation: Orientation = Orientation.Horizontal,
30 public val shimmer: Boolean = true,
31 public val onClick: (() -> Unit)? = None,
32 public val enabled: Boolean = true,
33 public val focus: FocusOptions = FocusOptions(),
34 public val focusOutline: OutlineStyle? = None,
35 public val fonts: FontCollection? = None,
36 public val theme: ThemeSource? = None,
37 public val style: StyleOverride = styleOverride(),
38 public val semantics: ComponentSemantics = ComponentSemantics()
41public class AttachmentBuilder internal constructor(): ConversationActionBuilder() {
42 public var description: PanelContent? = None
43 public var content: View? = None
44 public var media: AttachmentMedia? = None
45 public var actions: View? = None
46 public var state: AttachmentState = AttachmentState.Done
47 public var size: AttachmentSize = AttachmentSize.Default
48 public var orientation: Orientation = Orientation.Horizontal
49 public var shimmer: Boolean = true
50 public fun description(text: String) {
51 description = Some(PanelContent.Label(LabelProps(LabelText.Plain(text, None))))
52 }
53 public fun description(text: StyledText) {
54 description = Some(PanelContent.Label(LabelProps(LabelText.Styled(text))))
55 }
56 public fun description(content: View) {
57 description = Some(PanelContent.Custom(content))
58 }
59 public fun media(content: View) {
60 media = Some(AttachmentMedia.Icon(ButtonContent.Custom(content)))
61 }
62 public fun media(content: (ActionContentStyle) -> View) {
63 media = Some(AttachmentMedia.Icon(ButtonContent.Resolved(content)))
64 }
65 public fun image(image: GraphicsImage, naturalSize: Size) {
66 media = Some(AttachmentMedia.Image(image, naturalSize))
67 }
68 internal fun snapshot(title: ButtonContent): AttachmentProps =
69 AttachmentProps(
70 title,
71 description,
72 content,
73 media,
74 actions,
75 state,
76 size,
77 orientation,
78 shimmer,
79 onClick,
80 enabled,
81 focus,
82 focusOutline,
83 fonts,
84 theme,
85 style,
86 semantics
87 )
90public class Attachment(
91 private val props: Props<AttachmentProps>,
92 scope: ComponentScope
93): Component(scope) {
94 private val themes = ThemeAccess(scope)
95 private val titles = ActionTextStyle()
96 private val descriptions = ActionTextStyle()
97 private val medium = spanStyle { this.weight = Some(500) }
98 public override fun onMount() {
99 themes.mount(props.value.theme)
100 }
101 public override fun render(): View {
102 val input = props.value
103 val theme = themes.read(input.theme)
104 val p = theme.tokens.palette
105 val d = theme.tokens.typography.body.fontSize / 14.0
106 val metrics = attachmentMetrics(input.size)
107 val busy =
108 input.state == AttachmentState.Uploading || input.state == AttachmentState.Processing
109 val error = input.state == AttachmentState.Error
110 val color =
111 if (error)
112 p.destructive.background
113 else
114 p.card.foreground
115 val titleStyle =
116 titles.resolve(
117 theme.tokens.typography.body,
118 p.card.foreground,
119 Some(medium),
120 metrics.fontSize / 14.0,
121 Some(1.25)
122 )
123 val detailStyle =
124 descriptions.resolve(
125 theme.tokens.typography.body,
126 if (error)
127 color.withAlpha(0.8)
128 else
129 p.muted.foreground,
130 None,
131 12.0 / 14.0,
132 Some(1.25)
133 )
134 val title =
135 conversationCaption(
136 input.title,
137 titleStyle,
138 input.fonts,
139 scope.layoutDirection,
140 Some(1L)
141 )
142 val detail =
143 when (input.description) {
144 case Some(PanelContent.Label(val value)) -> conversationCaption(
145 ButtonContent.Label(value),
146 detailStyle,
147 input.fonts,
148 scope.layoutDirection,
149 Some(1L)
150 )
151 case Some(val value) -> panelCaption(value, detailStyle, input.fonts)
152 case None -> empty()
153 }
154 val text =
155 makeColumn {
156 this.add(
157 panelSlot(
158 conversationShimmer(title, busy && input.shimmer && input.enabled, theme),
159 0L
160 )
161 )
162 if (panelPresent(input.description))
163 this.add(panelSlot(detail, 1L))
164 if (panelPresent(input.content))
165 this.add(panelSlot(input.content!!, 2L))
166 this.spacing = 2.0 * d
167 this.alignment = CrossAlignment.Stretch
168 }
169 val iconSide =
170 if (input.orientation == Orientation.Vertical)
171 24.0 * d
172 else
173 if (input.size == AttachmentSize.Xs)
174 14.0 * d
175 else
176 16.0 * d
177 val media =
178 when (input.media) {
179 case Some(AttachmentMedia.Image(val image, val natural)) -> imageView(
180 image,
181 natural
182 ) {
183 this.fit = ContentFit.Cover
184 this.opacity =
185 if (
186 input.state ==
187 AttachmentState.Done ||
188 input.state ==
189 AttachmentState.Idle
190 )
191 1.0
192 else
193 0.6
194 }
195 case Some(AttachmentMedia.Icon(val value)) -> actionContent(
196 value,
197 ActionContentStyle(
198 titleStyle.copy { this.color = color },
199 iconSide,
200 8.0 * d,
201 InteractionVisual(),
202 false
203 )
204 )
205 case None -> if (busy)
206 spinner {
207 this.size = Some(iconSide);
208 this.color = Some(color);
209 this.running = input.enabled;
210 this.theme = input.theme
211 }
212 else
213 if (error)
214 toastIcon(ToastKind.Error, iconSide, color)
215 else
216 attachmentFileIcon(iconSide, color)
217 }
218 val mediaFrame =
219 element(
220 AttachmentMediaFrameProps(
221 when (input.media) {
222 case Some(AttachmentMedia.Image(_, _)) -> true
223 else -> false
224 },
225 if (error)
226 p.destructive.background.withAlpha(0.1)
227 else
228 p.muted.background,
229 if (input.size == AttachmentSize.Xs)
230 6.0 * d
231 else
232 8.0 * d
233 ),
234 { current, mount -> AttachmentMediaFrame(current, mount) },
235 [media],
236 key = ViewKey.Number(0L)
237 )
238 val body =
239 element(
240 AttachmentLayoutProps(
241 input.orientation,
242 metrics.media * d,
243 metrics.gap * d,
244 panelPresent(input.actions),
245 d
246 ),
247 { current, mount -> AttachmentLayout(current, mount) },
248 [mediaFrame, panelSlot(text, 1L), panelSlot(input.actions ?: empty(), 2L)]
249 )
250 val style =
251 conversationStyle(
252 p.card.background,
253 color,
254 Insets.all(metrics.padding * d),
255 if (input.size == AttachmentSize.Xs)
256 8.0 * d
257 else
258 12.0 * d,
259 if (input.state == AttachmentState.Idle)
260 Color.Transparent
261 else
262 if (error)
263 p.destructive.background.withAlpha(0.3)
264 else
265 p.border,
266 mixThemeColor(p.card.background, p.muted.background, 0.5),
267 input.focusOutline,
268 input.style
269 )
270 val ring =
271 if (!panelPresent(input.onClick))
272 0.0
273 else
274 theme.surfaceMetrics(
275 SurfaceVisual(SurfaceKind.Plain),
276 style.surface,
277 scope.layoutDirection
278 ).focusOutset
279 return element(
280 AttachmentOutlineProps(
281 input.state == AttachmentState.Idle,
282 p.border,
283 if (input.size == AttachmentSize.Xs)
284 8.0 * d
285 else
286 12.0 * d,
287 ring
288 ),
289 { current, mount -> AttachmentOutline(current, mount) },
290 [
291 conversationSurface(
292 ConversationSurfaceProps(
293 ButtonContent.Custom(body),
294 theme,
295 titleStyle,
296 style,
297 input.onClick,
298 input.enabled,
299 input.focus,
300 input.fonts,
301 semantics =
302 input.semantics.copy {
303 this.busy =
304 this.busy ||
305 input.state ==
306 AttachmentState.Uploading ||
307 input.state ==
308 AttachmentState.Processing
309 }
310 )
311 )
312 ]
313 )
314 }
317internal struct AttachmentMetrics(
318 val media: Double,
319 val gap: Double,
320 val padding: Double,
321 val fontSize: Double
324internal fun attachmentMetrics(size: AttachmentSize): AttachmentMetrics =
325 when (size) {
326 case Default -> AttachmentMetrics(40.0, 8.0, 8.0, 14.0)
327 case Sm -> AttachmentMetrics(32.0, 10.0, 6.0, 12.0)
328 case Xs -> AttachmentMetrics(28.0, 6.0, 4.0, 12.0)
329 }
331public fun attachment(title: String, configure: AttachmentBuilder.() -> Unit = {}): View =
332 makeAttachment(conversationText(title), configure)
334public fun attachment(title: StyledText, configure: AttachmentBuilder.() -> Unit = {}): View =
335 makeAttachment(conversationText(title), configure)
337public fun attachment(title: View, configure: AttachmentBuilder.() -> Unit = {}): View =
338 makeAttachment(ButtonContent.Custom(title), configure)
340internal fun makeAttachment(title: ButtonContent, configure: AttachmentBuilder.() -> Unit): View {
341 val builder = AttachmentBuilder();
342 builder.configure()
343 return component(
344 builder.snapshot(title),
345 { current, mount -> Attachment(current, mount) },
346 builder.key
347 )