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(self.src);
24
25        if self.shape == Shape::Circle {
26            img = img.rounded_full();
27        } else {
28            img = img.rounded_md();
29        }
30
31        img.size_4()
32            // todo!(Pull the avatar fallback background from the theme.)
33            .bg(gpui::red())
34    }
35}
36
37impl Avatar {
38    pub fn uri(src: impl Into<SharedString>) -> Self {
39        Self {
40            src: src.into().into(),
41            shape: Shape::Circle,
42        }
43    }
44    pub fn data(src: Arc<ImageData>) -> Self {
45        Self {
46            src: src.into(),
47            shape: Shape::Circle,
48        }
49    }
50
51    pub fn shape(mut self, shape: Shape) -> Self {
52        self.shape = shape;
53        self
54    }
55}