scoop:scoop.ui.component:0.1.0
src/navigation/breadcrumb.scoop
Selected production source · src/navigation/breadcrumb.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.*8
import scoop.ui.component.row as makeRow9
import scoop.ui.component.wrap as makeWrapped11
public class Breadcrumb public constructor(12
private val props: Props<BreadcrumbProps>,13
scope: ComponentScope14
): Component(scope) {15
private val themes = ThemeAccess(scope)16
public override fun onMount() {17
themes.mount(props.value.theme)18
}19
public override fun render(): View {20
val input = props.value21
val theme = themes.read(input.theme)22
val density = theme.tokens.typography.action.fontSize / 14.023
return semanticContent(24
panelFrame(25
makeWrapped {26
for (index in 0L ..< input.entries.size) {27
val entry = input.entries[index]28
this.item(29
component(30
BreadcrumbRowProps(entry, index == 0L, input, theme),31
{ current, mount -> BreadcrumbRow(current, scope = mount) },32
entry.key33
)34
) { this.max = entry.maxWidth }35
}36
this.spacing = input.spacing * density37
this.lineSpacing = input.lineSpacing * density38
this.alignment = CrossAlignment.Center39
},40
visual = input.style41
),42
SemanticRole.Group,43
input.semantics.properties(enabled = input.enabled),44
relations = input.semantics.relations,45
mergeLabels = false,46
reference = input.semantics.reference47
)48
}49
}51
private class BreadcrumbRowProps(52
val entry: BreadcrumbEntry,53
val first: Boolean,54
val root: BreadcrumbProps,55
val theme: Theme56
)58
private class BreadcrumbRow(59
private val props: Props<BreadcrumbRowProps>,60
scope: ComponentScope61
): Component(scope) {62
public override fun render(): View {63
val input = props.value64
val entry = input.entry65
val theme = input.theme66
val density = theme.tokens.typography.action.fontSize / 14.067
val plain =68
theme.tokens.typography.action.copy {69
this.weight = 400;70
this.lineHeight = Some(20.0 / 14.0);71
this.color = theme.tokens.palette.background.foreground72
}73
val body =74
when (entry.content) {75
case Link(val content, val action) -> semanticButton(76
ButtonProps(77
ButtonContent.Resolved {78
appearance ->79
val active =80
appearance.interaction.enabled &&81
(82
appearance.interaction.hovered ||83
appearance.interaction.pressed84
)85
val overrideText =86
when (entry.style) {87
case Some(val value) -> value.text;88
case None -> None89
}90
val overrideColor =91
when (overrideText) {92
case Some(val value) -> value.color;93
case None -> None94
}95
val color =96
overrideColor ?:97
if (active)98
theme.tokens.palette.background.foreground99
else100
theme.tokens.palette.muted.foreground101
actionContent(102
navigationLabel(content, input.root.fonts),103
ActionContentStyle(104
appearance.text.copy { this.color = color },105
appearance.iconSize,106
appearance.spacing,107
appearance.interaction,108
false109
)110
)111
},112
action,113
input.root.enabled && entry.enabled,114
breadcrumbButtonStyle(theme, entry.style),115
input.root.theme,116
entry.focus,117
variant = ButtonVariant.Ghost,118
semantics = entry.semantics119
),120
SemanticRole.Link,121
entry.semantics.properties(enabled = input.root.enabled && entry.enabled),122
SemanticActions(123
invoke =124
Some {125
action();126
SemanticActionResult.Completed127
}128
)129
)130
case Current(val content) -> semanticContent(131
actionContent(132
navigationLabel(content, input.root.fonts),133
ActionContentStyle(134
plain,135
16.0 * density,136
6.0 * density,137
InteractionVisual(),138
false139
)140
),141
SemanticRole.Link,142
entry.semantics.properties(selected = Some(true)),143
relations = entry.semantics.relations,144
reference = entry.semantics.reference145
)146
case Ellipsis -> navigationEllipsis(147
20.0 * density,148
16.0 * density,149
theme.tokens.palette.muted.foreground150
)151
case Custom(val content) -> content152
}153
return makeRow {154
this.add(155
if (input.first)156
empty()157
else158
input.root.separator ?:159
navigationIcon(160
NavigationGlyph.End,161
14.0 * density,162
theme.tokens.palette.muted.foreground163
)164
)165
this.add(body)166
this.spacing =167
if (input.first)168
0.0169
else170
input.root.spacing * density171
this.alignment = CrossAlignment.Center172
}173
}174
}176
internal fun navigationLabel(177
content: ButtonContent,178
fonts: FontCollection?,179
alignment: TextAlignment = TextAlignment.Start180
): ButtonContent =181
when (content) {182
case Label(val label) -> ButtonContent.Label(183
LabelProps(184
label.text,185
if (!panelPresent(label.fonts))186
fonts187
else188
label.fonts,189
if (!panelPresent(label.settings))190
Some(191
paragraphStyle {192
this.maxLines = Some(1L);193
this.alignment = alignment194
}195
)196
else197
label.settings,198
label.inlines,199
label.theme,200
label.baseStyle,201
label.underline,202
label.semantics203
)204
)205
else -> content206
}208
internal fun breadcrumbButtonStyle(theme: Theme, value: ButtonStyle?): ButtonStyle {209
val local = value ?: buttonStyle { this.kind = SurfaceKind.Plain }210
val density = theme.tokens.typography.action.fontSize / 14.0211
return local.copy {212
this.surface =213
this.surface.copy {214
if (this.padding == None)215
this.padding = Some(Insets())216
if (this.suggestedSize == None)217
this.suggestedSize = Some(Size(0.0, 20.0 * density))218
if (this.minimumSize == None)219
this.minimumSize = Some(Size(0.0, 0.0))220
this.style =221
styleOverride {222
this.background(Some(Paint(Color.Transparent)));223
this.border(None);224
this.boxShadows([])225
}.then(this.style)226
}227
this.text =228
Some(229
(this.text ?: spanStyle()).copy {230
if (this.weight == None)231
this.weight = Some(400)232
if (this.lineHeight == None)233
this.lineHeight = Some(Some(20.0 / 14.0))234
}235
)236
this.states =237
this.states.copy {238
this.pressed =239
styleOverride {240
this.background(241
Some(Paint(theme.tokens.palette.background.foreground.withAlpha(0.08)))242
)243
}.then(this.pressed)244
}245
}246
}