scoop:scoop.ui.component:0.1.0
src/conversation/marker.scoop
Selected production source · src/conversation/marker.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 MarkerVariant {9
Default, Border, Separator10
}12
public class MarkerProps public constructor(13
public val content: ButtonContent,14
public val icon: View? = None,15
public val variant: MarkerVariant = MarkerVariant.Default,16
public val loading: Boolean = false,17
public val shimmer: Boolean = true,18
public val onClick: (() -> Unit)? = None,19
public val enabled: Boolean = true,20
public val focus: FocusOptions = FocusOptions(),21
public val focusOutline: OutlineStyle? = None,22
public val fonts: FontCollection? = None,23
public val theme: ThemeSource? = None,24
public val style: StyleOverride = styleOverride(),25
public val semantics: ComponentSemantics = ComponentSemantics()26
)28
public class MarkerBuilder internal constructor(): ConversationActionBuilder() {29
public var icon: View? = None30
public var variant: MarkerVariant = MarkerVariant.Default31
public var loading: Boolean = false32
public var shimmer: Boolean = true33
internal fun snapshot(content: ButtonContent): MarkerProps =34
MarkerProps(35
content,36
icon,37
variant,38
loading,39
shimmer,40
onClick,41
enabled,42
focus,43
focusOutline,44
fonts,45
theme,46
style,47
semantics48
)49
}51
public class Marker(private val props: Props<MarkerProps>, scope: ComponentScope): Component(52
scope53
) {54
private val themes = ThemeAccess(scope)55
private val text = ActionTextStyle()56
public override fun onMount() {57
themes.mount(props.value.theme)58
}59
public override fun render(): View {60
val input = props.value61
val theme = themes.read(input.theme)62
val p = theme.tokens.palette63
val base = text.resolve(theme.tokens.typography.body, p.muted.foreground, None)64
val density = base.fontSize / 14.065
val hasIcon = panelPresent(input.icon) || input.loading66
val icon =67
input.icon ?:68
spinner {69
this.size = Some(16.0 * density);70
this.running = input.loading && input.enabled71
this.color = Some(p.muted.foreground);72
this.theme = input.theme73
}74
val content = conversationCaption(input.content, base, input.fonts, scope.layoutDirection)75
val line =76
element(77
MarkerLayoutProps(input.variant, hasIcon, density, p.border),78
{ current, mount -> MarkerLayout(current, mount) },79
[80
panelSlot(icon, 0L),81
panelSlot(82
conversationShimmer(83
content,84
input.loading && input.shimmer && input.enabled,85
theme86
),87
1L88
)89
]90
)91
return conversationSurface(92
ConversationSurfaceProps(93
ButtonContent.Custom(line),94
theme,95
base,96
conversationStyle(97
Color.Transparent,98
p.muted.foreground,99
Insets(),100
4.0 * density,101
hover =102
if (panelPresent(input.onClick))103
p.muted.background104
else105
Color.Transparent,106
outline = input.focusOutline,107
local = styleOverride { this.border(None) }.then(input.style)108
),109
input.onClick,110
input.enabled,111
input.focus,112
input.fonts,113
semantics = input.semantics.copy { busy = busy || input.loading }114
)115
)116
}117
}119
internal class MarkerLayoutProps(120
val variant: MarkerVariant,121
val icon: Boolean,122
val density: Double,123
val color: Color124
)126
internal struct MarkerLines(val left: Double, val right: Double, val y: Double)128
internal class MarkerLayout(129
private val props: Props<MarkerLayoutProps>,130
scope: ComponentScope131
): Element<MarkerLines>(scope) {132
public override fun measure(context: MeasureContext, constraints: Constraints): LayoutResult<133
MarkerLines134
> {135
val input = props.value136
val d = input.density137
val iconSize =138
if (input.icon)139
16.0 * d140
else141
0.0142
val iconSpace =143
if (input.icon)144
iconSize + 8.0 * d145
else146
0.0147
val reserve =148
if (input.variant == MarkerVariant.Separator && panelPresent(constraints.maxWidth))149
32.0 * d150
else151
0.0152
val body =153
conversationMeasure(154
context,155
1L,156
insetMaximum(constraints.maxWidth, iconSpace + reserve)157
)158
if (input.icon)159
context.measureChild(0L, Constraints.tight(Size(iconSize, iconSize)))160
else161
context.skipChild(0L)162
val height = lowerBound(16.0 * d, lowerBound(iconSize, body.size.height))163
val size =164
constraints.constrain(165
Size(166
constraints.maxWidth ?: (iconSpace + body.size.width),167
height +168
if (input.variant == MarkerVariant.Border)169
8.0 * d + 1.0170
else171
0.0172
)173
)174
val contentWidth = iconSpace + body.size.width175
val x =176
if (input.variant == MarkerVariant.Separator)177
lowerBound(0.0, (size.width - contentWidth) / 2.0)178
else179
0.0180
if (input.icon)181
context.placeChild(182
0L,183
Point(184
logicalX(x, iconSize, size.width, context.layoutDirection),185
(height - iconSize) / 2.0186
)187
)188
context.placeChild(189
1L,190
Point(191
logicalX(x + iconSpace, body.size.width, size.width, context.layoutDirection),192
(height - body.size.height) / 2.0193
)194
)195
return LayoutResult(196
LayoutMetrics(size),197
MarkerLines(198
lowerBound(0.0, x - 12.0 * d),199
lowerBound(0.0, size.width - x - contentWidth - 12.0 * d),200
height / 2.0201
)202
)203
}204
public override fun scene(context: SceneContext, layout: MarkerLines) {205
val input = props.value206
val size = context.size207
context.children()208
context.draw {209
paint ->210
if (input.variant == MarkerVariant.Border && size.height >= 1.0)211
paint.canvas.drawRect(212
Rect(0.0, size.height - 1.0, size.width, 1.0),213
Paint(input.color)214
)215
if (input.variant == MarkerVariant.Separator) {216
paint.canvas.drawRect(Rect(0.0, layout.y, layout.left, 1.0), Paint(input.color))217
paint.canvas.drawRect(218
Rect(size.width - layout.right, layout.y, layout.right, 1.0),219
Paint(input.color)220
)221
}222
}223
}224
}226
public fun marker(text: String, configure: MarkerBuilder.() -> Unit = {}): View =227
makeMarker(conversationText(text), configure)229
public fun marker(text: StyledText, configure: MarkerBuilder.() -> Unit = {}): View =230
makeMarker(conversationText(text), configure)232
public fun marker(content: View, configure: MarkerBuilder.() -> Unit = {}): View =233
makeMarker(ButtonContent.Custom(content), configure)235
public fun marker(236
content: (ActionContentStyle) -> View,237
configure: MarkerBuilder.() -> Unit = {}238
): View = makeMarker(ButtonContent.Resolved(content), configure)240
internal fun makeMarker(content: ButtonContent, configure: MarkerBuilder.() -> Unit): View {241
val builder = MarkerBuilder();242
builder.configure()243
return component(244
builder.snapshot(content),245
{ current, mount -> Marker(current, mount) },246
builder.key247
)248
}