scoop:scoop.ui.component:0.1.0
src/table/layout.scoop
Selected production source · src/table/layout.scoop
1
package scoop.ui.component3
import scoop.graphics.*4
import scoop.surface.*5
import scoop.ui.*6
import scoop.ui.style.*7
import scoop.window.accessibility.*9
internal class TableViewportInput(10
val tracks: TableTracks,11
val controls: ScrollControlProps,12
val width: Double?13
)15
internal class TableViewport(16
private val props: Props<TableViewportInput>,17
scope: ComponentScope18
): Element<Size>(scope) {19
private val control = ScrollControl(scope) { props.value.controls }20
public override fun update(): UiInvalidation {21
control.update();22
return UiInvalidation.Layout23
}24
public override fun measure(context: MeasureContext, constraints: Constraints): LayoutResult<25
Size26
> {27
val input = props.value28
val proposed = input.width ?: constraints.maxWidth ?: input.tracks.resolve(0.0).total29
nonnegative(proposed, "invalid table width")30
val viewport = constraints.constrain(Size(proposed, 0.0)).width31
if (viewport <= 0.0 || constraints.maxHeight == Some(0.0)) {32
context.skipChild(0L)33
return LayoutResult(34
LayoutMetrics(constraints.constrain(Size(0.0, 0.0))),35
Size(0.0, 0.0)36
)37
}38
val widths = input.tracks.resolve(viewport)39
val child =40
context.measureChild(41
0L,42
Constraints(widths.total, Some(widths.total), maxHeight = constraints.maxHeight)43
)44
context.placeChild(0L, Point(0.0, 0.0))45
return LayoutResult(46
LayoutMetrics(constraints.constrain(Size(viewport, child.size.height))),47
child.size48
)49
}50
public override fun scene(context: SceneContext, content: Size) {51
control.scene(context, control.layout(context.size, content))52
}53
}55
internal class TableRowInput(56
val row: TableRowProps,57
val tracks: TableTracks,58
val appearance: TableAppearance,59
val border: Boolean = true60
)62
internal class TableRowInk(private val props: Props<TableRowInput>, scope: ComponentScope): Element<63
Unit64
>(scope) {65
private val area =66
scope.createArea {67
context,68
_ -> if (context.phase == RoutedPhase.Notification && scope.isMounted)69
scope.requestFrame()70
}71
init {72
scope.routeChildren(area)73
}74
public override fun update(): UiInvalidation = UiInvalidation.Layout75
public override fun measure(context: MeasureContext, constraints: Constraints): LayoutResult<76
Unit77
> {78
val input = props.value79
val widths = input.tracks.resolve(constraints.maxWidth ?: constraints.minWidth)80
var at = 0L81
var height =82
if (input.row.section == TableSection.Header)83
40.0 * input.appearance.density84
else85
0.086
val sizes = ArrayList<Size>()87
val offsets = ArrayList<Double>()88
var x = 0.089
for (index in 0L ..< input.row.cells.size) {90
var width = 0.091
for (_ in 0L ..< input.row.cells[index].span) {92
width += widths.values[at];93
at += 1L94
}95
val child =96
context.measureChild(97
index,98
Constraints(width, Some(width), maxHeight = constraints.maxHeight)99
)100
sizes.add(child.size)101
offsets.add(x)102
x += width103
height = lowerBound(height, child.size.height)104
}105
val size = constraints.constrain(Size(widths.total, height))106
for (index in 0L ..< sizes.size)107
context.placeChild(108
index,109
Point(110
if (scope.layoutDirection == LayoutDirection.Rtl)111
widths.total - offsets[index] - sizes[index].width112
else113
offsets[index],114
lowerBound((size.height - sizes[index].height) / 2.0, 0.0)115
)116
)117
return LayoutResult(LayoutMetrics(size), Unit)118
}119
public override fun scene(context: SceneContext, value: Unit) {120
val input = props.value121
val color = input.appearance.theme.tokens.palette.muted.background122
val background =123
if (input.row.selected)124
Some(color)125
else126
if (area.scope.isHovered || input.row.section == TableSection.Footer)127
Some(color.withAlpha(color.alpha * 0.5))128
else129
None130
val bounds = Rect(0.0, 0.0, context.size.width, context.size.height)131
context.draw {132
paint ->133
when (background) {134
case Some(val color) -> paint.canvas.drawRect(bounds, Paint(color));135
case None -> {}136
}137
if (input.border)138
paint.canvas.drawRect(139
Rect(140
0.0,141
lowerBound(bounds.height - input.appearance.density, 0.0),142
bounds.width,143
input.appearance.density144
),145
Paint(input.appearance.theme.tokens.palette.border)146
)147
}148
context.place(area, bounds)149
context.children()150
}151
}153
internal fun tableRowView(154
row: TableRowProps,155
tracks: TableTracks,156
appearance: TableAppearance,157
key: ViewKey,158
border: Boolean = true,159
prepared: Array<View>? = None,160
rowIndex: Long = 0L,161
headers: TableHeaderSemantics? = None,162
columnHeaders: Array<SemanticsReference> = [],163
columnKeys: Array<SemanticItemKey> = [],164
logical: Boolean = false165
): View {166
val children =167
prepared ?:168
Array<View>(row.cells.size) {169
index -> appearance.cell(170
row.cells[index],171
row.cells[index].key,172
row.section == TableSection.Footer,173
Some(174
when (headers) {175
case Some(val value) -> value.cell(row, rowIndex, index)176
case None -> {177
var column = 0L178
for (previous in 0L ..< index)179
column += row.cells[previous].span180
basicCellSemantics(181
row.cells[index],182
Some(183
SemanticTableCell(184
rowIndex,185
column,186
columnSpan = row.cells[index].span,187
columnKey =188
if (column < columnKeys.size)189
Some(columnKeys[column])190
else191
None192
)193
),194
if (column < columnHeaders.size)195
semanticIds(columnHeaders[column].id)196
else197
[]198
)199
}200
}201
)202
)203
}204
val content =205
element(206
TableRowInput(row, tracks, appearance, border),207
{ props, scope -> TableRowInk(props, scope) },208
children,209
key210
)211
return semanticContent(212
content,213
SemanticRole.Row,214
SemanticProperties(215
selected =216
if (row.section == TableSection.Body)217
Some(row.selected)218
else219
None,220
ignored = logical221
),222
mergeLabels = false,223
key = key224
)225
}