Scoop API

scoop:scoop.ui.component:0.1.0

src/table/component.scoop

Selected production source · src/table/component.scoop

1package scoop.ui.component
3import scoop.ui.*
4import scoop.ui.style.*
5import scoop.window.accessibility.*
7public class Table public constructor(
8 private val props: Props<TableProps>,
9 scope: ComponentScope
10): Component(scope) {
11 private val themes = ThemeAccess(scope)
12 private val horizontal = ScrollState()
13 private val semanticHeaders = TableHeaderSemantics()
14 private var checked: TableProps? = None
15 private var appearance: TableAppearance? = None
16 private var tracks: TableTracks? = None
17 public override fun onMount() {
18 themes.mount(props.value.theme)
19 }
20 public override fun render(): View {
21 val input = props.value
22 val theme = themes.read(input.theme)
23 val fonts = input.fonts ?: scope.defaultFonts
24 val old = appearance
25 val changed =
26 when (old) {
27 case Some(val value) -> value.theme !==
28 theme ||
29 value.fonts !==
30 fonts ||
31 value.direction !=
32 scope.layoutDirection;
33 case None -> true
34 }
35 val sameInput =
36 when (checked) {
37 case Some(val value) -> value === input;
38 case None -> false
39 }
40 if (changed || !sameInput) {
41 tableCheckRows(input.rows, input.columns.size)
42 val next = TableAppearance(theme, fonts, input.theme, scope.layoutDirection)
43 val headers = MutableArray<String>(input.columns.size) { _ -> "" }
44 for (row in input.rows)
45 if (row.section == TableSection.Header) {
46 var at = 0L
47 for (cell in row.cells) {
48 if (cell.span == 1L)
49 when (cell.content) {
50 case Label(val label) -> headers[at] =
51 when (label.text) {
52 case Plain(val text, _) -> text;
53 case Styled(val text) -> text.text
54 }
55 else -> {}
56 }
57 at += cell.span
58 }
59 }
60 appearance = Some(next)
61 tracks =
62 Some(
63 TableTracks(
64 input.columns,
65 next.natural(
66 input.columns,
67 Array<String>(headers.size) { index -> headers[index] }
68 ),
69 next.density
70 )
71 )
72 checked = Some(input)
73 }
74 val visuals = appearance!!
75 val sizes = tracks!!
76 semanticHeaders.update(input.rows)
77 val rows =
78 column {
79 this.spacing = 0.0
80 this.alignment = CrossAlignment.Stretch
81 for (index in 0L ..< input.rows.size)
82 this.add(
83 tableRowView(
84 input.rows[index],
85 sizes,
86 visuals,
87 input.rows[index].key,
88 index + 1L < input.rows.size,
89 rowIndex = index,
90 headers = Some(this @Table.semanticHeaders)
91 )
92 )
93 }
94 val controls =
95 ScrollControlProps(
96 input.horizontal ?: horizontal,
97 ScrollAxes.Horizontal,
98 40.0,
99 ScrollChain.Chain,
100 ScrollbarVisibility.Auto,
101 scrollbarOptions(),
102 input.enabled,
103 FocusOptions(tabStop = false),
104 PaintCachePolicy.None,
105 theme
106 )
107 val viewport =
108 element(
109 TableViewportInput(sizes, controls, input.width),
110 { props, scope -> TableViewport(props, scope) },
111 [rows]
112 )
113 val content =
114 column {
115 this.spacing = 16.0 * visuals.density
116 this.alignment = CrossAlignment.Stretch
117 this.add(viewport)
118 when (input.caption) {
119 case Some(val value) -> this.add(visuals.content(value, dim = true));
120 case None -> {}
121 }
122 }
123 return semanticContent(
124 content,
125 SemanticRole.Table,
126 input.semantics.properties(enabled = input.enabled).copy {
127 table = Some(SemanticTableSize(input.rows.size, input.columns.size))
128 if (name == None && input.semantics.relations.labelledBy.size == 0L)
129 name = semanticCaption(input.caption)
130 },
131 relations = input.semantics.relations,
132 mergeLabels = false,
133 reference = input.semantics.reference
134 )
135 }