Scoop API

scoop:scoop.ui:0.1.0

src/scroll/input_watch.scoop

Selected production source · src/scroll/input_watch.scoop

1package scoop.ui
3import scoop.surface.*
4import scoop.window.*
6/** Also serves custom wheel consumers that do not install ScrollPan. */
7internal class ScrollViewportInput(
8 private val viewport: ScrollViewport,
9 val scope: InputScope
10): Closeable {
11 private val activity =
12 scope.observeActivity {
13 if (!scope.isInputAllowed)
14 viewport.cancelAnimation()
15 }
16 private val events =
17 scope.addEventHandler {
18 _,
19 event -> when (event) {
20 case Pointer(
21 InputPointerEvent.Button(_, _, ElementState.Pressed, _)
22 ) -> viewport.cancelAnimation()
23 case Pointer(InputPointerEvent.Cancelled(_, _)) -> viewport.cancelAnimation()
24 case Pointer(InputPointerEvent.ScrollCancelled(_, _)) -> viewport.cancelAnimation()
25 case Contact(InputContactEvent.Began(_)) -> viewport.cancelAnimation()
26 case Pointer(InputPointerEvent.Wheel(_, _, val phase, val momentum)) -> {
27 viewport.cancelInertia()
28 if (
29 phase ==
30 ScrollPhase.MayBegin ||
31 phase ==
32 ScrollPhase.Began ||
33 phase ==
34 ScrollPhase.Cancelled ||
35 momentum ==
36 ScrollPhase.Cancelled
37 )
38 viewport.cancelAnimation()
39 }
40 else -> {}
41 }
42 }
43 private val finished =
44 scope.addEventFinished {
45 event -> when (event) {
46 case Pointer(InputPointerEvent.Wheel(_, _, val phase, val momentum)) -> {
47 if (phase == ScrollPhase.Cancelled || momentum == ScrollPhase.Cancelled)
48 viewport.cancelAnimation()
49 else {
50 if (phase == ScrollPhase.Ended || momentum == ScrollPhase.Ended)
51 viewport.edge.release()
52 if (momentum == ScrollPhase.Ended)
53 viewport.edge.endMomentum()
54 }
55 }
56 else -> {}
57 }
58 }
59 public override fun close() {
60 activity.close()
61 events.close()
62 finished.close()
63 }