1use crate::prelude::*;
2use gpui::{img, Hsla, ImageSource, Img, IntoElement, Styled};
3
4#[derive(Debug, Default, PartialEq, Clone)]
5pub enum Shape {
6 #[default]
7 Circle,
8 RoundedRectangle,
9}
10
11#[derive(IntoElement)]
12pub struct Avatar {
13 image: Img,
14 border_color: Option<Hsla>,
15 is_available: Option<bool>,
16}
17
18impl RenderOnce for Avatar {
19 fn render(mut self, cx: &mut WindowContext) -> impl IntoElement {
20 if self.image.style().corner_radii.top_left.is_none() {
21 self = self.shape(Shape::Circle);
22 }
23
24 let size = cx.rem_size();
25
26 div()
27 .size(size + px(2.))
28 .map(|mut div| {
29 div.style().corner_radii = self.image.style().corner_radii.clone();
30 div
31 })
32 .when_some(self.border_color, |this, color| {
33 this.border().border_color(color)
34 })
35 .child(
36 self.image
37 .size(size)
38 .bg(cx.theme().colors().ghost_element_background),
39 )
40 .children(self.is_available.map(|is_free| {
41 // HACK: non-integer sizes result in oval indicators.
42 let indicator_size = (size * 0.4).round();
43
44 div()
45 .absolute()
46 .z_index(1)
47 .bg(if is_free {
48 cx.theme().status().created
49 } else {
50 cx.theme().status().deleted
51 })
52 .size(indicator_size)
53 .rounded(indicator_size)
54 .bottom_0()
55 .right_0()
56 }))
57 }
58}
59
60impl Avatar {
61 pub fn new(src: impl Into<ImageSource>) -> Self {
62 Avatar {
63 image: img(src),
64 is_available: None,
65 border_color: None,
66 }
67 }
68
69 pub fn shape(mut self, shape: Shape) -> Self {
70 self.image = match shape {
71 Shape::Circle => self.image.rounded_full(),
72 Shape::RoundedRectangle => self.image.rounded_md(),
73 };
74 self
75 }
76
77 pub fn grayscale(mut self, grayscale: bool) -> Self {
78 self.image = self.image.grayscale(grayscale);
79 self
80 }
81
82 pub fn border_color(mut self, color: impl Into<Hsla>) -> Self {
83 self.border_color = Some(color.into());
84 self
85 }
86
87 pub fn availability_indicator(mut self, is_available: impl Into<Option<bool>>) -> Self {
88 self.is_available = is_available.into();
89 self
90 }
91}