scoop:scoop.ui.component:0.1.0
src/content/card.scoop
Selected production source · src/content/card.scoop
1
package scoop.ui.component3
import scoop.graphics.*4
import scoop.graphics.text.*5
import scoop.ui.*6
import scoop.ui.component.column as panelColumn7
import scoop.ui.style.*9
public enum CardSize {10
Default, Sm11
}13
public class CardProps public constructor(14
public val content: View,15
public val title: PanelContent? = None,16
public val description: PanelContent? = None,17
public val header: View? = None,18
public val action: View? = None,19
public val footer: View? = None,20
public val size: CardSize = CardSize.Default,21
public val headerSeparator: Boolean = false,22
public val fonts: FontCollection? = None,23
public val theme: ThemeSource? = None,24
public val style: StyleOverride = styleOverride()25
)27
public class Card public constructor(28
private val props: Props<CardProps>,29
scope: ComponentScope30
): Component(scope) {31
private val themes = ThemeAccess(scope)32
private val titles = ActionTextStyle()33
private val descriptions = ActionTextStyle()34
public override fun onMount() {35
themes.mount(props.value.theme)36
}37
public override fun render(): View {38
val input = props.value39
val theme = themes.read(input.theme)40
val palette = theme.tokens.palette41
val density = theme.tokens.typography.body.fontSize / 14.042
val spacing =43
(44
if (input.size == CardSize.Sm)45
12.046
else47
16.048
) *49
density50
val titleStyle =51
titles.resolve(52
theme.tokens.typography.action,53
palette.card.foreground,54
None,55
if (input.size == CardSize.Sm)56
1.057
else58
16.0 / 14.0,59
Some(1.375)60
)61
val descriptionStyle =62
descriptions.resolve(63
theme.tokens.typography.body,64
componentForeground(theme, palette.muted.foreground, palette.card.background),65
None66
)67
val hasHeader =68
panelPresent(input.header) ||69
panelPresent(input.title) ||70
panelPresent(input.description) ||71
panelPresent(input.action)72
val headerText =73
panelColumn {74
when (input.header) {75
case Some(val value) -> this.add(panelSlot(value, 0L));76
case None -> {}77
}78
when (input.title) {79
case Some(val value) -> this.add(80
panelSlot(panelCaption(value, titleStyle, input.fonts), 1L)81
);82
case None -> {}83
}84
when (input.description) {85
case Some(val value) -> this.add(86
panelSlot(panelCaption(value, descriptionStyle, input.fonts), 2L)87
);88
case None -> {}89
}90
this.spacing = 4.0 * density91
this.alignment = CrossAlignment.Stretch92
}93
val header =94
panelAside(95
if (96
panelPresent(input.header) ||97
panelPresent(input.title) ||98
panelPresent(input.description)99
)100
Some(headerText)101
else102
None,103
input.action,104
spacing,105
theme.tokens.typography.body.fontSize * 8.0106
)107
val content =108
panelColumn {109
if (hasHeader)110
this.add(111
panelFrame(112
header,113
Insets(114
spacing,115
spacing,116
spacing,117
if (input.headerSeparator)118
spacing119
else120
0.0121
),122
styleOverride {123
if (input.headerSeparator)124
this.border(Some(bottomBorder(palette.border)))125
},126
key = ViewKey.Number(0L)127
)128
)129
this.add(130
panelFrame(131
input.content,132
Insets(133
spacing,134
if (hasHeader)135
0.0136
else137
spacing,138
spacing,139
if (!panelPresent(input.footer))140
spacing141
else142
0.0143
),144
key = ViewKey.Number(1L)145
)146
)147
when (input.footer) {148
case Some(val value) -> this.add(149
panelFrame(150
value,151
Insets.all(spacing),152
styleOverride {153
this.background(154
Some(Paint(componentPanelSection(theme, palette.card)))155
);156
this.border(157
Some(158
topBorder(159
componentForeground(160
theme,161
palette.border,162
palette.card.background,163
3.0164
)165
)166
)167
)168
},169
key = ViewKey.Number(2L)170
)171
)172
case None -> {}173
}174
this.spacing = spacing175
this.alignment = CrossAlignment.Stretch176
}177
return panelFrame(178
content,179
visual =180
styleOverride {181
this.background(Some(Paint(palette.card.background)))182
this.border(183
Some(184
Border.all(185
1.0,186
componentPanelBorder(187
theme,188
palette.card,189
palette.background.foreground.withAlpha(190
palette.background.foreground.alpha * 0.1191
)192
)193
)194
)195
)196
this.corners(Corners.all(theme.tokens.corners.large))197
this.clipContent(true)198
}.then(input.style)199
)200
}201
}203
public class CardBuilder internal constructor(): PanelBuilder() {204
public var header: View? = None205
public var action: View? = None206
public var footer: View? = None207
public var size: CardSize = CardSize.Default208
public var headerSeparator: Boolean = false209
public fun header(content: View): Unit {210
header = Some(content)211
}212
public fun action(content: View): Unit {213
action = Some(content)214
}215
public fun footer(content: View): Unit {216
footer = Some(content)217
}218
}220
public fun card(content: View, configure: CardBuilder.() -> Unit = {}): View =221
makeCard(content, configure)223
internal fun makeCard(content: View, configure: CardBuilder.() -> Unit): View {224
val builder = CardBuilder()225
builder.configure()226
return component(227
CardProps(228
content,229
builder.title,230
builder.description,231
builder.header,232
builder.action,233
builder.footer,234
builder.size,235
builder.headerSeparator,236
builder.fonts,237
builder.theme,238
builder.style239
),240
{ current, mount -> Card(current, scope = mount) },241
builder.key242
)243
}