scoop:scoop.ui.component:0.1.0
src/tree/root_layout.scoop
Selected production source · src/tree/root_layout.scoop
1
package scoop.ui.component3
import scoop.graphics.*4
import scoop.ui.*6
internal class TreeViewportInput(val contentWidth: Double, val horizontalOffset: Double)8
private class TreeBodyLayoutInput(val viewport: TreeViewportInput) {9
val layout = LayoutInput(viewport)10
}12
internal class TreeRootFrame(val layout: ScrollLayout)14
/** Horizontal scrolling moves header and row content while leaving the body viewport in place. */15
internal class TreeRoot<T>(private val props: Props<TreePlan<T>>, scope: ComponentScope) :16
Element<TreeRootFrame>(scope) {17
private val control = ScrollControl(scope) { props.value.horizontalControls }18
private var bodyInput: TreeBodyLayoutInput? = None19
public override fun update(): UiInvalidation {20
control.update();21
return UiInvalidation.Layout22
}23
public override fun measure(context: MeasureContext, constraints: Constraints): LayoutResult<24
TreeRootFrame25
> {26
val plan = props.value27
val input = plan.input28
when (input.width) {29
case Some(val value) -> nonnegative(value, "invalid tree width");30
case None -> {}31
}32
when (input.height) {33
case Some(val value) -> nonnegative(value, "invalid tree height");34
case None -> {}35
}36
val width = constraints37
.constrain(Size(input.width ?: constraints.maxWidth ?: constraints.minWidth, 0.0))38
.width39
val maximumHeight: Double? = when (input.height) {40
case Some(_) -> input.height;41
case None -> constraints.maxHeight42
}43
requireComponent(44
!input.virtualize || panelPresent(maximumHeight),45
"virtual tree requires a finite height"46
)47
val verticalGutter = plan.controller.ink?.gutter ?: Size(0.0, 0.0)48
val available = lowerBound(width - verticalGutter.width, 0.0)49
when (input.contentWidth) {50
case Some(val value) -> nonnegative(value, "invalid tree content width");51
case None -> {}52
}53
val widths = plan.tracks.resolve(lowerBound(available, input.contentWidth ?: 0.0))54
val header = context55
.measureChild(56
0L,57
Constraints(widths.total, Some(widths.total), maxHeight = maximumHeight)58
)59
val bodyLimit: Double? = when (maximumHeight) {60
case Some(val value) ->61
Some(lowerBound(value - header.size.height - control.gutter.height, 0.0));62
case None -> None63
}64
val estimatedHeight = bodyLimit ?:65
(plan.projection.count.toDouble() * itemEstimate(plan.extent))66
val preliminary = control67
.layout(68
Size(width, header.size.height + estimatedHeight),69
Size(widths.total + verticalGutter.width, header.size.height + estimatedHeight)70
)71
val request: TreeScrollRequest? = when (plan.controller.ink) {72
case Some(val ink) -> ink.requestValue73
case None -> None74
}75
val offset = treeHorizontalOffset(plan, widths, preliminary.snapshot, request)76
val body = context77
.measureChild(78
1L,79
Constraints(80
width,81
Some(width),82
if (panelPresent(bodyLimit)) bodyLimit!! else 0.0,83
bodyLimit84
),85
bodyLayoutInput(widths.total, offset)86
)87
val size = constraints88
.constrain(Size(width, header.size.height + body.size.height + control.gutter.height))89
val viewport = control.contentViewport(size, context.layoutDirection)90
val layout = control91
.layout(92
Size(viewport.width, viewport.height),93
Size(widths.total + verticalGutter.width, viewport.height),94
Some(ScrollAdjustment(plan.horizontal.requestRevision, Point(offset, 0.0)))95
)96
val bodyRect = plan.controller.ink!!.bodyViewport(body.size, context.layoutDirection)97
context.placeChild(0L, Point(bodyRect.x - layout.snapshot.padding.left, 0.0))98
context99
.placeChild(100
1L,101
Point(layout.snapshot.offset.x - layout.snapshot.padding.left, header.size.height)102
)103
return LayoutResult(LayoutMetrics(size), TreeRootFrame(layout))104
}105
private fun bodyLayoutInput(106
contentWidth: Double,107
horizontalOffset: Double108
): LayoutInput<TreeViewportInput> {109
when (bodyInput) {110
case Some(val cached) ->111
if (cached.viewport.contentWidth ==112
contentWidth &&113
cached.viewport.horizontalOffset ==114
horizontalOffset115
)116
return cached.layout117
case None -> {}118
}119
val input = TreeBodyLayoutInput(TreeViewportInput(contentWidth, horizontalOffset))120
bodyInput = Some(input)121
return input.layout122
}123
public override fun scene(context: SceneContext, frame: TreeRootFrame) {124
control125
.scene(126
context,127
frame.layout,128
children = { content ->129
content130
.withScrollReveal(131
control.viewport,132
Rect(133
0.0,134
0.0,135
frame.layout.snapshot.viewport.width,136
frame.layout.snapshot.viewport.height137
),138
ScrollAxes.Horizontal139
) { inner -> inner.children() }140
}141
)142
}143
}145
private fun <T> treeHorizontalOffset(146
plan: TreePlan<T>,147
widths: TableWidths,148
snapshot: ScrollSnapshot,149
request: TreeScrollRequest?150
): Double {151
val current = request ?: return snapshot.offset.x152
if (current.horizontalRevision != plan.horizontal.requestRevision) return snapshot.offset.x153
val column = current.column ?: return snapshot.offset.x154
val at = plan.columnIndex(column) ?: return snapshot.offset.x155
var start = 0.0156
for (index in 0L..<at)157
start += widths.values[index]158
if (plan.appearance.direction == LayoutDirection.Rtl)159
start = widths.total - start - widths.values[at]160
val end = start + widths.values[at]161
val view = snapshot.visibleSize.width162
val offset = when (current.alignment) {163
case Start -> start164
case End -> end - view165
case Center -> (start + end - view) / 2.0166
case Nearest ->167
if (start < snapshot.offset.x)168
start169
else if (end > snapshot.offset.x + view) end - view else snapshot.offset.x170
}171
return upperBound(lowerBound(offset, 0.0), snapshot.maximum.x)172
}