Scoop API

scoop:scoop.ui.component:0.1.0

src/content/avatar_ink.scoop

Selected production source · src/content/avatar_ink.scoop

1package scoop.ui.component
3import scoop.graphics.*
4import scoop.math.min
5import scoop.ui.*
6import scoop.ui.style.*
8internal 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: StyleOverride
17internal class AvatarInk(private val props: Props<AvatarInkProps>, scope: ComponentScope): Element<
18 Unit
19>(scope) {
20 private var painter: StylePainter? = None
21 public override fun onMount() {
22 painter = Some(scope.resource { StylePainter() })
23 }
24 public override fun measure(context: MeasureContext, constraints: Constraints): LayoutResult<
25 Unit
26 > {
27 val input = props.value
28 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 else
35 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.value
45 val size = context.size
46 val direction = context.layoutDirection
47 val visual =
48 input.style.resolve(
49 style {
50 this.background =
51 if (input.ready)
52 None
53 else
54 Some(Paint(input.background))
55 this.corners = Corners.all(min(size.width, size.height) / 2.0)
56 this.clipContent = true
57 }
58 )
59 painter!!.scene(context, visual) { scene -> scene.children() }
60 val blendBorder =
61 when (input.style.border) {
62 case Inherit -> true;
63 case Set(_) -> false
64 }
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.Lighten
78 else
79 BlendMode.Darken
80 )
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 }
93internal class AvatarLayout(private val props: Props<Boolean>, scope: ComponentScope): Element<
94 Unit
95>(scope) {
96 public override fun measure(context: MeasureContext, constraints: Constraints): LayoutResult<
97 Unit
98 > {
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.0
108 else
109 face.size.width - badge.size.width,
110 face.size.height - badge.size.height
111 )
112 )
113 }
114 return LayoutResult(face, Unit)
115 }
116 public override fun scene(context: SceneContext, measured: Unit) {
117 context.children()
118 }
121internal fun avatarRing(content: View, color: Color, key: ViewKey = ViewKey.Position): View =
122 element(color, { current, mount -> AvatarRing(current, scope = mount) }, [content], key)
124private class AvatarRing(private val props: Props<Color>, scope: ComponentScope): Element<Unit>(
125 scope
126) {
127 public override fun measure(context: MeasureContext, constraints: Constraints): LayoutResult<
128 Unit
129 > {
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.size
136 val radius = min(size.width, size.height) / 2.0 + 1.0
137 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 }