Scoop API

scoop:scoop.ui:0.1.0

src/scroll/physics.scoop

Selected production source · src/scroll/physics.scoop

1package scoop.ui
3import scoop.graphics.Point
4import scoop.math.abs
5import scoop.math.max
6import scoop.math.min
7import scoop.platform.system.OperatingSystem
8import scoop.platform.system.operatingSystem
10public enum ScrollBoundary {
11 Platform, Hard, Elastic
14/** Bounded presentation at an edge, independent of the logical scroll range. */
15public class ScrollPhysicsOptions public constructor(
16 public val boundary: ScrollBoundary = ScrollBoundary.Platform,
17 public val resistance: Double = 0.55,
18 public val maxOverscroll: Double = 64.0,
19 public val shortContent: Boolean = false,
20 public val stiffness: Double = 200.0,
21 public val dampingRatio: Double = 1.0,
22 public val inertia: ScrollInertiaOptions = ScrollInertiaOptions()
23) {
24 init {
25 requireUi(
26 resistance.isFinite() && resistance > 0.0 && resistance <= 1.0,
27 "invalid scroll resistance"
28 )
29 requireUi(maxOverscroll.isFinite() && maxOverscroll > 0.0, "invalid maximum overscroll")
30 requireUi(stiffness.isFinite() && stiffness > 0.0, "invalid scroll spring stiffness")
31 requireUi(dampingRatio.isFinite() && dampingRatio > 0.0, "invalid scroll spring damping")
32 }
33 internal val elastic: Boolean get() =
34 when (boundary) {
35 case Platform -> operatingSystem == OperatingSystem.Darwin
36 case Hard -> false
37 case Elastic -> true
38 }
41internal fun sameScrollPhysics(a: ScrollPhysicsOptions, b: ScrollPhysicsOptions): Boolean =
42 a.boundary ==
43 b.boundary &&
44 a.resistance ==
45 b.resistance &&
46 a.maxOverscroll ==
47 b.maxOverscroll &&
48 a.shortContent ==
49 b.shortContent &&
50 a.stiffness ==
51 b.stiffness &&
52 a.dampingRatio ==
53 b.dampingRatio &&
54 sameScrollInertia(a.inertia, b.inertia)
56internal fun scrollElasticDistance(pull: Double, options: ScrollPhysicsOptions): Double {
57 if (pull == 0.0)
58 return 0.0
59 val distance =
60 options.maxOverscroll / (1.0 + options.maxOverscroll / (abs(pull) * options.resistance))
61 return if (pull < 0.0)
62 -distance
63 else
64 distance
67/** Returns only the part that restores an existing stretch toward its resting edge. */
68internal fun scrollRestoringPart(pull: Double, delta: Double): Double =
69 if (pull > 0.0 && delta < 0.0)
70 max(-pull, delta)
71 else
72 if (pull < 0.0 && delta > 0.0)
73 min(-pull, delta)
74 else
75 0.0
77internal fun scrollAxisPoint(point: Point, axes: ScrollAxes): Point =
78 Point(
79 if (axes.horizontal)
80 point.x
81 else
82 0.0,
83 if (axes.vertical)
84 point.y
85 else
86 0.0
87 )