Scoop API

scoop:scoop.ui.component:0.1.0

src/tree/columns.scoop

Selected production source · src/tree/columns.scoop

1package scoop.ui.component
3import scoop.collections.HashMap
4import scoop.collections.HashSet
5import scoop.ui.*
7/** Header text names the column even when headerView supplies its visual content. */
8public class TreeColumn<T> public constructor(
9 public val id: String,
10 public val header: String,
11 public val cell: (TreeNode<T>) -> View,
12 public val size: TableColumnSize = TableColumnSize(),
13 public val alignment: AxisAlignment = AxisAlignment.Start,
14 public val hideable: Boolean = true,
15 public val resizable: Boolean = true,
16 public val compare: ((T, T) -> Int)? = None,
17 public val semanticText: ((TreeNode<T>) -> String)? = None,
18 public val headerView: View? = None,
19 /** Declares an offscreen cell's focus capability; focus still requires a real mounted target. */
20 public val focusable: Boolean = false
21) {
22 init {
23 requireComponent(id != "", "tree column id must not be empty")
24 }
25 public val sortable: Boolean
26 get() = when (compare) {
27 case Some(_) -> true
28 case None -> false
29 }
32/** A nonempty ordered column definition; the identified tree column cannot be hidden. */
33public class TreeColumns<T> public constructor(
34 public val columns: Array<TreeColumn<T>>,
35 public val treeColumn: String
36) {
37 private val keyed = HashMap<String, TreeColumn<T>>()
38 init {
39 requireComponent(columns.size > 0L, "tree columns must not be empty")
40 val seen = HashSet<String>()
41 for (column in columns) {
42 requireComponent(seen.add(column.id), "duplicate tree column id")
43 keyed[column.id] = column
44 }
45 requireComponent(seen.contains(treeColumn), "tree column must name a defined column")
46 }
47 public fun find(id: String): TreeColumn<T>? = keyed[id]
48 public fun column(id: String): TreeColumn<T> = keyed[id] ?:
49 throw IllegalArgumentException(Some(f"unknown tree column: ${id}"))
52/** Definition and presentation state form one value so controlled replacement is atomic. */
53public class TreeColumnState<T> public constructor(
54 public val columns: TreeColumns<T>? = None,
55 public val hiddenColumns: Array<String> = [],
56 public val widths: Array<TableColumnWidth> = [],
57 public val sorting: TableSort? = None
58) {
59 private val hidden = HashSet<String>()
60 private val overrides = HashMap<String, Double>()
61 public val visibleColumns: Array<TreeColumn<T>> = prepare()
62 private fun prepare(): Array<TreeColumn<T>> {
63 val visible = ArrayList<TreeColumn<T>>()
64 when (columns) {
65 case None ->
66 requireComponent(
67 hiddenColumns.size == 0L && widths.size == 0L && sorting == None,
68 "single column tree cannot contain column state"
69 )
71 case Some(val definition) -> {
72 for (id in hiddenColumns) {
73 val column = definition.column(id)
74 requireComponent(hidden.add(id), "duplicate hidden tree column")
75 requireComponent(
76 column.hideable && id != definition.treeColumn,
77 "tree column cannot be hidden"
78 )
79 }
80 for (width in widths) {
81 val column = definition.column(width.column)
82 requireComponent(overrides[width.column] == None, "duplicate tree column width")
83 requireComponent(
84 column.size.constrain(width.width) == width.width,
85 "tree column width is out of range"
86 )
87 overrides[width.column] = width.width
88 }
89 when (sorting) {
90 case Some(val sort) ->
91 requireComponent(
92 definition.column(sort.column).sortable,
93 "tree sort column requires a comparator"
94 )
95 case None -> {}
96 }
97 for (column in definition.columns)
98 if (!hidden.contains(column.id)) visible.add(column)
99 }
100 }
101 return visible.toArray()
102 }
103 public fun isVisible(id: String): Boolean {
104 when (columns) {
105 case Some(val value) -> value.column(id)
106 case None -> throw IllegalArgumentException(Some("tree has no explicit columns"))
107 }
108 return !hidden.contains(id)
109 }
110 public fun width(id: String): Double? {
111 when (columns) {
112 case Some(val value) -> value.column(id)
113 case None -> throw IllegalArgumentException(Some("tree has no explicit columns"))
114 }
115 return overrides[id]
116 }
119public enum TreeColumnChangeKind {
120 Definition, Visibility, Width, Sort
123public class TreeColumnChange<T> internal constructor(
124 public val column: String?,
125 public val kind: TreeColumnChangeKind,
126 public val value: TreeColumnState<T>,
127 public val origin: TreeChangeOrigin
130internal fun <T> treeColumnStatesEqual(
131 left: TreeColumnState<T>,
132 right: TreeColumnState<T>
133): Boolean {
134 if (left === right) return true
135 val same = when ((left.columns, right.columns)) {
136 case(Some(val a), Some(val b)) -> a === b
137 case(None, None) -> true
138 else -> false
139 }
140 if (!same ||
141 left.sorting !=
142 right.sorting ||
143 left.hiddenColumns.size !=
144 right.hiddenColumns.size ||
145 left.widths.size !=
146 right.widths.size
147 )
148 return false
149 for (index in 0L..<left.hiddenColumns.size)
150 if (left.hiddenColumns[index] != right.hiddenColumns[index]) return false
151 for (index in 0L..<left.widths.size)
152 if (left.widths[index].column !=
153 right.widths[index].column ||
154 left.widths[index].width !=
155 right.widths[index].width
156 )
157 return false
158 return true
161internal fun <T> treeReplacedColumns(
162 previous: TreeColumnState<T>,
163 columns: TreeColumns<T>?
164): TreeColumnState<T> {
165 val definition = columns ?: return TreeColumnState<T>()
166 val hidden = ArrayList<String>()
167 for (id in previous.hiddenColumns) {
168 val column = definition.find(id) ?: continue
169 if (column.hideable && id != definition.treeColumn) hidden.add(id)
170 }
171 val widths = ArrayList<TableColumnWidth>()
172 for (width in previous.widths) {
173 val column = definition.find(width.column) ?: continue
174 widths.add(TableColumnWidth(column.id, column.size.constrain(width.width)))
175 }
176 val sort = when (previous.sorting) {
177 case Some(val value) ->
178 when (definition.find(value.column)) {
179 case Some(val column) -> if (column.sortable) Some(value) else None
180 case None -> None
181 }
182 case None -> None
183 }
184 return TreeColumnState(columns, hidden.toArray(), widths.toArray(), sort)