Scoop API

scoop:scoop.ui.component:0.1.0

src/tree/controller.scoop

Selected production source · src/tree/controller.scoop

1package scoop.ui.component
3import scoop.collections.HashMap
4import scoop.ui.*
6internal interface TreeMountedRow {
7 val focused: Boolean
8 val pending: Boolean
9 val main: FocusHandle
10 val focusedColumn: String?
11 fun hasCell(column: String): Boolean
12 fun focusCell(column: String): Boolean
13 fun enter(): Boolean
16internal class TreeScrollRequest(
17 val key: ViewKey,
18 val column: String?,
19 val alignment: ScrollAlignment,
20 val revision: ULong,
21 val horizontalRevision: ULong,
22 val focus: Boolean
25internal class TreeViewController<T> : TreeTarget {
26 var ink: TreeInk<T>? = None
27 var plan: TreePlan<T>? = None
28 var presented: TreeProjection<T>? = None
29 val rows = HashMap<ViewKey, TreeMountedRow>()
30 public override val active: Boolean get() = ink?.active ?: false
31 public override val pending: Boolean
32 get() {
33 if (ink?.pending ?: false) return true
34 for (entry in rows.entries)
35 if (entry.value.pending) return true
36 return false
37 }
38 fun focusedKey(): ViewKey? {
39 for (entry in rows.entries)
40 if (entry.value.focused) return Some(entry.key)
41 return None
42 }
43 fun register(key: ViewKey, row: TreeMountedRow) {
44 rows[key] = row
45 }
46 fun unregister(key: ViewKey, row: TreeMountedRow) {
47 when (rows[key]) {
48 case Some(val current) -> if (current === row) rows.remove(key);
49 case None -> {}
50 }
51 }
52 /** Read before keyed reconciliation retires a focused row or cell. */
53 fun fallback(next: TreeProjection<T>, columns: Array<TreeColumn<T>>): ViewKey? {
54 val key = focusedKey() ?: return None
55 val session = rows[key]!!
56 when (next.indexOfKey(key)) {
57 case Some(val at) ->
58 if (next.rowAt(at).node.enabled) {
59 val column = session.focusedColumn ?: return None
60 for (value in columns)
61 if (value.id == column) return None
62 if (columns.size == 0L && column == "title") return None
63 return Some(key)
64 }
65 case None -> {}
66 }
67 val old = plan ?: return None
68 val index = old.projection.indexOfKey(key) ?: return None
69 var parent = old.projection.rowAt(index).parent
70 while (parent != None) {
71 val row = old.projection.rowAt(parent!!)
72 val candidate = next.indexOfKey(row.key)
73 if (candidate != None && next.rowAt(candidate!!).node.enabled) return Some(row.key)
74 parent = row.parent
75 }
76 if (next.count == 0L) return None
77 var at = if (index < next.count) index else next.count - 1L
78 while (at < next.count) {
79 if (next.rowAt(at).node.enabled) return Some(next.keyAt(at));
80 at += 1L
81 }
82 at = if (index < next.count) index else next.count - 1L
83 while (at >= 0L) {
84 if (next.rowAt(at).node.enabled) return Some(next.keyAt(at));
85 at -= 1L
86 }
87 return None
88 }
89 private fun prepare(key: ViewKey, column: String?, expand: Boolean): Boolean {
90 val current = plan ?: return false
91 if (!active || !current.input.enabled) return false
92 val node = current.input.state.data.node(key) ?: return false
93 if (!node.enabled) return false
94 when (column) {
95 case Some(val id) -> {
96 val state = current.input.state.columnState
97 when (state.columns) {
98 case Some(val definition) -> {
99 if (!panelPresent(definition.find(id)) || !state.isVisible(id)) return false
100 }
102 case None -> if (id != "title") return false
103 }
104 }
106 case None -> {}
107 }
108 if (expand) {
109 val path = current.input.state.pathTo(key) ?: return false
110 for (ancestor in path)
111 if (ancestor != key && !current.input.state.isExpanded(ancestor))
112 if (!current.input.state.setExpanded(ancestor, true)) return false
113 }
114 return current.input.state.projection.indexOfKey(key) != None
115 }
116 public override fun scroll(
117 key: ViewKey,
118 column: String?,
119 alignment: ScrollAlignment,
120 expand: Boolean
121 ): Boolean {
122 if (!prepare(key, column, expand)) return false
123 return ink!!.request(key, column, alignment, false)
124 }
125 public override fun focus(key: ViewKey, column: String?, expand: Boolean): Boolean {
126 if (!prepare(key, column, expand)) return false
127 when (column) {
128 case Some(val id) -> {
129 val input = plan!!.input
130 val focusable = when (input.state.columns) {
131 case Some(val definition) -> definition.find(id)?.focusable ?: false
132 case None -> input.titleFocusable
133 }
134 if (!focusable) return false
135 }
137 case None -> {}
138 }
139 when ((column, rows[key])) {
140 case(Some(val id), Some(val row)) -> {
141 if (row.hasCell(id) && !row.focusCell(id)) return false
142 }
144 case(None, Some(val row)) -> {
145 if (!row.main.requestFocus()) return false
146 }
148 else -> {}
149 }
150 return ink!!.request(key, column, ScrollAlignment.Nearest, true)
151 }
152 fun finishFocus(request: TreeScrollRequest) {
153 if (!request.focus) return
154 val row = rows[request.key] ?: return
155 when (request.column) {
156 case Some(val id) -> row.focusCell(id);
157 case None -> row.main.requestFocus()
158 }
159 }