Scoop API

scoop:scoop.ui.component:0.1.0

src/conversation/trace_layout.scoop

Selected production source · src/conversation/trace_layout.scoop

1package scoop.ui.component
3import scoop.graphics.*
4import scoop.ui.*
6internal class TraceLineLayoutProps(
7 val icon: Boolean,
8 val description: Boolean,
9 val arrow: Boolean,
10 val density: Double
13internal class TraceLineLayout(
14 private val props: Props<TraceLineLayoutProps>,
15 scope: ComponentScope
16): Element<Unit>(scope) {
17 public override fun measure(context: MeasureContext, constraints: Constraints): LayoutResult<
18 Unit
19 > {
20 val input = props.value
21 val side = 16.0 * input.density
22 val gap = 8.0 * input.density
23 val iconSpace =
24 if (input.icon)
25 side + gap
26 else
27 0.0
28 val arrowSpace =
29 if (input.arrow)
30 side + gap / 2.0
31 else
32 0.0
33 val descriptionMax =
34 when (constraints.maxWidth) {
35 case Some(val width) -> Some(lowerBound(0.0, width - iconSpace - arrowSpace) * 0.4)
36 case None -> None
37 }
38 val description =
39 if (input.description)
40 conversationMeasure(context, 2L, descriptionMax)
41 else {
42 context.skipChild(2L);
43 LayoutMetrics(Size(0.0, 0.0))
44 }
45 val detailSpace =
46 if (input.description)
47 description.size.width + gap
48 else
49 0.0
50 val title =
51 conversationMeasure(
52 context,
53 1L,
54 insetMaximum(constraints.maxWidth, iconSpace + arrowSpace + detailSpace)
55 )
56 if (input.icon)
57 context.measureChild(0L, Constraints.tight(Size(side, side)))
58 else
59 context.skipChild(0L)
60 if (input.arrow)
61 context.measureChild(3L, Constraints.tight(Size(side, side)))
62 else
63 context.skipChild(3L)
64 val height = lowerBound(side, lowerBound(title.size.height, description.size.height))
65 val size =
66 constraints.constrain(
67 Size(iconSpace + title.size.width + detailSpace + arrowSpace, height)
68 )
69 if (input.icon)
70 place(context, 0L, 0.0, side, height, side, size.width)
71 place(context, 1L, iconSpace, title.size.width, height, title.size.height, size.width)
72 if (input.description)
73 place(
74 context,
75 2L,
76 iconSpace + title.size.width + gap,
77 description.size.width,
78 height,
79 description.size.height,
80 size.width
81 )
82 if (input.arrow)
83 place(context, 3L, size.width - side, side, height, side, size.width)
84 return LayoutResult(LayoutMetrics(size), Unit)
85 }
86 private fun place(
87 context: MeasureContext,
88 index: Long,
89 x: Double,
90 width: Double,
91 height: Double,
92 childHeight: Double,
93 total: Double
94 ) {
95 context.placeChild(
96 index,
97 Point(
98 logicalX(lowerBound(0.0, x), width, total, context.layoutDirection),
99 lowerBound(0.0, (height - childHeight) / 2.0)
100 )
101 )
102 }
103 public override fun scene(context: SceneContext, measured: Unit) {
104 context.children()
105 }