Scoop API

scoop:scoop.ui.component:0.1.0

src/layout/absolute.scoop

Selected production source · src/layout/absolute.scoop

1package scoop.ui.component
3import scoop.collections.sortedBy
4import scoop.graphics.*
5import scoop.ui.*
6import scoop.ui.style.Insets
7import scoop.ui.style.LogicalInsets
9public enum AbsoluteAxis {
10 Start(val offset: Double = 0.0, val size: Double? = None),
11 End(val offset: Double = 0.0, val size: Double? = None),
12 Center(val offset: Double = 0.0, val size: Double? = None),
13 Stretch(val start: Double = 0.0, val end: Double = 0.0)
16public struct AbsoluteChild(
17 val view: View,
18 val horizontal: AbsoluteAxis = AbsoluteAxis.Start(),
19 val vertical: AbsoluteAxis = AbsoluteAxis.Start(),
20 val zIndex: Int = 0
23public class AbsoluteProps public constructor(
24 public val children: Array<AbsoluteChild>,
25 public val width: Double? = None,
26 public val height: Double? = None,
27 public val padding: Insets = Insets(),
28 public val logicalPadding: LogicalInsets? = None
29) {
30 internal val paddingSize = padding.totals()
31 init {
32 logicalPadding?.resolve(LayoutDirection.Ltr)
33 when (width) {
34 case Some(val value) -> nonnegative(value, "invalid absolute width")
35 case None -> {}
36 }
37 when (height) {
38 case Some(val value) -> nonnegative(value, "invalid absolute height")
39 case None -> {}
40 }
41 for (child in children) {
42 validateAbsoluteAxis(child.horizontal)
43 validateAbsoluteAxis(child.vertical)
44 }
45 }
48public class Absolute public constructor(
49 private val props: Props<AbsoluteProps>,
50 scope: ComponentScope
51): Component(scope) {
52 public override fun render(): View {
53 val value = props.value
54 val indices = Array<Long>(value.children.size) { index -> index }
55 val order = indices.sortedBy<Long, Int> { index -> value.children[index].zIndex }
56 return element(
57 AbsoluteInput(value, order.toArray()),
58 { current, mount -> AbsoluteInk(current, scope = mount) },
59 Array<View>(value.children.size) { index -> value.children[index].view }
60 )
61 }
64public fun absolute(configure: AbsoluteBuilder.() -> Unit = {}): View = makeAbsolute(configure)
66private class AbsoluteInput(val value: AbsoluteProps, val order: Array<Long>)
68private class AbsoluteInk(private val props: Props<AbsoluteInput>, scope: ComponentScope): Element<
69 Unit
70>(scope) {
71 public override fun measure(context: MeasureContext, constraints: Constraints): LayoutResult<
72 Unit
73 > {
74 val value = props.value.value
75 val size = absoluteSize(constraints, value.width, value.height)
76 val padding = value.logicalPadding?.resolve(context.layoutDirection) ?: value.padding
77 val total = padding.totals()
78 val width = lowerBound(size.width - total.width, 0.0)
79 val height = lowerBound(size.height - total.height, 0.0)
80 for (index in 0L ..< context.childCount) {
81 val child = value.children[index]
82 val x = absoluteRange(child.horizontal, width)
83 val y = absoluteRange(child.vertical, height)
84 val measured =
85 context.measureChild(
86 index,
87 Constraints(x.minimum, Some(x.maximum), y.minimum, Some(y.maximum))
88 )
89 context.placeChild(
90 index,
91 Point(
92 padding.left +
93 logicalX(
94 absoluteOrigin(child.horizontal, width, measured.size.width),
95 measured.size.width,
96 width,
97 context.layoutDirection
98 ),
99 padding.top + absoluteOrigin(child.vertical, height, measured.size.height)
100 )
101 )
102 }
103 return LayoutResult(LayoutMetrics(size), Unit)
104 }
105 public override fun scene(context: SceneContext, measured: Unit) {
106 context.children(props.value.order)
107 }