scoop:scoop.ui.component:0.1.0
src/tree/layout.scoop
Selected production source · src/tree/layout.scoop
1
package scoop.ui.component3
import scoop.collections.HashSet4
import scoop.collections.sortedBy5
import scoop.graphics.*6
import scoop.ui.*7
import scoop.ui.style.Orientation9
internal class TreeReadingPosition<T>(10
val plan: TreePlan<T>,11
val revision: ULong,12
val index: Long?,13
val snapshot: TreeReadingSnapshot,14
val stickyHeight: Double15
)17
internal 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 false25
for (index in 0L..<indices.size)26
if (this.indices[index] != indices[index]) return false27
return true28
}29
}31
internal 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: ItemExtentIndex43
)45
/** A measure pass resolves estimates, ancestor slots and positioning against the same row index. */46
internal 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.input57
private val projection = plan.projection58
private val viewport = context.input<TreeViewportInput>()59
private val extents = measurements60
.prepare(61
plan.source,62
context.itemCount,63
plan.extent,64
Orientation.Vertical,65
viewport.contentWidth,66
context.layoutDirection67
)68
private val state = plan.vertical69
private val revision = state.requestRevision70
private val measured = HashSet<Long>()71
private val requestActive = request?.revision == Some(revision)72
private val destination = if (requestActive) projection.indexOfKey(request!!.key) else None73
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.hasPositionAnimation82
case None -> false83
}84
private val anchor = if (mayAnchor) previous!!.snapshot.anchor else None85
private val anchorIndex = findAnchor()86
private val outer: Size87
get() = constraints88
.constrain(89
Size(90
constraints.maxWidth ?: constraints.minWidth,91
constraints.maxHeight ?: extents.total92
)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.098
private fun findAnchor(): Long? {99
val key = anchor?.key ?: return None100
when (projection.indexOfKey(key)) {101
case Some(val at) -> return Some(at);102
case None -> {}103
}104
val old = previous ?: return None105
var parent = when (old.index) {106
case Some(val at) -> old.plan.projection.rowAt(at).parent;107
case None -> None108
}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.parent116
}117
return if (projection.count == 0L)118
None119
else120
Some(121
if ((old.index ?: 0L) < projection.count) old.index ?: 0L else projection.count - 1L122
)123
}124
fun measure(): TreeFrame<T> {125
if (hasViewport) {126
val initial = when (destination) {127
case Some(_) -> destination;128
case None -> anchorIndex129
}130
when (initial) {131
case Some(val at) -> ensure(at);132
case None -> {}133
}134
}135
var changed = true136
while (changed) {137
changed = false138
for (at in context.pinnedItems)139
if (ensure(at)) changed = true140
val layout = snapshot()141
val pinning = pins(layout.snapshot.offset.y)142
for (row in pinning.rows)143
if (ensure(row.index)) changed = true144
val range = range(layout.snapshot.offset.y)145
for (at in range.start..<range.end)146
if (ensure(at)) changed = true147
}148
val layout = snapshot()149
val offset = layout.snapshot.offset.y150
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 = false162
for (at in measured) {163
var y = extents.prefix(at) - regularOrigin164
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.top168
} else169
regular.add(at)170
context171
.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 = true177
}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
else188
None189
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.y196
)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.height216
),217
request,218
requestActive && (destination == None || hasViewport),219
recheck,220
extents221
)222
}223
private fun ensure(at: Long): Boolean {224
if (measured.contains(at)) return false225
val extent = when (plan.extent) {226
case Fixed(val value) -> Some(value);227
case Variable(_) -> None228
}229
val child = context230
.measureItem(231
at,232
Constraints(233
viewport.contentWidth,234
Some(viewport.contentWidth),235
extent ?: 0.0,236
extent237
)238
)239
measured.add(at)240
measurements.record(at, child.size.height)241
return true242
}243
private fun pins(offset: Double): TreePinning = treePinning(244
projection,245
extents,246
offset,247
size.height,248
input.sticky249
)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.density254
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? = None263
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.atEnd271
)272
target = Some(base.snapshot.maximum.y)273
else274
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!!.stickyHeight286
)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 -> base295
}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 current303
val top = treeAnchoredOffset(304
projection,305
extents,306
at,307
0.0,308
size.height,309
input.sticky,310
current,311
pinning.height312
)313
return when (alignment) {314
case Start -> top315
case End -> end - size.height316
case Center -> (top + end - size.height) / 2.0317
case Nearest ->318
if (start < current + pinning.height)319
top320
else if (end > current + size.height) end - size.height else current321
}322
}323
}