Scoop API

scoop:scoop.ui.component:0.1.0

src/tree/header.scoop

Selected production source · src/tree/header.scoop

1package scoop.ui.component
3import scoop.graphics.*
4import scoop.ui.*
5import scoop.ui.style.*
7internal fun <T> treeHeader(plan: TreePlan<T>): View {
8 if (!plan.explicit || !plan.input.showHeader) return empty()
9 val input = plan.input
10 val model = input.state
11 val cells = ArrayList<TableCellProps>()
12 val views = ArrayList<View>()
13 for (index in 0L..<plan.columns.size) {
14 val column = plan.columns[index]
15 val title = column.header +
16 when (plan.columnState.sorting) {
17 case Some(val order) ->
18 if (order.column == column.id) {
19 if (order.direction == TableSortDirection.Ascending) " ↑" else " ↓"
20 } else
21 ""
22 case None -> ""
23 }
24 val caption = when (column.headerView) {
25 case Some(val view) -> view
26 case None -> plan.appearance.content(tableText(title), header = true)
27 }
28 val content = if (!column.sortable)
29 caption
30 else
31 treeActionButton(
32 caption, title, input.enabled, input.theme,
33 { origin -> model.toggleSort(column.id, origin) }, tabStop = true, size = ButtonSize
34 .Sm
35 )
36 val cell = TableCellProps(
37 PanelContent.Custom(content),
38 alignment = column.alignment,
39 header = true,
40 semantics = ComponentSemantics(name = Some(title))
41 )
42 cells.add(cell)
43 val view = plan
44 .appearance
45 .cell(
46 cell,
47 ViewKey.Text(column.id),
48 semantics = Some(basicCellSemantics(cell, reference = Some(plan.headers[index])))
49 )
50 views
51 .add(
52 if (!column.resizable)
53 view
54 else
55 element(
56 TableResizeInput(
57 binding(
58 { treeWidth(model, column.id) },
59 { width ->
60 treeResize(
61 model,
62 column.id,
63 width,
64 TreeChangeOrigin.Programmatic
65 )
66 }
67 ),
68 column.size,
69 plan.appearance.theme,
70 input.enabled,
71 ComponentSemantics(name = Some("Resize " + column.header)),
72 onWidthChange = Some { width, origin ->
73 treeResize(
74 model, column.id, width,
75 when (origin) {
76 case Programmatic -> TreeChangeOrigin.Programmatic
77 case Pointer -> TreeChangeOrigin.Pointer
78 case Keyboard -> TreeChangeOrigin.Keyboard
79 case Accessibility -> TreeChangeOrigin.Accessibility
80 }
81 )
82 }
83 ),
84 { value, scope -> TableResizeHeader(value, scope) }, [view], ViewKey
85 .Text(column.id)
86 )
87 )
88 }
89 return tableRowView(
90 TableRowProps(cells.toArray(), TableSection.Header),
91 plan.tracks,
92 plan.appearance,
93 ViewKey.Text("header"),
94 prepared = Some(views.toArray())
95 )
98private fun <T> treeResize(
99 model: TreeState<T>,
100 id: String,
101 width: Double?,
102 origin: TreeChangeOrigin
103) {
104 val definition = model.columns ?: return
105 if (!panelPresent(definition.find(id))) return
106 when (width) {
107 case Some(val value) -> model.resizeColumn(id, value, origin)
108 case None -> model.resetColumnWidth(id, origin)
109 }
112private fun <T> treeWidth(model: TreeState<T>, id: String): Double? {
113 val definition = model.columns ?: return None
114 if (!panelPresent(definition.find(id))) return None
115 return model.columnState.width(id)