scoop:scoop.ui.component:0.1.0
src/overlays/modal_header.scoop
Selected production source · src/overlays/modal_header.scoop
1
package scoop.ui.component3
import scoop.graphics.*4
import scoop.icons.lucide.x as closeIcon5
import scoop.math.min6
import scoop.math.max7
import scoop.ui.*9
internal class AlertHeaderProps(10
val density: Double,11
val small: Boolean,12
val media: Boolean,13
val title: Boolean,14
val description: Boolean15
)17
/** The same media and text slots move between a compact stack and a wide two-column header. */18
internal class AlertHeader(19
private val props: Props<AlertHeaderProps>,20
scope: ComponentScope21
): Element<Unit>(scope) {22
public override fun measure(context: MeasureContext, constraints: Constraints): LayoutResult<23
Unit24
> {25
val input = props.value26
val scale = input.density27
val width = constraints.maxWidth ?: constraints.minWidth28
val horizontal = !input.small && width >= 320.0 * scale29
val media =30
context.measureChild(31
0L,32
Constraints(33
maxWidth = Some(min(width, 40.0 * scale)),34
maxHeight = constraints.maxHeight35
)36
)37
val mediaGap =38
if (input.media)39
(40
if (horizontal)41
16.042
else43
8.044
) *45
scale46
else47
0.048
val textWidth =49
max(50
0.0,51
width -52
if (horizontal && input.media)53
media.size.width + mediaGap54
else55
0.056
)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.maxHeight64
)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.maxHeight74
)75
)76
val gap =77
if (input.title && input.description)78
6.0 * scale79
else80
0.081
val textHeight = title.size.height + gap + description.size.height82
val height =83
if (horizontal)84
max(media.size.height, textHeight)85
else86
media.size.height + mediaGap + textHeight87
val rtl = context.layoutDirection == LayoutDirection.Rtl88
val textStart =89
if (horizontal && input.media)90
media.size.width + mediaGap91
else92
0.093
val textY =94
if (horizontal)95
0.096
else97
media.size.height + mediaGap98
context.placeChild(99
0L,100
Point(101
if (horizontal) {102
if (rtl)103
width - media.size.width104
else105
0.0106
} else107
max(0.0, (width - media.size.width) / 2.0),108
0.0109
)110
)111
context.placeChild(112
1L,113
Point(114
if (horizontal) {115
if (rtl)116
width - textStart - title.size.width117
else118
textStart119
} else120
max(0.0, (width - title.size.width) / 2.0),121
textY122
)123
)124
context.placeChild(125
2L,126
Point(127
if (horizontal) {128
if (rtl)129
width - textStart - description.size.width130
else131
textStart132
} else133
max(0.0, (width - description.size.width) / 2.0),134
textY + title.size.height + gap135
)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
}142
}144
internal fun modalCloseIcon(size: Double, color: Color): View =145
makeIcon(closeIcon) {146
this.size = size147
this.color = Some(color)148
}