avatar.rs

 1use std::sync::Arc;
 2
 3use crate::prelude::*;
 4use gpui::{img, ImageData, ImageSource, Img, IntoElement};
 5
 6#[derive(Debug, Default, PartialEq, Clone)]
 7pub enum Shape {
 8    #[default]
 9    Circle,
10    RoundedRectangle,
11}
12
13#[derive(IntoElement)]
14pub struct Avatar {
15    src: ImageSource,
16    shape: Shape,
17}
18
19impl RenderOnce for Avatar {
20    type Rendered = Img;
21
22    fn render(self, _: &mut WindowContext) -> Self::Rendered {
23        let mut img = img();
24
25        if self.shape == Shape::Circle {
26            img = img.rounded_full();
27        } else {
28            img = img.rounded_md();
29        }
30
31        img.source(self.src.clone())
32            .size_4()
33            // todo!(Pull the avatar fallback background from the theme.)
34            .bg(gpui::red())
35    }
36}
37
38impl Avatar {
39    pub fn uri(src: impl Into<SharedString>) -> Self {
40        Self {
41            src: src.into().into(),
42            shape: Shape::Circle,
43        }
44    }
45    pub fn data(src: Arc<ImageData>) -> Self {
46        Self {
47            src: src.into(),
48            shape: Shape::Circle,
49        }
50    }
51
52    pub fn source(src: ImageSource) -> Self {
53        Self {
54            src,
55            shape: Shape::Circle,
56        }
57    }
58    pub fn shape(mut self, shape: Shape) -> Self {
59        self.shape = shape;
60        self
61    }
62}