Skip to main content

gpui_base/
avatar.rs

1use gpui::{
2    AnyElement, App, Div, ImageSource, InteractiveElement, Interactivity, IntoElement,
3    ParentElement, RenderOnce, StyleRefinement, Styled, Window, div, img,
4};
5use smallvec::SmallVec;
6
7/// An unstyled avatar root that renders its image slot or fallback slot.
8#[derive(IntoElement)]
9pub struct Avatar {
10    base: Div,
11    style: StyleRefinement,
12    image: Option<AvatarImage>,
13    fallback: Option<AvatarFallback>,
14}
15
16impl Avatar {
17    pub fn new() -> Self {
18        Self {
19            base: div(),
20            style: StyleRefinement::default(),
21            image: None,
22            fallback: None,
23        }
24    }
25
26    pub fn image(mut self, image: AvatarImage) -> Self {
27        self.image = Some(image);
28        self
29    }
30
31    pub fn fallback(mut self, fallback: AvatarFallback) -> Self {
32        self.fallback = Some(fallback);
33        self
34    }
35}
36
37impl Default for Avatar {
38    fn default() -> Self {
39        Self::new()
40    }
41}
42
43impl Styled for Avatar {
44    fn style(&mut self) -> &mut StyleRefinement {
45        &mut self.style
46    }
47}
48
49impl InteractiveElement for Avatar {
50    fn interactivity(&mut self) -> &mut Interactivity {
51        self.base.interactivity()
52    }
53}
54
55impl RenderOnce for Avatar {
56    fn render(self, _: &mut Window, _: &mut App) -> impl IntoElement {
57        let content = self
58            .image
59            .map(IntoElement::into_any_element)
60            .or_else(|| self.fallback.map(IntoElement::into_any_element));
61        self.base.children(content).refine_style(&self.style)
62    }
63}
64
65/// An unstyled avatar image slot.
66#[derive(IntoElement)]
67pub struct AvatarImage {
68    image: gpui::Img,
69    style: StyleRefinement,
70}
71
72impl AvatarImage {
73    pub fn new(source: impl Into<ImageSource>) -> Self {
74        Self {
75            image: img(source),
76            style: StyleRefinement::default(),
77        }
78    }
79}
80
81impl Styled for AvatarImage {
82    fn style(&mut self) -> &mut StyleRefinement {
83        &mut self.style
84    }
85}
86
87impl InteractiveElement for AvatarImage {
88    fn interactivity(&mut self) -> &mut Interactivity {
89        self.image.interactivity()
90    }
91}
92
93impl RenderOnce for AvatarImage {
94    fn render(self, _: &mut Window, _: &mut App) -> impl IntoElement {
95        self.image.refine_style(&self.style)
96    }
97}
98
99/// An unstyled avatar fallback slot for initials or an application-owned icon.
100#[derive(IntoElement)]
101pub struct AvatarFallback {
102    base: Div,
103    style: StyleRefinement,
104    children: SmallVec<[AnyElement; 1]>,
105}
106
107impl AvatarFallback {
108    pub fn new() -> Self {
109        Self {
110            base: div(),
111            style: StyleRefinement::default(),
112            children: SmallVec::new(),
113        }
114    }
115}
116
117impl Default for AvatarFallback {
118    fn default() -> Self {
119        Self::new()
120    }
121}
122
123impl Styled for AvatarFallback {
124    fn style(&mut self) -> &mut StyleRefinement {
125        &mut self.style
126    }
127}
128
129impl ParentElement for AvatarFallback {
130    fn extend(&mut self, elements: impl IntoIterator<Item = AnyElement>) {
131        self.children.extend(elements);
132    }
133}
134
135impl RenderOnce for AvatarFallback {
136    fn render(self, _: &mut Window, _: &mut App) -> impl IntoElement {
137        self.base.children(self.children).refine_style(&self.style)
138    }
139}
140
141#[cfg(test)]
142mod tests {
143    use super::*;
144    use gpui::{Context, Render, prelude::FluentBuilder as _, px};
145
146    struct Harness {
147        image: bool,
148    }
149
150    impl Render for Harness {
151        fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement {
152            Avatar::new()
153                .when(self.image, |this| {
154                    this.image(
155                        AvatarImage::new("avatar.png")
156                            .debug_selector(|| "avatar-image".into())
157                            .size(px(20.)),
158                    )
159                })
160                .fallback(
161                    AvatarFallback::new().child(
162                        div()
163                            .debug_selector(|| "avatar-fallback".into())
164                            .size(px(20.)),
165                    ),
166                )
167        }
168    }
169
170    #[gpui::test]
171    fn image_slot_takes_precedence_over_fallback(cx: &mut gpui::TestAppContext) {
172        let (_, cx) = cx.add_window_view(|_, _| Harness { image: true });
173        cx.update(|window, cx| window.draw(cx).clear(cx));
174        assert!(cx.debug_bounds("avatar-image").is_some());
175        assert!(cx.debug_bounds("avatar-fallback").is_none());
176    }
177
178    #[gpui::test]
179    fn fallback_renders_without_an_image(cx: &mut gpui::TestAppContext) {
180        let (_, cx) = cx.add_window_view(|_, _| Harness { image: false });
181        cx.update(|window, cx| window.draw(cx).clear(cx));
182        assert!(cx.debug_bounds("avatar-image").is_none());
183        assert!(cx.debug_bounds("avatar-fallback").is_some());
184    }
185}