scoop:scoop.ui.component:0.1.0
src/layout/arrange.scoop
Selected production source · src/layout/arrange.scoop
1
package scoop.ui.component3
import scoop.graphics.*4
import scoop.ui.Constraints5
import scoop.ui.LayoutDirection7
public enum AxisAlignment {8
Start, Center, End9
}11
public struct Alignment(12
val horizontal: AxisAlignment = AxisAlignment.Center,13
val vertical: AxisAlignment = AxisAlignment.Center14
)16
internal class LayoutPlan(val size: Size, val origins: Array<Point>)18
internal fun alignmentOffset(alignment: AxisAlignment, available: Double, content: Double): Double =19
when (alignment) {20
case Start -> 0.021
case Center -> (available - content) / 2.022
case End -> available - content23
}25
internal fun alignedOrigin(26
bounds: Rect,27
child: Size,28
alignment: Alignment,29
direction: LayoutDirection = LayoutDirection.Ltr30
): Point =31
Point(32
bounds.x +33
logicalX(34
alignmentOffset(alignment.horizontal, bounds.width, child.width),35
child.width,36
bounds.width,37
direction38
),39
bounds.y + alignmentOffset(alignment.vertical, bounds.height, child.height)40
)42
internal fun arrangeStack(43
children: Array<Size>,44
parent: Constraints,45
alignment: Alignment,46
direction: LayoutDirection = LayoutDirection.Ltr47
): LayoutPlan {48
var width = 0.049
var height = 0.050
for (child in children) {51
width = lowerBound(width, child.width);52
height = lowerBound(height, child.height)53
}54
val size = parent.constrain(Size(width, height))55
val bounds = Rect(0.0, 0.0, size.width, size.height)56
return LayoutPlan(57
size,58
Array<Point>(children.size) {59
index -> alignedOrigin(bounds, children[index], alignment, direction)60
}61
)62
}64
internal fun frameChildConstraints(65
parent: Constraints,66
width: Double?,67
height: Double?,68
inset: Size,69
maxWidth: Double? = None,70
maxHeight: Double? = None71
): Constraints = Constraints(72
maxWidth = insetMaximum(73
frameMaximum(width, parent.minWidth, parent.maxWidth, maxWidth),74
inset.width75
),76
maxHeight = insetMaximum(77
frameMaximum(height, parent.minHeight, parent.maxHeight, maxHeight),78
inset.height79
)80
)82
internal fun frameExtent(value: Double, cap: Double?): Double = upperBound(value, cap ?: value)84
private fun frameMaximum(85
requested: Double?,86
minimum: Double,87
maximum: Double?,88
cap: Double?89
): Double? {90
val available = when (requested) {91
case Some(val value) -> Some(frameExtent(lowerBound(value, minimum), maximum))92
case None -> maximum93
}94
return when (available) {95
case Some(val value) -> Some(frameExtent(value, cap))96
case None -> cap97
}98
}