scoop:scoop.ui.component:0.1.0
src/content/avatar.scoop
Selected production source · src/content/avatar.scoop
1
package scoop.ui.component3
import scoop.graphics.*4
import scoop.graphics.text.*5
import scoop.ui.*6
import scoop.ui.style.*7
import scoop.window.accessibility.*9
public enum AvatarSize {10
Sm, Default, Lg11
}13
public enum AvatarImageState {14
Empty, Loading, Failed, Ready(Image)15
}17
public enum AvatarBadge {18
None, Dot(Color?), Content(View, Color?)19
}21
public 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()30
)32
public class Avatar public constructor(33
private val props: Props<AvatarProps>,34
scope: ComponentScope35
): 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.value43
val theme = themes.read(input.theme)44
val palette = theme.tokens.palette45
val density = theme.tokens.typography.body.fontSize / 14.046
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.053
else54
1.055
)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 -> false68
}69
val badge =70
when (input.badge) {71
case None -> None72
case Dot(val color) -> Some(73
avatarBadge(74
empty(),75
avatarBadgeSide(input.size) * density,76
color ?: palette.primary.background,77
palette.background.background78
)79
)80
case Content(val view, val color) -> Some(81
avatarBadge(82
if (input.size == AvatarSize.Sm)83
empty()84
else85
view,86
avatarBadgeSide(input.size) * density,87
color ?: palette.primary.background,88
palette.background.background89
)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.style103
),104
{ current, mount -> AvatarInk(current, scope = mount) },105
[content]106
),107
0L108
)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.reference127
)128
}129
}131
internal fun avatarSide(size: AvatarSize): Double =132
when (size) {133
case Sm -> 24.0;134
case Default -> 32.0;135
case Lg -> 40.0136
}138
internal fun avatarBadgeSide(size: AvatarSize): Double =139
when (size) {140
case Sm -> 8.0;141
case Default -> 10.0;142
case Lg -> 12.0143
}145
private 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
ring159
)161
public class AvatarBuilder internal constructor() {162
public var image: AvatarImageState = AvatarImageState.Empty163
public var size: AvatarSize = AvatarSize.Default164
public var badge: AvatarBadge = AvatarBadge.None165
public var fonts: FontCollection? = None166
public var theme: ThemeSource? = None167
public var style: StyleOverride = styleOverride()168
public var key: ViewKey = ViewKey.Position169
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)181
}183
public fun avatar(fallback: String, configure: AvatarBuilder.() -> Unit = {}): View =184
makeAvatar(PanelContent.Label(LabelProps(LabelText.Plain(fallback, None))), configure)186
public fun avatar(fallback: StyledText, configure: AvatarBuilder.() -> Unit = {}): View =187
makeAvatar(PanelContent.Label(LabelProps(LabelText.Styled(fallback))), configure)189
public fun avatar(fallback: View, configure: AvatarBuilder.() -> Unit = {}): View =190
makeAvatar(PanelContent.Custom(fallback), configure)192
internal 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.key199
)200
}