scoop:scoop.ui.component:0.1.0
src/conversation/bubble.scoop
Selected production source · src/conversation/bubble.scoop
1
package scoop.ui.component3
import scoop.graphics.*4
import scoop.graphics.text.*5
import scoop.ui.*6
import scoop.ui.style.*8
public enum BubbleVariant {9
Default, Secondary, Muted, Tinted, Outline, Ghost, Destructive10
}12
public enum BubbleReactionSide {13
Top, Bottom14
}16
public class BubbleReactions public constructor(17
public val content: View,18
public val side: BubbleReactionSide = BubbleReactionSide.Bottom,19
public val align: MessageAlignment = MessageAlignment.End20
)22
public class BubbleProps public constructor(23
public val content: ButtonContent,24
public val variant: BubbleVariant = BubbleVariant.Default,25
public val align: MessageAlignment = MessageAlignment.Start,26
public val maxWidthFraction: Double? = None,27
public val reactions: BubbleReactions? = None,28
public val actions: View? = None,29
public val onClick: (() -> Unit)? = None,30
public val enabled: Boolean = true,31
public val focus: FocusOptions = FocusOptions(),32
public val focusOutline: OutlineStyle? = None,33
public val fonts: FontCollection? = None,34
public val theme: ThemeSource? = None,35
public val style: StyleOverride = styleOverride(),36
public val semantics: ComponentSemantics = ComponentSemantics()37
) {38
init {39
when (maxWidthFraction) {40
case Some(val value) -> requireComponent(41
value.isFinite() && value > 0.0 && value <= 1.0,42
"bubble width fraction must be in (0, 1]"43
)44
case None -> {}45
}46
}47
}49
public class BubbleBuilder internal constructor(): ConversationActionBuilder() {50
public var variant: BubbleVariant = BubbleVariant.Default51
public var align: MessageAlignment = MessageAlignment.Start52
public var maxWidthFraction: Double? = None53
public var reactions: BubbleReactions? = None54
public var actions: View? = None55
public fun reactions(56
content: View,57
side: BubbleReactionSide = BubbleReactionSide.Bottom,58
align: MessageAlignment = MessageAlignment.End59
) {60
reactions = Some(BubbleReactions(content, side, align))61
}62
internal fun snapshot(content: ButtonContent): BubbleProps =63
BubbleProps(64
content,65
variant,66
align,67
maxWidthFraction,68
reactions,69
actions,70
onClick,71
enabled,72
focus,73
focusOutline,74
fonts,75
theme,76
style,77
semantics78
)79
}81
public class Bubble(private val props: Props<BubbleProps>, scope: ComponentScope): Component(82
scope83
) {84
private val themes = ThemeAccess(scope)85
private val text = ActionTextStyle()86
public override fun onMount() {87
themes.mount(props.value.theme)88
}89
public override fun render(): View {90
val input = props.value91
val theme = themes.read(input.theme)92
val colors = bubbleColors(input.variant, theme)93
val density = theme.tokens.typography.body.fontSize / 14.094
val ghost = input.variant == BubbleVariant.Ghost95
val body =96
conversationSurface(97
ConversationSurfaceProps(98
input.content,99
theme,100
text.resolve(101
theme.tokens.typography.body,102
colors.foreground,103
None,104
lineHeight = Some(1.625)105
),106
conversationStyle(107
colors.background,108
colors.foreground,109
if (ghost)110
Insets()111
else112
Insets(12.0 * density, 8.0 * density, 12.0 * density, 8.0 * density),113
if (ghost)114
0.0115
else116
12.0 * density,117
colors.border,118
colors.hover,119
input.focusOutline,120
if (ghost)121
styleOverride { this.border(None) }.then(input.style)122
else123
input.style124
),125
input.onClick,126
input.enabled,127
input.focus,128
input.fonts,129
semantics = input.semantics130
)131
)132
val reactionInset =133
lowerBound(134
2.0 * density,135
theme.surfaceMetrics(SurfaceVisual(SurfaceKind.Plain)).focusOutset136
)137
val reactions =138
when (input.reactions) {139
case Some(val value) -> panelFrame(140
value.content,141
Insets(6.0 * density, reactionInset, 6.0 * density, reactionInset),142
styleOverride {143
this.background(Some(Paint(theme.tokens.palette.muted.background)))144
this.border(145
Some(Border.all(3.0 * density, theme.tokens.palette.card.background))146
)147
this.corners(Corners.all(999.0))148
}149
)150
case None -> empty()151
}152
return element(153
BubbleLayoutProps(154
input.align,155
input.maxWidthFraction ?:156
if (ghost)157
1.0158
else159
0.8,160
input.reactions,161
panelPresent(input.actions),162
input.enabled,163
density164
),165
{ current, mount -> BubbleLayout(current, mount) },166
[panelSlot(body, 0L), panelSlot(reactions, 1L), panelSlot(input.actions ?: empty(), 2L)]167
)168
}169
}171
public fun bubble(text: String, configure: BubbleBuilder.() -> Unit = {}): View =172
makeBubble(conversationText(text), configure)174
public fun bubble(text: StyledText, configure: BubbleBuilder.() -> Unit = {}): View =175
makeBubble(conversationText(text), configure)177
public fun bubble(content: View, configure: BubbleBuilder.() -> Unit = {}): View =178
makeBubble(ButtonContent.Custom(content), configure)180
public fun bubble(181
content: (ActionContentStyle) -> View,182
configure: BubbleBuilder.() -> Unit = {}183
): View = makeBubble(ButtonContent.Resolved(content), configure)185
internal fun makeBubble(content: ButtonContent, configure: BubbleBuilder.() -> Unit): View {186
val builder = BubbleBuilder();187
builder.configure()188
return component(189
builder.snapshot(content),190
{ current, scope -> Bubble(current, scope) },191
builder.key192
)193
}