scoop:scoop.ui.component:0.1.0
src/sidebar/layout.scoop
Selected production source · src/sidebar/layout.scoop
1
package scoop.ui.component3
import scoop.graphics.*4
import scoop.math.*5
import scoop.surface.*6
import scoop.ui.*7
import scoop.ui.style.*9
internal class SidebarHostInput(10
val props: SidebarProps,11
val theme: Theme,12
val shown: SidebarPresentation,13
val setMobile: (Boolean) -> Unit,14
val toggle: () -> Unit15
)17
internal fun sidebarOutside(input: SidebarProps, density: Double): Double =18
if (input.variant == SidebarVariant.Sidebar)19
0.020
else21
8.0 * density23
internal fun sidebarOnLeft(side: SidebarSide, direction: LayoutDirection): Boolean =24
(side == SidebarSide.Start) != (direction == LayoutDirection.Rtl)26
internal class SidebarHost(27
private val props: Props<SidebarHostInput>,28
scope: ComponentScope29
): Element<Boolean>(scope) {30
private var width: Animatable<Double>? = None31
private var keys: ShortcutScope? = None32
private var wasMobile = false33
private fun target(): Double {34
val input = props.value35
val density = input.theme.tokens.typography.body.fontSize / 14.036
val value = sidebarWidth(input.props, input.shown) * density37
return value +38
if (value > 0.0)39
sidebarOutside(input.props, density) * 2.040
else41
0.042
}43
public override fun onMount() {44
width = Some(scope.animation(target()))45
wasMobile = props.value.shown.mobile46
keys = Some(scope.resource { ShortcutScope(scope.input) })47
}48
public override fun update(): UiInvalidation {49
val input = props.value50
sidebarKeyBindings(keys!!, input.props, input.toggle)51
val animation = width!!52
val next = target()53
val motion = sidebarTransition(input.props, input.theme)54
if (wasMobile != input.shown.mobile || !panelPresent(motion)) {55
if (animation.value != next || animation.state == AnimationState.Running)56
animation.snapTo(next)57
} else58
if (animation.target != next)59
animation.animateTo(next, motion!!)60
wasMobile = input.shown.mobile61
return UiInvalidation.Layout62
}63
public override fun measure(context: MeasureContext, constraints: Constraints): LayoutResult<64
Boolean65
> {66
val input = props.value67
val density = input.theme.tokens.typography.body.fontSize / 14.068
val nextMobile = sidebarIsMobile(input.props, constraints.maxWidth, density)69
val occupied =70
if (input.shown.mobile || nextMobile)71
0.072
else73
min(max(0.0, width!!.value), constraints.maxWidth ?: width!!.value)74
val pane =75
context.measureChild(76
0L,77
Constraints(occupied, Some(occupied), maxHeight = constraints.maxHeight)78
)79
val inset = panelPresent(input.props.inset)80
val margins =81
if (!input.shown.mobile && input.props.variant == SidebarVariant.Inset)82
8.0 * density83
else84
0.085
val near =86
if (input.shown.expanded && occupied > 0.0)87
0.088
else89
margins90
val mainMaximum =91
when (constraints.maxWidth) {92
case Some(val value) -> Some(max(0.0, value - occupied - near - margins))93
case None -> None94
}95
val mainHeight =96
when (constraints.maxHeight) {97
case Some(val value) -> Some(max(0.0, value - margins * 2.0))98
case None -> None99
}100
val main =101
if (inset)102
context.measureChild(103
1L,104
Constraints(mainMaximum ?: 0.0, mainMaximum, mainHeight ?: 0.0, mainHeight)105
)106
else {107
context.skipChild(1L);108
LayoutMetrics(Size(0.0, 0.0))109
}110
val total =111
occupied +112
if (inset)113
main.size.width + near + margins114
else115
0.0116
val size =117
constraints.constrain(118
Size(119
total,120
max(121
pane.size.height,122
main.size.height +123
if (inset)124
margins * 2.0125
else126
0.0127
)128
)129
)130
val left = sidebarOnLeft(input.props.side, context.layoutDirection)131
context.placeChild(132
0L,133
Point(134
if (left)135
0.0136
else137
max(0.0, size.width - occupied),138
0.0139
)140
)141
if (inset)142
context.placeChild(143
1L,144
Point(145
if (left)146
occupied + near147
else148
margins,149
margins150
)151
)152
if (153
input.props.rail &&154
!input.shown.mobile &&155
!nextMobile &&156
input.props.collapsible !=157
SidebarCollapse.None &&158
size.height >159
0.0160
) {161
val rail =162
context.measureChild(163
2L,164
Constraints(165
16.0 * density,166
Some(16.0 * density),167
size.height,168
Some(size.height)169
)170
)171
context.placeChild(172
2L,173
Point(174
(175
if (left)176
occupied177
else178
size.width - occupied179
) -180
rail.size.width /181
2.0,182
0.0183
)184
)185
} else186
context.skipChild(2L)187
return LayoutResult(LayoutMetrics(size), nextMobile)188
}189
public override fun scene(context: SceneContext, nextMobile: Boolean) {190
context.children([1L, 0L, 2L])191
val desired = nextMobile192
context.onFrameComplete {193
frame -> if (frame.submission == FrameSubmission.Accepted && scope.isMounted)194
props.value.setMobile(desired)195
}196
}197
}199
internal class SidebarPaneInput(200
val props: SidebarProps,201
val shown: SidebarPresentation,202
val density: Double203
)205
internal class SidebarPane(206
private val props: Props<SidebarPaneInput>,207
scope: ComponentScope208
): Element<Unit>(scope) {209
public override fun measure(context: MeasureContext, constraints: Constraints): LayoutResult<210
Unit211
> {212
val input = props.value213
val outside = sidebarOutside(input.props, input.density)214
val width = constraints.maxWidth ?: constraints.minWidth215
if (width <= 0.0) {216
context.skipChild(0L)217
return LayoutResult(218
LayoutMetrics(constraints.constrain(Size(0.0, constraints.maxHeight ?: 0.0))),219
Unit220
)221
}222
val available = max(0.0, width - outside * 2.0)223
val full =224
if (input.props.collapsible == SidebarCollapse.Offcanvas)225
input.props.width * input.density226
else227
available228
val height =229
when (constraints.maxHeight) {230
case Some(val value) -> Some(max(0.0, value - outside * 2.0))231
case None -> None232
}233
val child = context.measureChild(0L, Constraints(full, Some(full), height ?: 0.0, height))234
val offset =235
if (sidebarOnLeft(input.props.side, context.layoutDirection))236
available - full237
else238
0.0239
context.placeChild(0L, Point(outside + offset, outside))240
return LayoutResult(241
LayoutMetrics(constraints.constrain(Size(width, child.size.height + outside * 2.0))),242
Unit243
)244
}245
public override fun scene(context: SceneContext, measured: Unit) {246
context.clipRect(Rect(0.0, 0.0, context.size.width, context.size.height))247
context.children()248
}249
}251
internal class SidebarGateInput(val enabled: Boolean, val shown: Boolean)253
internal fun sidebarGate(content: View, enabled: Boolean, shown: Boolean): View =254
element(255
SidebarGateInput(enabled, shown),256
{ current, mount -> SidebarGate(current, scope = mount) },257
[content]258
)260
private class SidebarGate(261
private val props: Props<SidebarGateInput>,262
scope: ComponentScope263
): Element<Boolean>(scope) {264
public override fun onMount() {265
scope.input.enabled = false266
}267
public override fun update(): UiInvalidation {268
val input = props.value269
if (!input.enabled || !input.shown)270
scope.input.enabled = false271
scope.isFrameEnabled = input.enabled && input.shown272
return UiInvalidation.Layout273
}274
public override fun measure(context: MeasureContext, constraints: Constraints): LayoutResult<275
Boolean276
> {277
if (!props.value.shown) {278
context.skipChild(0L);279
return LayoutResult(LayoutMetrics(constraints.constrain(Size(0.0, 0.0))), false)280
}281
val child = context.measureChild(0L, constraints)282
val placed = child.size.width > 0.0 && child.size.height > 0.0283
if (placed)284
context.placeChild(0L, Point(0.0, 0.0))285
return LayoutResult(child, placed)286
}287
public override fun scene(context: SceneContext, placed: Boolean) {288
context.children()289
val visible = placed290
context.onFrameComplete {291
frame -> if (frame.submission == FrameSubmission.Accepted && scope.isMounted)292
scope.input.enabled = visible && props.value.enabled && props.value.shown293
}294
}295
}297
internal fun sidebarKeyBindings(scope: ShortcutScope, input: SidebarProps, toggle: () -> Unit) {298
scope.bindings =299
when (input.shortcut) {300
case Some(val chord) -> [301
shortcutBinding(chord, toggle) {302
enabled = input.enabled && input.collapsible != SidebarCollapse.None303
}304
]305
case None -> []306
}307
}309
private class SidebarKeyInput(val props: SidebarProps, val toggle: () -> Unit)311
internal fun sidebarKeys(content: View, input: SidebarProps, toggle: () -> Unit): View =312
element(313
SidebarKeyInput(input, toggle),314
{ current, mount -> SidebarKeys(current, scope = mount) },315
[content]316
)318
private class SidebarKeys(319
private val props: Props<SidebarKeyInput>,320
scope: ComponentScope321
): Element<Unit>(scope) {322
private var keys: ShortcutScope? = None323
public override fun onMount() {324
keys = Some(scope.resource { ShortcutScope(scope.input) })325
}326
public override fun update(): UiInvalidation {327
sidebarKeyBindings(keys!!, props.value.props, props.value.toggle)328
return UiInvalidation.Layout329
}330
public override fun measure(context: MeasureContext, constraints: Constraints): LayoutResult<331
Unit332
> {333
val child = context.measureChild(0L, constraints);334
context.placeChild(0L, Point(0.0, 0.0));335
return LayoutResult(child, Unit)336
}337
public override fun scene(context: SceneContext, measured: Unit) {338
context.children()339
}340
}