scoop:scoop.ui.component:0.1.0
src/conversation/trace.scoop
Selected production source · src/conversation/trace.scoop
1
package scoop.ui.component3
import scoop.graphics.*4
import scoop.graphics.text.*5
import scoop.ui.*6
import scoop.ui.style.*7
import scoop.window.accessibility.*9
public enum TraceStatus {10
Complete, Running, Error, Pending11
}13
public class TraceLineProps public constructor(14
public val title: ButtonContent,15
public val status: TraceStatus = TraceStatus.Complete,16
public val icon: View? = None,17
public val description: PanelContent? = None,18
public val shimmer: Boolean = true,19
public val onClick: (() -> Unit)? = None,20
public val enabled: Boolean = true,21
public val focus: FocusOptions = FocusOptions(),22
public val focusOutline: OutlineStyle? = None,23
public val fonts: FontCollection? = None,24
public val theme: ThemeSource? = None,25
public val style: StyleOverride = styleOverride(),26
public val semantics: ComponentSemantics = ComponentSemantics()27
)29
public open class TraceLineBuilder internal constructor(): ConversationActionBuilder() {30
public var status: TraceStatus = TraceStatus.Complete31
public var icon: View? = None32
public var description: PanelContent? = None33
public var shimmer: Boolean = true34
public fun description(text: String) {35
description = Some(PanelContent.Label(LabelProps(LabelText.Plain(text, None))))36
}37
public fun description(text: StyledText) {38
description = Some(PanelContent.Label(LabelProps(LabelText.Styled(text))))39
}40
public fun description(content: View) {41
description = Some(PanelContent.Custom(content))42
}43
internal fun line(title: ButtonContent): TraceLineProps =44
TraceLineProps(45
title,46
status,47
icon,48
description,49
shimmer,50
onClick,51
enabled,52
focus,53
focusOutline,54
fonts,55
theme,56
style,57
semantics58
)59
}61
public class TraceBuilder internal constructor(): TraceLineBuilder() {62
public var open: Binding<Boolean>? = None63
public var retention: ContentRetention = ContentRetention.KeepMounted64
public var onOpenChange: (Boolean) -> Unit = {}65
}67
public class TraceProps public constructor(68
public val line: TraceLineProps,69
public val content: View,70
public val open: Binding<Boolean>? = None,71
public val retention: ContentRetention = ContentRetention.KeepMounted,72
public val onOpenChange: (Boolean) -> Unit = {}73
)75
internal class TraceExpansion {76
private val choice = state<Boolean?>(None)77
fun read(status: TraceStatus): Boolean = choice.value ?: (status == TraceStatus.Running)78
fun choose(value: Boolean) {79
choice.value = Some(value)80
}81
}83
public class Trace(private val props: Props<TraceProps>, scope: ComponentScope): Component(scope) {84
private val themes = ThemeAccess(scope)85
private val expansion = TraceExpansion()86
private val opened =87
binding(88
{ props.value.open?.value ?: expansion.read(props.value.line.status) },89
{ value -> change(value) }90
)91
public override fun onMount() {92
themes.mount(props.value.line.theme)93
}94
private fun change(value: Boolean) {95
val input = props.value96
if (!scope.isMounted || !input.line.enabled)97
return98
val previous = opened.value99
when (input.open) {100
case Some(val binding) -> binding.value = value101
case None -> expansion.choose(value)102
}103
if (scope.isMounted && previous != opened.value)104
props.value.onOpenChange(opened.value)105
}106
private fun expandSemantically(value: Boolean): SemanticActionResult {107
if (!scope.isMounted)108
return SemanticActionResult.Unavailable109
if (!props.value.line.enabled)110
return SemanticActionResult.Rejected111
change(value)112
return if (opened.value == value)113
SemanticActionResult.Completed114
else115
SemanticActionResult.Rejected116
}117
private fun activate() {118
change(!opened.value)119
if (scope.isMounted)120
props.value.line.onClick?.invoke()121
}122
public override fun render(): View {123
val input = props.value124
val theme = themes.read(input.line.theme)125
val density = theme.tokens.typography.body.fontSize / 14.0126
val expanded = opened.value127
val openBinding = opened128
val line =129
component(130
TraceLineInput(131
input.line,132
Some(expanded),133
Some({ activate() }),134
Some { next -> expandSemantically(next) }135
),136
{ current, mount -> TraceLine(current, mount) }137
)138
val body =139
frame(input.content) {140
this.logicalPadding =141
Some(142
LogicalInsets(143
start = 16.0 * density,144
end = 4.0 * density,145
top = 8.0 * density,146
bottom = 4.0 * density147
)148
)149
this.style =150
styleOverride {151
this.border(152
Some(153
Border.Logical(start = BorderEdge(1.0, theme.tokens.palette.border))154
)155
)156
}157
this.alignment = Alignment(AxisAlignment.Start, AxisAlignment.Start)158
}159
return makeColumn {160
this.add(panelSlot(line, 0L))161
this.add(162
panelSlot(163
component(164
CollapsibleProps(165
openBinding,166
body,167
enabled = input.line.enabled,168
retention = input.retention,169
spacing = 0.0,170
theme = input.line.theme171
),172
{ current, mount -> Collapsible(current, mount) }173
),174
1L175
)176
)177
this.spacing = 0.0178
this.alignment = CrossAlignment.Stretch179
}180
}181
}183
internal class TraceLineInput(184
val line: TraceLineProps,185
val expanded: Boolean? = None,186
val action: (() -> Unit)? = None,187
val expand: ((Boolean) -> SemanticActionResult)? = None188
)190
internal class TraceLine(191
private val props: Props<TraceLineInput>,192
scope: ComponentScope193
): Component(scope) {194
private val themes = ThemeAccess(scope)195
private val titles = ActionTextStyle()196
private val descriptions = ActionTextStyle()197
public override fun onMount() {198
themes.mount(props.value.line.theme)199
}200
public override fun render(): View {201
val frame = props.value202
val input = frame.line203
val theme = themes.read(input.theme)204
val p = theme.tokens.palette205
val color =206
if (input.status == TraceStatus.Error)207
p.destructive.background208
else209
p.muted.foreground210
val base = titles.resolve(theme.tokens.typography.body, color, None)211
val detail =212
descriptions.resolve(213
theme.tokens.typography.body,214
p.muted.foreground.withAlpha(0.7),215
None216
)217
val d = base.fontSize / 14.0218
val icon =219
input.icon ?:220
when (input.status) {221
case Running -> spinner {222
this.size = Some(16.0 * d);223
this.color = Some(color);224
this.running = input.enabled;225
this.theme = input.theme226
}227
case Error -> toastIcon(ToastKind.Error, 16.0 * d, color)228
case Pending -> navigationIcon(NavigationGlyph.More, 16.0 * d, color)229
case Complete -> empty()230
}231
val hasIcon = panelPresent(input.icon) || input.status != TraceStatus.Complete232
val title =233
conversationCaption(input.title, base, input.fonts, scope.layoutDirection, Some(1L))234
val description =235
when (input.description) {236
case Some(PanelContent.Label(val label)) -> conversationCaption(237
ButtonContent.Label(label),238
detail,239
input.fonts,240
scope.layoutDirection,241
Some(1L)242
)243
case Some(val value) -> panelCaption(value, detail, input.fonts)244
case None -> empty()245
}246
val arrow =247
when (frame.expanded) {248
case Some(val open) -> navigationIcon(249
if (open)250
NavigationGlyph.Down251
else252
NavigationGlyph.End,253
16.0 * d,254
color255
)256
case None -> empty()257
}258
val row =259
element(260
TraceLineLayoutProps(261
hasIcon,262
panelPresent(input.description),263
panelPresent(frame.expanded),264
d265
),266
{ current, mount -> TraceLineLayout(current, mount) },267
[268
panelSlot(icon, 0L),269
panelSlot(270
conversationShimmer(271
title,272
input.status == TraceStatus.Running && input.shimmer && input.enabled,273
theme274
),275
1L276
),277
panelSlot(description, 2L),278
panelSlot(arrow, 3L)279
]280
)281
val action =282
when (frame.action) {283
case Some(_) -> frame.action;284
case None -> input.onClick285
}286
return conversationShimmer(287
conversationSurface(288
ConversationSurfaceProps(289
ButtonContent.Custom(row),290
theme,291
base,292
conversationStyle(293
Color.Transparent,294
color,295
Insets(),296
4.0 * d,297
hover =298
if (panelPresent(action))299
p.muted.background.withAlpha(0.3)300
else301
Color.Transparent,302
outline = input.focusOutline,303
local = styleOverride { this.border(None) }.then(input.style)304
),305
action,306
input.enabled,307
input.focus,308
input.fonts,309
semantics =310
input.semantics.copy { busy = busy || input.status == TraceStatus.Running },311
semanticRole =312
if (props.value.expanded != None)313
Some(SemanticRole.Disclosure)314
else315
None,316
semanticState =317
Some(318
input.semantics.properties(319
enabled = input.enabled,320
expanded = props.value.expanded321
).copy { busy = busy || input.status == TraceStatus.Running }322
),323
semanticActions =324
when (props.value.expand) {325
case Some(val expand) -> Some(326
SemanticActions(327
invoke =328
Some {329
val result = expand(!(props.value.expanded ?: false))330
if (scope.isMounted)331
props.value.line.onClick?.invoke()332
if (scope.isMounted)333
result334
else335
SemanticActionResult.Unavailable336
},337
setExpanded = Some(expand)338
)339
)340
case None -> None341
}342
)343
),344
false,345
theme,346
if (input.status == TraceStatus.Pending)347
0.6348
else349
1.0350
)351
}352
}354
public fun traceLine(title: String, configure: TraceLineBuilder.() -> Unit = {}): View =355
makeTraceLine(conversationText(title), configure)357
public fun traceLine(title: StyledText, configure: TraceLineBuilder.() -> Unit = {}): View =358
makeTraceLine(conversationText(title), configure)360
public fun traceLine(title: View, configure: TraceLineBuilder.() -> Unit = {}): View =361
makeTraceLine(ButtonContent.Custom(title), configure)363
public fun traceLine(364
title: (ActionContentStyle) -> View,365
configure: TraceLineBuilder.() -> Unit = {}366
): View = makeTraceLine(ButtonContent.Resolved(title), configure)368
internal fun makeTraceLine(title: ButtonContent, configure: TraceLineBuilder.() -> Unit): View {369
val builder = TraceLineBuilder();370
builder.configure()371
return component(372
TraceLineInput(builder.line(title)),373
{ current, mount -> TraceLine(current, mount) },374
builder.key375
)376
}378
public fun trace(title: String, content: View, configure: TraceBuilder.() -> Unit = {}): View =379
makeTrace(conversationText(title), content, configure)381
public fun trace(title: StyledText, content: View, configure: TraceBuilder.() -> Unit = {}): View =382
makeTrace(conversationText(title), content, configure)384
public fun trace(title: View, content: View, configure: TraceBuilder.() -> Unit = {}): View =385
makeTrace(ButtonContent.Custom(title), content, configure)387
internal fun makeTrace(388
title: ButtonContent,389
content: View,390
configure: TraceBuilder.() -> Unit391
): View {392
val builder = TraceBuilder();393
builder.configure()394
return component(395
TraceProps(396
builder.line(title),397
content,398
builder.open,399
builder.retention,400
builder.onOpenChange401
),402
{ current, mount -> Trace(current, mount) },403
builder.key404
)405
}