Scoop API

scoop:scoop.ui.component:0.1.0

src/content/card.scoop

Selected production source · src/content/card.scoop

1package scoop.ui.component
3import scoop.graphics.*
4import scoop.graphics.text.*
5import scoop.ui.*
6import scoop.ui.component.column as panelColumn
7import scoop.ui.style.*
9public enum CardSize {
10 Default, Sm
13public 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()
27public class Card public constructor(
28 private val props: Props<CardProps>,
29 scope: ComponentScope
30): 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.value
39 val theme = themes.read(input.theme)
40 val palette = theme.tokens.palette
41 val density = theme.tokens.typography.body.fontSize / 14.0
42 val spacing =
43 (
44 if (input.size == CardSize.Sm)
45 12.0
46 else
47 16.0
48 ) *
49 density
50 val titleStyle =
51 titles.resolve(
52 theme.tokens.typography.action,
53 palette.card.foreground,
54 None,
55 if (input.size == CardSize.Sm)
56 1.0
57 else
58 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 None
66 )
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 * density
91 this.alignment = CrossAlignment.Stretch
92 }
93 val header =
94 panelAside(
95 if (
96 panelPresent(input.header) ||
97 panelPresent(input.title) ||
98 panelPresent(input.description)
99 )
100 Some(headerText)
101 else
102 None,
103 input.action,
104 spacing,
105 theme.tokens.typography.body.fontSize * 8.0
106 )
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 spacing
119 else
120 0.0
121 ),
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.0
136 else
137 spacing,
138 spacing,
139 if (!panelPresent(input.footer))
140 spacing
141 else
142 0.0
143 ),
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.0
164 )
165 )
166 )
167 )
168 },
169 key = ViewKey.Number(2L)
170 )
171 )
172 case None -> {}
173 }
174 this.spacing = spacing
175 this.alignment = CrossAlignment.Stretch
176 }
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.1
191 )
192 )
193 )
194 )
195 )
196 this.corners(Corners.all(theme.tokens.corners.large))
197 this.clipContent(true)
198 }.then(input.style)
199 )
200 }
203public class CardBuilder internal constructor(): PanelBuilder() {
204 public var header: View? = None
205 public var action: View? = None
206 public var footer: View? = None
207 public var size: CardSize = CardSize.Default
208 public var headerSeparator: Boolean = false
209 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 }
220public fun card(content: View, configure: CardBuilder.() -> Unit = {}): View =
221 makeCard(content, configure)
223internal 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.style
239 ),
240 { current, mount -> Card(current, scope = mount) },
241 builder.key
242 )