Scoop API

scoop:scoop.ui:0.1.0

src/scroll/pan.scoop

Selected production source · src/scroll/pan.scoop

1package scoop.ui
3import scoop.graphics.Point
4import scoop.math.abs
5import scoop.math.max
6import scoop.surface.*
7import scoop.window.*
9private class ScrollPanContact(
10 val start: ContactState,
11 val state: ScrollState,
12 val binding: ULong
13) {
14 var axes: ScrollAxes? = None
15 var claimed = false
16 var active = false
17 var latest = start
18 val velocity = ScrollVelocity()
19 init {
20 velocity.add(position(start), start.pointer.time)
21 }
22 fun sample(value: ContactState) {
23 latest = value
24 velocity.add(position(value), value.pointer.time)
25 }
28/** A direct-contact scroll behavior borrowing its mount's area and viewport. */
29public class ScrollPan internal constructor(
30 private val area: InputArea,
31 private val viewport: ScrollViewport,
32 private var axes: ScrollAxes,
33 private var chain: ScrollChain,
34 private var options: TouchScrollOptions
35): Closeable {
36 private var closed = false
37 private val contacts = ArrayList<ContactId>()
38 private var contact: ScrollPanContact? = None
39 private var moved = false
40 private var state = viewport.state
41 private var applying = false
42 private var ownIntent: ULong? = None
43 private var intent = state.observeIntent { value -> positionRequested(value) }
44 private val events = area.scope.addEventHandler { context, event -> observe(context, event) }
45 private val defaults = area.scope.addDefaultAction { context, event -> handle(context, event) }
46 private val finished = area.scope.addEventFinished { event -> finish(event) }
48 init {
49 viewport.watchInput(area.scope)
50 }
52 public val isPanning: Boolean get() {
53 area.isClosed
54 return contact?.active ?: false
55 }
56 public val isClosed: Boolean get() {
57 area.isClosed
58 return closed
59 }
61 public fun update(axes: ScrollAxes, chain: ScrollChain, options: TouchScrollOptions) {
62 requireUi(!closed && !area.isClosed && viewport.active, "scroll pan behavior is closed")
63 val changed =
64 this.axes !=
65 axes ||
66 this.chain !=
67 chain ||
68 this.options.threshold !=
69 options.threshold ||
70 this.options.lockAxis !=
71 options.lockAxis ||
72 this.options.claimAtBoundary !=
73 options.claimAtBoundary
74 this.axes = axes
75 this.chain = chain
76 this.options = options
77 if (changed || !options.enabled || state !== viewport.state)
78 cancel()
79 if (state !== viewport.state) {
80 intent.close()
81 state = viewport.state
82 intent = state.observeIntent { value -> positionRequested(value) }
83 }
84 }
86 private fun positionRequested(value: ScrollIntent) {
87 when (value) {
88 case LayoutCorrection(_) -> return
89 else -> {}
90 }
91 if (applying && ownIntent == None) {
92 ownIntent = Some(value.requestRevision)
93 return
94 }
95 if (contact?.active ?: false)
96 cancel()
97 }
99 private fun observe(context: RoutedEventContext, event: InputEvent) {
100 if (closed)
101 return
102 when (event) {
103 case Contact(InputContactEvent.Cancelled(val id, _, _)) -> {
104 forgetContact(contacts, id)
105 if (contact?.start?.id == Some(id))
106 clear(false)
107 }
108 case Contact(InputContactEvent.Began(val value)) -> {
109 if (context.phase == RoutedPhase.Notification)
110 return
111 rememberContact(contacts, value.id)
112 if (contacts.size > 1L) {
113 cancel()
114 return
115 }
116 if (options.enabled && viewport.active && state.hasSnapshot) {
117 state.cancelAnimation()
118 contact = Some(ScrollPanContact(value, state, viewport.bindingRevision))
119 }
120 }
121 case Pointer(InputPointerEvent.Wheel(_, _, _, _)) -> clear(true, false)
122 case Pointer(InputPointerEvent.Button(_, _, ElementState.Pressed, _)) -> {
123 viewport.cancelAnimation()
124 cancel()
125 }
126 else -> {}
127 }
128 }
130 private fun current(value: ContactState): ScrollPanContact? {
131 val candidate = contact ?: return None
132 if (candidate.start.id != value.id)
133 return None
134 if (!options.enabled || !viewport.matches(candidate.state, candidate.binding)) {
135 cancel()
136 return None
137 }
138 return Some(candidate)
139 }
141 private fun handle(context: RoutedEventContext, event: InputEvent) {
142 if (closed)
143 return
144 when (event) {
145 case Contact(InputContactEvent.Moved(val value)) -> {
146 moved = true
147 val candidate = current(value) ?: return
148 candidate.sample(value)
149 if (!candidate.active) {
150 if (!selectAxes(candidate, value))
151 return
152 if (context.contactRemaining == None) {
153 if (!options.claimAtBoundary && !canMove(candidate, value)) {
154 cancel()
155 return
156 }
157 val acquired = area.scope.claimContacts([value.id])
158 if (!acquired || !same(candidate) || !viewport.active || area.isClosed) {
159 if (acquired && !area.isClosed)
160 area.scope.cancelContactClaim(value.id)
161 clear(false)
162 return
163 }
164 candidate.claimed = true
165 }
166 candidate.active = true
167 }
168 consume(context, candidate)
169 }
170 case Contact(InputContactEvent.Ended(val value)) -> {
171 val candidate = current(value) ?: return
172 candidate.sample(value)
173 if (candidate.active)
174 consume(context, candidate, true)
175 clear(false, false)
176 }
177 else -> {}
178 }
179 }
181 private fun selectAxes(candidate: ScrollPanContact, value: ContactState): Boolean {
182 val start = area.toLocal(position(candidate.start))
183 val end = area.toLocal(position(value))
184 if (start == None || end == None) {
185 cancel()
186 return false
187 }
188 val dx = abs(end!!.x - start!!.x)
189 val dy = abs(end!!.y - start!!.y)
190 if (!dx.isFinite() || !dy.isFinite()) {
191 cancel()
192 return false
193 }
194 if (options.lockAxis) {
195 if (max(dx, dy) < options.threshold || max(dx, dy) == 0.0)
196 return false
197 val horizontal = dx >= dy
198 if ((horizontal && !axes.horizontal) || (!horizontal && !axes.vertical)) {
199 cancel()
200 return false
201 }
202 candidate.axes =
203 Some(
204 if (horizontal)
205 ScrollAxes.Horizontal
206 else
207 ScrollAxes.Vertical
208 )
209 } else {
210 val distance =
211 max(
212 if (axes.horizontal)
213 dx
214 else
215 0.0,
216 if (axes.vertical)
217 dy
218 else
219 0.0
220 )
221 if (distance < options.threshold || distance == 0.0)
222 return false
223 candidate.axes = Some(axes)
224 }
225 return true
226 }
228 private fun canMove(candidate: ScrollPanContact, value: ContactState): Boolean {
229 val start = area.toLocal(position(candidate.start)) ?: return false
230 val end = area.toLocal(position(value)) ?: return false
231 val selected = candidate.axes!!
232 val offset = candidate.state.offset
233 val maximum = candidate.state.maximum
234 return (
235 selected.horizontal &&
236 ((end.x < start.x && offset.x < maximum.x) || (end.x > start.x && offset.x > 0.0))
237 ) ||
238 (
239 selected.vertical &&
240 (
241 (end.y < start.y && offset.y < maximum.y) ||
242 (end.y > start.y && offset.y > 0.0)
243 )
244 )
245 }
247 private fun consume(
248 context: RoutedEventContext,
249 candidate: ScrollPanContact,
250 released: Boolean = false
251 ) {
252 if (!candidate.claimed && context.contactRemaining == None) {
253 cancel()
254 return
255 }
256 val selected = candidate.axes!!
257 applying = true
258 ownIntent = None
259 try {
260 viewport.consumeContact(
261 context,
262 selected,
263 if (released)
264 Some(ScrollRelease(position(candidate.latest), candidate.velocity.value()))
265 else
266 None
267 )
268 } finally {
269 applying = false
270 ownIntent = None
271 }
272 if (chain == ScrollChain.Contain && same(candidate))
273 context.preventDefault()
274 }
276 private fun same(candidate: ScrollPanContact): Boolean =
277 when (contact) {
278 case Some(val current) -> current === candidate
279 case None -> false
280 }
282 private fun finish(event: InputEvent) {
283 if (closed)
284 return
285 when (event) {
286 case Contact(InputContactEvent.Moved(val value)) -> {
287 if (!moved && contact?.start?.id == Some(value.id))
288 cancel()
289 moved = false
290 }
291 case Contact(InputContactEvent.Ended(val value)) -> {
292 forgetContact(contacts, value.id)
293 if (contact?.start?.id == Some(value.id))
294 cancel()
295 }
296 case Contact(InputContactEvent.Began(val value)) -> {
297 when (contact) {
298 case Some(val current) -> {
299 if (current.start.id != value.id)
300 cancel()
301 }
302 case None -> {}
303 }
304 }
305 else -> {}
306 }
307 }
309 public fun cancel() {
310 area.isClosed
311 if (!closed)
312 clear(true)
313 }
314 private fun clear(release: Boolean, stopEffect: Boolean = true) {
315 val previous = contact
316 contact = None
317 if (stopEffect && viewport.active)
318 viewport.cancelAnimation()
319 when (previous) {
320 case Some(val value) -> {
321 if (release && value.claimed && !area.isClosed)
322 area.scope.cancelContactClaim(value.start.id)
323 }
324 case None -> {}
325 }
326 }
327 public override fun close() {
328 area.isClosed
329 if (closed)
330 return
331 closed = true
332 var failure = cleanupUi(None) { clear(true) }
333 contacts.clear()
334 failure = cleanupUi(failure) { intent.close() }
335 failure = cleanupUi(failure) { events.close() }
336 failure = cleanupUi(failure) { defaults.close() }
337 failure = cleanupUi(failure) { finished.close() }
338 throwUi(failure)
339 }
342private fun position(value: ContactState): Point =
343 Point(value.pointer.logicalPosition.x, value.pointer.logicalPosition.y)