Scoop API

scoop:scoop.ui.component:0.1.0

src/navigation/breadcrumb.scoop

Selected production source · src/navigation/breadcrumb.scoop

1package scoop.ui.component
3import scoop.graphics.*
4import scoop.graphics.text.*
5import scoop.ui.*
6import scoop.ui.style.*
7import scoop.window.accessibility.*
8import scoop.ui.component.row as makeRow
9import scoop.ui.component.wrap as makeWrapped
11public class Breadcrumb public constructor(
12 private val props: Props<BreadcrumbProps>,
13 scope: ComponentScope
14): 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.value
21 val theme = themes.read(input.theme)
22 val density = theme.tokens.typography.action.fontSize / 14.0
23 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.key
33 )
34 ) { this.max = entry.maxWidth }
35 }
36 this.spacing = input.spacing * density
37 this.lineSpacing = input.lineSpacing * density
38 this.alignment = CrossAlignment.Center
39 },
40 visual = input.style
41 ),
42 SemanticRole.Group,
43 input.semantics.properties(enabled = input.enabled),
44 relations = input.semantics.relations,
45 mergeLabels = false,
46 reference = input.semantics.reference
47 )
48 }
51private class BreadcrumbRowProps(
52 val entry: BreadcrumbEntry,
53 val first: Boolean,
54 val root: BreadcrumbProps,
55 val theme: Theme
58private class BreadcrumbRow(
59 private val props: Props<BreadcrumbRowProps>,
60 scope: ComponentScope
61): Component(scope) {
62 public override fun render(): View {
63 val input = props.value
64 val entry = input.entry
65 val theme = input.theme
66 val density = theme.tokens.typography.action.fontSize / 14.0
67 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.foreground
72 }
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.pressed
84 )
85 val overrideText =
86 when (entry.style) {
87 case Some(val value) -> value.text;
88 case None -> None
89 }
90 val overrideColor =
91 when (overrideText) {
92 case Some(val value) -> value.color;
93 case None -> None
94 }
95 val color =
96 overrideColor ?:
97 if (active)
98 theme.tokens.palette.background.foreground
99 else
100 theme.tokens.palette.muted.foreground
101 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 false
109 )
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.semantics
119 ),
120 SemanticRole.Link,
121 entry.semantics.properties(enabled = input.root.enabled && entry.enabled),
122 SemanticActions(
123 invoke =
124 Some {
125 action();
126 SemanticActionResult.Completed
127 }
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 false
139 )
140 ),
141 SemanticRole.Link,
142 entry.semantics.properties(selected = Some(true)),
143 relations = entry.semantics.relations,
144 reference = entry.semantics.reference
145 )
146 case Ellipsis -> navigationEllipsis(
147 20.0 * density,
148 16.0 * density,
149 theme.tokens.palette.muted.foreground
150 )
151 case Custom(val content) -> content
152 }
153 return makeRow {
154 this.add(
155 if (input.first)
156 empty()
157 else
158 input.root.separator ?:
159 navigationIcon(
160 NavigationGlyph.End,
161 14.0 * density,
162 theme.tokens.palette.muted.foreground
163 )
164 )
165 this.add(body)
166 this.spacing =
167 if (input.first)
168 0.0
169 else
170 input.root.spacing * density
171 this.alignment = CrossAlignment.Center
172 }
173 }
176internal fun navigationLabel(
177 content: ButtonContent,
178 fonts: FontCollection?,
179 alignment: TextAlignment = TextAlignment.Start
180): ButtonContent =
181 when (content) {
182 case Label(val label) -> ButtonContent.Label(
183 LabelProps(
184 label.text,
185 if (!panelPresent(label.fonts))
186 fonts
187 else
188 label.fonts,
189 if (!panelPresent(label.settings))
190 Some(
191 paragraphStyle {
192 this.maxLines = Some(1L);
193 this.alignment = alignment
194 }
195 )
196 else
197 label.settings,
198 label.inlines,
199 label.theme,
200 label.baseStyle,
201 label.underline,
202 label.semantics
203 )
204 )
205 else -> content
206 }
208internal 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.0
211 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 }