scoop:scoop.ui.component:0.1.0
src/sidebar/parts.scoop
Selected production source · src/sidebar/parts.scoop
1
package scoop.ui.component3
import scoop.graphics.*4
import scoop.graphics.text.*5
import scoop.math.*6
import scoop.ui.*7
import scoop.ui.style.*8
import scoop.ui.component.column as sidebarColumn10
public 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()18
)20
public class SidebarGroupBuilder internal constructor() {21
public var label: PanelContent? = None22
public var action: View? = None23
public var collapsed: Boolean = false24
public var fonts: FontCollection? = None25
public var theme: ThemeSource? = None26
public var style: StyleOverride = styleOverride()27
public var key: ViewKey = ViewKey.Position28
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
}51
}53
public 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.key60
)61
}63
public class SidebarGroup public constructor(64
private val props: Props<SidebarGroupProps>,65
scope: ComponentScope66
): 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.075
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.background88
)89
},90
input.fonts91
)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
present106
)107
)108
add(input.content);109
spacing = 0.0;110
alignment = CrossAlignment.Stretch111
},112
Insets.all(8.0 * d),113
input.style114
)115
}116
}118
private 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.semantics130
)131
)132
case Custom(_) -> content133
}135
private class SidebarGroupHeading(136
private val density: Props<Double>,137
scope: ComponentScope138
): Element<Unit>(scope) {139
public override fun measure(context: MeasureContext, constraints: Constraints): LayoutResult<140
Unit141
> {142
val d = density.value143
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 * d151
else152
0.0153
val maximum =154
when (constraints.maxWidth) {155
case Some(val value) -> Some(max(0.0, value - action.size.width - gap));156
case None -> None157
}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.0169
)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.layoutDirection179
),180
(size.height - action.size.height) / 2.0181
)182
)183
return LayoutResult(LayoutMetrics(size), Unit)184
}185
public override fun scene(context: SceneContext, measured: Unit) {186
context.children()187
}188
}190
public 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()195
)197
public class SidebarSubmenuBuilder internal constructor() {198
public var collapsed: Boolean = false199
public var theme: ThemeSource? = None200
public var style: StyleOverride = styleOverride()201
public var key: ViewKey = ViewKey.Position202
}204
public 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.key211
)212
}214
public class SidebarSubmenu public constructor(215
private val props: Props<SidebarSubmenuProps>,216
scope: ComponentScope217
): 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.0226
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.collapsed245
)246
}247
}249
public 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? = None255
)257
public class SidebarBadgeBuilder internal constructor() {258
public var fonts: FontCollection? = None259
public var theme: ThemeSource? = None260
public var style: StyleOverride = styleOverride()261
public var color: Color? = None262
public var key: ViewKey = ViewKey.Position263
}265
public 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.key272
)273
}275
public class SidebarBadge public constructor(276
private val props: Props<SidebarBadgeProps>,277
scope: ComponentScope278
): 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.0287
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.foreground292
}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.style315
)316
}317
}319
private class SidebarBadgeLayout(320
private val density: Props<Double>,321
scope: ComponentScope322
): Element<Unit>(scope) {323
public override fun measure(context: MeasureContext, constraints: Constraints): LayoutResult<324
Unit325
> {326
val d = density.value327
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
else338
natural339
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
}348
}