scoop:scoop.ui.component:0.1.0
src/typography/list.scoop
Selected production source · src/typography/list.scoop
1
package scoop.ui.component3
import scoop.graphics.text.*4
import scoop.ui.*5
import scoop.ui.style.*6
import scoop.ui.component.frame as makeFrame7
import scoop.ui.component.row as makeRow9
private class TypographyListEntry(val content: PanelContent, val key: ViewKey)11
public class TypographyListBuilder internal constructor() {12
private val entries = ArrayList<TypographyListEntry>()13
public var fonts: FontCollection? = None14
public var theme: ThemeSource? = None15
public var key: ViewKey = ViewKey.Position16
public fun item(value: String, key: ViewKey = ViewKey.Position): Unit {17
entries.add(18
TypographyListEntry(PanelContent.Label(LabelProps(LabelText.Plain(value, None))), key)19
)20
}21
public fun item(value: StyledText, key: ViewKey = ViewKey.Position): Unit {22
entries.add(23
TypographyListEntry(PanelContent.Label(LabelProps(LabelText.Styled(value))), key)24
)25
}26
public fun item(value: View, key: ViewKey = ViewKey.Position): Unit {27
entries.add(TypographyListEntry(PanelContent.Custom(value), key))28
}29
internal fun build(): View =30
component(31
TypographyListProps(entries.toArray(), fonts, theme),32
{ current, mount -> TypographyList(current, scope = mount) },33
key34
)35
}37
public fun typographyList(configure: TypographyListBuilder.() -> Unit): View =38
makeTypographyList(configure)40
internal fun makeTypographyList(configure: TypographyListBuilder.() -> Unit): View {41
val builder = TypographyListBuilder()42
builder.configure()43
return builder.build()44
}46
private class TypographyListProps(47
val entries: Array<TypographyListEntry>,48
val fonts: FontCollection?,49
val theme: ThemeSource?50
)52
private class TypographyList(53
private val props: Props<TypographyListProps>,54
scope: ComponentScope55
): Component(scope) {56
private val themes = ThemeAccess(scope)57
public override fun onMount() {58
themes.mount(props.value.theme)59
}60
public override fun render(): View {61
val input = props.value62
val theme = themes.read(input.theme)63
val density = theme.tokens.typography.body.fontSize / 14.064
val base = typographyStyle(TypographyRole.P, theme)65
return column {66
this.spacing = 8.0 * density67
this.alignment = CrossAlignment.Stretch68
for (index in 0L ..< input.entries.size) {69
val entry = input.entries[index]70
this.add(71
makeRow {72
this.key =73
if (entry.key == ViewKey.Position)74
ViewKey.Number(index)75
else76
entry.key77
this.logicalPadding = Some(LogicalInsets(start = 8.0 * density))78
this.spacing = 8.0 * density79
this.alignment = CrossAlignment.Baseline80
this.add(81
makeFrame(82
makeStyledLabel(83
styledText {84
this.style = base85
this.text("•")86
}87
) { this.fonts = input.fonts }88
) { this.width = Some(8.0 * density) }89
)90
this.item(91
typographyCaption(entry.content, base, input.fonts, input.theme)92
) {93
this.grow = 1.094
this.basis = FlexBasis.Content95
}96
}97
)98
}99
}100
}101
}103
/** Styled captions keep explicit styles; custom views retain their own behavior. */104
internal fun typographyCaption(105
content: PanelContent,106
base: TextStyle,107
fonts: FontCollection?,108
theme: ThemeSource?109
): View =110
when (content) {111
case Custom(val view) -> view112
case Label(val input) -> {113
val text =114
when (input.text) {115
case Plain(val value, val local) -> TypographyContent.Styled(116
styledText {117
this.style =118
local?.resolve(input.baseStyle ?: base) ?: (input.baseStyle ?: base)119
this.text(value)120
}121
)122
case Styled(val value) -> TypographyContent.Styled(value)123
}124
makeTypography(text, TypographyRole.P) {125
this.fonts =126
if (panelPresent(input.fonts))127
input.fonts128
else129
fonts130
this.theme =131
if (panelPresent(input.theme))132
input.theme133
else134
theme135
this.settings = input.settings136
this.inlines = input.inlines137
}138
}139
}