Scoop API

scoop:scoop.ui.component:0.1.0

src/conversation/bubble_layout.scoop

Selected production source · src/conversation/bubble_layout.scoop

1package scoop.ui.component
3import scoop.graphics.*
4import scoop.ui.*
6internal class BubbleLayoutProps(
7 val align: MessageAlignment,
8 val fraction: Double,
9 val reactions: BubbleReactions?,
10 val actions: Boolean,
11 val enabled: Boolean,
12 val density: Double
15internal class BubbleLayout(
16 private val props: Props<BubbleLayoutProps>,
17 scope: ComponentScope
18): Element<Unit>(scope) {
19 private val area = scope.createArea()
20 init {
21 scope.routeChildren(area)
22 }
23 public override fun update(): UiInvalidation {
24 area.scope.enabled = props.value.enabled
25 return UiInvalidation.Layout
26 }
27 public override fun measure(context: MeasureContext, constraints: Constraints): LayoutResult<
28 Unit
29 > {
30 val input = props.value
31 val maximum =
32 when (constraints.maxWidth) {
33 case Some(val width) -> Some(width * input.fraction)
34 case None -> None
35 }
36 val body = conversationMeasure(context, 0L, maximum, constraints.maxHeight)
37 val reaction =
38 if (panelPresent(input.reactions))
39 conversationMeasure(context, 1L, maximum)
40 else {
41 context.skipChild(1L);
42 LayoutMetrics(Size(0.0, 0.0))
43 }
44 val overhang = reaction.size.height * 0.75
45 val top =
46 if (input.reactions?.side == Some(BubbleReactionSide.Top))
47 overhang
48 else
49 0.0
50 val bottom =
51 if (input.reactions?.side == Some(BubbleReactionSide.Bottom))
52 overhang
53 else
54 0.0
55 val action =
56 if (input.actions)
57 conversationMeasure(context, 2L, maximum)
58 else {
59 context.skipChild(2L);
60 LayoutMetrics(Size(0.0, 0.0))
61 }
62 val contentWidth =
63 lowerBound(body.size.width, lowerBound(reaction.size.width, action.size.width))
64 val height =
65 top +
66 body.size.height +
67 bottom +
68 if (input.actions)
69 4.0 * input.density + action.size.height
70 else
71 0.0
72 val size = constraints.constrain(Size(constraints.maxWidth ?: contentWidth, height))
73 val x = conversationOffset(input.align, size.width, body.size.width)
74 context.placeChild(
75 0L,
76 Point(logicalX(x, body.size.width, size.width, context.layoutDirection), top)
77 )
78 when (input.reactions) {
79 case Some(val value) -> {
80 val ideal =
81 x +
82 if (value.align == MessageAlignment.Start)
83 12.0 * input.density
84 else
85 body.size.width - reaction.size.width - 12.0 * input.density
86 val start =
87 upperBound(
88 lowerBound(0.0, ideal),
89 lowerBound(0.0, size.width - reaction.size.width)
90 )
91 context.placeChild(
92 1L,
93 Point(
94 logicalX(start, reaction.size.width, size.width, context.layoutDirection),
95 if (value.side == BubbleReactionSide.Top)
96 0.0
97 else
98 top + body.size.height - reaction.size.height * 0.25
99 )
100 )
101 }
102 case None -> {}
103 }
104 if (input.actions)
105 context.placeChild(
106 2L,
107 Point(
108 logicalX(
109 conversationOffset(input.align, size.width, action.size.width),
110 action.size.width,
111 size.width,
112 context.layoutDirection
113 ),
114 top + body.size.height + bottom + 4.0 * input.density
115 )
116 )
117 return LayoutResult(LayoutMetrics(size), Unit)
118 }
119 public override fun scene(context: SceneContext, measured: Unit) {
120 context.place(area, Rect(0.0, 0.0, context.size.width, context.size.height))
121 context.children()
122 }