scoop:scoop.ui.component:0.1.0
src/tree/ink.scoop
Selected production source · src/tree/ink.scoop
1
package scoop.ui.component3
import scoop.graphics.*4
import scoop.surface.FrameSubmission5
import scoop.ui.*6
import scoop.window.accessibility.*8
internal class TreeInk<T>(private val props: Props<TreePlan<T>>, scope: ComponentScope) :9
LazyElement<TreeFrame<T>>(scope) {10
private val control = ScrollControl(scope) { props.value.controls }11
private val collection = createComponentSemantics(scope, SemanticRole.Tree, control.semantics)12
private val focus = scope.lazyFocus(control.viewport)13
private val measurements = VirtualMeasurements()14
private val dragArea = scope.createArea()15
private val typeahead = scope16
.typeahead(17
props.value.source,18
{ props.value.controller.focusedKey() },19
{ key -> props.value.controller.focus(key, None, false) },20
TypeaheadOptions(keyboard = false, matches = { text, query -> menuPrefix(text, query) })21
)22
init {23
dragArea.ignoreSelf = true24
}25
private var handle: TreeHandle? = None26
private var updated: TreePlan<T>? = None27
private var committed: TreeReadingPosition<T>? = None28
private var drawing: TreeFrame<T>? = None29
private var requested: TreeScrollRequest? = None30
val active: Boolean get() = scope.isMounted31
val pending: Boolean get() = active && (panelPresent(requestValue) || focus.isPending)32
val requestValue: TreeScrollRequest?33
get() = when (requested) {34
case Some(val value) ->35
if (value.revision == props.value.vertical.requestRevision) Some(value) else None36
case None -> None37
}38
val gutter: Size get() = control.gutter39
fun bodyViewport(size: Size, direction: LayoutDirection): Rect = control40
.contentViewport(size, direction)41
fun isSticky(key: ViewKey): Boolean {42
val current = drawing ?: return false43
for (row in current.pinning.rows)44
if (current.plan.projection.keyAt(row.index) == key) return true45
return false46
}47
fun dragDelta(point: Point): Double {48
val frame = drawing ?: return 0.049
val local = dragArea.toLocal(point) ?: return 0.050
val viewport = control.contentViewport(frame.size, scope.layoutDirection)51
val top = viewport.y + frame.pinning.height52
val bottom = viewport.y + viewport.height53
if (local.x <54
viewport.x ||55
local.x >=56
viewport.x +57
viewport.width ||58
local.y <59
top ||60
local.y >=61
bottom62
)63
return 0.064
val edge = upperBound(24.0 * frame.plan.appearance.density, (bottom - top) / 3.0)65
val step = 12.0 * frame.plan.appearance.density66
return if (local.y < top + edge) -step else if (local.y >= bottom - edge) step else 0.067
}68
fun type(text: String) {69
typeahead.push(text)70
}71
fun page(index: Long, forward: Boolean): Long {72
val current = drawing ?: return index73
val extents = current.extents74
val step = lowerBound(current.reading.snapshot.bodyHeight, itemEstimate(props.value.extent))75
val next = extents76
.indexAt(lowerBound(extents.prefix(index) + if (forward) step else -step, 0.0))77
return if (next < extents.count) next else extents.count - 1L78
}79
fun request(80
key: ViewKey,81
column: String?,82
alignment: ScrollAlignment,83
focusItem: Boolean84
): Boolean {85
if (!active) return false86
val plan = props.value87
val revision = plan.vertical.beginLayoutRequest()88
val horizontalRevision = if (column != None)89
plan.horizontal.beginLayoutRequest()90
else91
plan.horizontal.requestRevision92
if (!active || plan.vertical.requestRevision != revision) return false93
requested = Some(94
TreeScrollRequest(key, column, alignment, revision, horizontalRevision, focusItem)95
)96
scope.requestLayout()97
return true98
}99
public override fun onMount() {100
props.value.controller.ink = Some(this);101
scope.routeSemanticsChildren(collection)102
}103
public override fun update(): UiInvalidation {104
val plan = props.value105
if (plan.input.enabled) plan.input.state.synchronizeLoading(interactive = true)106
control.update()107
plan.controller.plan = Some(plan)108
plan.controller.ink = Some(this)109
focus.options = LazyFocusOptions(110
ItemNavigation.Roving(),111
NavigationLayout.Vertical,112
plan.input.focus.order113
)114
val changed = when (updated) {115
case Some(val old) -> old !== plan;116
case None -> true117
}118
if (changed) {119
typeahead120
.update(121
plan.source,122
{ plan.controller.focusedKey() },123
{ key -> plan.controller.focus(key, None, false) },124
Some(125
TypeaheadOptions(126
enabled = plan.input.enabled,127
keyboard = false,128
matches = { text, query -> menuPrefix(text, query) }129
)130
)131
)132
when (updated) {133
case Some(val old) ->134
if (old.vertical !== plan.vertical) {135
committed = None;136
requested = None137
};138
case None -> {}139
}140
updated = Some(plan)141
}142
val sameHandle = when ((handle, plan.input.handle)) {143
case(Some(val a), Some(val b)) -> a === b;144
case(None, None) -> true;145
else -> false146
}147
if (!sameHandle) {148
plan.input.handle?.bind(plan.controller);149
handle?.unbind(plan.controller);150
handle = plan.input.handle151
}152
when (collection) {153
case Some(val value) -> {154
val references = ArrayList<SemanticsReference>()155
when (handle) {156
case Some(val target) -> references.add(target.semanticTarget);157
case None -> {}158
}159
when (plan.input.semantics.reference) {160
case Some(val target) -> references.add(target);161
case None -> {}162
}163
value.bindReferences(references.toArray())164
value165
.update(166
plan.input.semantics.properties(enabled = plan.input.enabled),167
relations = plan.input.semantics.relations168
)169
value170
.updateCollection(171
plan.source,172
Some(plan.semantics),173
Some(control.viewport)174
)175
}177
case None -> {}178
}179
if (changed)180
when (plan.restoreFocus) {181
case Some(val key) -> request(key, None, ScrollAlignment.Nearest, true);182
case None -> {}183
}184
return UiInvalidation.Layout185
}186
public override fun measure(187
context: LazyMeasureContext,188
constraints: Constraints189
): LayoutResult<TreeFrame<T>> {190
val plan = props.value191
val request = when (focus.pendingItem) {192
case Some(val key) ->193
Some(194
TreeScrollRequest(195
key,196
None,197
ScrollAlignment.Nearest,198
plan.vertical.requestRevision,199
plan.horizontal.requestRevision,200
false201
)202
)203
case None -> requestValue204
}205
val frame = TreeLayout(206
context,207
constraints,208
plan,209
control,210
measurements,211
committed,212
drawing?.regular,213
request214
)215
.measure()216
return LayoutResult(LayoutMetrics(frame.size), frame)217
}218
public override fun scene(context: SceneContext, frame: TreeFrame<T>) {219
val viewport = frame.layout.snapshot.viewport220
context.place(dragArea, Rect(0.0, 0.0, context.size.width, context.size.height))221
when (collection) {222
case Some(val value) ->223
context224
.semantics(value, control.contentViewport(frame.size, context.layoutDirection));225
case None -> {}226
}227
control228
.scene(229
context,230
frame.layout,231
Point(0.0, frame.layout.snapshot.offset.y),232
{ content ->233
val shift = frame.layout.overscroll.y + frame.layout.snapshot.padding.top234
val left = -frame.layout.snapshot.padding.left - frame.layout.overscroll.x235
val body = Rect(236
0.0,237
frame.pinning.height,238
viewport.width,239
lowerBound(viewport.height - frame.pinning.height, 0.0)240
)241
content242
.withScrollReveal(control.viewport, body, ScrollAxes.Vertical) { normal ->243
normal244
.clipRect(Rect(left, body.y - shift, body.width, body.height))245
normal246
.translate(247
0.0,248
frame.regularOrigin - frame.layout.snapshot.offset.y249
)250
for (key in frame.regular.keys)251
normal.item(key)252
}253
for (row in frame.pinning.rows)254
content255
.withScrollReveal(256
control.viewport,257
Rect(0.0, row.top, viewport.width, row.bottom - row.top),258
ScrollAxes.Vertical259
) { sticky ->260
sticky261
.clipRect(262
Rect(263
left,264
row.top - shift,265
viewport.width,266
row.bottom - row.top267
)268
)269
sticky.item(frame.plan.projection.keyAt(row.index))270
}271
}272
)273
if (frame.pinning.height > 0.0) {274
val body = control.contentViewport(frame.size, context.layoutDirection)275
val line = lowerBound(frame.plan.appearance.density, 1.0)276
context277
.draw { paint ->278
paint279
.canvas280
.drawRect(281
Rect(body.x, body.y + frame.pinning.height - line, body.width, line),282
Paint(frame.plan.appearance.theme.tokens.palette.border)283
)284
}285
}286
context287
.onFrameComplete { result ->288
if (result.submission ==289
FrameSubmission.Accepted &&290
active &&291
props.value ===292
frame.plan &&293
frame.reading.revision ==294
props.value.vertical.requestRevision295
) {296
committed = Some(frame.reading)297
drawing = Some(frame)298
frame.plan.controller.presented = Some(frame.plan.projection)299
handle?.publish(frame.plan.controller, frame.reading.snapshot)300
val same = when ((requested, frame.request)) {301
case(Some(val current), Some(val accepted)) -> current === accepted;302
else -> false303
}304
if (same && frame.requestDone) {305
val completed = requested!!306
requested = None307
frame.plan.controller.finishFocus(completed)308
}309
if (frame.recheck) scope.requestLayout()310
}311
}312
}313
public override fun onUnmount() {314
handle?.unbind(props.value.controller)315
props.value.controller.ink = None316
props.value.controller.plan = None317
props.value.controller.presented = None318
requested = None319
committed = None320
drawing = None321
}322
}