scoop:scoop.ui.component:0.1.0
src/table/appearance.scoop
Selected production source · src/table/appearance.scoop
1
package scoop.ui.component3
import scoop.graphics.*4
import scoop.graphics.text.*5
import scoop.ui.*6
import scoop.ui.style.*8
internal class TableAppearance(9
val theme: Theme,10
val fonts: FontCollection,11
val source: ThemeSource?,12
val direction: LayoutDirection13
) {14
val density = theme.tokens.typography.body.fontSize / 14.015
val body =16
theme.tokens.typography.body.copy {17
this.weight = 400;18
this.color = theme.tokens.palette.background.foreground19
}20
val head = body.copy { this.weight = 500 }21
val muted = body.copy { this.color = theme.tokens.palette.muted.foreground }22
private val focused = InteractionVisual(focusVisible = true)23
val padding =24
lowerBound(25
8.0 * density,26
lowerBound(27
theme.surfaceMetrics(28
SurfaceVisual(SurfaceKind.Action, focused),29
actionOptions(30
theme,31
ActionShape.Button(ButtonSize.Sm),32
ButtonVariant.Ghost,33
buttonStyle(),34
focused35
),36
direction37
).focusOutset,38
theme.surfaceMetrics(39
SurfaceVisual(SurfaceKind.Plain, focused),40
textFieldStyle().options(focused),41
direction42
).focusOutset43
)44
)45
val paragraph =46
paragraphStyle {47
this.maxLines = Some(1L)48
this.ellipsis = "…"49
this.direction =50
if (direction == LayoutDirection.Rtl)51
TextDirection.Rtl52
else53
TextDirection.Ltr54
}55
fun content(content: PanelContent, header: Boolean = false, dim: Boolean = false): View =56
when (content) {57
case Custom(val view) -> view58
case Label(val input) -> component(59
LabelProps(60
input.text,61
Some(input.fonts ?: fonts),62
Some(input.settings ?: paragraph),63
input.inlines,64
if (panelPresent(input.theme))65
input.theme66
else67
source,68
Some(69
input.baseStyle ?:70
(71
if (dim)72
muted73
else74
if (header)75
head76
else77
body78
)79
)80
),81
{ props, scope -> Label(props, scope) }82
)83
}84
fun cell(85
cell: TableCellProps,86
key: ViewKey = ViewKey.Position,87
emphasized: Boolean = false,88
semantics: TableCellSemantics? = None89
): View =90
element(91
TableCellLayout(92
padding,93
cell.alignment,94
cell.header || emphasized,95
semantics ?: basicCellSemantics(cell)96
),97
{ props, scope -> TableCellInk(props, scope) },98
[content(cell.content, cell.header || emphasized)],99
key100
)101
fun natural(102
columns: Array<TableColumnSize>,103
headers: Array<String>,104
extra: Double = 0.0105
): Array<Double> =106
Array<Double>(columns.size) {107
index ->108
val column = columns[index]109
var width = column.minimum110
when (column.track) {111
case Auto -> {112
if (index < headers.size)113
width = lowerBound(width, measure(headers[index]) + extra)114
for (text in column.samples)115
width = lowerBound(width, measure(text))116
width += padding * 2.0 / density117
}118
else -> {}119
}120
width121
}122
private fun measure(text: String): Double =123
Paragraph(fonts, text, head, paragraph).use {124
value -> value.layoutUnwrapped().use { layout -> layout.size.width / density }125
}126
}