Scoop API

scoop:scoop.ui:0.1.0

src/interaction/drag.scoop

Selected production source · src/interaction/drag.scoop

1package scoop.ui
3import scoop.graphics.Point
4import scoop.math.abs
5import scoop.surface.*
6import scoop.window.*
8/** An axis candidate becomes active only after claiming the original press path. */
9public class Drag internal constructor(
10 private val area: InputArea,
11 initial: DragOptions,
12 accepts: (Point) -> Boolean,
13 handler: (DragEvent) -> Unit
14): Closeable {
15 private var options = initial
16 private var accept = accepts
17 private var receive = handler
18 private var closed = false
19 private var start: PointerState? = None
20 private var previous: PointerState? = None
21 private var input: DragInput? = None
22 private val contacts = ArrayList<ContactId>()
23 private var latest: DragSample? = None
24 private var claimed = false
25 private var moved = false
26 private val events = area.scope.addEventHandler { context, event -> observe(context, event) }
27 private val defaults = area.scope.addDefaultAction { context, event -> handle(context, event) }
28 private val finished = area.scope.addEventFinished { event -> finish(event) }
29 public val isDragging: Boolean get() {
30 area.isClosed;
31 return claimed
32 }
33 public val isClosed: Boolean get() {
34 area.isClosed;
35 return closed
36 }
38 public fun update(
39 value: DragOptions,
40 accepts: (Point) -> Boolean,
41 handler: (DragEvent) -> Unit
42 ) {
43 requireUi(!closed && !area.isClosed, "drag behavior is closed")
44 val changed =
45 value.axis !=
46 options.axis ||
47 value.button !=
48 options.button ||
49 value.threshold !=
50 options.threshold ||
51 value.contacts !=
52 options.contacts ||
53 value.mouse !=
54 options.mouse
55 options = value
56 accept = accepts
57 receive = handler
58 if (!value.enabled)
59 cancel(DragCancelReason.Disabled)
60 else
61 if (changed)
62 cancel(DragCancelReason.ConfigurationChanged)
63 }
64 private fun observe(context: RoutedEventContext, event: InputEvent) {
65 if (closed)
66 return
67 if (context.phase == RoutedPhase.Notification) {
68 when (event) {
69 case Pointer(InputPointerEvent.Cancelled(val reason, _)) -> if (isMouse())
70 cancel(DragCancelReason.Input(reason), false)
71 case Contact(InputContactEvent.Cancelled(val id, val reason, _)) -> {
72 forgetContact(contacts, id)
73 if (isContact(id))
74 cancel(DragCancelReason.Input(reason), false)
75 }
76 else -> {}
77 }
78 return
79 }
80 when (event) {
81 case Pointer(
82 InputPointerEvent.Button(val point, val button, ElementState.Pressed, _)
83 ) -> {
84 if (
85 options.enabled &&
86 options.mouse &&
87 (!options.contacts || contacts.size == 0L) &&
88 button ==
89 options.button
90 )
91 begin(context, point.state, DragInput.Mouse(button))
92 }
93 case Contact(InputContactEvent.Began(val value)) -> {
94 rememberContact(contacts, value.id)
95 if (contacts.size > 1L) {
96 when (input) {
97 case Some(DragInput.Contact(_, _)) -> cancel(
98 DragCancelReason.AdditionalContact
99 )
100 else -> {}
101 }
102 return
103 }
104 if (options.enabled && options.contacts)
105 begin(context, value.pointer, DragInput.Contact(value.id, value.kind))
106 }
107 else -> {}
108 }
109 }
110 private fun begin(context: RoutedEventContext, point: PointerState, source: DragInput) {
111 if (start != None)
112 return
113 val local = context.localPosition ?: return
114 if (accept(local) && !closed && !area.isClosed) {
115 input = Some(source)
116 start = Some(point)
117 previous = Some(point)
118 latest = sample(point)
119 }
120 }
121 private fun isMouse(): Boolean =
122 when (input) {
123 case Some(DragInput.Mouse(_)) -> true
124 else -> false
125 }
126 private fun isContact(id: ContactId): Boolean =
127 when (input) {
128 case Some(DragInput.Contact(val current, _)) -> current == id
129 else -> false
130 }
131 private fun handle(context: RoutedEventContext, event: InputEvent) {
132 if (closed || !options.enabled || start == None)
133 return
134 when (event) {
135 case Pointer(
136 InputPointerEvent.Button(val point, val button, ElementState.Pressed, _)
137 ) -> if (isMouse() && button == options.button && options.threshold == 0.0)
138 advance(context, point.state, true)
139 case Pointer(InputPointerEvent.Moved(val point)) -> if (isMouse()) {
140 moved = true
141 advance(context, point.state)
142 }
143 case Pointer(
144 InputPointerEvent.Button(val point, val button, ElementState.Released, _)
145 ) -> {
146 if (isMouse() && button == options.button)
147 end(context, point.state)
148 }
149 case Contact(InputContactEvent.Began(val value)) -> if (
150 isContact(value.id) && options.threshold == 0.0
151 )
152 advance(context, value.pointer, true)
153 case Contact(InputContactEvent.Moved(val value)) -> if (isContact(value.id)) {
154 moved = true
155 advance(context, value.pointer)
156 }
157 case Contact(InputContactEvent.Ended(val value)) -> {
158 if (isContact(value.id))
159 end(context, value.pointer)
160 }
161 else -> {}
162 }
163 }
164 private fun end(context: RoutedEventContext, point: PointerState) {
165 if (!claimed)
166 return
167 val value = sample(point)
168 context.consume()
169 if (value == None) {
170 cancel(DragCancelReason.GeometryUnavailable)
171 return
172 }
173 clear()
174 receive(DragEvent.Ended(value!!))
175 }
176 private fun advance(
177 context: RoutedEventContext,
178 point: PointerState,
179 pressed: Boolean = false
180 ) {
181 val value = sample(point)
182 if (value == None) {
183 cancel(DragCancelReason.GeometryUnavailable);
184 return
185 }
186 val current = value!!
187 latest = value
188 if (!claimed) {
189 if (!pressed && current.position == current.start)
190 return
191 val cross =
192 if (options.axis == DragAxis.Horizontal)
193 current.position.y - current.start.y
194 else
195 current.position.x - current.start.x
196 if (abs(current.displacement) < options.threshold && abs(cross) < options.threshold) {
197 previous = Some(point);
198 return
199 }
200 if (abs(current.displacement) < abs(cross)) {
201 clear();
202 return
203 }
204 val source = input ?: return
205 val acquired =
206 when (source) {
207 case Mouse(val button) -> area.scope.claimPointer(button)
208 case Contact(val id, _) -> area.scope.claimContacts([id])
209 }
210 if (!acquired || closed || area.isClosed || input != Some(source) || start == None) {
211 if (acquired && !area.isClosed)
212 release(source)
213 clear();
214 return
215 }
216 claimed = true
217 previous = Some(point)
218 context.consume()
219 receive(DragEvent.Started(current))
220 } else {
221 previous = Some(point)
222 context.consume()
223 receive(DragEvent.Moved(current))
224 }
225 }
226 private fun sample(point: PointerState): DragSample? {
227 val origin = start ?: return None
228 val source = input ?: return None
229 val before = previous ?: origin
230 val local = area.toLocal(windowPoint(point)) ?: return None
231 val beginning = area.toLocal(windowPoint(origin)) ?: return None
232 val prior = area.toLocal(windowPoint(before)) ?: return None
233 val duration = point.time.mark() - before.time.mark()
234 val seconds =
235 duration.floorSeconds.toDouble() +
236 duration.nanosecondsOfSecond.toDouble() /
237 1000000000.0
238 val displacement = axis(local) - axis(beginning)
239 val speed =
240 if (seconds > 0.0)
241 (axis(local) - axis(prior)) / seconds
242 else
243 0.0
244 if (!displacement.isFinite() || !speed.isFinite())
245 return None
246 return Some(
247 DragSample(beginning, local, displacement, speed, origin.time, point.time, source)
248 )
249 }
250 private fun axis(point: Point): Double =
251 if (options.axis == DragAxis.Horizontal)
252 point.x
253 else
254 point.y
255 private fun windowPoint(point: PointerState): Point =
256 Point(point.logicalPosition.x, point.logicalPosition.y)
257 private fun finish(event: InputEvent) {
258 if (closed)
259 return
260 when (event) {
261 case Pointer(InputPointerEvent.Moved(_)) -> {
262 if (isMouse() && start != None && !moved)
263 cancel(DragCancelReason.PreventedMovement)
264 moved = false
265 }
266 case Contact(InputContactEvent.Moved(val value)) -> {
267 if (isContact(value.id) && start != None && !moved)
268 cancel(DragCancelReason.PreventedMovement)
269 moved = false
270 }
271 case Pointer(InputPointerEvent.Button(_, val button, ElementState.Released, _)) -> {
272 if (isMouse() && button == options.button && start != None)
273 cancel(DragCancelReason.PreventedRelease)
274 }
275 case Contact(InputContactEvent.Ended(val value)) -> {
276 forgetContact(contacts, value.id)
277 if (isContact(value.id))
278 cancel(DragCancelReason.PreventedRelease)
279 }
280 case Contact(InputContactEvent.Began(val value)) -> {
281 when (input) {
282 case Some(DragInput.Contact(val current, _)) -> {
283 if (current != value.id)
284 cancel(DragCancelReason.AdditionalContact)
285 }
286 else -> {}
287 }
288 }
289 else -> {}
290 }
291 }
292 public fun cancel() {
293 area.isClosed
294 if (!closed)
295 cancel(DragCancelReason.Explicit)
296 }
297 private fun clear() {
298 start = None;
299 previous = None;
300 latest = None;
301 input = None;
302 claimed = false
303 moved = false
304 }
305 private fun release(source: DragInput) {
306 when (source) {
307 case Mouse(_) -> area.scope.cancelPointerClaim()
308 case Contact(val id, _) -> area.scope.cancelContactClaim(id)
309 }
310 }
311 private fun cancel(reason: DragCancelReason, releaseClaim: Boolean = true) {
312 val active = claimed
313 val snapshot = latest
314 val source = input
315 clear()
316 var failure =
317 cleanupUi(None) {
318 if (active && releaseClaim && !area.isClosed && source != None)
319 release(source!!)
320 }
321 failure =
322 cleanupUi(failure) {
323 if (active && snapshot != None)
324 receive(DragEvent.Cancelled(snapshot!!, reason))
325 }
326 throwUi(failure)
327 }
328 public override fun close() {
329 area.isClosed
330 if (closed)
331 return
332 closed = true
333 var failure = cleanupUi(None) { cancel(DragCancelReason.Closed) }
334 contacts.clear()
335 failure = cleanupUi(failure) { events.close() }
336 failure = cleanupUi(failure) { defaults.close() }
337 failure = cleanupUi(failure) { finished.close() }
338 throwUi(failure)
339 }
342public fun ComponentScope.drag(
343 area: InputArea,
344 options: DragOptions,
345 accepts: (Point) -> Boolean = { _ -> true },
346 handler: (DragEvent) -> Unit
347): Drag {
348 requireBehaviorArea(area)
349 return resource { Drag(area, options, accepts, handler) }