scoop:scoop.ui:0.1.0
src/text/handles/scroll.scoop
Selected production source · src/text/handles/scroll.scoop
1
package scoop.ui.text3
import scoop.collections.map4
import scoop.graphics.*5
import scoop.math.*6
import scoop.surface.InputArea7
import scoop.ui.*9
private class TextHandleScrollBinding(boundary: ScrollReveal) {10
val viewport = boundary.viewport11
val state = boundary.state12
val binding = boundary.layout.binding13
}15
/** Uses the current focus/reveal path, including enclosing read-only viewports. */16
internal class TextHandleScroll(17
private val focus: FocusTarget,18
private val area: InputArea,19
private val interrupt: () -> Unit20
): Closeable {21
private var closed = false22
private val bindings =23
focus.node.root.focus.textSelectionScrollPath(area).map {24
value -> TextHandleScrollBinding(value)25
}26
private val subscriptions = ArrayList<Closeable>()27
private var issuing: ScrollState? = None28
private var revision = 0UL30
init {31
for (binding in bindings)32
subscriptions.add(33
binding.state.observeIntent {34
event -> when (event) {35
case LayoutCorrection(_) -> {}36
case User(val value) -> {37
val own =38
when (issuing) {39
case Some(val state) -> state ===40
binding.state &&41
revision ==42
value43
case None -> false44
}45
if (!closed && !own)46
interrupt()47
}48
case Programmatic(_) -> if (!closed)49
interrupt()50
}51
}52
)53
}55
fun valid(): Boolean {56
if (closed)57
return false58
val route = focus.node.root.focus.textSelectionScrollPath(area)59
if (route.size != bindings.size)60
return false61
for (index in 0L ..< route.size) {62
val actual = route[index]63
val expected = bindings[index]64
if (65
actual.viewport !==66
expected.viewport ||67
actual.state !==68
expected.state ||69
actual.layout.binding !=70
expected.binding ||71
!actual.active72
)73
return false74
}75
return true76
}78
fun canMove(pointer: Point): Boolean {79
if (!valid())80
return false81
for (boundary in focus.node.root.focus.textSelectionScrollPath(area))82
if (textHandleScrollDelta(boundary, pointer) != Point(0.0, 0.0))83
return true84
return false85
}87
fun move(pointer: Point): Boolean {88
if (!valid())89
return false90
val route = focus.node.root.focus.textSelectionScrollPath(area)91
var index = route.size92
while (index > 0L) {93
index -= 1L94
val boundary = route[index]95
val delta = textHandleScrollDelta(boundary, pointer)96
if (delta == Point(0.0, 0.0))97
continue98
issuing = Some(boundary.state)99
revision = boundary.state.requestRevision + 1UL100
val consumed =101
try {102
boundary.state.scrollBy(delta, ScrollOrigin.User)103
} finally {104
issuing = None105
}106
return consumed != Point(0.0, 0.0)107
}108
return false109
}111
public override fun close() {112
if (closed)113
return114
closed = true115
var failure: Throwable? = None116
for (subscription in subscriptions)117
failure = cleanupUi(failure) { subscription.close() }118
subscriptions.clear()119
throwUi(failure)120
}121
}123
private fun textHandleScrollDelta(boundary: ScrollReveal, pointer: Point): Point {124
if (!boundary.current)125
return Point(0.0, 0.0)126
val visible = boundary.visibleBounds ?: return Point(0.0, 0.0)127
val bounds = localReveal(boundary.geometry, visible) ?: return Point(0.0, 0.0)128
val point = boundary.geometry.toLocal(pointer) ?: return Point(0.0, 0.0)129
val snapshot = boundary.layout.snapshot130
return Point(131
if (boundary.horizontal)132
textHandleEdge(point.x, bounds.x, bounds.width, snapshot.offset.x, snapshot.maximum.x)133
else134
0.0,135
if (boundary.vertical)136
textHandleEdge(point.y, bounds.y, bounds.height, snapshot.offset.y, snapshot.maximum.y)137
else138
0.0139
)140
}142
private fun textHandleEdge(143
value: Double,144
start: Double,145
size: Double,146
offset: Double,147
limit: Double148
): Double {149
val inset = min(24.0, size / 3.0)150
val distance =151
if (value < start + inset && offset > 0.0)152
value - start - inset153
else154
if (value > start + size - inset && offset < limit)155
value - start - size + inset156
else157
0.0158
if (distance == 0.0)159
return 0.0160
val step = min(40.0, max(2.0, abs(distance) * 0.25))161
return if (distance < 0.0)162
-step163
else164
step165
}