Scoop API

scoop:scoop.ui.component:0.1.0

src/table/data_component.scoop

Selected production source · src/table/data_component.scoop

1package scoop.ui.component
3import scoop.collections.HashSet
4import scoop.graphics.*
5import scoop.ui.*
6import scoop.ui.style.*
7import scoop.window.accessibility.*
8import scoop.ui.component.frame as dataTableFrame
10internal 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: TableAppearance
16) {
17 val columns: Array<TableColumn<Row>> = visible()
18 val semanticHeaders =
19 Array<SemanticsReference>(
20 columns.size +
21 if (selection)
22 1L
23 else
24 0L
25 ) { _ -> SemanticsReference() }
26 val columnKeys =
27 Array<SemanticItemKey>(semanticHeaders.size) {
28 index -> if (selection && index == 0L)
29 SemanticItemKey.Text("selection")
30 else
31 SemanticItemKey.Text(
32 f"column:${
33 columns[
34 index -
35 if (selection)
36 1L
37 else
38 0L
39 ].id
40 }"
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.size
60 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.samples
67 );
68 case None -> size
69 }
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.density
78 )
79 }
82public class DataTable<Row> public constructor(
83 private val props: Props<DataTableProps<Row>>,
84 scope: ComponentScope
85): 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>? = None
91 public override fun onMount() {
92 themes.mount(props.value.theme)
93 }
94 public override fun render(): View {
95 val input = props.value
96 val model = input.state
97 val menuOpen = this.menuOpen
98 val theme = themes.read(input.theme)
99 val fonts = input.fonts ?: scope.defaultFonts
100 val hidden = model.hiddenColumns
101 val widths = model.widths
102 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.layoutDirection
118 case None -> false
119 }
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.appearance
133 val density = appearance.density
134 val rows = model.result.rows
135 val selected = model.selectedKeys
136 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.semantics
147 this.height = Some(input.bodyHeight * density)
148 this.state = Some(input.vertical ?: this @DataTable.vertical)
149 this.handle = input.handle
150 this.overscan = 48.0 * density
151 this.enabled = input.enabled
152 this.theme = input.theme
153 }
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.0
169 this.alignment = CrossAlignment.Stretch
170 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 theme
185 )
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 * density
194 this.alignment = CrossAlignment.Stretch
195 if (input.columnMenu || panelPresent(input.toolbar))
196 this.row {
197 this.spacing = 8.0 * density
198 this.alignment = CrossAlignment.Center
199 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 }
217private 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.count
229 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 else
244 plan.columns[
245 index -
246 if (input.selection)
247 1L
248 else
249 0L
250 ].header
251 ),
252 plan.semanticHeaders[index].id
253 )
254 }
255 return ItemSemantics(
256 this,
257 { index -> describe(index) },
258 selection =
259 if (input.selection)
260 Some(SemanticCollectionSelection(selected.toArray(), multiple = true))
261 else
262 None,
263 setSelected =
264 if (input.selection)
265 Some { key, value -> setSelected(key, value) }
266 else
267 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.Rejected
274 val current = input.state.result.rows
275 val index = current.indexOfKey(key) ?: return SemanticActionResult.Unavailable
276 if (!input.state.selectable(current.rowAt(index)))
277 return SemanticActionResult.Rejected
278 input.state.setSelected(key, value)
279 return if (input.state.isSelected(key) == value)
280 SemanticActionResult.Completed
281 else
282 SemanticActionResult.Rejected
283 }
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 None
291 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 1L
305 else
306 0L
307 ]
308 val text = value.semanticText ?: return None
309 return Some(
310 SemanticItemProperties(
311 name = Some(value.header),
312 value = Some(text(row)),
313 enabled = input.enabled
314 )
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 else
326 None
327 )
328 )
329 public override fun build(index: Long): View {
330 val input = this.input
331 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.theme
346 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 = true
369 )
370 }