Scoop API

scoop:scoop.surface:0.1.0

src/shared/input/scroll_consumption.scoop

Selected production source · src/shared/input/scroll_consumption.scoop

1package scoop.surface
3import scoop.graphics.Point
4import scoop.graphics.native.CanvasGeometry
5import scoop.math.*
7/** Transforms one routed displacement while retaining its unconsumed source units. */
8internal class ScrollConsumption(
9 var remaining: InputScrollDelta,
10 val point: Point,
11 val route: InputScrollRoute?
12) {
13 private var applying = false
15 fun consume(
16 lineStep: Double,
17 geometry: CanvasGeometry?,
18 localPosition: Point?,
19 apply: (Point) -> Point,
20 notifyEmpty: Boolean = false
21 ) {
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 return
28 val factor =
29 when (remaining) {
30 case Lines(_, _) -> lineStep
31 case Pixels(_, _) -> 1.0
32 }
33 val endpoint = Point(point.x + source.x * factor, point.y + source.y * factor)
34 checkedScrollPoint(endpoint)
35 val start = localPosition ?: point
36 val end =
37 when (geometry) {
38 case Some(val value) -> value.toLocal(endpoint) ?:
39 throw IllegalArgumentException(Some("scroll transformation overflow"))
40 case None -> endpoint
41 }
42 val requested = Point(end.x - start.x, end.y - start.y)
43 checkedScrollPoint(requested)
44 applying = true
45 val returned =
46 try {
47 apply(requested)
48 } finally {
49 applying = false
50 }
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 return
58 if (consumed == requested) {
59 remaining = scrollWithVector(remaining, Point(0.0, 0.0))
60 return
61 }
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 -> consumed
70 }
71 val rest = Point(source.x - window.x / factor, source.y - window.y / factor)
72 checkedScrollPoint(rest)
73 remaining = scrollWithVector(remaining, rest)
74 }
77internal 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 route
84 )
85 )
86 else -> None
87 }
89internal 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 }
95private 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 }
101internal fun checkedScrollPoint(value: Point) {
102 requireInput(value.x.isFinite() && value.y.isFinite(), "non-finite scroll displacement")
105private 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-9
108 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))