scoop:scoop.ui.component:0.1.0
src/content/aside.scoop
Selected production source · src/content/aside.scoop
1
package scoop.ui.component3
import scoop.graphics.*4
import scoop.ui.*6
private enum PanelAsideShape {7
Empty, Content, Action, Both8
}10
private struct PanelAsideProps(11
val shape: PanelAsideShape,12
val gap: Double,13
val minimumContent: Double,14
val alignment: AxisAlignment15
)17
/** Keeps the same content and action mounts when a narrow panel moves the action below. */18
internal fun panelAside(19
content: View?,20
action: View?,21
gap: Double,22
minimumContent: Double,23
alignment: AxisAlignment = AxisAlignment.Start24
): 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.Both38
else39
PanelAsideShape.Content40
} else41
if (panelPresent(action))42
PanelAsideShape.Action43
else44
PanelAsideShape.Empty45
return element(46
PanelAsideProps(shape, gap, minimumContent, alignment),47
{ current, mount -> PanelAside(current, scope = mount) },48
children.toArray()49
)50
}52
private class PanelAside(private val props: Props<PanelAsideProps>, scope: ComponentScope): Element<53
Unit54
>(scope) {55
public override fun measure(context: MeasureContext, constraints: Constraints): LayoutResult<56
Unit57
> {58
when (props.value.shape) {59
case Empty -> return LayoutResult(60
LayoutMetrics(constraints.constrain(Size(0.0, 0.0))),61
Unit62
)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
else69
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.Action75
val right = (context.layoutDirection == LayoutDirection.Ltr) == end76
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.width82
else83
0.0,84
y85
)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.value93
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.minimumContent97
case None -> false98
}99
val maximum =100
if (stacked)101
constraints.maxWidth102
else103
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.height110
)111
else112
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.Rtl120
val contentY =121
if (stacked)122
0.0123
else124
alignmentOffset(input.alignment, size.height, content.size.height)125
context.placeChild(126
0L,127
Point(128
if (rtl)129
size.width - content.size.width130
else131
0.0,132
contentY133
)134
)135
context.placeChild(136
1L,137
Point(138
if (rtl)139
0.0140
else141
size.width - action.size.width,142
if (stacked)143
content.size.height + input.gap144
else145
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
}153
}155
private 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
else161
natural162
case None -> natural163
}164
}