Scoop API

scoop:scoop.ui.component:0.1.0

src/tree/root_layout.scoop

Selected production source · src/tree/root_layout.scoop

1package scoop.ui.component
3import scoop.graphics.*
4import scoop.ui.*
6internal class TreeViewportInput(val contentWidth: Double, val horizontalOffset: Double)
8private class TreeBodyLayoutInput(val viewport: TreeViewportInput) {
9 val layout = LayoutInput(viewport)
12internal class TreeRootFrame(val layout: ScrollLayout)
14/** Horizontal scrolling moves header and row content while leaving the body viewport in place. */
15internal 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? = None
19 public override fun update(): UiInvalidation {
20 control.update();
21 return UiInvalidation.Layout
22 }
23 public override fun measure(context: MeasureContext, constraints: Constraints): LayoutResult<
24 TreeRootFrame
25 > {
26 val plan = props.value
27 val input = plan.input
28 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 = constraints
37 .constrain(Size(input.width ?: constraints.maxWidth ?: constraints.minWidth, 0.0))
38 .width
39 val maximumHeight: Double? = when (input.height) {
40 case Some(_) -> input.height;
41 case None -> constraints.maxHeight
42 }
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 = context
55 .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 -> None
63 }
64 val estimatedHeight = bodyLimit ?:
65 (plan.projection.count.toDouble() * itemEstimate(plan.extent))
66 val preliminary = control
67 .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.requestValue
73 case None -> None
74 }
75 val offset = treeHorizontalOffset(plan, widths, preliminary.snapshot, request)
76 val body = context
77 .measureChild(
78 1L,
79 Constraints(
80 width,
81 Some(width),
82 if (panelPresent(bodyLimit)) bodyLimit!! else 0.0,
83 bodyLimit
84 ),
85 bodyLayoutInput(widths.total, offset)
86 )
87 val size = constraints
88 .constrain(Size(width, header.size.height + body.size.height + control.gutter.height))
89 val viewport = control.contentViewport(size, context.layoutDirection)
90 val layout = control
91 .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 context
99 .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: Double
108 ): LayoutInput<TreeViewportInput> {
109 when (bodyInput) {
110 case Some(val cached) ->
111 if (cached.viewport.contentWidth ==
112 contentWidth &&
113 cached.viewport.horizontalOffset ==
114 horizontalOffset
115 )
116 return cached.layout
117 case None -> {}
118 }
119 val input = TreeBodyLayoutInput(TreeViewportInput(contentWidth, horizontalOffset))
120 bodyInput = Some(input)
121 return input.layout
122 }
123 public override fun scene(context: SceneContext, frame: TreeRootFrame) {
124 control
125 .scene(
126 context,
127 frame.layout,
128 children = { content ->
129 content
130 .withScrollReveal(
131 control.viewport,
132 Rect(
133 0.0,
134 0.0,
135 frame.layout.snapshot.viewport.width,
136 frame.layout.snapshot.viewport.height
137 ),
138 ScrollAxes.Horizontal
139 ) { inner -> inner.children() }
140 }
141 )
142 }
145private 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.x
152 if (current.horizontalRevision != plan.horizontal.requestRevision) return snapshot.offset.x
153 val column = current.column ?: return snapshot.offset.x
154 val at = plan.columnIndex(column) ?: return snapshot.offset.x
155 var start = 0.0
156 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.width
162 val offset = when (current.alignment) {
163 case Start -> start
164 case End -> end - view
165 case Center -> (start + end - view) / 2.0
166 case Nearest ->
167 if (start < snapshot.offset.x)
168 start
169 else if (end > snapshot.offset.x + view) end - view else snapshot.offset.x
170 }
171 return upperBound(lowerBound(offset, 0.0), snapshot.maximum.x)