avatar.rs

 1use std::marker::PhantomData;
 2
 3use gpui3::{img, ArcCow};
 4
 5use crate::prelude::*;
 6use crate::theme::theme;
 7
 8#[derive(Element, Clone)]
 9pub struct Avatar<S: 'static + Send + Sync> {
10    state_type: PhantomData<S>,
11    src: ArcCow<'static, str>,
12    shape: Shape,
13}
14
15impl<S: 'static + Send + Sync> Avatar<S> {
16    pub fn new(src: impl Into<ArcCow<'static, str>>) -> Self {
17        Self {
18            state_type: PhantomData,
19            src: src.into(),
20            shape: Shape::Circle,
21        }
22    }
23
24    pub fn shape(mut self, shape: Shape) -> Self {
25        self.shape = shape;
26        self
27    }
28
29    fn render(&mut self, cx: &mut ViewContext<S>) -> impl Element<State = S> {
30        let theme = theme(cx);
31
32        let mut img = img();
33
34        if self.shape == Shape::Circle {
35            img = img.rounded_full();
36        } else {
37            img = img.rounded_md();
38        }
39
40        img.uri(self.src.clone())
41            .size_4()
42            .fill(theme.middle.warning.default.foreground)
43    }
44}