scoop:scoop.ui.component:0.1.0
src/overlays/modal_footer.scoop
Selected production source · src/overlays/modal_footer.scoop
1
package scoop.ui.component3
import scoop.graphics.*4
import scoop.math.min5
import scoop.math.max6
import scoop.ui.*7
import scoop.ui.style.*9
public class DialogFooterProps public constructor(10
public val children: Array<View>,11
public val theme: ThemeSource? = None12
)14
public class DialogFooter public constructor(15
private val props: Props<DialogFooterProps>,16
scope: ComponentScope17
): Component(scope) {18
private val themes = ThemeAccess(scope)19
public override fun onMount() {20
themes.mount(props.value.theme)21
}22
public override fun render(): View {23
val density = themes.read(props.value.theme).tokens.typography.body.fontSize / 14.024
return modalActions(props.value.children, density, false)25
}26
}28
public fun dialogFooter(configure: ChildrenBuilder.() -> Unit): View {29
val builder = ChildrenBuilder()30
builder.configure()31
return component(32
DialogFooterProps(builder.childrenSnapshot()),33
{ current, mount -> DialogFooter(current, scope = mount) }34
)35
}37
internal fun modalActions(children: Array<View>, density: Double, balanced: Boolean): View =38
element(39
ModalActionsProps(8.0 * density, balanced),40
{ current, mount -> ModalActions(current, scope = mount) },41
children42
)44
private class ModalActionsProps(val spacing: Double, val balanced: Boolean)46
/** Reflows the same child sequence; only placements change at the narrow boundary. */47
private class ModalActions(48
private val props: Props<ModalActionsProps>,49
scope: ComponentScope50
): Element<Unit>(scope) {51
public override fun measure(context: MeasureContext, constraints: Constraints): LayoutResult<52
Unit53
> {54
val count = context.childCount55
if (count == 0L)56
return LayoutResult(LayoutMetrics(constraints.constrain(Size(0.0, 0.0))), Unit)57
val gap = props.value.spacing58
val natural =59
Array<LayoutMetrics>(count) { index -> context.measureChild(index, Constraints()) }60
var total = gap * (count - 1L).toDouble()61
for (item in natural)62
total += item.size.width63
val width = constraints.maxWidth ?: max(constraints.minWidth, total)64
val balanced = props.value.balanced && count == 2L65
val row = balanced || total <= width66
var height = 0.067
val sizes = ArrayList<Size>()68
for (index in 0L ..< count) {69
val itemWidth =70
if (balanced)71
max(0.0, (width - gap) / 2.0)72
else73
if (row)74
min(natural[index].size.width, width)75
else76
width77
val child =78
context.measureChild(79
index,80
Constraints(itemWidth, Some(itemWidth), maxHeight = constraints.maxHeight)81
)82
if (row)83
height = max(height, child.size.height)84
else85
height += child.size.height86
sizes.add(child.size)87
}88
if (!row)89
height += gap * (count - 1L).toDouble()90
val size = constraints.constrain(Size(width, height))91
var offset =92
if (row && !balanced)93
max(0.0, width - total)94
else95
0.096
for (index in 0L ..< count) {97
val child = sizes[index]98
if (row) {99
val x =100
if (context.layoutDirection == LayoutDirection.Rtl)101
width - offset - child.width102
else103
offset104
context.placeChild(index, Point(x, max(0.0, (size.height - child.height) / 2.0)))105
offset += child.width + gap106
} else {107
context.placeChild(index, Point(0.0, max(0.0, height - offset - child.height)))108
offset += child.height + gap109
}110
}111
return LayoutResult(LayoutMetrics(size), Unit)112
}113
public override fun scene(context: SceneContext, measured: Unit) {114
context.children()115
}116
}