Scoop API

scoop:scoop.surface:0.1.0

src/shared/input/contact_consumption.scoop

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

1package scoop.surface
3import scoop.graphics.Point
4import scoop.graphics.native.CanvasGeometry
5import scoop.window.ContactState
7/** Sharing begins with the claimant, separately for each movement delivery. */
8internal class ContactConsumption(private val route: InputContactRoute) {
9 private var shared: ScrollConsumption? = None
11 val remaining: Point? get() =
12 when (shared) {
13 case Some(val value) -> Some(scrollVector(value.remaining))
14 case None -> None
15 }
17 fun consume(
18 receiver: InputScope,
19 geometry: CanvasGeometry?,
20 apply: (Point) -> Point,
21 notifyEmpty: Boolean = false
22 ) {
23 val value =
24 when (shared) {
25 case Some(val current) -> current
26 case None -> {
27 if (!sameScope(route.claimant, Some(receiver)))
28 throw IllegalStateException(
29 Some("contact movement must be opened by its owner")
30 )
31 val start =
32 position(
33 if (route.movementStarted)
34 route.previous
35 else
36 route.origin
37 )
38 val end = position(route.latest)
39 val delta = Point(end.x - start.x, end.y - start.y)
40 checkedScrollPoint(delta)
41 val current =
42 ScrollConsumption(InputScrollDelta.Pixels(delta.x, delta.y), start, None)
43 shared = Some(current)
44 route.movementStarted = true
45 current
46 }
47 }
48 val anchor =
49 when (geometry) {
50 case Some(val current) -> Some(
51 current.toLocal(value.point) ?:
52 throw IllegalArgumentException(
53 Some("contact movement transformation overflow")
54 )
55 )
56 case None -> None
57 }
58 value.consume(1.0, geometry, anchor, apply, notifyEmpty)
59 }
62internal fun contactConsumption(event: InputEvent, route: InputContactRoute?): ContactConsumption? {
63 val current = route ?: return None
64 return when (event) {
65 case Contact(InputContactEvent.Moved(_)) -> Some(ContactConsumption(current))
66 case Contact(InputContactEvent.Ended(_)) -> Some(ContactConsumption(current))
67 else -> None
68 }
71private fun position(value: ContactState): Point =
72 Point(value.pointer.logicalPosition.x, value.pointer.logicalPosition.y)