scoop:scoop.ui.component:0.1.0
src/table/data_component.scoop
Selected production source · src/table/data_component.scoop
1
package scoop.ui.component3
import scoop.collections.HashSet4
import scoop.graphics.*5
import scoop.ui.*6
import scoop.ui.style.*7
import scoop.window.accessibility.*8
import scoop.ui.component.frame as dataTableFrame10
internal class DataTablePlan<Row>(11
val state: DataTableState<Row>,12
val hidden: Array<String>,13
val widths: Array<TableColumnWidth>,14
val selection: Boolean,15
val appearance: TableAppearance16
) {17
val columns: Array<TableColumn<Row>> = visible()18
val semanticHeaders =19
Array<SemanticsReference>(20
columns.size +21
if (selection)22
1L23
else24
0L25
) { _ -> SemanticsReference() }26
val columnKeys =27
Array<SemanticItemKey>(semanticHeaders.size) {28
index -> if (selection && index == 0L)29
SemanticItemKey.Text("selection")30
else31
SemanticItemKey.Text(32
f"column:${33
columns[34
index -35
if (selection)36
1L37
else38
0L39
].id40
}"41
)42
}43
val tracks = makeTracks()44
private fun visible(): Array<TableColumn<Row>> {45
val result = ArrayList<TableColumn<Row>>()46
for (column in state.columns)47
if (state.isColumnVisible(column.id))48
result.add(column)49
return result.toArray()50
}51
private fun makeTracks(): TableTracks {52
val sizes = ArrayList<TableColumnSize>()53
val headers = ArrayList<String>()54
if (selection) {55
sizes.add(TableColumnSize(GridTrack.Fixed(40.0), minimum = 40.0));56
headers.add("")57
}58
for (column in columns) {59
val size = column.size60
sizes.add(61
when (state.columnWidth(column.id)) {62
case Some(val width) -> TableColumnSize(63
GridTrack.Fixed(width),64
size.minimum,65
size.maximum,66
size.samples67
);68
case None -> size69
}70
)71
headers.add(column.header)72
}73
val values = sizes.toArray()74
return TableTracks(75
values,76
appearance.natural(values, headers.toArray(), 32.0),77
appearance.density78
)79
}80
}82
public class DataTable<Row> public constructor(83
private val props: Props<DataTableProps<Row>>,84
scope: ComponentScope85
): Component(scope) {86
private val themes = ThemeAccess(scope)87
private val horizontal = ScrollState()88
private val vertical = ScrollState()89
private val menuOpen = state(false)90
private var plan: DataTablePlan<Row>? = None91
public override fun onMount() {92
themes.mount(props.value.theme)93
}94
public override fun render(): View {95
val input = props.value96
val model = input.state97
val menuOpen = this.menuOpen98
val theme = themes.read(input.theme)99
val fonts = input.fonts ?: scope.defaultFonts100
val hidden = model.hiddenColumns101
val widths = model.widths102
val reusable =103
when (plan) {104
case Some(val old) -> old.state ===105
model &&106
old.hidden ===107
hidden &&108
old.widths ===109
widths &&110
old.selection ==111
input.selection &&112
old.appearance.theme ===113
theme &&114
old.appearance.fonts ===115
fonts &&116
old.appearance.direction ==117
scope.layoutDirection118
case None -> false119
}120
if (!reusable)121
plan =122
Some(123
DataTablePlan(124
model,125
hidden,126
widths,127
input.selection,128
TableAppearance(theme, fonts, input.theme, scope.layoutDirection)129
)130
)131
val geometry = plan!!132
val appearance = geometry.appearance133
val density = appearance.density134
val rows = model.result.rows135
val selected = model.selectedKeys136
val items = DataTableItems(input, geometry, rows, selected)137
val list =138
virtualList(139
items,140
when (input.itemExtent) {141
case Fixed(val value) -> ItemExtent.Fixed(value * density);142
case Variable(val value) -> ItemExtent.Variable(value * density)143
}144
) {145
itemSemantics = Some(items.semantics())146
collectionSemantics = input.semantics147
this.height = Some(input.bodyHeight * density)148
this.state = Some(input.vertical ?: this @DataTable.vertical)149
this.handle = input.handle150
this.overscan = 48.0 * density151
this.enabled = input.enabled152
this.theme = input.theme153
}154
val body =155
stack {156
add(list)157
if (rows.count == 0L)158
add(159
dataTableFrame(appearance.content(input.empty, dim = true)) {160
this.height = Some(input.bodyHeight * density)161
this.alignment = Alignment(AxisAlignment.Center, AxisAlignment.Center)162
this.padding = Some(Insets.all(appearance.padding))163
}164
)165
}166
val content =167
column {168
this.spacing = 0.0169
this.alignment = CrossAlignment.Stretch170
this.add(dataTableHeader(input, geometry))171
this.add(body)172
}173
val controls =174
ScrollControlProps(175
input.horizontal ?: horizontal,176
ScrollAxes.Horizontal,177
40.0,178
ScrollChain.Chain,179
ScrollbarVisibility.Auto,180
scrollbarOptions(),181
input.enabled,182
FocusOptions(tabStop = false),183
PaintCachePolicy.None,184
theme185
)186
val viewport =187
element(188
TableViewportInput(geometry.tracks, controls, input.width),189
{ props, scope -> TableViewport(props, scope) },190
[content]191
)192
return column {193
this.spacing = 12.0 * density194
this.alignment = CrossAlignment.Stretch195
if (input.columnMenu || panelPresent(input.toolbar))196
this.row {197
this.spacing = 8.0 * density198
this.alignment = CrossAlignment.Center199
when (input.toolbar) {200
case Some(val view) -> this.item(view) { this.grow = 1.0 };201
case None -> this.item(empty()) { this.grow = 1.0 }202
}203
if (input.columnMenu)204
this.add(dataTableColumnMenu(input, menuOpen.binding()))205
}206
this.add(viewport)207
if (input.pagination)208
this.add(dataTablePagination(input, appearance))209
when (input.footer) {210
case Some(val view) -> this.add(view);211
case None -> {}212
}213
}214
}215
}217
private class DataTableItems<Row>(218
private val input: DataTableProps<Row>,219
private val plan: DataTablePlan<Row>,220
private val rows: TableSource<Row>,221
private val selectedKeys: Array<ViewKey>222
): ItemSource {223
private val selected = HashSet<ViewKey>()224
init {225
for (key in selectedKeys)226
this.selected.add(key)227
}228
public override val count: Long get() = rows.count229
public override fun keyAt(index: Long): ViewKey = rows.keyAt(index)230
public override fun indexOfKey(key: ViewKey): Long? = rows.indexOfKey(key)231
fun semantics(): ItemSemantics {232
val selected = ArrayList<SemanticItemKey>()233
for (key in selectedKeys)234
if (rows.indexOfKey(key) != None)235
selected.add(componentSemanticKey(key))236
val columns =237
Array<SemanticGridColumn>(plan.columnKeys.size) {238
index -> SemanticGridColumn(239
plan.columnKeys[index],240
Some(241
if (input.selection && index == 0L)242
"Selection"243
else244
plan.columns[245
index -246
if (input.selection)247
1L248
else249
0L250
].header251
),252
plan.semanticHeaders[index].id253
)254
}255
return ItemSemantics(256
this,257
{ index -> describe(index) },258
selection =259
if (input.selection)260
Some(SemanticCollectionSelection(selected.toArray(), multiple = true))261
else262
None,263
setSelected =264
if (input.selection)265
Some { key, value -> setSelected(key, value) }266
else267
None,268
grid = Some(SemanticGrid(columns, { key, column -> cell(key, column) }))269
)270
}271
private fun setSelected(key: ViewKey, value: Boolean): SemanticActionResult {272
if (!input.enabled)273
return SemanticActionResult.Rejected274
val current = input.state.result.rows275
val index = current.indexOfKey(key) ?: return SemanticActionResult.Unavailable276
if (!input.state.selectable(current.rowAt(index)))277
return SemanticActionResult.Rejected278
input.state.setSelected(key, value)279
return if (input.state.isSelected(key) == value)280
SemanticActionResult.Completed281
else282
SemanticActionResult.Rejected283
}284
private fun cell(key: SemanticItemKey, column: Long): SemanticItemProperties? {285
val rowKey =286
when (key) {287
case Number(val value) -> ViewKey.Number(value);288
case Text(val value) -> ViewKey.Text(value)289
}290
val index = rows.indexOfKey(rowKey) ?: return None291
val row = rows.rowAt(index)292
if (input.selection && column == 0L)293
return Some(294
SemanticItemProperties(295
name = Some("Select row"),296
enabled = input.enabled && input.state.selectable(row),297
selected = Some(selected.contains(rowKey))298
)299
)300
val value =301
plan.columns[302
column -303
if (input.selection)304
1L305
else306
0L307
]308
val text = value.semanticText ?: return None309
return Some(310
SemanticItemProperties(311
name = Some(value.header),312
value = Some(text(row)),313
enabled = input.enabled314
)315
)316
}317
fun describe(index: Long): SemanticItemProperties? =318
Some(319
SemanticItemProperties(320
name = dataTableRowText(plan.columns, rows.rowAt(index)),321
enabled = input.enabled,322
selected =323
if (input.selection)324
Some(selected.contains(rows.keyAt(index)))325
else326
None327
)328
)329
public override fun build(index: Long): View {330
val input = this.input331
val row = rows.rowAt(index)332
val key = rows.keyAt(index)333
val cells = ArrayList<TableCellProps>()334
if (input.selection)335
cells.add(336
TableCellProps(337
PanelContent.Custom(338
checkbox(339
binding(340
{ input.state.isSelected(key) },341
{ value -> input.state.setSelected(key, value) }342
)343
) {344
this.enabled = input.enabled && input.state.selectable(row);345
this.theme = input.theme346
this.semantics = ComponentSemantics(name = Some("Select row"))347
}348
),349
key = ViewKey.Text("selection")350
)351
)352
for (column in plan.columns)353
cells.add(354
TableCellProps(355
PanelContent.Custom(column.cell(row)),356
alignment = column.alignment,357
key = ViewKey.Text(f"column:${column.id}")358
)359
)360
return tableRowView(361
TableRowProps(cells.toArray(), selected = selected.contains(key)),362
plan.tracks,363
plan.appearance,364
key,365
rowIndex = index,366
columnHeaders = plan.semanticHeaders,367
columnKeys = plan.columnKeys,368
logical = true369
)370
}371
}