Scoop API

scoop:scoop.ui:0.1.0

src/scroll/inertia_options.scoop

Selected production source · src/scroll/inertia_options.scoop

1package scoop.ui
3import scoop.graphics.Point
4import scoop.math.*
6/** Direct-contact coast parameters in window-logical units and seconds. */
7public class ScrollInertiaOptions public constructor(
8 public val enabled: Boolean = true,
9 public val deceleration: Double = 2400.0,
10 public val minVelocity: Double = 50.0,
11 public val maxVelocity: Double = 8000.0
12) {
13 init {
14 requireUi(deceleration.isFinite() && deceleration > 0.0, "invalid scroll deceleration")
15 requireUi(maxVelocity.isFinite() && maxVelocity > 0.0, "invalid maximum scroll velocity")
16 requireUi(
17 minVelocity.isFinite() && minVelocity >= 0.0 && minVelocity <= maxVelocity,
18 "invalid minimum scroll velocity"
19 )
20 val seconds = maxVelocity / deceleration
21 requireUi(
22 seconds.isFinite() &&
23 seconds *
24 1000000000.0 <
25 Long.MAX_VALUE.toDouble() &&
26 (maxVelocity * (seconds / 2.0)).isFinite(),
27 "scroll inertia exceeds representable time or distance"
28 )
29 }
32internal fun sameScrollInertia(a: ScrollInertiaOptions, b: ScrollInertiaOptions): Boolean =
33 a.enabled ==
34 b.enabled &&
35 a.deceleration ==
36 b.deceleration &&
37 a.minVelocity ==
38 b.minVelocity &&
39 a.maxVelocity ==
40 b.maxVelocity
42internal fun scrollInertiaVelocity(value: Point, options: ScrollInertiaOptions): Point {
43 val scale = max(abs(value.x), abs(value.y))
44 if (!options.enabled || scale == 0.0 || !scale.isFinite())
45 return Point(0.0, 0.0)
46 val length = hypot(value.x / scale, value.y / scale)
47 if (scale < options.minVelocity / length)
48 return Point(0.0, 0.0)
49 if (scale > options.maxVelocity / length) {
50 val factor = (options.maxVelocity / scale) / length
51 return Point(value.x * factor, value.y * factor)
52 }
53 return value