scoop:scoop.ui.component:0.1.0
src/sidebar/skeleton.scoop
Selected production source · src/sidebar/skeleton.scoop
1
package scoop.ui.component3
import scoop.graphics.*4
import scoop.math.*5
import scoop.ui.*6
import scoop.ui.style.*8
public 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
}21
}23
public class SidebarSkeletonBuilder internal constructor() {24
public var showIcon: Boolean = false25
public var collapsed: Boolean = false26
public var textFraction: Double = 0.727
public var theme: ThemeSource? = None28
public var style: StyleOverride = styleOverride()29
public var key: ViewKey = ViewKey.Position30
}32
public 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.key39
)40
}42
public class SidebarSkeleton public constructor(43
private val props: Props<SidebarSkeletonProps>,44
scope: ComponentScope45
): 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.054
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.theme63
},64
skeleton {65
height = Some(16.0 * d);66
this.theme = input.theme67
}68
]69
),70
visual = input.style71
)72
}73
}75
private class SidebarSkeletonInput(val props: SidebarSkeletonProps, val density: Double)77
private class SidebarSkeletonLayout(78
private val props: Props<SidebarSkeletonInput>,79
scope: ComponentScope80
): Element<Unit>(scope) {81
public override fun measure(context: MeasureContext, constraints: Constraints): LayoutResult<82
Unit83
> {84
val input = props.value;85
val d = input.density86
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 * d100
else101
0.0102
val length =103
max(0.0, size.width - 16.0 * d - icon.size.width - gap) * input.props.textFraction104
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.layoutDirection120
),121
8.0 * d122
)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 * d131
)132
)133
return LayoutResult(LayoutMetrics(size), Unit)134
}135
public override fun scene(context: SceneContext, measured: Unit) {136
context.children()137
}138
}