scoop:scoop.ui.component:0.1.0
src/drawer/geometry.scoop
Selected production source · src/drawer/geometry.scoop
1
package scoop.ui.component3
import scoop.graphics.*4
import scoop.math.*5
import scoop.ui.*6
import scoop.ui.style.*8
internal class DrawerGeometry(9
val viewport: Size,10
val bounds: Rect,11
val side: OverlaySide,12
val snaps: Array<Double>13
) {14
val vertical: Boolean get() = side == OverlaySide.Top || side == OverlaySide.Bottom15
val extent: Double get() =16
if (vertical)17
bounds.height18
else19
bounds.width20
val outward: Double get() =21
if (side == OverlaySide.Top || side == OverlaySide.Left)22
-1.023
else24
1.025
}27
internal fun sameDrawerGeometry(a: DrawerGeometry, b: DrawerGeometry): Boolean {28
if (29
a.viewport !=30
b.viewport ||31
a.bounds !=32
b.bounds ||33
a.side !=34
b.side ||35
a.snaps.size !=36
b.snaps.size37
)38
return false39
for (index in 0L ..< a.snaps.size)40
if (a.snaps[index] != b.snaps[index])41
return false42
return true43
}45
internal fun drawerPhysicalSide(side: SheetSide, direction: LayoutDirection): OverlaySide =46
when (side) {47
case Top -> OverlaySide.Top48
case Bottom -> OverlaySide.Bottom49
case Start -> if (direction == LayoutDirection.Rtl)50
OverlaySide.Right51
else52
OverlaySide.Left53
case End -> if (direction == LayoutDirection.Rtl)54
OverlaySide.Left55
else56
OverlaySide.Right57
}59
internal fun drawerSnaps(points: Array<DrawerSnapPoint>, extent: Double, density: Double): Array<60
Double61
> {62
if (points.size == 0L)63
return [extent]64
return Array<Double>(points.size) {65
index -> min(66
extent,67
when (points[index]) {68
case Fraction(val value) -> extent * value69
case Length(val value) -> value * density70
}71
)72
}73
}75
internal fun drawerIndex(value: Long, count: Long): Long = max(0L, min(value, count - 1L))77
internal fun drawerAdjacent(snaps: Array<Double>, index: Long, expand: Boolean): Long? {78
if (index < 0L || index >= snaps.size)79
return None80
val current = snaps[index]81
var best: Long? = None82
for (candidate in 0L ..< snaps.size) {83
val next = snaps[candidate]84
if ((expand && next > current) || (!expand && next < current)) {85
when (best) {86
case Some(val old) -> if (abs(next - current) < abs(snaps[old] - current))87
best = Some(candidate)88
case None -> best = Some(candidate)89
}90
}91
}92
return best93
}95
internal fun drawerReleased(96
shape: DrawerGeometry,97
position: Double,98
velocity: Double,99
projection: Double,100
close: Boolean101
): SnapTarget? {102
if (!close)103
return snapTarget(position, velocity, shape.snaps, projection)104
val anchors =105
Array<Double>(shape.snaps.size + 1L) {106
index -> if (index == 0L)107
0.0108
else109
shape.snaps[index - 1L]110
}111
val target = snapTarget(position, velocity, anchors, projection) ?: return None112
return Some(SnapTarget(target.index - 1L, target.position))113
}115
internal fun drawerCorners(side: SheetSide, radius: Double): Corners {116
val corner = Size(radius, radius)117
return when (side) {118
case Top -> Corners.Logical(bottomStart = corner, bottomEnd = corner)119
case Bottom -> Corners.Logical(topStart = corner, topEnd = corner)120
case Start -> Corners.Logical(topEnd = corner, bottomEnd = corner)121
case End -> Corners.Logical(topStart = corner, bottomStart = corner)122
}123
}