1use gpui::img;
2
3use crate::prelude::*;
4
5#[derive(Component)]
6pub struct Avatar {
7 src: SharedString,
8 shape: Shape,
9}
10
11impl Avatar {
12 pub fn new(src: impl Into<SharedString>) -> Self {
13 Self {
14 src: src.into(),
15 shape: Shape::Circle,
16 }
17 }
18
19 pub fn shape(mut self, shape: Shape) -> Self {
20 self.shape = shape;
21 self
22 }
23
24 fn render<V: 'static>(self, _view: &mut V, cx: &mut ViewContext<V>) -> impl Component<V> {
25 let mut img = img();
26
27 if self.shape == Shape::Circle {
28 img = img.rounded_full();
29 } else {
30 img = img.rounded_md();
31 }
32
33 img.uri(self.src.clone())
34 .size_4()
35 // todo!(Pull the avatar fallback background from the theme.)
36 .bg(gpui::red())
37 }
38}
39
40#[cfg(feature = "stories")]
41pub use stories::*;
42
43#[cfg(feature = "stories")]
44mod stories {
45 use super::*;
46 use crate::Story;
47 use gpui::{Div, Render};
48
49 pub struct AvatarStory;
50
51 impl Render for AvatarStory {
52 type Element = Div<Self>;
53
54 fn render(&mut self, cx: &mut ViewContext<Self>) -> Self::Element {
55 Story::container(cx)
56 .child(Story::title_for::<_, Avatar>(cx))
57 .child(Story::label(cx, "Default"))
58 .child(Avatar::new(
59 "https://avatars.githubusercontent.com/u/1714999?v=4",
60 ))
61 .child(Avatar::new(
62 "https://avatars.githubusercontent.com/u/326587?v=4",
63 ))
64 // .child(Avatar::new(
65 // "https://avatars.githubusercontent.com/u/326587?v=4",
66 // ))
67 // .child(Avatar::new(
68 // "https://avatars.githubusercontent.com/u/482957?v=4",
69 // ))
70 // .child(Avatar::new(
71 // "https://avatars.githubusercontent.com/u/1714999?v=4",
72 // ))
73 // .child(Avatar::new(
74 // "https://avatars.githubusercontent.com/u/1486634?v=4",
75 // ))
76 .child(Story::label(cx, "Rounded rectangle"))
77 // .child(
78 // Avatar::new("https://avatars.githubusercontent.com/u/1714999?v=4")
79 // .shape(Shape::RoundedRectangle),
80 // )
81 }
82 }
83}