Scoop API

scoop:scoop.ui.component:0.1.0

src/sidebar/parts.scoop

Selected production source · src/sidebar/parts.scoop

1package scoop.ui.component
3import scoop.graphics.*
4import scoop.graphics.text.*
5import scoop.math.*
6import scoop.ui.*
7import scoop.ui.style.*
8import scoop.ui.component.column as sidebarColumn
10public class SidebarGroupProps public constructor(
11 public val content: View,
12 public val label: PanelContent? = None,
13 public val action: View? = None,
14 public val collapsed: Boolean = false,
15 public val fonts: FontCollection? = None,
16 public val theme: ThemeSource? = None,
17 public val style: StyleOverride = styleOverride()
20public class SidebarGroupBuilder internal constructor() {
21 public var label: PanelContent? = None
22 public var action: View? = None
23 public var collapsed: Boolean = false
24 public var fonts: FontCollection? = None
25 public var theme: ThemeSource? = None
26 public var style: StyleOverride = styleOverride()
27 public var key: ViewKey = ViewKey.Position
28 public fun label(text: String) {
29 label =
30 Some(
31 PanelContent.Label(
32 LabelProps(
33 LabelText.Plain(text, None),
34 settings =
35 Some(
36 paragraphStyle {
37 maxLines = Some(1L);
38 ellipsis = "…"
39 }
40 )
41 )
42 )
43 )
44 }
45 public fun label(content: View) {
46 label = Some(PanelContent.Custom(content))
47 }
48 public fun action(content: View) {
49 action = Some(content)
50 }
53public fun sidebarGroup(content: View, configure: SidebarGroupBuilder.() -> Unit = {}): View {
54 val b = SidebarGroupBuilder();
55 b.configure()
56 return component(
57 SidebarGroupProps(content, b.label, b.action, b.collapsed, b.fonts, b.theme, b.style),
58 { current, mount -> SidebarGroup(current, scope = mount) },
59 b.key
60 )
63public class SidebarGroup public constructor(
64 private val props: Props<SidebarGroupProps>,
65 scope: ComponentScope
66): Component(scope) {
67 private val themes = ThemeAccess(scope)
68 public override fun onMount() {
69 themes.mount(props.value.theme)
70 }
71 public override fun render(): View {
72 val input = props.value;
73 val theme = themes.read(input.theme)
74 val d = theme.tokens.typography.body.fontSize / 14.0
75 val label =
76 when (input.label) {
77 case Some(val value) -> panelCaption(
78 sidebarGroupDirection(value, scope.layoutDirection.textDirection()),
79 theme.tokens.typography.body.copy {
80 fontSize = 12.0 * d;
81 weight = 500;
82 color =
83 componentTint(
84 theme,
85 theme.tokens.palette.sidebar.background.foreground.withAlpha(1.0),
86 0.7,
87 theme.tokens.palette.sidebar.background.background
88 )
89 },
90 input.fonts
91 )
92 case None -> empty()
93 }
94 val present = !input.collapsed && (panelPresent(input.label) || panelPresent(input.action))
95 return panelFrame(
96 sidebarColumn {
97 add(
98 sidebarGate(
99 element(
100 d,
101 { value, mount -> SidebarGroupHeading(value, scope = mount) },
102 [label, input.action ?: empty()]
103 ),
104 present,
105 present
106 )
107 )
108 add(input.content);
109 spacing = 0.0;
110 alignment = CrossAlignment.Stretch
111 },
112 Insets.all(8.0 * d),
113 input.style
114 )
115 }
118private fun sidebarGroupDirection(content: PanelContent, direction: TextDirection): PanelContent =
119 when (content) {
120 case Label(val value) -> PanelContent.Label(
121 LabelProps(
122 value.text,
123 value.fonts,
124 Some((value.settings ?: paragraphStyle()).copy { this.direction = direction }),
125 value.inlines,
126 value.theme,
127 value.baseStyle,
128 value.underline,
129 value.semantics
130 )
131 )
132 case Custom(_) -> content
133 }
135private class SidebarGroupHeading(
136 private val density: Props<Double>,
137 scope: ComponentScope
138): Element<Unit>(scope) {
139 public override fun measure(context: MeasureContext, constraints: Constraints): LayoutResult<
140 Unit
141 > {
142 val d = density.value
143 val action =
144 context.measureChild(
145 1L,
146 Constraints(maxWidth = Some(20.0 * d), maxHeight = Some(20.0 * d))
147 )
148 val gap =
149 if (action.size.width > 0.0)
150 8.0 * d
151 else
152 0.0
153 val maximum =
154 when (constraints.maxWidth) {
155 case Some(val value) -> Some(max(0.0, value - action.size.width - gap));
156 case None -> None
157 }
158 val label =
159 context.measureChild(0L, Constraints(maxWidth = maximum, maxHeight = Some(32.0 * d)))
160 val size =
161 constraints.constrain(
162 Size(constraints.maxWidth ?: (label.size.width + gap + action.size.width), 32.0 * d)
163 )
164 context.placeChild(
165 0L,
166 Point(
167 logicalX(0.0, label.size.width, size.width, context.layoutDirection),
168 (size.height - label.size.height) / 2.0
169 )
170 )
171 context.placeChild(
172 1L,
173 Point(
174 logicalX(
175 size.width - action.size.width,
176 action.size.width,
177 size.width,
178 context.layoutDirection
179 ),
180 (size.height - action.size.height) / 2.0
181 )
182 )
183 return LayoutResult(LayoutMetrics(size), Unit)
184 }
185 public override fun scene(context: SceneContext, measured: Unit) {
186 context.children()
187 }
190public class SidebarSubmenuProps public constructor(
191 public val content: View,
192 public val collapsed: Boolean = false,
193 public val theme: ThemeSource? = None,
194 public val style: StyleOverride = styleOverride()
197public class SidebarSubmenuBuilder internal constructor() {
198 public var collapsed: Boolean = false
199 public var theme: ThemeSource? = None
200 public var style: StyleOverride = styleOverride()
201 public var key: ViewKey = ViewKey.Position
204public fun sidebarSubmenu(content: View, configure: SidebarSubmenuBuilder.() -> Unit = {}): View {
205 val b = SidebarSubmenuBuilder();
206 b.configure()
207 return component(
208 SidebarSubmenuProps(content, b.collapsed, b.theme, b.style),
209 { current, mount -> SidebarSubmenu(current, scope = mount) },
210 b.key
211 )
214public class SidebarSubmenu public constructor(
215 private val props: Props<SidebarSubmenuProps>,
216 scope: ComponentScope
217): Component(scope) {
218 private val themes = ThemeAccess(scope)
219 public override fun onMount() {
220 themes.mount(props.value.theme)
221 }
222 public override fun render(): View {
223 val input = props.value;
224 val theme = themes.read(input.theme)
225 val d = theme.tokens.typography.body.fontSize / 14.0
226 return sidebarGate(
227 panelFrame(
228 panelFrame(
229 input.content,
230 Insets(10.0 * d, 2.0 * d, 10.0 * d, 2.0 * d),
231 styleOverride {
232 border(
233 Some(
234 Border.Logical(
235 start = BorderEdge(d, theme.tokens.palette.sidebar.border)
236 )
237 )
238 )
239 }.then(input.style)
240 ),
241 Insets(14.0 * d, 0.0, 14.0 * d, 0.0)
242 ),
243 !input.collapsed,
244 !input.collapsed
245 )
246 }
249public class SidebarBadgeProps public constructor(
250 public val text: String,
251 public val fonts: FontCollection? = None,
252 public val theme: ThemeSource? = None,
253 public val style: StyleOverride = styleOverride(),
254 public val color: Color? = None
257public class SidebarBadgeBuilder internal constructor() {
258 public var fonts: FontCollection? = None
259 public var theme: ThemeSource? = None
260 public var style: StyleOverride = styleOverride()
261 public var color: Color? = None
262 public var key: ViewKey = ViewKey.Position
265public fun sidebarMenuBadge(text: String, configure: SidebarBadgeBuilder.() -> Unit = {}): View {
266 val b = SidebarBadgeBuilder();
267 b.configure()
268 return component(
269 SidebarBadgeProps(text, b.fonts, b.theme, b.style, b.color),
270 { current, mount -> SidebarBadge(current, scope = mount) },
271 b.key
272 )
275public class SidebarBadge public constructor(
276 private val props: Props<SidebarBadgeProps>,
277 scope: ComponentScope
278): Component(scope) {
279 private val themes = ThemeAccess(scope)
280 public override fun onMount() {
281 themes.mount(props.value.theme)
282 }
283 public override fun render(): View {
284 val input = props.value;
285 val theme = themes.read(input.theme)
286 val d = theme.tokens.typography.body.fontSize / 14.0
287 val text =
288 theme.tokens.typography.body.copy {
289 fontSize = 12.0 * d;
290 weight = 500;
291 color = input.color ?: theme.tokens.palette.sidebar.background.foreground
292 }
293 return panelFrame(
294 element(
295 d,
296 { value, mount -> SidebarBadgeLayout(value, scope = mount) },
297 [
298 component(
299 LabelProps(
300 LabelText.Plain(input.text, None),
301 input.fonts,
302 Some(
303 paragraphStyle {
304 maxLines = Some(1L);
305 ellipsis = "…"
306 }
307 ),
308 baseStyle = Some(text)
309 ),
310 { current, mount -> Label(current, scope = mount) }
311 )
312 ]
313 ),
314 visual = input.style
315 )
316 }
319private class SidebarBadgeLayout(
320 private val density: Props<Double>,
321 scope: ComponentScope
322): Element<Unit>(scope) {
323 public override fun measure(context: MeasureContext, constraints: Constraints): LayoutResult<
324 Unit
325 > {
326 val d = density.value
327 val natural = context.measureChild(0L, Constraints(maxHeight = Some(20.0 * d)))
328 val size =
329 constraints.constrain(Size(max(20.0 * d, natural.size.width + 8.0 * d), 20.0 * d))
330 val available = max(0.0, size.width - 8.0 * d)
331 val child =
332 if (natural.size.width > available)
333 context.measureChild(
334 0L,
335 Constraints(maxWidth = Some(available), maxHeight = Some(20.0 * d))
336 )
337 else
338 natural
339 context.placeChild(
340 0L,
341 Point((size.width - child.size.width) / 2.0, (size.height - child.size.height) / 2.0)
342 )
343 return LayoutResult(LayoutMetrics(size), Unit)
344 }
345 public override fun scene(context: SceneContext, measured: Unit) {
346 context.children()
347 }