scoop:scoop.ui.component:0.1.0
src/layout/absolute.scoop
Selected production source · src/layout/absolute.scoop
1
package scoop.ui.component3
import scoop.collections.sortedBy4
import scoop.graphics.*5
import scoop.ui.*6
import scoop.ui.style.Insets7
import scoop.ui.style.LogicalInsets9
public 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)14
}16
public struct AbsoluteChild(17
val view: View,18
val horizontal: AbsoluteAxis = AbsoluteAxis.Start(),19
val vertical: AbsoluteAxis = AbsoluteAxis.Start(),20
val zIndex: Int = 021
)23
public 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? = None29
) {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
}46
}48
public class Absolute public constructor(49
private val props: Props<AbsoluteProps>,50
scope: ComponentScope51
): Component(scope) {52
public override fun render(): View {53
val value = props.value54
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
}62
}64
public fun absolute(configure: AbsoluteBuilder.() -> Unit = {}): View = makeAbsolute(configure)66
private class AbsoluteInput(val value: AbsoluteProps, val order: Array<Long>)68
private class AbsoluteInk(private val props: Props<AbsoluteInput>, scope: ComponentScope): Element<69
Unit70
>(scope) {71
public override fun measure(context: MeasureContext, constraints: Constraints): LayoutResult<72
Unit73
> {74
val value = props.value.value75
val size = absoluteSize(constraints, value.width, value.height)76
val padding = value.logicalPadding?.resolve(context.layoutDirection) ?: value.padding77
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.layoutDirection98
),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
}108
}