Scoop API

scoop:scoop.ui.component:0.1.0

src/tabs/layout.scoop

Selected production source · src/tabs/layout.scoop

1package scoop.ui.component
3import scoop.graphics.*
4import scoop.ui.*
6internal class TabsLayoutProps(val axis: FlexAxis, val spacing: Double)
8/** Axis changes keep the list and panels under the same two logical parents. */
9internal class TabsLayout(
10 private val props: Props<TabsLayoutProps>,
11 scope: ComponentScope
12): Element<Boolean>(scope) {
13 public override fun measure(context: MeasureContext, constraints: Constraints): LayoutResult<
14 Boolean
15 > {
16 val sideBySide = props.value.axis == FlexAxis.Vertical
17 val header = context.measureChild(0L, Constraints(maxWidth = constraints.maxWidth))
18 val spacing = props.value.spacing
19 val remaining =
20 if (sideBySide)
21 insetMaximum(constraints.maxWidth, header.size.width + spacing)
22 else
23 constraints.maxWidth
24 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.width
29 else
30 lowerBound(header.size.width, panel.size.width)
31 val height =
32 if (sideBySide)
33 lowerBound(header.size.height, panel.size.height)
34 else
35 header.size.height + spacing + panel.size.height
36 val size = constraints.constrain(Size(width, height))
37 val clipped = width > size.width || height > size.height
38 if (size.width > 0.0 && size.height > 0.0) {
39 val rtl = context.layoutDirection == LayoutDirection.Rtl
40 context.placeChild(
41 0L,
42 Point(
43 if (rtl)
44 size.width - header.size.width
45 else
46 0.0,
47 0.0
48 )
49 )
50 context.placeChild(
51 1L,
52 Point(
53 if (sideBySide && !rtl)
54 header.size.width + spacing
55 else
56 0.0,
57 if (sideBySide)
58 0.0
59 else
60 header.size.height + spacing
61 )
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 }
73internal class TabPanels(private val selected: Props<Long?>, scope: ComponentScope): Element<Unit>(
74 scope
75) {
76 public override fun measure(context: MeasureContext, constraints: Constraints): LayoutResult<
77 Unit
78 > {
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).size
83 if (size.width > 0.0 && size.height > 0.0)
84 context.placeChild(index, Point(0.0, 0.0))
85 } else
86 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 }