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 crate::Story;
47
48 use super::*;
49
50 #[derive(Component)]
51 pub struct AvatarStory;
52
53 impl AvatarStory {
54 pub fn new() -> Self {
55 Self
56 }
57
58 fn render<V: 'static>(self, _view: &mut V, cx: &mut ViewContext<V>) -> impl Component<V> {
59 Story::container(cx)
60 .child(Story::title_for::<_, Avatar>(cx))
61 .child(Story::label(cx, "Default"))
62 .child(Avatar::new(
63 "https://avatars.githubusercontent.com/u/1714999?v=4",
64 ))
65 .child(Story::label(cx, "Rounded rectangle"))
66 .child(
67 Avatar::new("https://avatars.githubusercontent.com/u/1714999?v=4")
68 .shape(Shape::RoundedRectangle),
69 )
70 }
71 }
72}