scoop:scoop.ui.component:0.1.0
src/layout/grid.scoop
Selected production source · src/layout/grid.scoop
1
package scoop.ui.component3
import scoop.collections.sortedBy4
import scoop.graphics.*5
import scoop.ui.*7
public class Grid public constructor(8
private val props: Props<GridProps>,9
scope: ComponentScope10
): Component(scope) {11
public override fun render(): View =12
element(13
props.value,14
{ value, mount -> GridInk(value, scope = mount) },15
props.value.children16
)17
}19
private struct GridRequirement(val start: Long, val span: Long, val extent: Double)21
private class GridInk(private val props: Props<GridProps>, scope: ComponentScope): Element<Unit>(22
scope23
) {24
public override fun measure(context: MeasureContext, constraints: Constraints): LayoutResult<25
Unit26
> {27
val input = props.value28
val padding = input.logicalPadding?.resolve(context.layoutDirection) ?: input.padding29
val inset = padding.totals()30
val columns =31
GridTracks(32
input.columns,33
input.columnGap,34
insetMaximum(constraints.maxWidth, inset.width)35
)36
val rows =37
GridTracks(input.rows, input.rowGap, insetMaximum(constraints.maxHeight, inset.height))38
rows.distribute()39
val columnNeeds = ArrayList<GridRequirement>()40
for (index in 0L ..< input.items.size) {41
val item = input.items[index]42
if (columns.needsContent(item.column, item.columnSpan)) {43
val maximumHeight =44
if (rows.needsContent(item.row, item.rowSpan))45
None46
else47
Some(rows.spanExtent(item.row, item.rowSpan))48
val measured = context.measureChild(index, Constraints(maxHeight = maximumHeight))49
columnNeeds.add(GridRequirement(item.column, item.columnSpan, measured.size.width))50
}51
}52
applyGridNeeds(columns, columnNeeds)53
columns.distribute()55
val rowNeeds = ArrayList<GridRequirement>()56
for (index in 0L ..< input.items.size) {57
val item = input.items[index]58
val width = columns.spanExtent(item.column, item.columnSpan)59
val maximumHeight =60
if (rows.needsContent(item.row, item.rowSpan))61
None62
else63
Some(rows.spanExtent(item.row, item.rowSpan))64
val measured =65
context.measureChild(66
index,67
Constraints(68
minWidth =69
if (item.horizontal == CrossAlignment.Stretch)70
width71
else72
0.0,73
maxWidth = Some(width),74
maxHeight = maximumHeight75
)76
)77
rowNeeds.add(GridRequirement(item.row, item.rowSpan, measured.size.height))78
}79
applyGridNeeds(rows, rowNeeds)80
rows.distribute()81
val size =82
constraints.constrain(Size(columns.total() + inset.width, rows.total() + inset.height))83
val innerWidth = lowerBound(size.width - inset.width, 0.0)84
for (index in 0L ..< input.items.size) {85
val item = input.items[index]86
val width = columns.spanExtent(item.column, item.columnSpan)87
val height = rows.spanExtent(item.row, item.rowSpan)88
val measured =89
context.measureChild(90
index,91
Constraints(92
minWidth =93
if (item.horizontal == CrossAlignment.Stretch)94
width95
else96
0.0,97
maxWidth = Some(width),98
minHeight =99
if (item.vertical == CrossAlignment.Stretch)100
height101
else102
0.0,103
maxHeight = Some(height)104
)105
)106
val start =107
columns.origin(item.column) +108
cellOffset(item.horizontal, width - measured.size.width)109
val top =110
rows.origin(item.row) + cellOffset(item.vertical, height - measured.size.height)111
context.placeChild(112
index,113
Point(114
padding.left +115
logicalX(start, measured.size.width, innerWidth, context.layoutDirection),116
padding.top + top117
)118
)119
}120
return LayoutResult(LayoutMetrics(size), Unit)121
}122
public override fun scene(context: SceneContext, measured: Unit) {123
containerScene(context, props.value.clip)124
}125
}127
private fun applyGridNeeds(tracks: GridTracks, values: ArrayList<GridRequirement>) {128
// Resolve single tracks before spans; equal spans preserve declaration order.129
for (value in values.sortedBy<GridRequirement, Long> { it -> it.span })130
tracks.requireContent(value.start, value.span, value.extent)131
}133
private fun cellOffset(align: CrossAlignment, remaining: Double): Double =134
when (align) {135
case Start -> 0.0136
case Stretch -> 0.0137
case Center -> remaining / 2.0138
case End -> remaining139
case Baseline -> throw IllegalArgumentException(140
Some("grid cells require start, center, end or stretch alignment")141
)142
}