scoop:scoop.ui.component:0.1.0
src/tree/controller.scoop
Selected production source · src/tree/controller.scoop
1
package scoop.ui.component3
import scoop.collections.HashMap4
import scoop.ui.*6
internal interface TreeMountedRow {7
val focused: Boolean8
val pending: Boolean9
val main: FocusHandle10
val focusedColumn: String?11
fun hasCell(column: String): Boolean12
fun focusCell(column: String): Boolean13
fun enter(): Boolean14
}16
internal 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: Boolean23
)25
internal class TreeViewController<T> : TreeTarget {26
var ink: TreeInk<T>? = None27
var plan: TreePlan<T>? = None28
var presented: TreeProjection<T>? = None29
val rows = HashMap<ViewKey, TreeMountedRow>()30
public override val active: Boolean get() = ink?.active ?: false31
public override val pending: Boolean32
get() {33
if (ink?.pending ?: false) return true34
for (entry in rows.entries)35
if (entry.value.pending) return true36
return false37
}38
fun focusedKey(): ViewKey? {39
for (entry in rows.entries)40
if (entry.value.focused) return Some(entry.key)41
return None42
}43
fun register(key: ViewKey, row: TreeMountedRow) {44
rows[key] = row45
}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 None55
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 None60
for (value in columns)61
if (value.id == column) return None62
if (columns.size == 0L && column == "title") return None63
return Some(key)64
}65
case None -> {}66
}67
val old = plan ?: return None68
val index = old.projection.indexOfKey(key) ?: return None69
var parent = old.projection.rowAt(index).parent70
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.parent75
}76
if (next.count == 0L) return None77
var at = if (index < next.count) index else next.count - 1L78
while (at < next.count) {79
if (next.rowAt(at).node.enabled) return Some(next.keyAt(at));80
at += 1L81
}82
at = if (index < next.count) index else next.count - 1L83
while (at >= 0L) {84
if (next.rowAt(at).node.enabled) return Some(next.keyAt(at));85
at -= 1L86
}87
return None88
}89
private fun prepare(key: ViewKey, column: String?, expand: Boolean): Boolean {90
val current = plan ?: return false91
if (!active || !current.input.enabled) return false92
val node = current.input.state.data.node(key) ?: return false93
if (!node.enabled) return false94
when (column) {95
case Some(val id) -> {96
val state = current.input.state.columnState97
when (state.columns) {98
case Some(val definition) -> {99
if (!panelPresent(definition.find(id)) || !state.isVisible(id)) return false100
}102
case None -> if (id != "title") return false103
}104
}106
case None -> {}107
}108
if (expand) {109
val path = current.input.state.pathTo(key) ?: return false110
for (ancestor in path)111
if (ancestor != key && !current.input.state.isExpanded(ancestor))112
if (!current.input.state.setExpanded(ancestor, true)) return false113
}114
return current.input.state.projection.indexOfKey(key) != None115
}116
public override fun scroll(117
key: ViewKey,118
column: String?,119
alignment: ScrollAlignment,120
expand: Boolean121
): Boolean {122
if (!prepare(key, column, expand)) return false123
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 false127
when (column) {128
case Some(val id) -> {129
val input = plan!!.input130
val focusable = when (input.state.columns) {131
case Some(val definition) -> definition.find(id)?.focusable ?: false132
case None -> input.titleFocusable133
}134
if (!focusable) return false135
}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 false142
}144
case(None, Some(val row)) -> {145
if (!row.main.requestFocus()) return false146
}148
else -> {}149
}150
return ink!!.request(key, column, ScrollAlignment.Nearest, true)151
}152
fun finishFocus(request: TreeScrollRequest) {153
if (!request.focus) return154
val row = rows[request.key] ?: return155
when (request.column) {156
case Some(val id) -> row.focusCell(id);157
case None -> row.main.requestFocus()158
}159
}160
}