scoop:scoop.ui.component:0.1.0
src/table/builder.scoop
Selected production source · src/table/builder.scoop
1
package scoop.ui.component3
import scoop.collections.HashSet4
import scoop.graphics.text.*5
import scoop.ui.*6
import scoop.ui.style.*8
public enum TableSection {9
Header, Body, Footer10
}12
public 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()19
)21
public 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.Position26
)28
public 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()38
)40
public class TableCellBuilder internal constructor() {41
public var semantics: ComponentSemantics = ComponentSemantics()42
public var span: Long = 1L43
public var alignment: AxisAlignment = AxisAlignment.Start44
public var key: ViewKey = ViewKey.Position45
}47
public class TableRowBuilder internal constructor() {48
private val cells = ArrayList<TableCellProps>()49
public var selected: Boolean = false50
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.() -> Unit72
) {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.semantics83
)84
)85
}86
internal fun snapshot(section: TableSection, key: ViewKey): TableRowProps =87
TableRowProps(cells.toArray(), section, selected, key)88
}90
public 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
}97
}99
public class TableBuilder internal constructor() {100
public var semantics: ComponentSemantics = ComponentSemantics()101
private val rows = ArrayList<TableRowProps>()102
private var caption: PanelContent? = None103
public var columns: Array<TableColumnSize>? = None104
public var horizontal: ScrollState? = None105
public var width: Double? = None106
public var enabled: Boolean = true107
public var fonts: FontCollection? = None108
public var theme: ThemeSource? = None109
public var key: ViewKey = ViewKey.Position110
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 = 0L137
if (values.size > 0L)138
for (cell in values[0L].cells)139
count += cell.span140
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
key145
)146
}147
}149
public fun table(configure: TableBuilder.() -> Unit): View {150
val builder = TableBuilder()151
builder.configure()152
return builder.build()153
}155
internal fun tableText(text: String): PanelContent =156
PanelContent.Label(LabelProps(LabelText.Plain(text, None)))158
internal 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 = 0L165
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.span171
}172
requireComponent(covered == count && count > 0L, "table row must cover all columns")173
}174
}