Scoop API

scoop:scoop.ui.component:0.1.0

src/overlays/modal_footer.scoop

Selected production source · src/overlays/modal_footer.scoop

1package scoop.ui.component
3import scoop.graphics.*
4import scoop.math.min
5import scoop.math.max
6import scoop.ui.*
7import scoop.ui.style.*
9public class DialogFooterProps public constructor(
10 public val children: Array<View>,
11 public val theme: ThemeSource? = None
14public class DialogFooter public constructor(
15 private val props: Props<DialogFooterProps>,
16 scope: ComponentScope
17): 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.0
24 return modalActions(props.value.children, density, false)
25 }
28public 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 )
37internal 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 children
42 )
44private class ModalActionsProps(val spacing: Double, val balanced: Boolean)
46/** Reflows the same child sequence; only placements change at the narrow boundary. */
47private class ModalActions(
48 private val props: Props<ModalActionsProps>,
49 scope: ComponentScope
50): Element<Unit>(scope) {
51 public override fun measure(context: MeasureContext, constraints: Constraints): LayoutResult<
52 Unit
53 > {
54 val count = context.childCount
55 if (count == 0L)
56 return LayoutResult(LayoutMetrics(constraints.constrain(Size(0.0, 0.0))), Unit)
57 val gap = props.value.spacing
58 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.width
63 val width = constraints.maxWidth ?: max(constraints.minWidth, total)
64 val balanced = props.value.balanced && count == 2L
65 val row = balanced || total <= width
66 var height = 0.0
67 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 else
73 if (row)
74 min(natural[index].size.width, width)
75 else
76 width
77 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 else
85 height += child.size.height
86 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 else
95 0.0
96 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.width
102 else
103 offset
104 context.placeChild(index, Point(x, max(0.0, (size.height - child.height) / 2.0)))
105 offset += child.width + gap
106 } else {
107 context.placeChild(index, Point(0.0, max(0.0, height - offset - child.height)))
108 offset += child.height + gap
109 }
110 }
111 return LayoutResult(LayoutMetrics(size), Unit)
112 }
113 public override fun scene(context: SceneContext, measured: Unit) {
114 context.children()
115 }