Scoop API

scoop:scoop.ui.component:0.1.0

src/table/layout.scoop

Selected production source · src/table/layout.scoop

1package scoop.ui.component
3import scoop.graphics.*
4import scoop.surface.*
5import scoop.ui.*
6import scoop.ui.style.*
7import scoop.window.accessibility.*
9internal class TableViewportInput(
10 val tracks: TableTracks,
11 val controls: ScrollControlProps,
12 val width: Double?
15internal class TableViewport(
16 private val props: Props<TableViewportInput>,
17 scope: ComponentScope
18): Element<Size>(scope) {
19 private val control = ScrollControl(scope) { props.value.controls }
20 public override fun update(): UiInvalidation {
21 control.update();
22 return UiInvalidation.Layout
23 }
24 public override fun measure(context: MeasureContext, constraints: Constraints): LayoutResult<
25 Size
26 > {
27 val input = props.value
28 val proposed = input.width ?: constraints.maxWidth ?: input.tracks.resolve(0.0).total
29 nonnegative(proposed, "invalid table width")
30 val viewport = constraints.constrain(Size(proposed, 0.0)).width
31 if (viewport <= 0.0 || constraints.maxHeight == Some(0.0)) {
32 context.skipChild(0L)
33 return LayoutResult(
34 LayoutMetrics(constraints.constrain(Size(0.0, 0.0))),
35 Size(0.0, 0.0)
36 )
37 }
38 val widths = input.tracks.resolve(viewport)
39 val child =
40 context.measureChild(
41 0L,
42 Constraints(widths.total, Some(widths.total), maxHeight = constraints.maxHeight)
43 )
44 context.placeChild(0L, Point(0.0, 0.0))
45 return LayoutResult(
46 LayoutMetrics(constraints.constrain(Size(viewport, child.size.height))),
47 child.size
48 )
49 }
50 public override fun scene(context: SceneContext, content: Size) {
51 control.scene(context, control.layout(context.size, content))
52 }
55internal class TableRowInput(
56 val row: TableRowProps,
57 val tracks: TableTracks,
58 val appearance: TableAppearance,
59 val border: Boolean = true
62internal class TableRowInk(private val props: Props<TableRowInput>, scope: ComponentScope): Element<
63 Unit
64>(scope) {
65 private val area =
66 scope.createArea {
67 context,
68 _ -> if (context.phase == RoutedPhase.Notification && scope.isMounted)
69 scope.requestFrame()
70 }
71 init {
72 scope.routeChildren(area)
73 }
74 public override fun update(): UiInvalidation = UiInvalidation.Layout
75 public override fun measure(context: MeasureContext, constraints: Constraints): LayoutResult<
76 Unit
77 > {
78 val input = props.value
79 val widths = input.tracks.resolve(constraints.maxWidth ?: constraints.minWidth)
80 var at = 0L
81 var height =
82 if (input.row.section == TableSection.Header)
83 40.0 * input.appearance.density
84 else
85 0.0
86 val sizes = ArrayList<Size>()
87 val offsets = ArrayList<Double>()
88 var x = 0.0
89 for (index in 0L ..< input.row.cells.size) {
90 var width = 0.0
91 for (_ in 0L ..< input.row.cells[index].span) {
92 width += widths.values[at];
93 at += 1L
94 }
95 val child =
96 context.measureChild(
97 index,
98 Constraints(width, Some(width), maxHeight = constraints.maxHeight)
99 )
100 sizes.add(child.size)
101 offsets.add(x)
102 x += width
103 height = lowerBound(height, child.size.height)
104 }
105 val size = constraints.constrain(Size(widths.total, height))
106 for (index in 0L ..< sizes.size)
107 context.placeChild(
108 index,
109 Point(
110 if (scope.layoutDirection == LayoutDirection.Rtl)
111 widths.total - offsets[index] - sizes[index].width
112 else
113 offsets[index],
114 lowerBound((size.height - sizes[index].height) / 2.0, 0.0)
115 )
116 )
117 return LayoutResult(LayoutMetrics(size), Unit)
118 }
119 public override fun scene(context: SceneContext, value: Unit) {
120 val input = props.value
121 val color = input.appearance.theme.tokens.palette.muted.background
122 val background =
123 if (input.row.selected)
124 Some(color)
125 else
126 if (area.scope.isHovered || input.row.section == TableSection.Footer)
127 Some(color.withAlpha(color.alpha * 0.5))
128 else
129 None
130 val bounds = Rect(0.0, 0.0, context.size.width, context.size.height)
131 context.draw {
132 paint ->
133 when (background) {
134 case Some(val color) -> paint.canvas.drawRect(bounds, Paint(color));
135 case None -> {}
136 }
137 if (input.border)
138 paint.canvas.drawRect(
139 Rect(
140 0.0,
141 lowerBound(bounds.height - input.appearance.density, 0.0),
142 bounds.width,
143 input.appearance.density
144 ),
145 Paint(input.appearance.theme.tokens.palette.border)
146 )
147 }
148 context.place(area, bounds)
149 context.children()
150 }
153internal fun tableRowView(
154 row: TableRowProps,
155 tracks: TableTracks,
156 appearance: TableAppearance,
157 key: ViewKey,
158 border: Boolean = true,
159 prepared: Array<View>? = None,
160 rowIndex: Long = 0L,
161 headers: TableHeaderSemantics? = None,
162 columnHeaders: Array<SemanticsReference> = [],
163 columnKeys: Array<SemanticItemKey> = [],
164 logical: Boolean = false
165): View {
166 val children =
167 prepared ?:
168 Array<View>(row.cells.size) {
169 index -> appearance.cell(
170 row.cells[index],
171 row.cells[index].key,
172 row.section == TableSection.Footer,
173 Some(
174 when (headers) {
175 case Some(val value) -> value.cell(row, rowIndex, index)
176 case None -> {
177 var column = 0L
178 for (previous in 0L ..< index)
179 column += row.cells[previous].span
180 basicCellSemantics(
181 row.cells[index],
182 Some(
183 SemanticTableCell(
184 rowIndex,
185 column,
186 columnSpan = row.cells[index].span,
187 columnKey =
188 if (column < columnKeys.size)
189 Some(columnKeys[column])
190 else
191 None
192 )
193 ),
194 if (column < columnHeaders.size)
195 semanticIds(columnHeaders[column].id)
196 else
197 []
198 )
199 }
200 }
201 )
202 )
203 }
204 val content =
205 element(
206 TableRowInput(row, tracks, appearance, border),
207 { props, scope -> TableRowInk(props, scope) },
208 children,
209 key
210 )
211 return semanticContent(
212 content,
213 SemanticRole.Row,
214 SemanticProperties(
215 selected =
216 if (row.section == TableSection.Body)
217 Some(row.selected)
218 else
219 None,
220 ignored = logical
221 ),
222 mergeLabels = false,
223 key = key
224 )