Scoop API

scoop:scoop.ui.component:0.1.0

src/overlays/modal_header.scoop

Selected production source · src/overlays/modal_header.scoop

1package scoop.ui.component
3import scoop.graphics.*
4import scoop.icons.lucide.x as closeIcon
5import scoop.math.min
6import scoop.math.max
7import scoop.ui.*
9internal class AlertHeaderProps(
10 val density: Double,
11 val small: Boolean,
12 val media: Boolean,
13 val title: Boolean,
14 val description: Boolean
17/** The same media and text slots move between a compact stack and a wide two-column header. */
18internal class AlertHeader(
19 private val props: Props<AlertHeaderProps>,
20 scope: ComponentScope
21): Element<Unit>(scope) {
22 public override fun measure(context: MeasureContext, constraints: Constraints): LayoutResult<
23 Unit
24 > {
25 val input = props.value
26 val scale = input.density
27 val width = constraints.maxWidth ?: constraints.minWidth
28 val horizontal = !input.small && width >= 320.0 * scale
29 val media =
30 context.measureChild(
31 0L,
32 Constraints(
33 maxWidth = Some(min(width, 40.0 * scale)),
34 maxHeight = constraints.maxHeight
35 )
36 )
37 val mediaGap =
38 if (input.media)
39 (
40 if (horizontal)
41 16.0
42 else
43 8.0
44 ) *
45 scale
46 else
47 0.0
48 val textWidth =
49 max(
50 0.0,
51 width -
52 if (horizontal && input.media)
53 media.size.width + mediaGap
54 else
55 0.0
56 )
57 val naturalTitle = context.measureChild(1L, Constraints(maxHeight = constraints.maxHeight))
58 val title =
59 context.measureChild(
60 1L,
61 Constraints(
62 maxWidth = Some(min(textWidth, naturalTitle.size.width)),
63 maxHeight = constraints.maxHeight
64 )
65 )
66 val naturalDescription =
67 context.measureChild(2L, Constraints(maxHeight = constraints.maxHeight))
68 val description =
69 context.measureChild(
70 2L,
71 Constraints(
72 maxWidth = Some(min(textWidth, naturalDescription.size.width)),
73 maxHeight = constraints.maxHeight
74 )
75 )
76 val gap =
77 if (input.title && input.description)
78 6.0 * scale
79 else
80 0.0
81 val textHeight = title.size.height + gap + description.size.height
82 val height =
83 if (horizontal)
84 max(media.size.height, textHeight)
85 else
86 media.size.height + mediaGap + textHeight
87 val rtl = context.layoutDirection == LayoutDirection.Rtl
88 val textStart =
89 if (horizontal && input.media)
90 media.size.width + mediaGap
91 else
92 0.0
93 val textY =
94 if (horizontal)
95 0.0
96 else
97 media.size.height + mediaGap
98 context.placeChild(
99 0L,
100 Point(
101 if (horizontal) {
102 if (rtl)
103 width - media.size.width
104 else
105 0.0
106 } else
107 max(0.0, (width - media.size.width) / 2.0),
108 0.0
109 )
110 )
111 context.placeChild(
112 1L,
113 Point(
114 if (horizontal) {
115 if (rtl)
116 width - textStart - title.size.width
117 else
118 textStart
119 } else
120 max(0.0, (width - title.size.width) / 2.0),
121 textY
122 )
123 )
124 context.placeChild(
125 2L,
126 Point(
127 if (horizontal) {
128 if (rtl)
129 width - textStart - description.size.width
130 else
131 textStart
132 } else
133 max(0.0, (width - description.size.width) / 2.0),
134 textY + title.size.height + gap
135 )
136 )
137 return LayoutResult(LayoutMetrics(constraints.constrain(Size(width, height))), Unit)
138 }
139 public override fun scene(context: SceneContext, measured: Unit) {
140 context.children()
141 }
144internal fun modalCloseIcon(size: Double, color: Color): View =
145 makeIcon(closeIcon) {
146 this.size = size
147 this.color = Some(color)
148 }