Scoop API

scoop:scoop.ui.component:0.1.0

src/content/aside.scoop

Selected production source · src/content/aside.scoop

1package scoop.ui.component
3import scoop.graphics.*
4import scoop.ui.*
6private enum PanelAsideShape {
7 Empty, Content, Action, Both
8}
10private struct PanelAsideProps(
11 val shape: PanelAsideShape,
12 val gap: Double,
13 val minimumContent: Double,
14 val alignment: AxisAlignment
17/** Keeps the same content and action mounts when a narrow panel moves the action below. */
18internal fun panelAside(
19 content: View?,
20 action: View?,
21 gap: Double,
22 minimumContent: Double,
23 alignment: AxisAlignment = AxisAlignment.Start
24): View {
25 val children = ArrayList<View>()
26 when (content) {
27 case Some(val value) -> children.add(panelSlot(value, 0L));
28 case None -> {}
29 }
30 when (action) {
31 case Some(val value) -> children.add(panelSlot(value, 1L));
32 case None -> {}
33 }
34 val shape =
35 if (panelPresent(content)) {
36 if (panelPresent(action))
37 PanelAsideShape.Both
38 else
39 PanelAsideShape.Content
40 } else
41 if (panelPresent(action))
42 PanelAsideShape.Action
43 else
44 PanelAsideShape.Empty
45 return element(
46 PanelAsideProps(shape, gap, minimumContent, alignment),
47 { current, mount -> PanelAside(current, scope = mount) },
48 children.toArray()
49 )
52private class PanelAside(private val props: Props<PanelAsideProps>, scope: ComponentScope): Element<
53 Unit
54>(scope) {
55 public override fun measure(context: MeasureContext, constraints: Constraints): LayoutResult<
56 Unit
57 > {
58 when (props.value.shape) {
59 case Empty -> return LayoutResult(
60 LayoutMetrics(constraints.constrain(Size(0.0, 0.0))),
61 Unit
62 )
63 case Both -> return LayoutResult(paired(context, constraints), Unit)
64 else -> {
65 val child =
66 if (props.value.shape == PanelAsideShape.Action)
67 measureAction(context, 0L, constraints.maxWidth)
68 else
69 context.measureChild(0L, Constraints(maxWidth = constraints.maxWidth))
70 val size =
71 constraints.constrain(
72 Size(constraints.maxWidth ?: child.size.width, child.size.height)
73 )
74 val end = props.value.shape == PanelAsideShape.Action
75 val right = (context.layoutDirection == LayoutDirection.Ltr) == end
76 val y = alignmentOffset(props.value.alignment, size.height, child.size.height)
77 context.placeChild(
78 0L,
79 Point(
80 if (right)
81 size.width - child.size.width
82 else
83 0.0,
84 y
85 )
86 )
87 return LayoutResult(offsetMetrics(child, size, y), Unit)
88 }
89 }
90 }
91 private fun paired(context: MeasureContext, constraints: Constraints): LayoutMetrics {
92 val input = props.value
93 val action = measureAction(context, 1L, constraints.maxWidth)
94 val stacked =
95 when (constraints.maxWidth) {
96 case Some(val width) -> width - action.size.width - input.gap < input.minimumContent
97 case None -> false
98 }
99 val maximum =
100 if (stacked)
101 constraints.maxWidth
102 else
103 insetMaximum(constraints.maxWidth, action.size.width + input.gap)
104 val content = context.measureChild(0L, Constraints(maxWidth = maximum))
105 val natural =
106 if (stacked)
107 Size(
108 lowerBound(content.size.width, action.size.width),
109 content.size.height + input.gap + action.size.height
110 )
111 else
112 Size(
113 content.size.width + input.gap + action.size.width,
114 lowerBound(content.size.height, action.size.height)
115 )
116 validSize(natural)
117 val size =
118 constraints.constrain(Size(constraints.maxWidth ?: natural.width, natural.height))
119 val rtl = context.layoutDirection == LayoutDirection.Rtl
120 val contentY =
121 if (stacked)
122 0.0
123 else
124 alignmentOffset(input.alignment, size.height, content.size.height)
125 context.placeChild(
126 0L,
127 Point(
128 if (rtl)
129 size.width - content.size.width
130 else
131 0.0,
132 contentY
133 )
134 )
135 context.placeChild(
136 1L,
137 Point(
138 if (rtl)
139 0.0
140 else
141 size.width - action.size.width,
142 if (stacked)
143 content.size.height + input.gap
144 else
145 alignmentOffset(input.alignment, size.height, action.size.height)
146 )
147 )
148 return offsetMetrics(content, size, contentY)
149 }
150 public override fun scene(context: SceneContext, measured: Unit) {
151 context.children()
152 }
155private fun measureAction(context: MeasureContext, index: Long, maximum: Double?): LayoutMetrics {
156 val natural = context.measureChild(index, Constraints())
157 return when (maximum) {
158 case Some(val width) -> if (natural.size.width > width)
159 context.measureChild(index, Constraints(maxWidth = Some(width)))
160 else
161 natural
162 case None -> natural
163 }