scoop:scoop.ui.component:0.1.0
src/tree/drag.scoop
Selected production source · src/tree/drag.scoop
1
package scoop.ui.component3
import scoop.graphics.*4
import scoop.platform.time.MonotonicTimeSource5
import scoop.surface.*6
import scoop.time.Duration7
import scoop.ui.*8
import scoop.window.*10
/** The source and receiver borrow the row's real input route, including sticky placement. */11
internal class TreeDragControl<T>(12
private val scope: ComponentScope,13
private val area: InputArea,14
private val source: DragSource,15
private val read: () -> TreeRowInput<T>16
) : Closeable {17
private val hover = state<TreeDrop<T>?>(None)18
private val expand = scope.createDeadline { expandHovered() }19
private val scroll = scope.createDeadline { scrollHovered() }20
private var offerValue: DropOffer? = None21
private var hoverLeave: ((TreeDrop<T>) -> Unit)? = None22
private var height = 0.023
private var ready = false24
private var closed = false25
private var enabled = false26
private var sourceEnabled = false27
private val handlers = DropAreaHandlers(28
{ value, point -> offer(value, point) },29
{ value, point -> drop(value, point) },30
{ leave() }31
)32
private val target = scope.dropTarget(area, handlers, false)33
val position: TreeDropPosition? get() = hover.value?.position34
private val activity = scope35
.observeWindowActivity { value -> if (!value.visible || value.occluded) leave() }36
fun update() {37
val input = read()38
val options = input.plan.input.drag39
val next = input.plan.input.enabled && input.row.node.enabled && (options?.enabled ?: false)40
if (enabled != next) {41
enabled = next42
target.update(handlers, next)43
}44
val draggable = next && input.row.node.draggable45
if (sourceEnabled != draggable) {46
sourceEnabled = draggable47
source.update(DragSourceOptions(enabled = draggable)) { value -> prepare(value) }48
}49
if (!next)50
leave()51
else52
when (offerValue) {53
case Some(val value) -> {54
val point = area.toLocal(Point(value.position.x, value.position.y))55
if (!panelPresent(point))56
leave()57
else if (!panelPresent(resolve(value, point!!))) leave()58
}60
case None -> {}61
}62
}63
private fun prepare(start: DragSourceStart): DragSourceData? {64
if (closed || !enabled) return None65
val input = read()66
val options = input.plan.input.drag ?: return None67
val model = input.plan.input.state68
val node = model.data.node(input.row.key) ?: return None69
if (!node.enabled || !node.draggable) return None70
options.onStart(node, start)71
return Some(72
DragSourceData(73
options.content(node), DragEffects(false, true, false),74
Some(TreeDragPayload(model, node)), onCompleted = { result ->75
options.onEnd(node, result)76
}77
)78
)79
}80
private fun resolve(value: DropOffer, point: Point): TreeDrop<T>? {81
if (closed || !enabled || !ready || !value.effects.move || !scope.isMounted) return None82
val payload = when (value.localData) {83
case Some(val data) -> data as? TreeDragPayload<T>84
case None -> None85
} ?:86
return None87
val input = read()88
val model = input.plan.input.state89
if (payload.state !== model) return None90
val node = model.data.node(payload.node.key) ?: return None91
if (node !== payload.node) return None92
val destination = model.data.node(input.row.key) ?: return None93
if (model.projection.indexOfKey(destination.key) == None) return None94
val branch = when (destination.children) {95
case Leaf -> false;96
else -> true97
}98
val position = if (point.y < height * (if (branch) 0.25 else 0.5))99
TreeDropPosition.Before100
else if (point.y >= height * (if (branch) 0.75 else 0.5))101
TreeDropPosition.After102
else103
TreeDropPosition.Inside104
val result = model.dropTarget(node.key, destination.key, position) ?: return None105
val options = input.plan.input.drag ?: return None106
return if (options.allowDrop(result)) Some(result) else None107
}108
private fun offer(value: DropOffer, point: Point): DropDecision {109
val next = resolve(value, point)110
when (next) {111
case None -> {112
leave();113
return DropDecision.Reject114
}116
case Some(val move) -> {117
val options = read().plan.input.drag ?: return DropDecision.Reject118
val previous = hover.value119
val same = when (previous) {120
case Some(val old) ->121
old.source.key ==122
move.source.key &&123
old.target.key ==124
move.target.key &&125
old.position ==126
move.position127
case None -> false128
}129
if (!same) {130
leave()131
hover.value = Some(move)132
hoverLeave = Some(options.onLeave)133
scope.requestFrame()134
options.onEnter(move)135
if (move.position ==136
TreeDropPosition.Inside &&137
move.target.expandable &&138
!read().plan.input.state.isExpanded(move.target.key)139
)140
expand.schedule(MonotonicTimeSource.now() + options.expandDelay)141
}142
offerValue = Some(value)143
options.onOver(move)144
scheduleScroll()145
return DropDecision146
.Accept(147
if (value.formats.text)148
DragFormat.Text149
else if (value.formats.url) DragFormat.Url else DragFormat.Files,150
DragEffect.Move151
)152
}153
}154
}155
private fun drop(value: DropRequest, point: Point): Boolean {156
val move = resolve(value.offer, point) ?: return false157
val options = read().plan.input.drag ?: return false158
try {159
return value.effect == DragEffect.Move && options.onDrop(move)160
} finally {161
leave()162
}163
}164
private fun expandHovered() {165
val value = offerValue ?: return166
val point = area.toLocal(Point(value.position.x, value.position.y)) ?: return167
val move = resolve(value, point) ?: return168
if (move.position == TreeDropPosition.Inside)169
read().plan.input.state.setExpanded(move.target.key, true, TreeChangeOrigin.Pointer)170
}171
private fun scheduleScroll() {172
val value = offerValue ?: return173
val delta = read()174
.plan175
.controller176
.ink177
?.dragDelta(Point(value.position.x, value.position.y)) ?:178
0.0179
if (delta == 0.0)180
scroll.cancel()181
else if (!scroll.isScheduled)182
scroll.schedule(MonotonicTimeSource.now() + Duration.milliseconds(16L))183
}184
private fun scrollHovered() {185
val value = offerValue ?: return186
val point = Point(value.position.x, value.position.y)187
if (closed ||188
!scope.isMounted ||189
scope.nativeWindow.state !=190
WindowState.Open ||191
!scope.nativeWindow.isVisible ||192
scope.nativeWindow.isOccluded ||193
!area.hitTest(point)194
) {195
leave()196
return197
}198
val plan = read().plan199
val delta = plan.controller.ink?.dragDelta(point) ?: 0.0200
val before = plan.vertical.offset201
if (delta != 0.0) plan.vertical.scrollBy(Point(0.0, delta), ScrollOrigin.User)202
if (plan.vertical.offset != before) scheduleScroll()203
}204
private fun leave() {205
if (closed) return206
expand.cancel()207
scroll.cancel()208
offerValue = None209
val previous = hover.value210
val callback = hoverLeave211
hoverLeave = None212
when (previous) {213
case Some(_) -> {214
hover.value = None;215
if (scope.isMounted) scope.requestFrame()216
}218
case None -> {}219
}220
when (previous) {221
case Some(val value) -> callback?.invoke(value)222
case None -> {}223
}224
}225
public override fun close() {226
if (closed) return227
enabled = false228
ready = false229
try {230
leave()231
} finally {232
closed = true233
activity.close()234
source.close()235
target.close()236
expand.close()237
scroll.close()238
}239
}240
fun scene(context: SceneContext, bounds: Rect) {241
val current = position242
when (current) {243
case Some(val value) ->244
context245
.draw { paint ->246
val color = read().plan.appearance.theme.tokens.palette.ring247
val thickness = 2.0 * read().plan.appearance.density248
if (value == TreeDropPosition.Inside)249
paint.canvas.drawRect(bounds, Paint(color.withAlpha(0.16)))250
else251
paint252
.canvas253
.drawRect(254
Rect(255
0.0,256
if (value == TreeDropPosition.Before)257
0.0258
else259
lowerBound(bounds.height - thickness, 0.0),260
bounds.width,261
thickness262
),263
Paint(color)264
)265
}266
case None -> {}267
}268
context269
.onFrameComplete { result ->270
if (result.submission == FrameSubmission.Accepted && scope.isMounted && !closed) {271
height = bounds.height272
ready = true273
}274
}275
}276
}