Scoop API

scoop:scoop.ui.component:0.1.0

src/tree/layout.scoop

Selected production source · src/tree/layout.scoop

1package scoop.ui.component
3import scoop.collections.HashSet
4import scoop.collections.sortedBy
5import scoop.graphics.*
6import scoop.ui.*
7import scoop.ui.style.Orientation
9internal class TreeReadingPosition<T>(
10 val plan: TreePlan<T>,
11 val revision: ULong,
12 val index: Long?,
13 val snapshot: TreeReadingSnapshot,
14 val stickyHeight: Double
17internal class TreeRegularRows<T>(
18 private val projection: TreeProjection<T>,
19 private val indices: Array<Long>
20) {
21 val keys = Array<ViewKey>(indices.size) { index -> projection.keyAt(indices[index]) }
23 fun matches(projection: TreeProjection<T>, indices: Array<Long>): Boolean {
24 if (this.projection !== projection || this.indices.size != indices.size) return false
25 for (index in 0L..<indices.size)
26 if (this.indices[index] != indices[index]) return false
27 return true
28 }
31internal class TreeFrame<T>(
32 val plan: TreePlan<T>,
33 val size: Size,
34 val layout: ScrollLayout,
35 val pinning: TreePinning,
36 val regular: TreeRegularRows<T>,
37 val regularOrigin: Double,
38 val reading: TreeReadingPosition<T>,
39 val request: TreeScrollRequest?,
40 val requestDone: Boolean,
41 val recheck: Boolean,
42 val extents: ItemExtentIndex
45/** A measure pass resolves estimates, ancestor slots and positioning against the same row index. */
46internal class TreeLayout<T>(
47 private val context: LazyMeasureContext,
48 private val constraints: Constraints,
49 private val plan: TreePlan<T>,
50 private val control: ScrollControl,
51 private val measurements: VirtualMeasurements,
52 private val previous: TreeReadingPosition<T>?,
53 private val previousRegular: TreeRegularRows<T>?,
54 private val request: TreeScrollRequest?
55) {
56 private val input = plan.input
57 private val projection = plan.projection
58 private val viewport = context.input<TreeViewportInput>()
59 private val extents = measurements
60 .prepare(
61 plan.source,
62 context.itemCount,
63 plan.extent,
64 Orientation.Vertical,
65 viewport.contentWidth,
66 context.layoutDirection
67 )
68 private val state = plan.vertical
69 private val revision = state.requestRevision
70 private val measured = HashSet<Long>()
71 private val requestActive = request?.revision == Some(revision)
72 private val destination = if (requestActive) projection.indexOfKey(request!!.key) else None
73 private val mayAnchor = input.anchor !=
74 VirtualListAnchor.None &&
75 when (previous) {
76 case Some(val value) ->
77 value.plan.vertical ===
78 state &&
79 value.revision ==
80 revision &&
81 !state.hasPositionAnimation
82 case None -> false
83 }
84 private val anchor = if (mayAnchor) previous!!.snapshot.anchor else None
85 private val anchorIndex = findAnchor()
86 private val outer: Size
87 get() = constraints
88 .constrain(
89 Size(
90 constraints.maxWidth ?: constraints.minWidth,
91 constraints.maxHeight ?: extents.total
92 )
93 )
94 private val body: Rect get() = control.contentViewport(outer, context.layoutDirection)
95 private val size: Size get() = Size(body.width, body.height)
96 private val hasViewport: Boolean get() = body.width > 0.0 && body.height > 0.0
98 private fun findAnchor(): Long? {
99 val key = anchor?.key ?: return None
100 when (projection.indexOfKey(key)) {
101 case Some(val at) -> return Some(at);
102 case None -> {}
103 }
104 val old = previous ?: return None
105 var parent = when (old.index) {
106 case Some(val at) -> old.plan.projection.rowAt(at).parent;
107 case None -> None
108 }
109 while (parent != None) {
110 val row = old.plan.projection.rowAt(parent!!)
111 when (projection.indexOfKey(row.key)) {
112 case Some(val at) -> return Some(at);
113 case None -> {}
114 }
115 parent = row.parent
116 }
117 return if (projection.count == 0L)
118 None
119 else
120 Some(
121 if ((old.index ?: 0L) < projection.count) old.index ?: 0L else projection.count - 1L
122 )
123 }
124 fun measure(): TreeFrame<T> {
125 if (hasViewport) {
126 val initial = when (destination) {
127 case Some(_) -> destination;
128 case None -> anchorIndex
129 }
130 when (initial) {
131 case Some(val at) -> ensure(at);
132 case None -> {}
133 }
134 }
135 var changed = true
136 while (changed) {
137 changed = false
138 for (at in context.pinnedItems)
139 if (ensure(at)) changed = true
140 val layout = snapshot()
141 val pinning = pins(layout.snapshot.offset.y)
142 for (row in pinning.rows)
143 if (ensure(row.index)) changed = true
144 val range = range(layout.snapshot.offset.y)
145 for (at in range.start..<range.end)
146 if (ensure(at)) changed = true
147 }
148 val layout = snapshot()
149 val offset = layout.snapshot.offset.y
150 val pinning = pins(offset)
151 // Rebase near the viewport so the shared scroll transform stays small.
152 val regularOrigin = extents.prefix(extents.indexAt(offset))
153 val pinned = HashSet<Long>()
154 for (row in pinning.rows)
155 pinned.add(row.index)
156 val range = range(offset)
157 val regular = ArrayList<Long>()
158 val active = HashSet<Long>()
159 for (at in context.pinnedItems)
160 active.add(at)
161 var recheck = false
162 for (at in measured) {
163 var y = extents.prefix(at) - regularOrigin
164 if (pinned.contains(at)) {
165 for (row in pinning.rows)
166 if (row.index == at)
167 y = row.y - layout.overscroll.y - layout.snapshot.padding.top
168 } else
169 regular.add(at)
170 context
171 .placeItem(at, Point(-viewport.horizontalOffset - layout.snapshot.padding.left, y))
172 if (!pinned.contains(at) &&
173 !active.contains(at) &&
174 (at < range.start || at >= range.end)
175 )
176 recheck = true
177 }
178 val start = extents.indexAt(offset + pinning.height)
179 val end = extents.endBefore(offset + size.height)
180 val nextAnchor = if (hasViewport && start < extents.count)
181 Some(
182 VirtualReadingAnchor(
183 projection.keyAt(start),
184 lowerBound(offset + pinning.height - extents.prefix(start), 0.0)
185 )
186 )
187 else
188 None
189 val reading = TreeReadingSnapshot(
190 VirtualRange(start, if (end < start) start else end), nextAnchor,
191 Array<ViewKey>(pinning.rows.size) { index ->
192 projection.keyAt(pinning.rows[index].index)
193 },
194 lowerBound(size.height - pinning.height, 0.0), offset, offset >=
195 layout.snapshot.maximum.y
196 )
197 val indices = regular.sortedBy<Long, Long> { value -> value }.toArray()
198 val rows = when (previousRegular) {
199 case Some(val old) ->
200 if (old.matches(projection, indices)) old else TreeRegularRows(projection, indices)
201 case None -> TreeRegularRows(projection, indices)
202 }
203 return TreeFrame(
204 plan,
205 outer,
206 layout,
207 pinning,
208 rows,
209 regularOrigin,
210 TreeReadingPosition(
211 plan,
212 revision,
213 if (start < extents.count) Some(start) else None,
214 reading,
215 pinning.height
216 ),
217 request,
218 requestActive && (destination == None || hasViewport),
219 recheck,
220 extents
221 )
222 }
223 private fun ensure(at: Long): Boolean {
224 if (measured.contains(at)) return false
225 val extent = when (plan.extent) {
226 case Fixed(val value) -> Some(value);
227 case Variable(_) -> None
228 }
229 val child = context
230 .measureItem(
231 at,
232 Constraints(
233 viewport.contentWidth,
234 Some(viewport.contentWidth),
235 extent ?: 0.0,
236 extent
237 )
238 )
239 measured.add(at)
240 measurements.record(at, child.size.height)
241 return true
242 }
243 private fun pins(offset: Double): TreePinning = treePinning(
244 projection,
245 extents,
246 offset,
247 size.height,
248 input.sticky
249 )
250 private fun range(offset: Double): VirtualRange {
251 if (!input.virtualize) return VirtualRange(0L, extents.count)
252 if (!hasViewport) return VirtualRange(0L, 0L)
253 val extra = input.overscan * plan.appearance.density
254 return VirtualRange(
255 extents.indexAt(lowerBound(offset - extra, 0.0)),
256 extents.endBefore(offset + size.height + extra)
257 )
258 }
259 private fun snapshot(): ScrollLayout {
260 val content = Size(size.width, extents.total)
261 val base = control.layout(size, content)
262 var target: Double? = None
263 when (destination) {
264 case Some(val at) ->
265 target = Some(aligned(at, base.snapshot.offset.y, request!!.alignment))
266 case None ->
267 if (mayAnchor &&
268 input.anchor ==
269 VirtualListAnchor.FollowEnd &&
270 previous!!.snapshot.atEnd
271 )
272 target = Some(base.snapshot.maximum.y)
273 else
274 when (anchorIndex) {
275 case Some(val at) ->
276 target = Some(
277 treeAnchoredOffset(
278 projection,
279 extents,
280 at,
281 anchor!!.offset,
282 size.height,
283 input.sticky,
284 previous!!.snapshot.offset,
285 previous!!.stickyHeight
286 )
287 )
288 case None -> if (mayAnchor && extents.count == 0L) target = Some(0.0)
289 }
290 }
291 return when (target) {
292 case Some(val value) ->
293 control.layout(size, content, Some(ScrollAdjustment(revision, Point(0.0, value))))
294 case None -> base
295 }
296 }
297 private fun aligned(at: Long, current: Double, alignment: ScrollAlignment): Double {
298 val start = extents.prefix(at)
299 val end = start + extents.extent(at)
300 val pinning = pins(current)
301 for (row in pinning.rows)
302 if (row.index == at && alignment == ScrollAlignment.Nearest) return current
303 val top = treeAnchoredOffset(
304 projection,
305 extents,
306 at,
307 0.0,
308 size.height,
309 input.sticky,
310 current,
311 pinning.height
312 )
313 return when (alignment) {
314 case Start -> top
315 case End -> end - size.height
316 case Center -> (top + end - size.height) / 2.0
317 case Nearest ->
318 if (start < current + pinning.height)
319 top
320 else if (end > current + size.height) end - size.height else current
321 }
322 }