Scoop API

scoop:scoop.ui:0.1.0

src/scroll/edge.scoop

Selected production source · src/scroll/edge.scoop

1package scoop.ui
3import scoop.graphics.Point
4import scoop.math.max
5import scoop.math.min
6import scoop.surface.*
8/** One viewport's held stretch or release spring; it never changes ScrollSnapshot. */
9internal class ScrollEdge(
10 private val viewport: ScrollViewport,
11 private val animation: Animatable<Point>
12): Closeable {
13 private var pull = Point(0.0, 0.0)
14 private var releasing = false
15 private var momentumEdge = Point(0.0, 0.0)
16 val moving: Boolean get() = releasing
17 val held: Boolean get() = pull != Point(0.0, 0.0)
18 val inputActive: Boolean get() = held || releasing || momentumEdge != Point(0.0, 0.0)
20 /** A native momentum tail cannot hold or restart an already reached edge. */
21 fun consumeMomentum(amount: Point): Point {
22 if (held)
23 release()
24 val current =
25 if (releasing)
26 animation.value
27 else
28 Point(0.0, 0.0)
29 val direction =
30 Point(
31 if (momentumEdge.x == 0.0)
32 current.x
33 else
34 momentumEdge.x,
35 if (momentumEdge.y == 0.0)
36 current.y
37 else
38 momentumEdge.y
39 )
40 val reverseX = scrollOpposesEdge(amount.x, direction.x)
41 val reverseY = scrollOpposesEdge(amount.y, direction.y)
42 val next =
43 Point(
44 if (reverseX)
45 0.0
46 else
47 direction.x,
48 if (reverseY)
49 0.0
50 else
51 direction.y
52 )
53 if (releasing && (reverseX || reverseY)) {
54 val speed = animation.velocity
55 val start =
56 Point(
57 if (reverseX)
58 0.0
59 else
60 current.x,
61 if (reverseY)
62 0.0
63 else
64 current.y
65 )
66 if (start == Point(0.0, 0.0))
67 stop()
68 else
69 beginRelease(
70 start,
71 Point(
72 if (reverseX)
73 0.0
74 else
75 speed.x,
76 if (reverseY)
77 0.0
78 else
79 speed.y
80 )
81 )
82 }
83 momentumEdge = next
84 return Point(
85 if (direction.x != 0.0 && !reverseX)
86 amount.x
87 else
88 0.0,
89 if (direction.y != 0.0 && !reverseY)
90 amount.y
91 else
92 0.0
93 )
94 }
96 fun absorbMomentum(amount: Point, axes: ScrollAxes): Point {
97 val used = absorb(amount, axes)
98 if (releasing && used != Point(0.0, 0.0))
99 kick(used, Point(0.0, 0.0), used.x != 0.0, used.y != 0.0)
100 else
101 release()
102 momentumEdge =
103 Point(
104 if (used.x != 0.0)
105 used.x
106 else
107 momentumEdge.x,
108 if (used.y != 0.0)
109 used.y
110 else
111 momentumEdge.y
112 )
113 return used
114 }
116 fun endMomentum() {
117 momentumEdge = Point(0.0, 0.0)
118 }
120 fun restoringFraction(amount: Point): Double =
121 max(
122 if (amount.x == 0.0)
123 0.0
124 else
125 scrollRestoringPart(pull.x, amount.x) / amount.x,
126 if (amount.y == 0.0)
127 0.0
128 else
129 scrollRestoringPart(pull.y, amount.y) / amount.y
130 )
132 fun sample(snapshot: ScrollSnapshot): Point {
133 val options = viewport.physics
134 if (viewport.motionReduced || !options.elastic)
135 return Point(0.0, 0.0)
136 val amount =
137 if (releasing)
138 animation.value
139 else
140 Point(
141 scrollElasticDistance(pull.x, options),
142 scrollElasticDistance(pull.y, options)
143 )
144 return Point(
145 edgeValue(amount.x, snapshot.offset.x, snapshot.maximum.x, options),
146 edgeValue(amount.y, snapshot.offset.y, snapshot.maximum.y, options)
147 )
148 }
150 fun restore(amount: Point): Point {
151 val used =
152 Point(scrollRestoringPart(pull.x, amount.x), scrollRestoringPart(pull.y, amount.y))
153 if (used != Point(0.0, 0.0)) {
154 pull = Point(pull.x + used.x, pull.y + used.y)
155 viewport.requestFrame()
156 }
157 return used
158 }
160 fun absorb(amount: Point, axes: ScrollAxes): Point {
161 val options = viewport.physics
162 if (viewport.motionReduced || !options.elastic)
163 return Point(0.0, 0.0)
164 val snapshot = viewport.state.current
165 val used =
166 Point(
167 if (axes.horizontal && (snapshot.maximum.x > 0.0 || options.shortContent))
168 amount.x
169 else
170 0.0,
171 if (axes.vertical && (snapshot.maximum.y > 0.0 || options.shortContent))
172 amount.y
173 else
174 0.0
175 )
176 if (used != Point(0.0, 0.0)) {
177 val next = Point(pull.x + used.x, pull.y + used.y)
178 checkScrollPoint(next)
179 pull = next
180 viewport.requestFrame()
181 }
182 return used
183 }
185 fun release() {
186 if (releasing || pull == Point(0.0, 0.0))
187 return
188 if (viewport.motionReduced || !viewport.physics.elastic) {
189 stop()
190 return
191 }
192 val state = viewport.state
193 val accepted = state.current
194 val start = sample(state.plan(accepted.viewport, accepted.content, accepted.padding))
195 pull = Point(0.0, 0.0)
196 if (start == Point(0.0, 0.0)) {
197 viewport.requestFrame()
198 return
199 }
200 beginRelease(start, Point(0.0, 0.0))
201 }
203 fun kick(amount: Point, velocity: Point, horizontal: Boolean, vertical: Boolean) {
204 val options = viewport.physics
205 if (viewport.motionReduced || !options.elastic)
206 return
207 val state = viewport.state
208 val current = state.current
209 val snapshot = state.plan(current.viewport, current.content, current.padding)
210 val x = horizontal && (snapshot.maximum.x > 0.0 || options.shortContent)
211 val y = vertical && (snapshot.maximum.y > 0.0 || options.shortContent)
212 if (!x && !y)
213 return
214 val before = sample(snapshot)
215 val speed =
216 if (releasing)
217 animation.velocity
218 else
219 AnimationVector()
220 val start =
221 Point(
222 if (x)
223 edgeValue(
224 before.x + scrollElasticDistance(amount.x, options),
225 snapshot.offset.x,
226 snapshot.maximum.x,
227 options
228 )
229 else
230 before.x,
231 if (y)
232 edgeValue(
233 before.y + scrollElasticDistance(amount.y, options),
234 snapshot.offset.y,
235 snapshot.maximum.y,
236 options
237 )
238 else
239 before.y
240 )
241 beginRelease(
242 start,
243 Point(
244 if (x)
245 velocity.x * options.resistance
246 else
247 speed.x,
248 if (y)
249 velocity.y * options.resistance
250 else
251 speed.y
252 )
253 )
254 }
256 private fun beginRelease(start: Point, velocity: Point) {
257 pull = Point(0.0, 0.0)
258 animation.snapTo(start)
259 val options = viewport.physics
260 animation.animateTo(
261 Point(0.0, 0.0),
262 AnimationSpec.Spring(options.stiffness, options.dampingRatio, 0.1, 1.0),
263 initialVelocity = Some(AnimationVector(velocity.x, velocity.y))
264 )
265 releasing = true
266 viewport.state.changed()
267 viewport.requestFrame()
268 }
270 fun complete() {
271 if (releasing && animation.state == AnimationState.Finished) {
272 releasing = false
273 viewport.state.changed()
274 }
275 }
277 fun interruptRelease() {
278 if (releasing)
279 stop()
280 }
282 fun stop() {
283 val moving = releasing
284 val changed = moving || pull != Point(0.0, 0.0)
285 releasing = false
286 pull = Point(0.0, 0.0)
287 momentumEdge = Point(0.0, 0.0)
288 if (moving) {
289 animation.snapTo(Point(0.0, 0.0))
290 viewport.state.changed()
291 }
292 if (changed)
293 viewport.requestFrame()
294 }
296 public override fun close() {
297 stop()
298 animation.close()
299 }
302private fun scrollOpposesEdge(amount: Double, edge: Double): Boolean =
303 (amount < 0.0 && edge > 0.0) || (amount > 0.0 && edge < 0.0)
305private fun edgeValue(
306 amount: Double,
307 offset: Double,
308 maximum: Double,
309 options: ScrollPhysicsOptions
310): Double {
311 if (maximum == 0.0 && !options.shortContent)
312 return 0.0
313 val limit = options.maxOverscroll
314 if (amount > 0.0 && offset == 0.0)
315 return min(limit, amount)
316 if (amount < 0.0 && offset == maximum)
317 return max(-limit, amount)
318 return 0.0