scoop:scoop.surface:0.1.0
src/shared/input/scroll_consumption.scoop
Selected production source · src/shared/input/scroll_consumption.scoop
1
package scoop.surface3
import scoop.graphics.Point4
import scoop.graphics.native.CanvasGeometry5
import scoop.math.*7
/** Transforms one routed displacement while retaining its unconsumed source units. */8
internal class ScrollConsumption(9
var remaining: InputScrollDelta,10
val point: Point,11
val route: InputScrollRoute?12
) {13
private var applying = false15
fun consume(16
lineStep: Double,17
geometry: CanvasGeometry?,18
localPosition: Point?,19
apply: (Point) -> Point,20
notifyEmpty: Boolean = false21
) {22
requireInput(lineStep.isFinite() && lineStep > 0.0, "invalid scroll line step")23
if (applying)24
throw IllegalStateException(Some("scroll consumption cannot recurse"))25
val source = scrollVector(remaining)26
if (source == Point(0.0, 0.0) && !notifyEmpty)27
return28
val factor =29
when (remaining) {30
case Lines(_, _) -> lineStep31
case Pixels(_, _) -> 1.032
}33
val endpoint = Point(point.x + source.x * factor, point.y + source.y * factor)34
checkedScrollPoint(endpoint)35
val start = localPosition ?: point36
val end =37
when (geometry) {38
case Some(val value) -> value.toLocal(endpoint) ?:39
throw IllegalArgumentException(Some("scroll transformation overflow"))40
case None -> endpoint41
}42
val requested = Point(end.x - start.x, end.y - start.y)43
checkedScrollPoint(requested)44
applying = true45
val returned =46
try {47
apply(requested)48
} finally {49
applying = false50
}51
val consumed =52
Point(53
checkedScrollPart(returned.x, requested.x),54
checkedScrollPart(returned.y, requested.y)55
)56
if (consumed == Point(0.0, 0.0))57
return58
if (consumed == requested) {59
remaining = scrollWithVector(remaining, Point(0.0, 0.0))60
return61
}62
val window =63
when (geometry) {64
case Some(val value) -> {65
val origin = value.toWindow(start)66
val moved = value.toWindow(Point(start.x + consumed.x, start.y + consumed.y))67
Point(moved.x - origin.x, moved.y - origin.y)68
}69
case None -> consumed70
}71
val rest = Point(source.x - window.x / factor, source.y - window.y / factor)72
checkedScrollPoint(rest)73
remaining = scrollWithVector(remaining, rest)74
}75
}77
internal fun scrollConsumption(event: InputEvent, route: InputScrollRoute?): ScrollConsumption? =78
when (event) {79
case Pointer(InputPointerEvent.Wheel(val pointer, val delta, _, _)) -> Some(80
ScrollConsumption(81
delta,82
Point(pointer.state.logicalPosition.x, pointer.state.logicalPosition.y),83
route84
)85
)86
else -> None87
}89
internal fun scrollVector(delta: InputScrollDelta): Point =90
when (delta) {91
case Lines(val x, val y) -> Point(x, y)92
case Pixels(val x, val y) -> Point(x, y)93
}95
private fun scrollWithVector(delta: InputScrollDelta, value: Point): InputScrollDelta =96
when (delta) {97
case Lines(_, _) -> InputScrollDelta.Lines(value.x, value.y)98
case Pixels(_, _) -> InputScrollDelta.Pixels(value.x, value.y)99
}101
internal fun checkedScrollPoint(value: Point) {102
requireInput(value.x.isFinite() && value.y.isFinite(), "non-finite scroll displacement")103
}105
private fun checkedScrollPart(value: Double, requested: Double): Double {106
requireInput(value.isFinite(), "non-finite consumed scroll displacement")107
val tolerance = max(1.0, abs(requested)) * 1.0e-9108
requireInput(109
value >= min(0.0, requested) - tolerance && value <= max(0.0, requested) + tolerance,110
"consumed scroll must have the requested direction and not exceed it"111
)112
return max(min(0.0, requested), min(max(0.0, requested), value))113
}