scoop:scoop.ui.component:0.1.0
src/content/item_layout.scoop
Selected production source · src/content/item_layout.scoop
1
package scoop.ui.component3
import scoop.graphics.text.*4
import scoop.ui.*5
import scoop.ui.component.column as itemColumn6
import scoop.ui.component.row as itemRow7
import scoop.ui.style.*9
internal 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.026
val gap = itemGap(input.size) * density27
val direction = scope.layoutDirection28
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.042
else43
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
1L65
)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.076
else77
4.0 * density78
this.alignment = CrossAlignment.Stretch79
}80
)81
else82
None83
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
0L97
)98
) {99
this.shrink = 0.0100
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.Center114
),115
1L116
)117
) { this.grow = 1.0 }118
this.spacing = gap119
this.alignment = CrossAlignment.Center120
}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 = gap137
this.alignment = CrossAlignment.Stretch138
}139
return element(140
ItemInkProps(input, theme, interaction.asVisual(input.enabled), behavior, semantics),141
{ current, mount -> ItemInk(current, scope = mount) },142
[content]143
)144
}145
}147
internal fun itemGap(size: ItemSize): Double =148
if (size == ItemSize.Xs)149
8.0150
else151
10.0153
private fun itemCaption(154
content: PanelContent,155
base: TextStyle,156
fonts: FontCollection?,157
lines: Long,158
direction: LayoutDirection159
): View =160
when (content) {161
case Custom(val view) -> view162
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.semantics179
)180
),181
base,182
fonts183
)184
}