Scoop API

scoop:scoop.ui.component:0.1.0

src/content/item_layout.scoop

Selected production source · src/content/item_layout.scoop

1package scoop.ui.component
3import scoop.graphics.text.*
4import scoop.ui.*
5import scoop.ui.component.column as itemColumn
6import scoop.ui.component.row as itemRow
7import scoop.ui.style.*
9internal class ItemRenderer(private val scope: ComponentScope) {
10 private val themes = ThemeAccess(scope)
11 private val titles = ActionTextStyle()
12 private val descriptions = ActionTextStyle()
13 private val medium = spanStyle { this.weight = Some(500) }
14 private val regular = spanStyle { this.weight = Some(400) }
15 fun mount(theme: ThemeSource?) {
16 themes.mount(theme)
17 }
18 fun render(
19 input: ItemProps,
20 interaction: InteractionState,
21 behavior: ActionBehavior,
22 semantics: SemanticsHandle?
23 ): View {
24 val theme = themes.read(input.theme)
25 val density = theme.tokens.typography.body.fontSize / 14.0
26 val gap = itemGap(input.size) * density
27 val direction = scope.layoutDirection
28 val title =
29 titles.resolve(
30 theme.tokens.typography.body,
31 theme.tokens.palette.background.foreground,
32 Some(medium),
33 lineHeight = Some(1.375)
34 )
35 val description =
36 descriptions.resolve(
37 theme.tokens.typography.body,
38 theme.tokens.palette.muted.foreground,
39 Some(regular),
40 if (input.size == ItemSize.Xs)
41 12.0 / 14.0
42 else
43 1.0,
44 Some(1.5)
45 )
46 val hasText =
47 panelPresent(input.title) ||
48 panelPresent(input.description) ||
49 panelPresent(input.content)
50 val text =
51 if (hasText)
52 Some(
53 itemColumn {
54 when (input.title) {
55 case Some(val value) -> this.add(
56 panelSlot(itemCaption(value, title, input.fonts, 1L, direction), 0L)
57 )
58 case None -> {}
59 }
60 when (input.description) {
61 case Some(val value) -> this.add(
62 panelSlot(
63 itemCaption(value, description, input.fonts, 2L, direction),
64 1L
65 )
66 )
67 case None -> {}
68 }
69 when (input.content) {
70 case Some(val value) -> this.add(panelSlot(value, 2L))
71 case None -> {}
72 }
73 this.spacing =
74 if (input.size == ItemSize.Xs)
75 0.0
76 else
77 4.0 * density
78 this.alignment = CrossAlignment.Stretch
79 }
80 )
81 else
82 None
83 val middle =
84 itemRow {
85 when (input.media) {
86 case Some(val value) -> this.item(
87 panelSlot(
88 itemMedia(
89 value,
90 input.mediaVariant,
91 input.size,
92 theme.tokens.corners.small,
93 density,
94 panelPresent(input.description)
95 ),
96 0L
97 )
98 ) {
99 this.shrink = 0.0
100 if (panelPresent(input.description))
101 this.align = Some(CrossAlignment.Start)
102 }
103 case None -> {}
104 }
105 if (hasText || panelPresent(input.actions))
106 this.item(
107 panelSlot(
108 panelAside(
109 text,
110 input.actions,
111 gap,
112 theme.tokens.typography.body.fontSize * 8.0,
113 AxisAlignment.Center
114 ),
115 1L
116 )
117 ) { this.grow = 1.0 }
118 this.spacing = gap
119 this.alignment = CrossAlignment.Center
120 }
121 val content =
122 itemColumn {
123 when (input.header) {
124 case Some(val value) -> this.add(panelSlot(value, 0L))
125 case None -> {}
126 }
127 if (hasText || panelPresent(input.media) || panelPresent(input.actions))
128 this.item(panelSlot(middle, 1L)) {
129 // The row fills finite widths itself; keep its intrinsic measurement intact.
130 this.align = Some(CrossAlignment.Start)
131 }
132 when (input.footer) {
133 case Some(val value) -> this.add(panelSlot(value, 2L))
134 case None -> {}
135 }
136 this.spacing = gap
137 this.alignment = CrossAlignment.Stretch
138 }
139 return element(
140 ItemInkProps(input, theme, interaction.asVisual(input.enabled), behavior, semantics),
141 { current, mount -> ItemInk(current, scope = mount) },
142 [content]
143 )
144 }
147internal fun itemGap(size: ItemSize): Double =
148 if (size == ItemSize.Xs)
149 8.0
150 else
151 10.0
153private fun itemCaption(
154 content: PanelContent,
155 base: TextStyle,
156 fonts: FontCollection?,
157 lines: Long,
158 direction: LayoutDirection
159): View =
160 when (content) {
161 case Custom(val view) -> view
162 case Label(val input) -> panelCaption(
163 PanelContent.Label(
164 LabelProps(
165 input.text,
166 input.fonts,
167 Some(
168 input.settings ?:
169 paragraphStyle {
170 this.maxLines = Some(lines)
171 this.direction = direction.textDirection()
172 }
173 ),
174 input.inlines,
175 input.theme,
176 input.baseStyle,
177 input.underline,
178 input.semantics
179 )
180 ),
181 base,
182 fonts
183 )
184 }