scoop:scoop.ui.component:0.1.0
src/content/empty_state.scoop
Selected production source · src/content/empty_state.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.component.frame as panelBox8
import scoop.ui.style.*10
public enum EmptyMediaVariant {11
Default, Icon12
}14
public class EmptyStateProps public constructor(15
public val content: View,16
public val title: PanelContent? = None,17
public val description: PanelContent? = None,18
public val header: View? = None,19
public val media: View? = None,20
public val mediaVariant: EmptyMediaVariant = EmptyMediaVariant.Default,21
public val outlined: Boolean = false,22
public val fonts: FontCollection? = None,23
public val theme: ThemeSource? = None,24
public val style: StyleOverride = styleOverride()25
)27
public class EmptyState public constructor(28
private val props: Props<EmptyStateProps>,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 colors = theme.tokens.palette41
val density = theme.tokens.typography.body.fontSize / 14.042
val titleStyle =43
titles.resolve(theme.tokens.typography.action, colors.background.foreground, None)44
val descriptionStyle =45
descriptions.resolve(46
theme.tokens.typography.body,47
colors.muted.foreground,48
None,49
lineHeight = Some(1.625)50
)51
val header =52
panelColumn {53
when (input.media) {54
case Some(val value) -> {55
val icon = input.mediaVariant == EmptyMediaVariant.Icon56
val media =57
panelBox(value) {58
this.width =59
if (icon)60
Some(32.0 * density)61
else62
None63
this.height =64
if (icon)65
Some(32.0 * density)66
else67
None68
style {69
if (icon) {70
this.background(Some(Paint(colors.muted.background)))71
this.corners(Corners.all(theme.tokens.corners.medium))72
}73
}74
}75
this.add(76
panelFrame(77
media,78
Insets(0.0, 0.0, 0.0, 8.0 * density),79
key = ViewKey.Number(0L)80
)81
)82
}83
case None -> {}84
}85
when (input.header) {86
case Some(val value) -> this.add(panelSlot(value, 1L));87
case None -> {}88
}89
when (input.title) {90
case Some(val value) -> this.add(91
panelSlot(panelCaption(value, titleStyle, input.fonts, true), 2L)92
);93
case None -> {}94
}95
when (input.description) {96
case Some(val value) -> this.add(97
panelSlot(panelCaption(value, descriptionStyle, input.fonts, true), 3L)98
);99
case None -> {}100
}101
this.spacing = 8.0 * density102
this.alignment = CrossAlignment.Center103
}104
val body =105
panelColumn {106
if (107
panelPresent(input.media) ||108
panelPresent(input.header) ||109
panelPresent(input.title) ||110
panelPresent(input.description)111
)112
this.add(emptyRegion(header, 384.0 * density, 0L))113
this.add(emptyRegion(input.content, 384.0 * density, 1L))114
this.spacing = 16.0 * density115
this.padding = Insets.all(24.0 * density)116
this.alignment = CrossAlignment.Center117
}118
val visual =119
styleOverride { this.corners(Corners.all(theme.tokens.corners.large)) }.then(120
input.style121
).resolve(style())122
val outlined =123
when (input.style.border) {124
case Inherit -> input.outlined;125
case Set(_) -> false126
}127
return element(128
EmptyFrameProps(visual, outlined, colors.border),129
{ current, mount -> EmptyFrame(current, scope = mount) },130
[body]131
)132
}133
}135
private fun emptyRegion(content: View, width: Double, key: Long): View =136
frame(content) {137
this.key = ViewKey.Number(key)138
this.width = Some(width)139
this.alignment = Alignment(AxisAlignment.Center, AxisAlignment.Start)140
}142
public class EmptyStateBuilder internal constructor(): PanelBuilder() {143
public var header: View? = None144
public var media: View? = None145
public var mediaVariant: EmptyMediaVariant = EmptyMediaVariant.Default146
public var outlined: Boolean = false147
public fun header(content: View): Unit {148
header = Some(content)149
}150
public fun media(content: View): Unit {151
media = Some(content)152
}153
}155
public fun emptyState(content: View, configure: EmptyStateBuilder.() -> Unit = {}): View =156
makeEmptyState(content, configure)158
internal fun makeEmptyState(content: View, configure: EmptyStateBuilder.() -> Unit): View {159
val builder = EmptyStateBuilder()160
builder.configure()161
return component(162
EmptyStateProps(163
content,164
builder.title,165
builder.description,166
builder.header,167
builder.media,168
builder.mediaVariant,169
builder.outlined,170
builder.fonts,171
builder.theme,172
builder.style173
),174
{ current, mount -> EmptyState(current, scope = mount) },175
builder.key176
)177
}