Scoop API

scoop:scoop.ui.component:0.1.0

src/layout/arrange.scoop

Selected production source · src/layout/arrange.scoop

1package scoop.ui.component
3import scoop.graphics.*
4import scoop.ui.Constraints
5import scoop.ui.LayoutDirection
7public enum AxisAlignment {
8 Start, Center, End
9}
11public struct Alignment(
12 val horizontal: AxisAlignment = AxisAlignment.Center,
13 val vertical: AxisAlignment = AxisAlignment.Center
16internal class LayoutPlan(val size: Size, val origins: Array<Point>)
18internal fun alignmentOffset(alignment: AxisAlignment, available: Double, content: Double): Double =
19 when (alignment) {
20 case Start -> 0.0
21 case Center -> (available - content) / 2.0
22 case End -> available - content
23 }
25internal fun alignedOrigin(
26 bounds: Rect,
27 child: Size,
28 alignment: Alignment,
29 direction: LayoutDirection = LayoutDirection.Ltr
30): Point =
31 Point(
32 bounds.x +
33 logicalX(
34 alignmentOffset(alignment.horizontal, bounds.width, child.width),
35 child.width,
36 bounds.width,
37 direction
38 ),
39 bounds.y + alignmentOffset(alignment.vertical, bounds.height, child.height)
40 )
42internal fun arrangeStack(
43 children: Array<Size>,
44 parent: Constraints,
45 alignment: Alignment,
46 direction: LayoutDirection = LayoutDirection.Ltr
47): LayoutPlan {
48 var width = 0.0
49 var height = 0.0
50 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 )
64internal fun frameChildConstraints(
65 parent: Constraints,
66 width: Double?,
67 height: Double?,
68 inset: Size,
69 maxWidth: Double? = None,
70 maxHeight: Double? = None
71): Constraints = Constraints(
72 maxWidth = insetMaximum(
73 frameMaximum(width, parent.minWidth, parent.maxWidth, maxWidth),
74 inset.width
75 ),
76 maxHeight = insetMaximum(
77 frameMaximum(height, parent.minHeight, parent.maxHeight, maxHeight),
78 inset.height
79 )
82internal fun frameExtent(value: Double, cap: Double?): Double = upperBound(value, cap ?: value)
84private 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 -> maximum
93 }
94 return when (available) {
95 case Some(val value) -> Some(frameExtent(value, cap))
96 case None -> cap
97 }