Scoop API

scoop:scoop.ui.component:0.1.0

src/table/appearance.scoop

Selected production source · src/table/appearance.scoop

1package scoop.ui.component
3import scoop.graphics.*
4import scoop.graphics.text.*
5import scoop.ui.*
6import scoop.ui.style.*
8internal class TableAppearance(
9 val theme: Theme,
10 val fonts: FontCollection,
11 val source: ThemeSource?,
12 val direction: LayoutDirection
13) {
14 val density = theme.tokens.typography.body.fontSize / 14.0
15 val body =
16 theme.tokens.typography.body.copy {
17 this.weight = 400;
18 this.color = theme.tokens.palette.background.foreground
19 }
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 focused
35 ),
36 direction
37 ).focusOutset,
38 theme.surfaceMetrics(
39 SurfaceVisual(SurfaceKind.Plain, focused),
40 textFieldStyle().options(focused),
41 direction
42 ).focusOutset
43 )
44 )
45 val paragraph =
46 paragraphStyle {
47 this.maxLines = Some(1L)
48 this.ellipsis = "…"
49 this.direction =
50 if (direction == LayoutDirection.Rtl)
51 TextDirection.Rtl
52 else
53 TextDirection.Ltr
54 }
55 fun content(content: PanelContent, header: Boolean = false, dim: Boolean = false): View =
56 when (content) {
57 case Custom(val view) -> view
58 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.theme
66 else
67 source,
68 Some(
69 input.baseStyle ?:
70 (
71 if (dim)
72 muted
73 else
74 if (header)
75 head
76 else
77 body
78 )
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? = None
89 ): 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 key
100 )
101 fun natural(
102 columns: Array<TableColumnSize>,
103 headers: Array<String>,
104 extra: Double = 0.0
105 ): Array<Double> =
106 Array<Double>(columns.size) {
107 index ->
108 val column = columns[index]
109 var width = column.minimum
110 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 / density
117 }
118 else -> {}
119 }
120 width
121 }
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 }