scoop:scoop.ui.component:0.1.0
src/tree/row.scoop
Selected production source · src/tree/row.scoop
1
package scoop.ui.component3
import scoop.collections.HashMap4
import scoop.graphics.*5
import scoop.icons.lucide.file as fileIcon6
import scoop.icons.lucide.folder as folderIcon7
import scoop.surface.*8
import scoop.ui.*9
import scoop.ui.style.*10
import scoop.window.*11
import scoop.ui.component.row as makeTreeRow12
import scoop.ui.component.frame as treeSlot13
import scoop.ui.component.spinner as treeSpinner14
import scoop.ui.component.label as treeLabel15
import scoop.ui.component.checkbox as treeCheckbox16
import scoop.ui.component.icon as treeIcon18
internal class TreeRowInput<T>(val plan: TreePlan<T>, val index: Long) {19
val row: TreeRow<T> get() = plan.projection.rowAt(index)20
}22
internal class TreeCellTarget(val scope: ComponentScope, val focus: FocusScope)24
internal class TreeRowView<T>(private val props: Props<TreeRowInput<T>>, scope: ComponentScope) :25
Component(scope),26
TreeMountedRow {27
public override val main = FocusHandle()28
val area = scope.createArea(focusable = true)29
private val focus = scope.focusTarget(area, FocusOptions(Some(main)))30
// Keep drag admission ahead of Press, including when drag is enabled after mounting.31
private val source = scope.dragSource(area, DragSourceOptions(enabled = false)) { _ -> None }32
private val press = scope.press(area, PressOptions()) { activate() }33
val cells = HashMap<String, TreeCellTarget>()34
var cellGroup: FocusScope? = None35
private var pointer: PointerState? = None36
private var clicks = 137
var inputOrigin = TreeChangeOrigin.Pointer38
var drag: TreeDragControl<T>? = None39
public override val focused: Boolean get() = scope.input.activity.hasFocus40
public override val pending: Boolean41
get() {42
if (main.isPending || (cellGroup?.isPending ?: false)) return true43
for (entry in cells.entries)44
if (entry.value.focus.isPending) return true45
return false46
}47
public override val focusedColumn: String?48
get() {49
for (entry in cells.entries)50
if (entry.value.scope.input.activity.hasFocus) return Some(entry.key)51
return None52
}53
init {54
scope.routeChildren(area)55
area.scope.defaultAction = { context, event -> handle(context, event) }56
scope57
.resource {58
area59
.scope60
.addEventHandler { context, event ->61
if (context.phase ==62
RoutedPhase.Capture ||63
context.phase ==64
RoutedPhase.Target65
)66
when (event) {67
case Keyboard(_) -> inputOrigin = TreeChangeOrigin.Keyboard69
case Pointer(70
InputPointerEvent.Button(val value, _, _, val count)71
) -> {72
inputOrigin = TreeChangeOrigin.Pointer73
pointer = Some(value.state);74
clicks = count75
}77
case Contact(InputContactEvent.Began(val value)) -> {78
inputOrigin = TreeChangeOrigin.Pointer79
pointer = Some(value.pointer);80
clicks = 181
}83
else -> {}84
}85
}86
}87
scope88
.resource {89
scope90
.input91
.observeActivity {92
if (scope.isMounted) {93
updateTabStop();94
scope.requestFrame()95
}96
}97
}98
}99
public override fun onMount() {100
props.value.plan.controller.register(props.value.row.key, this)101
}102
public override fun onUnmount() {103
drag?.close()104
drag = None105
props.value.plan.controller.unregister(props.value.row.key, this)106
}107
fun update() {108
val input = props.value109
val enabled = input.plan.input.enabled && input.row.node.enabled110
area.scope.enabled = enabled111
press.update(PressOptions(enabled, false), { activate() })112
if (panelPresent(input.plan.input.drag) && !panelPresent(drag))113
drag = Some(TreeDragControl(scope, area, source, { props.value }))114
drag?.update()115
updateTabStop()116
}117
private fun updateTabStop() {118
focus.options = FocusOptions(119
Some(main),120
props.value.plan.input.focus.tabStop && (!focused || main.hasFocus)121
)122
}123
public override fun focusCell(column: String): Boolean = cells[column]?.focus?.requestFocus() ?:124
false125
public override fun hasCell(column: String): Boolean = panelPresent(cells[column])126
public override fun enter(): Boolean = cellGroup?.requestFocus() ?: false127
private fun handle(context: RoutedEventContext, event: InputEvent) {128
val input = props.value129
if (!input.plan.input.enabled || !input.row.node.enabled) return130
when (event) {131
case Keyboard(val key) -> {132
if (key.logicalKey ==133
LogicalKey.Named(NamedKey.Escape) &&134
key.state ==135
ElementState.Pressed &&136
!main.hasFocus137
) {138
context.consume();139
main.requestFocus(FocusOrigin.Keyboard)140
} else if (main.hasFocus) {141
if (treeKeyboard(input.plan, input.index, key))142
context.consume()143
else if (key.state ==144
ElementState.Pressed &&145
!key.synthetic &&146
!key.modifiers.control &&147
!key.modifiers.superKey &&148
!key.modifiers.alt149
)150
when (key.logicalKey) {151
case Character(val text) -> {152
input.plan.controller.ink?.type(text);153
context.consume()154
}156
else -> {}157
}158
}159
}161
case Pointer(162
InputPointerEvent.Button(val value, val button, ElementState.Pressed, _)163
) -> {164
if (isContextMenuClick(button, value.state.modifiers)) {165
context.consume()166
main.requestFocus(FocusOrigin.Pointer)167
input168
.plan169
.input170
.onContextMenu(171
TreeActivation(172
input.row.node,173
TreeChangeOrigin.Pointer,174
Some(value.state)175
)176
)177
}178
}180
else -> {}181
}182
}183
private fun activate() {184
val input = props.value185
val plan = input.plan186
val row = input.row187
if (!plan.input.enabled || !row.node.enabled) return188
if (plan.controller.ink?.isSticky(row.key) ?: false)189
plan.controller.scroll(row.key, None, ScrollAlignment.Start, false)190
treeSelect(plan, row.key, pointer?.modifiers, TreeChangeOrigin.Pointer)191
val activation = TreeActivation(row.node, TreeChangeOrigin.Pointer, pointer)192
plan.input.onActivate(activation)193
if (clicks == 2) plan.input.onDoubleClick(activation)194
if (plan.input.expandAction ==195
TreeExpandAction.Click ||196
(plan.input.expandAction == TreeExpandAction.DoubleClick && clicks == 2)197
)198
plan.input.state.setExpanded(row.key, !row.expanded, TreeChangeOrigin.Pointer)199
}200
public override fun render(): View {201
val input = props.value202
val plan = input.plan203
val row = input.row204
val selected = plan.input.state.isSelected(row.key)205
val interaction = press.interaction206
val content = ArrayList<View>()207
if (!plan.explicit)208
content209
.add(210
treeCell(input, this, 0L, "title", treeTitle(input, this), AxisAlignment.Start)211
)212
else213
for (index in 0L..<plan.columns.size) {214
val column = plan.columns[index]215
content216
.add(217
treeCell(218
input, this, index, column.id,219
if (column.id == plan.treeColumn)220
treeTitle(input, this, Some(column.cell(row.node)))221
else222
column.cell(row.node), column.alignment223
)224
)225
}226
return element(227
TreeRowDrawing(input, this, selected, interaction),228
{ value, mount -> TreeRowInk(value, mount) },229
content.toArray()230
)231
}232
}234
private fun <T> treeTitle(235
input: TreeRowInput<T>,236
owner: TreeRowView<T>,237
custom: View? = None238
): View {239
val row = input.row240
val plan = input.plan241
val options = plan.input242
val density = plan.appearance.density243
val slot = treeRowSlotSize * density244
val title = when (options.title) {245
case Some(val build) -> build(row)246
case None ->247
custom ?:248
label(row.node.label) {249
this.theme = options.theme250
this.fonts = Some(plan.appearance.fonts)251
this.settings = Some(plan.appearance.paragraph)252
this253
.style {254
this.weight = Some(if (row.matched) 500 else 400)255
this.color = Some(256
if (!options.enabled || !row.node.enabled)257
plan.appearance.theme.tokens.palette.muted.foreground258
else if (row.matched)259
plan.appearance.theme.tokens.palette.primary.background260
else261
plan.appearance.body.color262
)263
}264
}265
}266
return makeTreeRow {267
this.spacing = 0.0268
this.alignment = CrossAlignment.Center269
this270
.add(271
treeSlot(empty()) {272
this.width = Some(row.depth.toDouble() * options.indent * density);273
this.height = Some(1.0)274
}275
)276
val status = options.state.loadStatus(row.key)277
val failed = when (status) {278
case Failed(_) -> true;279
else -> false280
}281
val loading = when (status) {282
case Loading -> true;283
else -> false284
}285
val disclosure = if (!row.node.expandable)286
empty()287
else288
treeActionButton(289
if (loading)290
when (options.loadingIcon) {291
case Some(val build) -> build(row)292
case None ->293
treeSpinner {294
this.size = Some(14.0 * density);295
this.theme = options.theme296
}297
}298
else if (failed)299
treeLabel("↻") {300
this.theme = options.theme;301
this.fonts = Some(plan.appearance.fonts)302
}303
else304
when (options.switcher) {305
case Some(val build) -> build(row);306
case None -> treeArrow(input)307
},308
(if (failed) "Retry loading " else if (row.expanded) "Collapse " else "Expand ") +309
row.node.label,310
options.enabled && row.node.enabled, options.theme,311
{ origin ->312
if (failed)313
options.state.retryLoad(row.key)314
else315
options.state.setExpanded(row.key, !row.expanded, origin)316
}317
)318
this319
.add(320
treeSlot(disclosure) {321
this.width = Some(slot);322
this.height = Some(slot)323
}324
)325
if (treeCheckingEnabled(options.state.checking)) {326
val check = if (!row.node.checkable)327
empty()328
else329
treeCheckbox(330
binding(331
{ options.state.checkState(row.key) },332
{ next ->333
options334
.state335
.setChecked(336
row.key,337
next == CheckState.Checked,338
owner.inputOrigin339
)340
}341
)342
) {343
this.enabled = options.enabled && row.node.canCheck344
this.theme = options.theme345
this.focus = FocusOptions(tabStop = false)346
this.style { this.padding = Some(Insets.all(0.0)) }347
this.semantics = ComponentSemantics(348
name = Some("Check " + row.node.label),349
ignored = true350
)351
}352
this353
.add(354
treeSlot(check) {355
this.width = Some(slot);356
this.height = Some(slot)357
}358
)359
}360
val decoration: View? = when (options.icon) {361
case Some(val build) -> build(row)362
case None ->363
if (!options.showIcons)364
None365
else366
Some(367
treeIcon(368
when (row.node.children) {369
case Leaf -> fileIcon;370
else -> folderIcon371
}372
) {373
this.size = 16.0 * density;374
this.theme = options.theme375
}376
)377
}378
when (decoration) {379
case Some(val view) ->380
this381
.add(382
treeSlot(view) {383
this.width = Some(slot);384
this.height = Some(slot)385
}386
);387
case None -> {}388
}389
this.item(title) { this.shrink = 1.0 }390
}391
}