scoop:scoop.ui.component:0.1.0
src/conversation/attachment_layout.scoop
Selected production source · src/conversation/attachment_layout.scoop
1
package scoop.ui.component3
import scoop.graphics.*4
import scoop.ui.*5
import scoop.ui.style.*7
internal class AttachmentMediaFrameProps(8
val image: Boolean,9
val background: Color,10
val radius: Double11
)13
internal class AttachmentMediaFrame(14
private val props: Props<AttachmentMediaFrameProps>,15
scope: ComponentScope16
): Element<Unit>(scope) {17
private val painter = scope.resource { StylePainter() }18
public override fun measure(context: MeasureContext, constraints: Constraints): LayoutResult<19
Unit20
> {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
else28
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.value38
painter.scene(39
context,40
style {41
this.background = Some(Paint(input.background))42
this.corners = Corners.all(input.radius)43
this.clipContent = true44
}45
) { scene -> scene.children() }46
}47
}49
internal class AttachmentLayoutProps(50
val orientation: Orientation,51
val media: Double,52
val gap: Double,53
val actions: Boolean,54
val density: Double55
)57
internal class AttachmentLayout(58
private val props: Props<AttachmentLayoutProps>,59
scope: ComponentScope60
): Element<Unit>(scope) {61
public override fun measure(context: MeasureContext, constraints: Constraints): LayoutResult<62
Unit63
> {64
val input = props.value65
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.077
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.density86
val extra =87
mediaSide +88
input.gap +89
if (hasActions && !stack)90
action.size.width + input.gap91
else92
0.093
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.width101
else102
0.0103
)104
)105
val rowHeight =106
lowerBound(107
mediaSide,108
lowerBound(109
text.size.height,110
if (!stack)111
action.size.height112
else113
0.0114
)115
)116
val size =117
constraints.constrain(118
Size(119
naturalWidth,120
rowHeight +121
if (stack)122
input.gap + action.size.height123
else124
0.0125
)126
)127
context.placeChild(128
0L,129
Point(130
logicalX(0.0, mediaSide, size.width, context.layoutDirection),131
(rowHeight - mediaSide) / 2.0132
)133
)134
context.placeChild(135
1L,136
Point(137
logicalX(138
mediaSide + input.gap,139
text.size.width,140
size.width,141
context.layoutDirection142
),143
(rowHeight - text.size.height) / 2.0144
)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.layoutDirection155
),156
if (stack)157
rowHeight + input.gap158
else159
(rowHeight - action.size.height) / 2.0160
)161
)162
return LayoutResult(LayoutMetrics(size), Unit)163
}164
private fun vertical(context: MeasureContext, constraints: Constraints): LayoutResult<Unit> {165
val input = props.value166
val width = constraints.constrain(Size(102.0 * input.density, 0.0)).width167
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.gap183
)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.layoutDirection194
),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
}203
}205
internal class AttachmentOutlineProps(206
val idle: Boolean,207
val color: Color,208
val radius: Double,209
val inset: Double210
)212
internal class AttachmentOutline(213
private val props: Props<AttachmentOutlineProps>,214
scope: ComponentScope215
): Element<Unit>(scope) {216
private val outline = scope.resource { EmptyBorderCache() }217
public override fun measure(context: MeasureContext, constraints: Constraints): LayoutResult<218
Unit219
> {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.value226
val size = context.size227
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
}245
}247
private class AttachmentFileIconProps(val size: Double, val color: Color)249
internal fun attachmentFileIcon(size: Double, color: Color): View =250
element(251
AttachmentFileIconProps(size, color),252
{ current, scope -> AttachmentFileIcon(current, scope) }253
)255
private class AttachmentFileIcon(256
private val props: Props<AttachmentFileIconProps>,257
scope: ComponentScope258
): Element<Unit>(scope) {259
public override fun measure(context: MeasureContext, constraints: Constraints): LayoutResult<260
Unit261
> =262
LayoutResult(263
LayoutMetrics(constraints.constrain(Size(props.value.size, props.value.size))),264
Unit265
)266
public override fun scene(context: SceneContext, measured: Unit) {267
val size = context.size;268
val color = props.value.color269
context.draw {270
paint ->271
val canvas = paint.canvas272
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
}283
}