Scoop API

scoop:scoop.ui:0.1.0

src/interaction/swipe.scoop

Selected production source · src/interaction/swipe.scoop

1package scoop.ui
3import scoop.graphics.Point
4import scoop.surface.*
5import scoop.window.*
7/** Combines a finite drag release with an independently enabled native swipe. */
8public class SwipeGesture internal constructor(
9 private val area: InputArea,
10 initial: SwipeGestureOptions,
11 accepts: (Point) -> Boolean,
12 onSwipe: (SwipeGestureSample) -> Unit
13): Closeable {
14 private var options = initial
15 private var accept = accepts
16 private var receive = onSwipe
17 private var closed = false
18 private var drag: Drag? = None
19 private val defaults = area.scope.addDefaultAction { context, event -> native(context, event) }
21 init {
22 updateDrag()
23 }
25 public val isClosed: Boolean get() {
26 area.isClosed
27 return closed
28 }
30 public fun update(
31 value: SwipeGestureOptions,
32 accepts: (Point) -> Boolean,
33 onSwipe: (SwipeGestureSample) -> Unit
34 ) {
35 requireUi(!closed && !area.isClosed, "swipe gesture is closed")
36 val changed =
37 value.axis !=
38 options.axis ||
39 value.enabled !=
40 options.enabled ||
41 value.native !=
42 options.native ||
43 value.contacts !=
44 options.contacts ||
45 value.mouse !=
46 options.mouse ||
47 value.distance !=
48 options.distance ||
49 value.maxDuration !=
50 options.maxDuration
51 options = value
52 accept = accepts
53 receive = onSwipe
54 if (changed)
55 drag?.cancel()
56 if (!closed && !area.isClosed)
57 updateDrag()
58 }
60 private fun updateDrag() {
61 if (!options.mouse && !options.contacts) {
62 val previous = drag
63 drag = None
64 previous?.close()
65 return
66 }
67 val value =
68 DragOptions(
69 options.axis,
70 options.distance,
71 options.enabled,
72 contacts = options.contacts,
73 mouse = options.mouse
74 )
75 when (drag) {
76 case Some(val current) -> current.update(
77 value,
78 { point -> accepts(point) },
79 { event -> dragged(event) }
80 )
81 case None -> drag =
82 Some(Drag(area, value, { point -> accepts(point) }, { event -> dragged(event) }))
83 }
84 }
86 private fun current(value: SwipeGestureOptions): Boolean =
87 !closed &&
88 !area.isClosed &&
89 options.enabled &&
90 options ===
91 value &&
92 area.scope.isInputAllowed
94 private fun accepts(point: Point): Boolean {
95 val before = options
96 return current(before) && accept(point) && current(before)
97 }
99 private fun dragged(event: DragEvent) {
100 if (closed || area.isClosed || !options.enabled)
101 return
102 when (event) {
103 case Started(val sample) -> if (!swipeInTime(sample, options.maxDuration))
104 drag?.cancel()
105 case Moved(val sample) -> if (!swipeInTime(sample, options.maxDuration))
106 drag?.cancel()
107 case Ended(val sample) -> when (swipeRelease(sample, options)) {
108 case Some(val value) -> receive(value)
109 case None -> {}
110 }
111 case Cancelled(_, _) -> return
112 }
113 }
115 private fun native(context: RoutedEventContext, event: InputEvent) {
116 val before = options
117 if (!current(before) || !before.native)
118 return
119 val value =
120 when (event) {
121 case Gesture(
122 InputGestureEvent.Native(GestureEvent.Swipe(val state, val x, val y))
123 ) ->
124 nativeSwipeSample(area, state, x, y, before.axis) ?: return
125 else -> return
126 }
127 if (!accepts(value.position) || !current(before))
128 return
129 context.consume()
130 drag?.cancel()
131 if (current(before))
132 receive(value)
133 }
135 public fun cancel() {
136 area.isClosed
137 drag?.cancel()
138 }
140 public override fun close() {
141 area.isClosed
142 if (closed)
143 return
144 closed = true
145 val previous = drag
146 drag = None
147 var failure = cleanupUi(None) { previous?.close() }
148 failure = cleanupUi(failure) { defaults.close() }
149 throwUi(failure)
150 }
153public fun ComponentScope.swipeGesture(
154 area: InputArea,
155 options: SwipeGestureOptions = SwipeGestureOptions(),
156 accepts: (Point) -> Boolean = { _ -> true },
157 onSwipe: (SwipeGestureSample) -> Unit
158): SwipeGesture {
159 requireBehaviorArea(area)
160 return resource { SwipeGesture(area, options, accepts, onSwipe) }