Scoop API

scoop:scoop.ui.component:0.1.0

src/content/avatar_group.scoop

Selected production source · src/content/avatar_group.scoop

1package scoop.ui.component
3import scoop.graphics.*
4import scoop.graphics.text.*
5import scoop.math.min
6import scoop.math.max
7import scoop.ui.*
8import scoop.ui.style.*
10private class AvatarGroupEntry(val content: View, val key: ViewKey)
12public class AvatarGroupBuilder internal constructor(private val size: AvatarSize) {
13 private val entries = ArrayList<AvatarGroupEntry>()
14 public var overlap: Double = 8.0
15 public var key: ViewKey = ViewKey.Position
16 public fun avatar(fallback: String, configure: AvatarBuilder.() -> Unit = {}): Unit {
17 append(PanelContent.Label(LabelProps(LabelText.Plain(fallback, None))), configure)
18 }
19 public fun avatar(fallback: StyledText, configure: AvatarBuilder.() -> Unit = {}): Unit {
20 append(PanelContent.Label(LabelProps(LabelText.Styled(fallback))), configure)
21 }
22 public fun avatar(fallback: View, configure: AvatarBuilder.() -> Unit = {}): Unit {
23 append(PanelContent.Custom(fallback), configure)
24 }
25 public fun count(value: Long, key: ViewKey = ViewKey.Position): Unit {
26 requireComponent(value >= 0L, "avatar group count must be nonnegative")
27 avatar(f"+${value}") { this.key = key }
28 }
29 public fun count(content: View, key: ViewKey = ViewKey.Position): Unit {
30 avatar(content) { this.key = key }
31 }
32 private fun append(fallback: PanelContent, configure: AvatarBuilder.() -> Unit) {
33 val builder = AvatarBuilder()
34 builder.size = size
35 builder.configure()
36 entries.add(
37 AvatarGroupEntry(
38 component(
39 builder.snapshot(fallback),
40 { current, mount -> Avatar(current, scope = mount) }
41 ),
42 builder.key
43 )
44 )
45 }
46 internal fun build(): View {
47 nonnegative(overlap, "invalid avatar group overlap")
48 return component(
49 AvatarGroupProps(entries.toArray(), overlap),
50 { current, mount -> AvatarGroup(current, scope = mount) },
51 key
52 )
53 }
56public fun avatarGroup(
57 size: AvatarSize = AvatarSize.Default,
58 configure: AvatarGroupBuilder.() -> Unit
59): View = makeAvatarGroup(size, configure)
61internal fun makeAvatarGroup(size: AvatarSize, configure: AvatarGroupBuilder.() -> Unit): View {
62 val builder = AvatarGroupBuilder(size)
63 builder.configure()
64 return builder.build()
67private class AvatarGroupProps(val entries: Array<AvatarGroupEntry>, val overlap: Double)
69private class AvatarGroup(
70 private val props: Props<AvatarGroupProps>,
71 scope: ComponentScope
72): Component(scope) {
73 private val themes = ThemeAccess(scope)
74 public override fun onMount() {
75 themes.mount(None)
76 }
77 public override fun render(): View {
78 val color = themes.read(None).tokens.palette.background.background
79 val input = props.value
80 val children =
81 Array<View>(input.entries.size) {
82 index ->
83 val entry = input.entries[index]
84 avatarRing(
85 entry.content,
86 color,
87 if (entry.key == ViewKey.Position)
88 ViewKey.Number(index)
89 else
90 entry.key
91 )
92 }
93 return element(
94 AvatarGroupLayoutProps(input.entries.size, input.overlap),
95 { current, mount -> AvatarGroupLayout(current, scope = mount) },
96 children
97 )
98 }
101private struct AvatarGroupLayoutProps(val count: Long, val overlap: Double)
103private class AvatarGroupLayout(
104 private val props: Props<AvatarGroupLayoutProps>,
105 scope: ComponentScope
106): Element<Unit>(scope) {
107 public override fun measure(context: MeasureContext, constraints: Constraints): LayoutResult<
108 Unit
109 > {
110 val input = props.value
111 if (input.count == 0L)
112 return LayoutResult(LayoutMetrics(constraints.constrain(Size(0.0, 0.0))), Unit)
113 val children =
114 Array<Size>(input.count) {
115 index -> context.measureChild(
116 index,
117 Constraints(
118 maxWidth = insetMaximum(constraints.maxWidth, 4.0),
119 maxHeight = insetMaximum(constraints.maxHeight, 4.0)
120 )
121 ).size
122 }
123 val positions = avatarGroupPositions(children, input.overlap)
124 var height = 0.0
125 var width = 0.0
126 for (index in 0L ..< children.size) {
127 width = max(width, positions[index] + children[index].width)
128 height = max(height, children[index].height)
129 }
130 val natural = Size(width + 4.0, height + 4.0)
131 validSize(natural)
132 val size = constraints.constrain(natural)
133 for (index in 0L ..< children.size) {
134 val child = children[index]
135 context.placeChild(
136 index,
137 Point(
138 if (context.layoutDirection == LayoutDirection.Rtl)
139 size.width - 2.0 - positions[index] - child.width
140 else
141 2.0 + positions[index],
142 (size.height - child.height) / 2.0
143 )
144 )
145 }
146 return LayoutResult(LayoutMetrics(size), Unit)
147 }
148 public override fun scene(context: SceneContext, measured: Unit) {
149 context.children()
150 }
153internal fun avatarGroupPositions(children: Array<Size>, overlap: Double): Array<Double> {
154 val positions = ArrayList<Double>()
155 var x = 0.0
156 for (index in 0L ..< children.size) {
157 if (index > 0L)
158 x += children[index - 1L].width -
159 min(overlap, min(children[index - 1L].width, children[index].width))
160 requireComponent(x.isFinite(), "avatar group geometry is not representable")
161 positions.add(x)
162 }
163 return positions.toArray()