Scoop API

scoop:scoop.ui.component:0.1.0

src/typography/list.scoop

Selected production source · src/typography/list.scoop

1package scoop.ui.component
3import scoop.graphics.text.*
4import scoop.ui.*
5import scoop.ui.style.*
6import scoop.ui.component.frame as makeFrame
7import scoop.ui.component.row as makeRow
9private class TypographyListEntry(val content: PanelContent, val key: ViewKey)
11public class TypographyListBuilder internal constructor() {
12 private val entries = ArrayList<TypographyListEntry>()
13 public var fonts: FontCollection? = None
14 public var theme: ThemeSource? = None
15 public var key: ViewKey = ViewKey.Position
16 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 key
34 )
37public fun typographyList(configure: TypographyListBuilder.() -> Unit): View =
38 makeTypographyList(configure)
40internal fun makeTypographyList(configure: TypographyListBuilder.() -> Unit): View {
41 val builder = TypographyListBuilder()
42 builder.configure()
43 return builder.build()
46private class TypographyListProps(
47 val entries: Array<TypographyListEntry>,
48 val fonts: FontCollection?,
49 val theme: ThemeSource?
52private class TypographyList(
53 private val props: Props<TypographyListProps>,
54 scope: ComponentScope
55): 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.value
62 val theme = themes.read(input.theme)
63 val density = theme.tokens.typography.body.fontSize / 14.0
64 val base = typographyStyle(TypographyRole.P, theme)
65 return column {
66 this.spacing = 8.0 * density
67 this.alignment = CrossAlignment.Stretch
68 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 else
76 entry.key
77 this.logicalPadding = Some(LogicalInsets(start = 8.0 * density))
78 this.spacing = 8.0 * density
79 this.alignment = CrossAlignment.Baseline
80 this.add(
81 makeFrame(
82 makeStyledLabel(
83 styledText {
84 this.style = base
85 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.0
94 this.basis = FlexBasis.Content
95 }
96 }
97 )
98 }
99 }
100 }
103/** Styled captions keep explicit styles; custom views retain their own behavior. */
104internal fun typographyCaption(
105 content: PanelContent,
106 base: TextStyle,
107 fonts: FontCollection?,
108 theme: ThemeSource?
109): View =
110 when (content) {
111 case Custom(val view) -> view
112 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.fonts
128 else
129 fonts
130 this.theme =
131 if (panelPresent(input.theme))
132 input.theme
133 else
134 theme
135 this.settings = input.settings
136 this.inlines = input.inlines
137 }
138 }
139 }