scoop:scoop.ui.component:0.1.0
src/tree/row_layout.scoop
Selected production source · src/tree/row_layout.scoop
1
package scoop.ui.component3
import scoop.graphics.*4
import scoop.surface.*5
import scoop.ui.*6
import scoop.window.accessibility.*8
private const val treeCellPadding: Double = 4.010
internal const val treeRowSlotSize: Double = 20.012
internal class TreeRowDrawing<T>(13
val input: TreeRowInput<T>,14
val owner: TreeRowView<T>,15
val selected: Boolean,16
val interaction: InteractionState17
)19
internal class TreeRowInk<T>(private val props: Props<TreeRowDrawing<T>>, scope: ComponentScope) :20
Element<Rect>(scope) {21
// The row's main target lives outside this group; only F2 enters cell Tab order.22
private val group = scope.focusScope()23
private val selected = scope.animation(if (props.value.selected) 1.0 else 0.0)24
private val hovered = scope.animation(if (props.value.interaction.hovered) 1.0 else 0.0)25
private val reveal = scope.animation(1.0)26
private var previous = props.value.input.plan.projection27
private var previousIndex = props.value.input.index28
public override fun onMount() {29
props.value.owner.cellGroup = Some(group)30
when (props.value.input.plan.controller.presented) {31
case Some(val old) ->32
if (old.indexOfKey(props.value.input.row.key) == None) reveal.snapTo(0.0)33
case None -> {}34
}35
}36
public override fun onUnmount() {37
props.value.owner.cellGroup = None38
}39
public override fun update(): UiInvalidation {40
val drawing = props.value41
drawing.owner.update()42
val input = drawing.input43
val motion = input.plan.appearance.theme.tokens.motion44
val enabled = input.plan.input.enabled && input.row.node.enabled45
changeProgress(46
selected,47
if (drawing.selected) 1.0 else 0.0,48
if (enabled) motion.resolve(motion.selection) else None49
)50
changeProgress(51
hovered, if (drawing.interaction.hovered && enabled) 1.0 else 0.0,52
if (enabled) motion.resolve(motion.hover) else None53
)54
if (previous !== input.plan.projection && previousIndex != input.index) reveal.snapTo(0.72)55
previous = input.plan.projection56
previousIndex = input.index57
changeProgress(reveal, 1.0, if (enabled) motion.resolve(motion.selection) else None)58
return UiInvalidation.Layout59
}60
public override fun measure(context: MeasureContext, constraints: Constraints): LayoutResult<61
Rect62
> {63
val plan = props.value.input.plan64
val widths = plan.tracks.resolve(constraints.maxWidth ?: constraints.minWidth)65
val count = if (plan.explicit) plan.columns.size else 1L66
val sizes = ArrayList<Size>()67
var height = 24.0 * plan.appearance.density68
for (index in 0L..<count) {69
val value = context70
.measureChild(71
index,72
Constraints(73
widths.values[index],74
Some(widths.values[index]),75
maxHeight = constraints.maxHeight76
)77
)78
sizes.add(value.size)79
height = lowerBound(height, value.size.height)80
}81
val size = constraints.constrain(Size(widths.total, height))82
val treeIndex = plan.columnIndex(plan.treeColumn)!!83
val rtl = context.layoutDirection == LayoutDirection.Rtl84
var treeX = 0.085
var x = 0.086
for (index in 0L..<count) {87
val childX = if (rtl) widths.total - x - sizes[index].width else x88
if (index == treeIndex) treeX = childX89
context90
.placeChild(91
index,92
Point(childX, lowerBound((size.height - sizes[index].height) / 2.0, 0.0))93
)94
x += widths.values[index]95
}96
return LayoutResult(97
LayoutMetrics(size),98
Rect(treeX, 0.0, sizes[treeIndex].width, size.height)99
)100
}101
public override fun scene(context: SceneContext, treeColumn: Rect) {102
val drawing = props.value103
val plan = drawing.input.plan104
val palette = plan.appearance.theme.tokens.palette105
val bounds = Rect(0.0, 0.0, context.size.width, context.size.height)106
val direction = context.layoutDirection107
val selection = selected.value108
val hover = hovered.value109
context110
.draw { paint ->111
// Round fills and guides at the same device-pixel boundary between rows.112
paint113
.canvas114
.drawRect(115
bounds, Paint(116
palette.background.background.withAlpha(1.0),117
antiAlias = false118
)119
)120
if (hover > 0.0)121
paint122
.canvas123
.drawRect(124
bounds,125
Paint(126
palette.muted.background.withAlpha(0.5 * hover),127
antiAlias = false128
)129
)130
if (selection > 0.0)131
paint132
.canvas133
.drawRect(134
bounds,135
Paint(palette.muted.background.withAlpha(selection), antiAlias = false)136
)137
if (plan.input.showLines)138
drawTreeLines(paint.canvas, drawing.input, treeColumn, direction)139
if (drawing.interaction.focusVisible) {140
val line = lowerBound(plan.appearance.density, 1.0)141
paint.canvas.drawRect(Rect(0.0, 0.0, bounds.width, line), Paint(palette.ring))142
paint143
.canvas144
.drawRect(145
Rect(0.0, lowerBound(bounds.height - line, 0.0), bounds.width, line),146
Paint(palette.ring)147
)148
}149
}150
if (plan.explicit || plan.input.fullRow || panelPresent(plan.input.drag))151
context.place(drawing.owner.area, bounds)152
context.save()153
context.clipRect(bounds)154
context.saveLayer(reveal.value)155
context.children()156
// Restore the reveal layer and row clip before drawing drag feedback.157
context.restore()158
context.restore()159
drawing.owner.drag?.scene(context, bounds)160
}161
}163
internal class TreeCellInput<T>(164
val row: TreeRowInput<T>,165
val owner: TreeRowView<T>,166
val column: Long,167
val id: String,168
val content: View,169
val alignment: AxisAlignment170
)172
internal fun <T> treeCell(173
row: TreeRowInput<T>,174
owner: TreeRowView<T>,175
column: Long,176
id: String,177
content: View,178
alignment: AxisAlignment179
): View = component(180
TreeCellInput(row, owner, column, id, content, alignment),181
{ value, scope -> TreeCellView(value, scope) },182
ViewKey.Text(id)183
)185
private class TreeCellView<T>(private val props: Props<TreeCellInput<T>>, scope: ComponentScope) :186
Component(scope) {187
private val target = TreeCellTarget(scope, scope.focusScope())188
public override fun onMount() {189
props.value.owner.cells[props.value.id] = target190
}191
public override fun onUnmount() {192
props.value.owner.cells.remove(props.value.id)193
}194
public override fun render(): View = element(195
props.value,196
{ value, mount -> TreeCellInk(value, mount) },197
[props.value.content]198
)199
}201
private class TreeCellInk<T>(private val props: Props<TreeCellInput<T>>, scope: ComponentScope) :202
Element<Rect>(scope) {203
private val semantics = createComponentSemantics(scope, SemanticRole.Cell)204
init {205
scope.routeSemanticsChildren(semantics)206
}207
public override fun update(): UiInvalidation {208
val input = props.value209
val plan = input.row.plan210
val row = input.row.row211
semantics212
?.update(213
SemanticProperties(214
ignored = !plan.explicit,215
cell = if (plan.explicit)216
Some(217
SemanticTableCell(218
input.row.index,219
input.column,220
columnKey = Some(SemanticItemKey.Text(input.id))221
)222
)223
else224
None225
),226
relations = SemanticRelations(227
columnHeaders = if (plan.explicit)228
semanticIds(plan.headers[input.column].id)229
else230
[]231
)232
)233
return UiInvalidation.Layout234
}235
public override fun measure(context: MeasureContext, constraints: Constraints): LayoutResult<236
Rect237
> {238
val input = props.value239
val density = input.row.plan.appearance.density240
val padding = treeCellPadding * density241
val child = context242
.measureChild(243
0L,244
Constraints(245
maxWidth = insetMaximum(constraints.maxWidth, padding * 2.0),246
maxHeight = insetMaximum(constraints.maxHeight, padding * 2.0)247
)248
)249
val size = constraints250
.constrain(Size(child.size.width + padding * 2.0, child.size.height + padding * 2.0))251
val origin = alignedOrigin(252
Rect(253
padding,254
padding,255
lowerBound(size.width - padding * 2.0, 0.0),256
lowerBound(size.height - padding * 2.0, 0.0)257
),258
child.size,259
Alignment(input.alignment, AxisAlignment.Center),260
context.layoutDirection261
)262
context.placeChild(0L, origin)263
return LayoutResult(LayoutMetrics(size), Rect(origin.x, 0.0, child.size.width, size.height))264
}265
public override fun scene(context: SceneContext, content: Rect) {266
val input = props.value267
val plan = input.row.plan268
val bounds = Rect(0.0, 0.0, context.size.width, context.size.height)269
when (semantics) {270
case Some(val value) -> context.semantics(value, bounds);271
case None -> {}272
}273
if (input.id ==274
plan.treeColumn &&275
!plan.explicit &&276
!plan.input.fullRow &&277
!panelPresent(plan.input.drag)278
)279
context.place(input.owner.area, content)280
context.clipRect(bounds)281
context.children()282
}283
}285
private fun <T> drawTreeLines(286
canvas: Canvas,287
input: TreeRowInput<T>,288
column: Rect,289
direction: LayoutDirection290
) {291
val plan = input.plan292
val row = input.row293
val hasVisibleChildren = row.subtreeEnd > input.index + 1L294
if (row.depth == 0L && !hasVisibleChildren) return295
val density = plan.appearance.density296
val step = plan.input.indent * density297
val halfSlot = treeRowSlotSize / 2.0 * density298
val rtl = direction == LayoutDirection.Rtl299
val columnRight = column.x + column.width300
val height = column.height301
val center = height / 2.0302
val color = Paint(plan.appearance.theme.tokens.palette.border, antiAlias = false)303
fun stemX(depth: Long): Double {304
val offset = treeCellPadding * density + halfSlot + depth.toDouble() * step305
return if (rtl) columnRight - offset - density else column.x + offset306
}307
fun segment(x: Double, width: Double, top: Double, bottom: Double) {308
val left = lowerBound(x, column.x)309
val right = upperBound(x + width, columnRight)310
if (right > left && bottom > top)311
canvas.drawRect(Rect(left, top, right - left, bottom - top), color)312
}313
var at = row.parent314
while (at != None) {315
val ancestor = plan.projection.rowAt(at!!)316
if (ancestor.depth > 0L && !ancestor.lastSibling)317
segment(stemX(ancestor.depth - 1L), density, 0.0, height)318
at = ancestor.parent319
}320
if (row.depth > 0L) {321
val x = stemX(row.depth - 1L)322
val top = lowerBound(center - density / 2.0, 0.0)323
val bottom = upperBound(center + density / 2.0, height)324
segment(x, density, 0.0, if (row.lastSibling) bottom else height)325
segment(if (rtl) x + density - step else x + density, step - density, top, bottom)326
}327
if (hasVisibleChildren) segment(stemX(row.depth), density, center + halfSlot, height)328
}