Scoop API

scoop:scoop.ui.component:0.1.0

src/table/builder.scoop

Selected production source · src/table/builder.scoop

1package scoop.ui.component
3import scoop.collections.HashSet
4import scoop.graphics.text.*
5import scoop.ui.*
6import scoop.ui.style.*
8public enum TableSection {
9 Header, Body, Footer
12public class TableCellProps public constructor(
13 public val content: PanelContent,
14 public val span: Long = 1L,
15 public val alignment: AxisAlignment = AxisAlignment.Start,
16 public val header: Boolean = false,
17 public val key: ViewKey = ViewKey.Position,
18 public val semantics: ComponentSemantics = ComponentSemantics()
21public class TableRowProps public constructor(
22 public val cells: Array<TableCellProps>,
23 public val section: TableSection = TableSection.Body,
24 public val selected: Boolean = false,
25 public val key: ViewKey = ViewKey.Position
28public class TableProps public constructor(
29 public val rows: Array<TableRowProps>,
30 public val columns: Array<TableColumnSize>,
31 public val caption: PanelContent? = None,
32 public val horizontal: ScrollState? = None,
33 public val width: Double? = None,
34 public val enabled: Boolean = true,
35 public val fonts: FontCollection? = None,
36 public val theme: ThemeSource? = None,
37 public val semantics: ComponentSemantics = ComponentSemantics()
40public class TableCellBuilder internal constructor() {
41 public var semantics: ComponentSemantics = ComponentSemantics()
42 public var span: Long = 1L
43 public var alignment: AxisAlignment = AxisAlignment.Start
44 public var key: ViewKey = ViewKey.Position
47public class TableRowBuilder internal constructor() {
48 private val cells = ArrayList<TableCellProps>()
49 public var selected: Boolean = false
50 public fun cell(text: String, configure: TableCellBuilder.() -> Unit = {}) {
51 append(tableText(text), false, configure)
52 }
53 public fun cell(text: StyledText, configure: TableCellBuilder.() -> Unit = {}) {
54 append(PanelContent.Label(LabelProps(LabelText.Styled(text))), false, configure)
55 }
56 public fun cell(view: View, configure: TableCellBuilder.() -> Unit = {}) {
57 append(PanelContent.Custom(view), false, configure)
58 }
59 public fun head(text: String, configure: TableCellBuilder.() -> Unit = {}) {
60 append(tableText(text), true, configure)
61 }
62 public fun head(text: StyledText, configure: TableCellBuilder.() -> Unit = {}) {
63 append(PanelContent.Label(LabelProps(LabelText.Styled(text))), true, configure)
64 }
65 public fun head(view: View, configure: TableCellBuilder.() -> Unit = {}) {
66 append(PanelContent.Custom(view), true, configure)
67 }
68 private fun append(
69 content: PanelContent,
70 header: Boolean,
71 configure: TableCellBuilder.() -> Unit
72 ) {
73 val builder = TableCellBuilder()
74 builder.configure()
75 cells.add(
76 TableCellProps(
77 content,
78 builder.span,
79 builder.alignment,
80 header,
81 builder.key,
82 builder.semantics
83 )
84 )
85 }
86 internal fun snapshot(section: TableSection, key: ViewKey): TableRowProps =
87 TableRowProps(cells.toArray(), section, selected, key)
90public class TableSectionBuilder internal constructor(private val section: TableSection) {
91 internal val rows = ArrayList<TableRowProps>()
92 public fun row(key: ViewKey = ViewKey.Position, configure: TableRowBuilder.() -> Unit) {
93 val builder = TableRowBuilder()
94 builder.configure()
95 rows.add(builder.snapshot(section, key))
96 }
99public class TableBuilder internal constructor() {
100 public var semantics: ComponentSemantics = ComponentSemantics()
101 private val rows = ArrayList<TableRowProps>()
102 private var caption: PanelContent? = None
103 public var columns: Array<TableColumnSize>? = None
104 public var horizontal: ScrollState? = None
105 public var width: Double? = None
106 public var enabled: Boolean = true
107 public var fonts: FontCollection? = None
108 public var theme: ThemeSource? = None
109 public var key: ViewKey = ViewKey.Position
110 public fun header(configure: TableSectionBuilder.() -> Unit) {
111 append(TableSection.Header, configure)
112 }
113 public fun body(configure: TableSectionBuilder.() -> Unit) {
114 append(TableSection.Body, configure)
115 }
116 public fun footer(configure: TableSectionBuilder.() -> Unit) {
117 append(TableSection.Footer, configure)
118 }
119 public fun caption(text: String) {
120 caption = Some(tableText(text))
121 }
122 public fun caption(text: StyledText) {
123 caption = Some(PanelContent.Label(LabelProps(LabelText.Styled(text))))
124 }
125 public fun caption(view: View) {
126 caption = Some(PanelContent.Custom(view))
127 }
128 private fun append(section: TableSection, configure: TableSectionBuilder.() -> Unit) {
129 val builder = TableSectionBuilder(section)
130 builder.configure()
131 for (value in builder.rows)
132 rows.add(value)
133 }
134 internal fun build(): View {
135 val values = rows.toArray()
136 var count = 0L
137 if (values.size > 0L)
138 for (cell in values[0L].cells)
139 count += cell.span
140 val sizes = columns ?: Array<TableColumnSize>(count) { _ -> TableColumnSize() }
141 return component(
142 TableProps(values, sizes, caption, horizontal, width, enabled, fonts, theme, semantics),
143 { props, scope -> Table(props, scope) },
144 key
145 )
146 }
149public fun table(configure: TableBuilder.() -> Unit): View {
150 val builder = TableBuilder()
151 builder.configure()
152 return builder.build()
155internal fun tableText(text: String): PanelContent =
156 PanelContent.Label(LabelProps(LabelText.Plain(text, None)))
158internal fun tableCheckRows(rows: Array<TableRowProps>, count: Long) {
159 val keys = HashSet<ViewKey>()
160 for (index in 0L ..< rows.size) {
161 val row = rows[index]
162 if (row.key != ViewKey.Position)
163 requireComponent(keys.add(row.key), "duplicate table row key")
164 var covered = 0L
165 for (cell in row.cells) {
166 requireComponent(
167 cell.span > 0L && cell.span <= count - covered,
168 "table cell span exceeds columns"
169 )
170 covered += cell.span
171 }
172 requireComponent(covered == count && count > 0L, "table row must cover all columns")
173 }