scoop:scoop.ui.component:0.1.0
src/tabs/layout.scoop
Selected production source · src/tabs/layout.scoop
1
package scoop.ui.component3
import scoop.graphics.*4
import scoop.ui.*6
internal class TabsLayoutProps(val axis: FlexAxis, val spacing: Double)8
/** Axis changes keep the list and panels under the same two logical parents. */9
internal class TabsLayout(10
private val props: Props<TabsLayoutProps>,11
scope: ComponentScope12
): Element<Boolean>(scope) {13
public override fun measure(context: MeasureContext, constraints: Constraints): LayoutResult<14
Boolean15
> {16
val sideBySide = props.value.axis == FlexAxis.Vertical17
val header = context.measureChild(0L, Constraints(maxWidth = constraints.maxWidth))18
val spacing = props.value.spacing19
val remaining =20
if (sideBySide)21
insetMaximum(constraints.maxWidth, header.size.width + spacing)22
else23
constraints.maxWidth24
val panel =25
context.measureChild(1L, Constraints(minWidth = remaining ?: 0.0, maxWidth = remaining))26
val width =27
if (sideBySide)28
header.size.width + spacing + panel.size.width29
else30
lowerBound(header.size.width, panel.size.width)31
val height =32
if (sideBySide)33
lowerBound(header.size.height, panel.size.height)34
else35
header.size.height + spacing + panel.size.height36
val size = constraints.constrain(Size(width, height))37
val clipped = width > size.width || height > size.height38
if (size.width > 0.0 && size.height > 0.0) {39
val rtl = context.layoutDirection == LayoutDirection.Rtl40
context.placeChild(41
0L,42
Point(43
if (rtl)44
size.width - header.size.width45
else46
0.0,47
0.048
)49
)50
context.placeChild(51
1L,52
Point(53
if (sideBySide && !rtl)54
header.size.width + spacing55
else56
0.0,57
if (sideBySide)58
0.059
else60
header.size.height + spacing61
)62
)63
}64
return LayoutResult(LayoutMetrics(size), clipped)65
}66
public override fun scene(context: SceneContext, clipped: Boolean) {67
if (clipped)68
context.clipRect(Rect(0.0, 0.0, context.size.width, context.size.height))69
context.children()70
}71
}73
internal class TabPanels(private val selected: Props<Long?>, scope: ComponentScope): Element<Unit>(74
scope75
) {76
public override fun measure(context: MeasureContext, constraints: Constraints): LayoutResult<77
Unit78
> {79
var size = Size(constraints.maxWidth ?: constraints.minWidth, 0.0)80
for (index in 0L ..< context.childCount) {81
if (selected.value == Some(index)) {82
size = context.measureChild(index, constraints).size83
if (size.width > 0.0 && size.height > 0.0)84
context.placeChild(index, Point(0.0, 0.0))85
} else86
context.skipChild(index)87
}88
return LayoutResult(LayoutMetrics(constraints.constrain(size)), Unit)89
}90
public override fun scene(context: SceneContext, measured: Unit) {91
context.children()92
}93
}