Scoop API

scoop:scoop.ui.component:0.1.0

src/conversation/attachment_layout.scoop

Selected production source · src/conversation/attachment_layout.scoop

1package scoop.ui.component
3import scoop.graphics.*
4import scoop.ui.*
5import scoop.ui.style.*
7internal class AttachmentMediaFrameProps(
8 val image: Boolean,
9 val background: Color,
10 val radius: Double
13internal class AttachmentMediaFrame(
14 private val props: Props<AttachmentMediaFrameProps>,
15 scope: ComponentScope
16): Element<Unit>(scope) {
17 private val painter = scope.resource { StylePainter() }
18 public override fun measure(context: MeasureContext, constraints: Constraints): LayoutResult<
19 Unit
20 > {
21 val size = constraints.constrain(Size(0.0, 0.0))
22 val child =
23 context.measureChild(
24 0L,
25 if (props.value.image)
26 Constraints.tight(size)
27 else
28 Constraints(maxWidth = Some(size.width), maxHeight = Some(size.height))
29 )
30 context.placeChild(
31 0L,
32 Point((size.width - child.size.width) / 2.0, (size.height - child.size.height) / 2.0)
33 )
34 return LayoutResult(LayoutMetrics(size), Unit)
35 }
36 public override fun scene(context: SceneContext, measured: Unit) {
37 val input = props.value
38 painter.scene(
39 context,
40 style {
41 this.background = Some(Paint(input.background))
42 this.corners = Corners.all(input.radius)
43 this.clipContent = true
44 }
45 ) { scene -> scene.children() }
46 }
49internal class AttachmentLayoutProps(
50 val orientation: Orientation,
51 val media: Double,
52 val gap: Double,
53 val actions: Boolean,
54 val density: Double
57internal class AttachmentLayout(
58 private val props: Props<AttachmentLayoutProps>,
59 scope: ComponentScope
60): Element<Unit>(scope) {
61 public override fun measure(context: MeasureContext, constraints: Constraints): LayoutResult<
62 Unit
63 > {
64 val input = props.value
65 if (input.orientation == Orientation.Vertical)
66 return vertical(context, constraints)
67 val mediaSide = upperBound(input.media, constraints.maxWidth ?: input.media)
68 context.measureChild(0L, Constraints.tight(Size(mediaSide, mediaSide)))
69 val action =
70 if (input.actions)
71 conversationMeasure(context, 2L, constraints.maxWidth)
72 else {
73 context.skipChild(2L);
74 LayoutMetrics(Size(0.0, 0.0))
75 }
76 val hasActions = input.actions && action.size.width > 0.0
77 val stack =
78 hasActions &&
79 (constraints.maxWidth ?: Double.MAX_VALUE) -
80 mediaSide -
81 input.gap *
82 2.0 -
83 action.size.width <
84 64.0 *
85 input.density
86 val extra =
87 mediaSide +
88 input.gap +
89 if (hasActions && !stack)
90 action.size.width + input.gap
91 else
92 0.0
93 val text = conversationMeasure(context, 1L, insetMaximum(constraints.maxWidth, extra))
94 val naturalWidth =
95 lowerBound(
96 140.0 * input.density,
97 lowerBound(
98 text.size.width + extra,
99 if (stack)
100 action.size.width
101 else
102 0.0
103 )
104 )
105 val rowHeight =
106 lowerBound(
107 mediaSide,
108 lowerBound(
109 text.size.height,
110 if (!stack)
111 action.size.height
112 else
113 0.0
114 )
115 )
116 val size =
117 constraints.constrain(
118 Size(
119 naturalWidth,
120 rowHeight +
121 if (stack)
122 input.gap + action.size.height
123 else
124 0.0
125 )
126 )
127 context.placeChild(
128 0L,
129 Point(
130 logicalX(0.0, mediaSide, size.width, context.layoutDirection),
131 (rowHeight - mediaSide) / 2.0
132 )
133 )
134 context.placeChild(
135 1L,
136 Point(
137 logicalX(
138 mediaSide + input.gap,
139 text.size.width,
140 size.width,
141 context.layoutDirection
142 ),
143 (rowHeight - text.size.height) / 2.0
144 )
145 )
146 if (hasActions)
147 context.placeChild(
148 2L,
149 Point(
150 logicalX(
151 lowerBound(0.0, size.width - action.size.width),
152 action.size.width,
153 size.width,
154 context.layoutDirection
155 ),
156 if (stack)
157 rowHeight + input.gap
158 else
159 (rowHeight - action.size.height) / 2.0
160 )
161 )
162 return LayoutResult(LayoutMetrics(size), Unit)
163 }
164 private fun vertical(context: MeasureContext, constraints: Constraints): LayoutResult<Unit> {
165 val input = props.value
166 val width = constraints.constrain(Size(102.0 * input.density, 0.0)).width
167 context.measureChild(0L, Constraints.tight(Size(width, width)))
168 val body = context.measureChild(1L, Constraints(maxWidth = Some(width)))
169 val action =
170 if (input.actions)
171 conversationMeasure(context, 2L, Some(width))
172 else {
173 context.skipChild(2L);
174 LayoutMetrics(Size(0.0, 0.0))
175 }
176 val size = constraints.constrain(Size(width, width + input.gap + body.size.height))
177 context.placeChild(0L, Point(0.0, 0.0))
178 context.placeChild(
179 1L,
180 Point(
181 logicalX(0.0, body.size.width, size.width, context.layoutDirection),
182 width + input.gap
183 )
184 )
185 if (input.actions)
186 context.placeChild(
187 2L,
188 Point(
189 logicalX(
190 lowerBound(0.0, width - action.size.width - 4.0 * input.density),
191 action.size.width,
192 size.width,
193 context.layoutDirection
194 ),
195 upperBound(4.0 * input.density, lowerBound(0.0, width - action.size.height))
196 )
197 )
198 return LayoutResult(LayoutMetrics(size), Unit)
199 }
200 public override fun scene(context: SceneContext, measured: Unit) {
201 context.children()
202 }
205internal class AttachmentOutlineProps(
206 val idle: Boolean,
207 val color: Color,
208 val radius: Double,
209 val inset: Double
212internal class AttachmentOutline(
213 private val props: Props<AttachmentOutlineProps>,
214 scope: ComponentScope
215): Element<Unit>(scope) {
216 private val outline = scope.resource { EmptyBorderCache() }
217 public override fun measure(context: MeasureContext, constraints: Constraints): LayoutResult<
218 Unit
219 > {
220 val child = context.measureChild(0L, constraints);
221 context.placeChild(0L, Point(0.0, 0.0))
222 return LayoutResult(child, Unit)
223 }
224 public override fun scene(context: SceneContext, measured: Unit) {
225 val input = props.value
226 val size = context.size
227 context.children()
228 val bounds =
229 Rect(
230 input.inset + 0.5,
231 input.inset + 0.5,
232 lowerBound(0.0, size.width - input.inset * 2.0 - 1.0),
233 lowerBound(0.0, size.height - input.inset * 2.0 - 1.0)
234 )
235 if (input.idle && bounds.width > 0.0 && bounds.height > 0.0) {
236 val path =
237 outline.get(
238 RoundedOutline(bounds, CornerRadii.all(lowerBound(0.0, input.radius - 0.5)))
239 )
240 context.draw {
241 paint -> paint.canvas.drawPath(path, Paint(input.color, PaintingStyle.Stroke, 1.0))
242 }
243 }
244 }
247private class AttachmentFileIconProps(val size: Double, val color: Color)
249internal fun attachmentFileIcon(size: Double, color: Color): View =
250 element(
251 AttachmentFileIconProps(size, color),
252 { current, scope -> AttachmentFileIcon(current, scope) }
253 )
255private class AttachmentFileIcon(
256 private val props: Props<AttachmentFileIconProps>,
257 scope: ComponentScope
258): Element<Unit>(scope) {
259 public override fun measure(context: MeasureContext, constraints: Constraints): LayoutResult<
260 Unit
261 > =
262 LayoutResult(
263 LayoutMetrics(constraints.constrain(Size(props.value.size, props.value.size))),
264 Unit
265 )
266 public override fun scene(context: SceneContext, measured: Unit) {
267 val size = context.size;
268 val color = props.value.color
269 context.draw {
270 paint ->
271 val canvas = paint.canvas
272 canvas.scale(size.width / 24.0, size.height / 24.0)
273 val stroke = Paint(color, PaintingStyle.Stroke, 1.7, StrokeCap.Round, StrokeJoin.Round)
274 canvas.drawLine(Point(6.0, 3.0), Point(14.0, 3.0), stroke)
275 canvas.drawLine(Point(14.0, 3.0), Point(19.0, 8.0), stroke)
276 canvas.drawLine(Point(19.0, 8.0), Point(19.0, 21.0), stroke)
277 canvas.drawLine(Point(19.0, 21.0), Point(6.0, 21.0), stroke)
278 canvas.drawLine(Point(6.0, 21.0), Point(6.0, 3.0), stroke)
279 canvas.drawLine(Point(14.0, 3.0), Point(14.0, 8.0), stroke)
280 canvas.drawLine(Point(14.0, 8.0), Point(19.0, 8.0), stroke)
281 }
282 }