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 theme = theme(cx);
26
27        let mut img = img();
28
29        if self.shape == Shape::Circle {
30            img = img.rounded_full();
31        } else {
32            img = img.rounded_md();
33        }
34
35        img.uri(self.src.clone())
36            .size_4()
37            .bg(theme.image_fallback_background)
38    }
39}
40
41#[cfg(feature = "stories")]
42pub use stories::*;
43
44#[cfg(feature = "stories")]
45mod stories {
46    use super::*;
47    use crate::Story;
48    use gpui2::{Div, Render};
49
50    pub struct AvatarStory;
51
52    impl Render for AvatarStory {
53        type Element = Div<Self>;
54
55        fn render(&mut self, cx: &mut ViewContext<Self>) -> Self::Element {
56            Story::container(cx)
57                .child(Story::title_for::<_, Avatar>(cx))
58                .child(Story::label(cx, "Default"))
59                .child(Avatar::new(
60                    "https://avatars.githubusercontent.com/u/1714999?v=4",
61                ))
62                .child(Story::label(cx, "Rounded rectangle"))
63                .child(
64                    Avatar::new("https://avatars.githubusercontent.com/u/1714999?v=4")
65                        .shape(Shape::RoundedRectangle),
66                )
67        }
68    }
69}