scoop:scoop.ui.component:0.1.0
src/conversation/trace_layout.scoop
Selected production source · src/conversation/trace_layout.scoop
1
package scoop.ui.component3
import scoop.graphics.*4
import scoop.ui.*6
internal class TraceLineLayoutProps(7
val icon: Boolean,8
val description: Boolean,9
val arrow: Boolean,10
val density: Double11
)13
internal class TraceLineLayout(14
private val props: Props<TraceLineLayoutProps>,15
scope: ComponentScope16
): Element<Unit>(scope) {17
public override fun measure(context: MeasureContext, constraints: Constraints): LayoutResult<18
Unit19
> {20
val input = props.value21
val side = 16.0 * input.density22
val gap = 8.0 * input.density23
val iconSpace =24
if (input.icon)25
side + gap26
else27
0.028
val arrowSpace =29
if (input.arrow)30
side + gap / 2.031
else32
0.033
val descriptionMax =34
when (constraints.maxWidth) {35
case Some(val width) -> Some(lowerBound(0.0, width - iconSpace - arrowSpace) * 0.4)36
case None -> None37
}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 + gap48
else49
0.050
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
else59
context.skipChild(0L)60
if (input.arrow)61
context.measureChild(3L, Constraints.tight(Size(side, side)))62
else63
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.width81
)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: Double94
) {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
}106
}