scoop:scoop.ui.component:0.1.0
src/content/avatar_ink.scoop
Selected production source · src/content/avatar_ink.scoop
1
package scoop.ui.component3
import scoop.graphics.*4
import scoop.math.min5
import scoop.ui.*6
import scoop.ui.style.*8
internal class AvatarInkProps(9
val side: Double,10
val ready: Boolean,11
val background: Color,12
val border: Color,13
val mode: ThemeMode,14
val style: StyleOverride15
)17
internal class AvatarInk(private val props: Props<AvatarInkProps>, scope: ComponentScope): Element<18
Unit19
>(scope) {20
private var painter: StylePainter? = None21
public override fun onMount() {22
painter = Some(scope.resource { StylePainter() })23
}24
public override fun measure(context: MeasureContext, constraints: Constraints): LayoutResult<25
Unit26
> {27
val input = props.value28
val size = constraints.constrain(Size(input.side, input.side))29
val child =30
context.measureChild(31
0L,32
if (input.ready)33
Constraints.tight(size)34
else35
Constraints(maxWidth = Some(size.width), maxHeight = Some(size.height))36
)37
context.placeChild(38
0L,39
Point((size.width - child.size.width) / 2.0, (size.height - child.size.height) / 2.0)40
)41
return LayoutResult(LayoutMetrics(size), Unit)42
}43
public override fun scene(context: SceneContext, measured: Unit) {44
val input = props.value45
val size = context.size46
val direction = context.layoutDirection47
val visual =48
input.style.resolve(49
style {50
this.background =51
if (input.ready)52
None53
else54
Some(Paint(input.background))55
this.corners = Corners.all(min(size.width, size.height) / 2.0)56
this.clipContent = true57
}58
)59
painter!!.scene(context, visual) { scene -> scene.children() }60
val blendBorder =61
when (input.style.border) {62
case Inherit -> true;63
case Set(_) -> false64
}65
if (blendBorder && size.width > 1.0 && size.height > 1.0) {66
val outline =67
RoundedOutline(68
Rect(0.0, 0.0, size.width, size.height),69
visual.corners.resolve(direction)70
).inset(0.5, 0.5, 0.5, 0.5)71
context.draw {72
paint -> paint.canvas.withState {73
canvas ->74
canvas.saveLayer(75
blendMode =76
if (input.mode == ThemeMode.Dark)77
BlendMode.Lighten78
else79
BlendMode.Darken80
)81
canvas.drawRoundRect(82
outline.bounds,83
outline.radii,84
Paint(input.border, PaintingStyle.Stroke, 1.0)85
)86
canvas.restore()87
}88
}89
}90
}91
}93
internal class AvatarLayout(private val props: Props<Boolean>, scope: ComponentScope): Element<94
Unit95
>(scope) {96
public override fun measure(context: MeasureContext, constraints: Constraints): LayoutResult<97
Unit98
> {99
val face = context.measureChild(0L, constraints)100
context.placeChild(0L, Point(0.0, 0.0))101
if (props.value) {102
val badge = context.measureChild(1L, Constraints())103
context.placeChild(104
1L,105
Point(106
if (context.layoutDirection == LayoutDirection.Rtl)107
0.0108
else109
face.size.width - badge.size.width,110
face.size.height - badge.size.height111
)112
)113
}114
return LayoutResult(face, Unit)115
}116
public override fun scene(context: SceneContext, measured: Unit) {117
context.children()118
}119
}121
internal fun avatarRing(content: View, color: Color, key: ViewKey = ViewKey.Position): View =122
element(color, { current, mount -> AvatarRing(current, scope = mount) }, [content], key)124
private class AvatarRing(private val props: Props<Color>, scope: ComponentScope): Element<Unit>(125
scope126
) {127
public override fun measure(context: MeasureContext, constraints: Constraints): LayoutResult<128
Unit129
> {130
val child = context.measureChild(0L, constraints)131
context.placeChild(0L, Point(0.0, 0.0))132
return LayoutResult(child, Unit)133
}134
public override fun scene(context: SceneContext, measured: Unit) {135
val size = context.size136
val radius = min(size.width, size.height) / 2.0 + 1.0137
if (size.width > 0.0 && size.height > 0.0)138
context.draw {139
paint -> paint.canvas.drawRoundRect(140
Rect(-1.0, -1.0, size.width + 2.0, size.height + 2.0),141
radius,142
radius,143
Paint(props.value, PaintingStyle.Stroke, 2.0)144
)145
}146
context.children()147
}148
}