Scoop API

scoop:scoop.ui.component:0.1.0

src/tree/row_layout.scoop

Selected production source · src/tree/row_layout.scoop

1package scoop.ui.component
3import scoop.graphics.*
4import scoop.surface.*
5import scoop.ui.*
6import scoop.window.accessibility.*
8private const val treeCellPadding: Double = 4.0
10internal const val treeRowSlotSize: Double = 20.0
12internal class TreeRowDrawing<T>(
13 val input: TreeRowInput<T>,
14 val owner: TreeRowView<T>,
15 val selected: Boolean,
16 val interaction: InteractionState
19internal class TreeRowInk<T>(private val props: Props<TreeRowDrawing<T>>, scope: ComponentScope) :
20 Element<Rect>(scope) {
21 // The row's main target lives outside this group; only F2 enters cell Tab order.
22 private val group = scope.focusScope()
23 private val selected = scope.animation(if (props.value.selected) 1.0 else 0.0)
24 private val hovered = scope.animation(if (props.value.interaction.hovered) 1.0 else 0.0)
25 private val reveal = scope.animation(1.0)
26 private var previous = props.value.input.plan.projection
27 private var previousIndex = props.value.input.index
28 public override fun onMount() {
29 props.value.owner.cellGroup = Some(group)
30 when (props.value.input.plan.controller.presented) {
31 case Some(val old) ->
32 if (old.indexOfKey(props.value.input.row.key) == None) reveal.snapTo(0.0)
33 case None -> {}
34 }
35 }
36 public override fun onUnmount() {
37 props.value.owner.cellGroup = None
38 }
39 public override fun update(): UiInvalidation {
40 val drawing = props.value
41 drawing.owner.update()
42 val input = drawing.input
43 val motion = input.plan.appearance.theme.tokens.motion
44 val enabled = input.plan.input.enabled && input.row.node.enabled
45 changeProgress(
46 selected,
47 if (drawing.selected) 1.0 else 0.0,
48 if (enabled) motion.resolve(motion.selection) else None
49 )
50 changeProgress(
51 hovered, if (drawing.interaction.hovered && enabled) 1.0 else 0.0,
52 if (enabled) motion.resolve(motion.hover) else None
53 )
54 if (previous !== input.plan.projection && previousIndex != input.index) reveal.snapTo(0.72)
55 previous = input.plan.projection
56 previousIndex = input.index
57 changeProgress(reveal, 1.0, if (enabled) motion.resolve(motion.selection) else None)
58 return UiInvalidation.Layout
59 }
60 public override fun measure(context: MeasureContext, constraints: Constraints): LayoutResult<
61 Rect
62 > {
63 val plan = props.value.input.plan
64 val widths = plan.tracks.resolve(constraints.maxWidth ?: constraints.minWidth)
65 val count = if (plan.explicit) plan.columns.size else 1L
66 val sizes = ArrayList<Size>()
67 var height = 24.0 * plan.appearance.density
68 for (index in 0L..<count) {
69 val value = context
70 .measureChild(
71 index,
72 Constraints(
73 widths.values[index],
74 Some(widths.values[index]),
75 maxHeight = constraints.maxHeight
76 )
77 )
78 sizes.add(value.size)
79 height = lowerBound(height, value.size.height)
80 }
81 val size = constraints.constrain(Size(widths.total, height))
82 val treeIndex = plan.columnIndex(plan.treeColumn)!!
83 val rtl = context.layoutDirection == LayoutDirection.Rtl
84 var treeX = 0.0
85 var x = 0.0
86 for (index in 0L..<count) {
87 val childX = if (rtl) widths.total - x - sizes[index].width else x
88 if (index == treeIndex) treeX = childX
89 context
90 .placeChild(
91 index,
92 Point(childX, lowerBound((size.height - sizes[index].height) / 2.0, 0.0))
93 )
94 x += widths.values[index]
95 }
96 return LayoutResult(
97 LayoutMetrics(size),
98 Rect(treeX, 0.0, sizes[treeIndex].width, size.height)
99 )
100 }
101 public override fun scene(context: SceneContext, treeColumn: Rect) {
102 val drawing = props.value
103 val plan = drawing.input.plan
104 val palette = plan.appearance.theme.tokens.palette
105 val bounds = Rect(0.0, 0.0, context.size.width, context.size.height)
106 val direction = context.layoutDirection
107 val selection = selected.value
108 val hover = hovered.value
109 context
110 .draw { paint ->
111 // Round fills and guides at the same device-pixel boundary between rows.
112 paint
113 .canvas
114 .drawRect(
115 bounds, Paint(
116 palette.background.background.withAlpha(1.0),
117 antiAlias = false
118 )
119 )
120 if (hover > 0.0)
121 paint
122 .canvas
123 .drawRect(
124 bounds,
125 Paint(
126 palette.muted.background.withAlpha(0.5 * hover),
127 antiAlias = false
128 )
129 )
130 if (selection > 0.0)
131 paint
132 .canvas
133 .drawRect(
134 bounds,
135 Paint(palette.muted.background.withAlpha(selection), antiAlias = false)
136 )
137 if (plan.input.showLines)
138 drawTreeLines(paint.canvas, drawing.input, treeColumn, direction)
139 if (drawing.interaction.focusVisible) {
140 val line = lowerBound(plan.appearance.density, 1.0)
141 paint.canvas.drawRect(Rect(0.0, 0.0, bounds.width, line), Paint(palette.ring))
142 paint
143 .canvas
144 .drawRect(
145 Rect(0.0, lowerBound(bounds.height - line, 0.0), bounds.width, line),
146 Paint(palette.ring)
147 )
148 }
149 }
150 if (plan.explicit || plan.input.fullRow || panelPresent(plan.input.drag))
151 context.place(drawing.owner.area, bounds)
152 context.save()
153 context.clipRect(bounds)
154 context.saveLayer(reveal.value)
155 context.children()
156 // Restore the reveal layer and row clip before drawing drag feedback.
157 context.restore()
158 context.restore()
159 drawing.owner.drag?.scene(context, bounds)
160 }
163internal class TreeCellInput<T>(
164 val row: TreeRowInput<T>,
165 val owner: TreeRowView<T>,
166 val column: Long,
167 val id: String,
168 val content: View,
169 val alignment: AxisAlignment
172internal fun <T> treeCell(
173 row: TreeRowInput<T>,
174 owner: TreeRowView<T>,
175 column: Long,
176 id: String,
177 content: View,
178 alignment: AxisAlignment
179): View = component(
180 TreeCellInput(row, owner, column, id, content, alignment),
181 { value, scope -> TreeCellView(value, scope) },
182 ViewKey.Text(id)
185private class TreeCellView<T>(private val props: Props<TreeCellInput<T>>, scope: ComponentScope) :
186 Component(scope) {
187 private val target = TreeCellTarget(scope, scope.focusScope())
188 public override fun onMount() {
189 props.value.owner.cells[props.value.id] = target
190 }
191 public override fun onUnmount() {
192 props.value.owner.cells.remove(props.value.id)
193 }
194 public override fun render(): View = element(
195 props.value,
196 { value, mount -> TreeCellInk(value, mount) },
197 [props.value.content]
198 )
201private class TreeCellInk<T>(private val props: Props<TreeCellInput<T>>, scope: ComponentScope) :
202 Element<Rect>(scope) {
203 private val semantics = createComponentSemantics(scope, SemanticRole.Cell)
204 init {
205 scope.routeSemanticsChildren(semantics)
206 }
207 public override fun update(): UiInvalidation {
208 val input = props.value
209 val plan = input.row.plan
210 val row = input.row.row
211 semantics
212 ?.update(
213 SemanticProperties(
214 ignored = !plan.explicit,
215 cell = if (plan.explicit)
216 Some(
217 SemanticTableCell(
218 input.row.index,
219 input.column,
220 columnKey = Some(SemanticItemKey.Text(input.id))
221 )
222 )
223 else
224 None
225 ),
226 relations = SemanticRelations(
227 columnHeaders = if (plan.explicit)
228 semanticIds(plan.headers[input.column].id)
229 else
230 []
231 )
232 )
233 return UiInvalidation.Layout
234 }
235 public override fun measure(context: MeasureContext, constraints: Constraints): LayoutResult<
236 Rect
237 > {
238 val input = props.value
239 val density = input.row.plan.appearance.density
240 val padding = treeCellPadding * density
241 val child = context
242 .measureChild(
243 0L,
244 Constraints(
245 maxWidth = insetMaximum(constraints.maxWidth, padding * 2.0),
246 maxHeight = insetMaximum(constraints.maxHeight, padding * 2.0)
247 )
248 )
249 val size = constraints
250 .constrain(Size(child.size.width + padding * 2.0, child.size.height + padding * 2.0))
251 val origin = alignedOrigin(
252 Rect(
253 padding,
254 padding,
255 lowerBound(size.width - padding * 2.0, 0.0),
256 lowerBound(size.height - padding * 2.0, 0.0)
257 ),
258 child.size,
259 Alignment(input.alignment, AxisAlignment.Center),
260 context.layoutDirection
261 )
262 context.placeChild(0L, origin)
263 return LayoutResult(LayoutMetrics(size), Rect(origin.x, 0.0, child.size.width, size.height))
264 }
265 public override fun scene(context: SceneContext, content: Rect) {
266 val input = props.value
267 val plan = input.row.plan
268 val bounds = Rect(0.0, 0.0, context.size.width, context.size.height)
269 when (semantics) {
270 case Some(val value) -> context.semantics(value, bounds);
271 case None -> {}
272 }
273 if (input.id ==
274 plan.treeColumn &&
275 !plan.explicit &&
276 !plan.input.fullRow &&
277 !panelPresent(plan.input.drag)
278 )
279 context.place(input.owner.area, content)
280 context.clipRect(bounds)
281 context.children()
282 }
285private fun <T> drawTreeLines(
286 canvas: Canvas,
287 input: TreeRowInput<T>,
288 column: Rect,
289 direction: LayoutDirection
290) {
291 val plan = input.plan
292 val row = input.row
293 val hasVisibleChildren = row.subtreeEnd > input.index + 1L
294 if (row.depth == 0L && !hasVisibleChildren) return
295 val density = plan.appearance.density
296 val step = plan.input.indent * density
297 val halfSlot = treeRowSlotSize / 2.0 * density
298 val rtl = direction == LayoutDirection.Rtl
299 val columnRight = column.x + column.width
300 val height = column.height
301 val center = height / 2.0
302 val color = Paint(plan.appearance.theme.tokens.palette.border, antiAlias = false)
303 fun stemX(depth: Long): Double {
304 val offset = treeCellPadding * density + halfSlot + depth.toDouble() * step
305 return if (rtl) columnRight - offset - density else column.x + offset
306 }
307 fun segment(x: Double, width: Double, top: Double, bottom: Double) {
308 val left = lowerBound(x, column.x)
309 val right = upperBound(x + width, columnRight)
310 if (right > left && bottom > top)
311 canvas.drawRect(Rect(left, top, right - left, bottom - top), color)
312 }
313 var at = row.parent
314 while (at != None) {
315 val ancestor = plan.projection.rowAt(at!!)
316 if (ancestor.depth > 0L && !ancestor.lastSibling)
317 segment(stemX(ancestor.depth - 1L), density, 0.0, height)
318 at = ancestor.parent
319 }
320 if (row.depth > 0L) {
321 val x = stemX(row.depth - 1L)
322 val top = lowerBound(center - density / 2.0, 0.0)
323 val bottom = upperBound(center + density / 2.0, height)
324 segment(x, density, 0.0, if (row.lastSibling) bottom else height)
325 segment(if (rtl) x + density - step else x + density, step - density, top, bottom)
326 }
327 if (hasVisibleChildren) segment(stemX(row.depth), density, center + halfSlot, height)