avatar.rs

 1use gpui2::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(gpui2::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 gpui2::{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(Story::label(cx, "Rounded rectangle"))
62                .child(
63                    Avatar::new("https://avatars.githubusercontent.com/u/1714999?v=4")
64                        .shape(Shape::RoundedRectangle),
65                )
66        }
67    }
68}