scoop:scoop.ui.component:0.1.0
src/conversation/attachment.scoop
Selected production source · src/conversation/attachment.scoop
1
package scoop.ui.component3
import scoop.graphics.*4
import scoop.graphics.Image as GraphicsImage5
import scoop.graphics.text.*6
import scoop.ui.*7
import scoop.ui.style.*9
public enum AttachmentState {10
Idle, Uploading, Processing, Error, Done11
}13
public enum AttachmentSize {14
Default, Sm, Xs15
}17
public enum AttachmentMedia {18
Icon(ButtonContent), Image(GraphicsImage, Size)19
}21
public 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()39
)41
public class AttachmentBuilder internal constructor(): ConversationActionBuilder() {42
public var description: PanelContent? = None43
public var content: View? = None44
public var media: AttachmentMedia? = None45
public var actions: View? = None46
public var state: AttachmentState = AttachmentState.Done47
public var size: AttachmentSize = AttachmentSize.Default48
public var orientation: Orientation = Orientation.Horizontal49
public var shimmer: Boolean = true50
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
semantics87
)88
}90
public class Attachment(91
private val props: Props<AttachmentProps>,92
scope: ComponentScope93
): 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.value103
val theme = themes.read(input.theme)104
val p = theme.tokens.palette105
val d = theme.tokens.typography.body.fontSize / 14.0106
val metrics = attachmentMetrics(input.size)107
val busy =108
input.state == AttachmentState.Uploading || input.state == AttachmentState.Processing109
val error = input.state == AttachmentState.Error110
val color =111
if (error)112
p.destructive.background113
else114
p.card.foreground115
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
else129
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
0L160
)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 * d167
this.alignment = CrossAlignment.Stretch168
}169
val iconSide =170
if (input.orientation == Orientation.Vertical)171
24.0 * d172
else173
if (input.size == AttachmentSize.Xs)174
14.0 * d175
else176
16.0 * d177
val media =178
when (input.media) {179
case Some(AttachmentMedia.Image(val image, val natural)) -> imageView(180
image,181
natural182
) {183
this.fit = ContentFit.Cover184
this.opacity =185
if (186
input.state ==187
AttachmentState.Done ||188
input.state ==189
AttachmentState.Idle190
)191
1.0192
else193
0.6194
}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
false203
)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.theme211
}212
else213
if (error)214
toastIcon(ToastKind.Error, iconSide, color)215
else216
attachmentFileIcon(iconSide, color)217
}218
val mediaFrame =219
element(220
AttachmentMediaFrameProps(221
when (input.media) {222
case Some(AttachmentMedia.Image(_, _)) -> true223
else -> false224
},225
if (error)226
p.destructive.background.withAlpha(0.1)227
else228
p.muted.background,229
if (input.size == AttachmentSize.Xs)230
6.0 * d231
else232
8.0 * d233
),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
d246
),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 * d257
else258
12.0 * d,259
if (input.state == AttachmentState.Idle)260
Color.Transparent261
else262
if (error)263
p.destructive.background.withAlpha(0.3)264
else265
p.border,266
mixThemeColor(p.card.background, p.muted.background, 0.5),267
input.focusOutline,268
input.style269
)270
val ring =271
if (!panelPresent(input.onClick))272
0.0273
else274
theme.surfaceMetrics(275
SurfaceVisual(SurfaceKind.Plain),276
style.surface,277
scope.layoutDirection278
).focusOutset279
return element(280
AttachmentOutlineProps(281
input.state == AttachmentState.Idle,282
p.border,283
if (input.size == AttachmentSize.Xs)284
8.0 * d285
else286
12.0 * d,287
ring288
),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.Processing309
}310
)311
)312
]313
)314
}315
}317
internal struct AttachmentMetrics(318
val media: Double,319
val gap: Double,320
val padding: Double,321
val fontSize: Double322
)324
internal 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
}331
public fun attachment(title: String, configure: AttachmentBuilder.() -> Unit = {}): View =332
makeAttachment(conversationText(title), configure)334
public fun attachment(title: StyledText, configure: AttachmentBuilder.() -> Unit = {}): View =335
makeAttachment(conversationText(title), configure)337
public fun attachment(title: View, configure: AttachmentBuilder.() -> Unit = {}): View =338
makeAttachment(ButtonContent.Custom(title), configure)340
internal 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.key347
)348
}