Scoop API

scoop:scoop.ui.component:0.1.0

src/sidebar/skeleton.scoop

Selected production source · src/sidebar/skeleton.scoop

1package scoop.ui.component
3import scoop.graphics.*
4import scoop.math.*
5import scoop.ui.*
6import scoop.ui.style.*
8public class SidebarSkeletonProps public constructor(
9 public val showIcon: Boolean = false,
10 public val collapsed: Boolean = false,
11 public val textFraction: Double = 0.7,
12 public val theme: ThemeSource? = None,
13 public val style: StyleOverride = styleOverride()
14) {
15 init {
16 requireComponent(
17 textFraction.isFinite() && textFraction >= 0.0 && textFraction <= 1.0,
18 "invalid sidebar skeleton fraction"
19 )
20 }
23public class SidebarSkeletonBuilder internal constructor() {
24 public var showIcon: Boolean = false
25 public var collapsed: Boolean = false
26 public var textFraction: Double = 0.7
27 public var theme: ThemeSource? = None
28 public var style: StyleOverride = styleOverride()
29 public var key: ViewKey = ViewKey.Position
32public fun sidebarMenuSkeleton(configure: SidebarSkeletonBuilder.() -> Unit = {}): View {
33 val b = SidebarSkeletonBuilder();
34 b.configure()
35 return component(
36 SidebarSkeletonProps(b.showIcon, b.collapsed, b.textFraction, b.theme, b.style),
37 { current, mount -> SidebarSkeleton(current, scope = mount) },
38 b.key
39 )
42public class SidebarSkeleton public constructor(
43 private val props: Props<SidebarSkeletonProps>,
44 scope: ComponentScope
45): Component(scope) {
46 private val themes = ThemeAccess(scope)
47 public override fun onMount() {
48 themes.mount(props.value.theme)
49 }
50 public override fun render(): View {
51 val input = props.value;
52 val theme = themes.read(input.theme)
53 val d = theme.tokens.typography.body.fontSize / 14.0
54 return panelFrame(
55 element(
56 SidebarSkeletonInput(input, d),
57 { value, mount -> SidebarSkeletonLayout(value, scope = mount) },
58 [
59 skeleton {
60 width = Some(16.0 * d);
61 height = Some(16.0 * d);
62 this.theme = input.theme
63 },
64 skeleton {
65 height = Some(16.0 * d);
66 this.theme = input.theme
67 }
68 ]
69 ),
70 visual = input.style
71 )
72 }
75private class SidebarSkeletonInput(val props: SidebarSkeletonProps, val density: Double)
77private class SidebarSkeletonLayout(
78 private val props: Props<SidebarSkeletonInput>,
79 scope: ComponentScope
80): Element<Unit>(scope) {
81 public override fun measure(context: MeasureContext, constraints: Constraints): LayoutResult<
82 Unit
83 > {
84 val input = props.value;
85 val d = input.density
86 val size = constraints.constrain(Size(constraints.maxWidth ?: 160.0 * d, 32.0 * d))
87 val icon =
88 if (input.props.showIcon)
89 context.measureChild(
90 0L,
91 Constraints(16.0 * d, Some(16.0 * d), 16.0 * d, Some(16.0 * d))
92 )
93 else {
94 context.skipChild(0L);
95 LayoutMetrics(Size(0.0, 0.0))
96 }
97 val gap =
98 if (input.props.showIcon)
99 8.0 * d
100 else
101 0.0
102 val length =
103 max(0.0, size.width - 16.0 * d - icon.size.width - gap) * input.props.textFraction
104 if (input.props.collapsed)
105 context.skipChild(1L)
106 else {
107 val text =
108 context.measureChild(
109 1L,
110 Constraints(length, Some(length), 16.0 * d, Some(16.0 * d))
111 )
112 context.placeChild(
113 1L,
114 Point(
115 logicalX(
116 8.0 * d + icon.size.width + gap,
117 text.size.width,
118 size.width,
119 context.layoutDirection
120 ),
121 8.0 * d
122 )
123 )
124 }
125 if (input.props.showIcon)
126 context.placeChild(
127 0L,
128 Point(
129 logicalX(8.0 * d, icon.size.width, size.width, context.layoutDirection),
130 8.0 * d
131 )
132 )
133 return LayoutResult(LayoutMetrics(size), Unit)
134 }
135 public override fun scene(context: SceneContext, measured: Unit) {
136 context.children()
137 }