scoop:scoop.ui:0.1.0
src/scroll/physics.scoop
Selected production source · src/scroll/physics.scoop
1
package scoop.ui3
import scoop.graphics.Point4
import scoop.math.abs5
import scoop.math.max6
import scoop.math.min7
import scoop.platform.system.OperatingSystem8
import scoop.platform.system.operatingSystem10
public enum ScrollBoundary {11
Platform, Hard, Elastic12
}14
/** Bounded presentation at an edge, independent of the logical scroll range. */15
public 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.Darwin36
case Hard -> false37
case Elastic -> true38
}39
}41
internal 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)56
internal fun scrollElasticDistance(pull: Double, options: ScrollPhysicsOptions): Double {57
if (pull == 0.0)58
return 0.059
val distance =60
options.maxOverscroll / (1.0 + options.maxOverscroll / (abs(pull) * options.resistance))61
return if (pull < 0.0)62
-distance63
else64
distance65
}67
/** Returns only the part that restores an existing stretch toward its resting edge. */68
internal fun scrollRestoringPart(pull: Double, delta: Double): Double =69
if (pull > 0.0 && delta < 0.0)70
max(-pull, delta)71
else72
if (pull < 0.0 && delta > 0.0)73
min(-pull, delta)74
else75
0.077
internal fun scrollAxisPoint(point: Point, axes: ScrollAxes): Point =78
Point(79
if (axes.horizontal)80
point.x81
else82
0.0,83
if (axes.vertical)84
point.y85
else86
0.087
)