Scoop API

scoop:scoop.ui.component:0.1.0

src/content/avatar.scoop

Selected production source · src/content/avatar.scoop

1package scoop.ui.component
3import scoop.graphics.*
4import scoop.graphics.text.*
5import scoop.ui.*
6import scoop.ui.style.*
7import scoop.window.accessibility.*
9public enum AvatarSize {
10 Sm, Default, Lg
13public enum AvatarImageState {
14 Empty, Loading, Failed, Ready(Image)
17public enum AvatarBadge {
18 None, Dot(Color?), Content(View, Color?)
21public class AvatarProps public constructor(
22 public val fallback: PanelContent,
23 public val image: AvatarImageState = AvatarImageState.Empty,
24 public val size: AvatarSize = AvatarSize.Default,
25 public val badge: AvatarBadge = AvatarBadge.None,
26 public val fonts: FontCollection? = None,
27 public val theme: ThemeSource? = None,
28 public val style: StyleOverride = styleOverride(),
29 public val semantics: ComponentSemantics = ComponentSemantics()
32public class Avatar public constructor(
33 private val props: Props<AvatarProps>,
34 scope: ComponentScope
35): Component(scope) {
36 private val themes = ThemeAccess(scope)
37 private val captions = ActionTextStyle()
38 public override fun onMount() {
39 themes.mount(props.value.theme)
40 }
41 public override fun render(): View {
42 val input = props.value
43 val theme = themes.read(input.theme)
44 val palette = theme.tokens.palette
45 val density = theme.tokens.typography.body.fontSize / 14.0
46 val text =
47 captions.resolve(
48 theme.tokens.typography.body,
49 palette.muted.foreground,
50 None,
51 if (input.size == AvatarSize.Sm)
52 12.0 / 14.0
53 else
54 1.0
55 )
56 val content =
57 when (input.image) {
58 case Ready(val image) -> imageView(
59 image,
60 Size(image.width.toDouble(), image.height.toDouble())
61 ) { this.fit = ContentFit.Cover }
62 else -> panelCaption(input.fallback, text, input.fonts, true)
63 }
64 val ready =
65 when (input.image) {
66 case Ready(_) -> true;
67 else -> false
68 }
69 val badge =
70 when (input.badge) {
71 case None -> None
72 case Dot(val color) -> Some(
73 avatarBadge(
74 empty(),
75 avatarBadgeSide(input.size) * density,
76 color ?: palette.primary.background,
77 palette.background.background
78 )
79 )
80 case Content(val view, val color) -> Some(
81 avatarBadge(
82 if (input.size == AvatarSize.Sm)
83 empty()
84 else
85 view,
86 avatarBadgeSide(input.size) * density,
87 color ?: palette.primary.background,
88 palette.background.background
89 )
90 )
91 }
92 val children = ArrayList<View>()
93 children.add(
94 panelSlot(
95 element(
96 AvatarInkProps(
97 avatarSide(input.size) * density,
98 ready,
99 palette.muted.background,
100 palette.border,
101 theme.tokens.mode,
102 input.style
103 ),
104 { current, mount -> AvatarInk(current, scope = mount) },
105 [content]
106 ),
107 0L
108 )
109 )
110 when (badge) {
111 case Some(val value) -> children.add(panelSlot(value, 1L));
112 case None -> {}
113 }
114 return semanticContent(
115 element(
116 panelPresent(badge),
117 { current, mount -> AvatarLayout(current, scope = mount) },
118 children.toArray()
119 ),
120 SemanticRole.Image,
121 input.semantics.properties().copy {
122 if (name == None && input.semantics.relations.labelledBy.size == 0L)
123 name = candidateCaption(input.fallback)
124 },
125 relations = input.semantics.relations,
126 reference = input.semantics.reference
127 )
128 }
131internal fun avatarSide(size: AvatarSize): Double =
132 when (size) {
133 case Sm -> 24.0;
134 case Default -> 32.0;
135 case Lg -> 40.0
136 }
138internal fun avatarBadgeSide(size: AvatarSize): Double =
139 when (size) {
140 case Sm -> 8.0;
141 case Default -> 10.0;
142 case Lg -> 12.0
143 }
145private fun avatarBadge(content: View, side: Double, color: Color, ring: Color): View =
146 avatarRing(
147 frame(content) {
148 this.width = Some(side)
149 this.height = Some(side)
150 this.padding = Some(Insets())
151 this.style =
152 styleOverride {
153 this.background(Some(Paint(color)))
154 this.corners(Corners.all(side / 2.0))
155 this.clipContent(true)
156 }
157 },
158 ring
159 )
161public class AvatarBuilder internal constructor() {
162 public var image: AvatarImageState = AvatarImageState.Empty
163 public var size: AvatarSize = AvatarSize.Default
164 public var badge: AvatarBadge = AvatarBadge.None
165 public var fonts: FontCollection? = None
166 public var theme: ThemeSource? = None
167 public var style: StyleOverride = styleOverride()
168 public var key: ViewKey = ViewKey.Position
169 public var semantics: ComponentSemantics = ComponentSemantics()
170 public fun badge(color: Color? = None): Unit {
171 badge = AvatarBadge.Dot(color)
172 }
173 public fun badge(content: View, color: Color? = None): Unit {
174 badge = AvatarBadge.Content(content, color)
175 }
176 public fun style(configure: StyleOverrideBuilder.() -> Unit): Unit {
177 style = style.copy(configure)
178 }
179 internal fun snapshot(fallback: PanelContent): AvatarProps =
180 AvatarProps(fallback, image, size, badge, fonts, theme, style, semantics)
183public fun avatar(fallback: String, configure: AvatarBuilder.() -> Unit = {}): View =
184 makeAvatar(PanelContent.Label(LabelProps(LabelText.Plain(fallback, None))), configure)
186public fun avatar(fallback: StyledText, configure: AvatarBuilder.() -> Unit = {}): View =
187 makeAvatar(PanelContent.Label(LabelProps(LabelText.Styled(fallback))), configure)
189public fun avatar(fallback: View, configure: AvatarBuilder.() -> Unit = {}): View =
190 makeAvatar(PanelContent.Custom(fallback), configure)
192internal fun makeAvatar(fallback: PanelContent, configure: AvatarBuilder.() -> Unit): View {
193 val builder = AvatarBuilder()
194 builder.configure()
195 return component(
196 builder.snapshot(fallback),
197 { current, mount -> Avatar(current, scope = mount) },
198 builder.key
199 )