scoop:scoop.ui.component:0.1.0
src/interaction/snap.scoop
Selected production source · src/interaction/snap.scoop
1
package scoop.ui.component3
import scoop.math.*5
public struct SplitSizes(val first: Double, val second: Double)7
/** Applies a single constrained displacement while retaining the pair's total extent. */8
public fun resizePair(9
first: Double,10
second: Double,11
delta: Double,12
firstMin: Double = 0.0,13
firstMax: Double? = None,14
secondMin: Double = 0.0,15
secondMax: Double? = None16
): SplitSizes {17
nonnegative(first, "invalid first pane extent")18
nonnegative(second, "invalid second pane extent")19
nonnegative(firstMin, "invalid first pane minimum")20
nonnegative(secondMin, "invalid second pane minimum")21
val total = first + second22
nonnegative(total, "pane total overflow")23
requireComponent(delta.isFinite(), "resize displacement must be finite")24
val limitFirst = firstMax ?: total25
val limitSecond = secondMax ?: total26
nonnegative(limitFirst, "invalid first pane maximum")27
nonnegative(limitSecond, "invalid second pane maximum")28
requireComponent(29
first >= firstMin && first <= limitFirst && second >= secondMin && second <= limitSecond,30
"pane extents violate their constraints"31
)32
val low = max(firstMin - first, second - limitSecond)33
val high = min(limitFirst - first, second - secondMin)34
val value = first + min(max(delta, low), high)35
return SplitSizes(value, total - value)36
}38
public struct SnapTarget(val index: Long, val position: Double)40
/** Resolves actual layout anchors; the caller drives its existing Animatable. */41
public fun snapTarget(42
position: Double,43
velocity: Double,44
anchors: Array<Double>,45
projectionSeconds: Double = 0.1546
): SnapTarget? {47
requireComponent(48
position.isFinite() && velocity.isFinite(),49
"snap position and velocity must be finite"50
)51
nonnegative(projectionSeconds, "invalid snap projection duration")52
for (anchor in anchors)53
requireComponent(anchor.isFinite(), "snap anchor must be finite")54
if (anchors.size == 0L)55
return None56
val projected = position + velocity * projectionSeconds57
var selected = 0L58
for (index in 1L ..< anchors.size) {59
val previous = anchors[selected]60
val current = anchors[index]61
val distance = abs(current / 2.0 - projected / 2.0)62
val best = abs(previous / 2.0 - projected / 2.0)63
if (64
(65
!projected.isFinite() &&66
(67
if (velocity > 0.0)68
current > previous69
else70
current < previous71
)72
) ||73
(74
projected.isFinite() &&75
(76
distance <77
best ||78
(79
distance ==80
best &&81
(82
if (velocity > 0.0)83
current > previous84
else85
current < previous86
)87
)88
)89
)90
)91
selected = index92
}93
return Some(SnapTarget(selected, anchors[selected]))94
}