Skip to main content

gpui_component/avatar/
avatar_group.rs

1use gpui::{
2    Div, InteractiveElement, Interactivity, IntoElement, ParentElement as _, RenderOnce,
3    StyleRefinement, Styled, div, prelude::FluentBuilder as _,
4};
5use gpui_base::LogicalStyledExt as _;
6
7use crate::{ActiveTheme, Sizable, Size, StyledExt as _, avatar::Avatar};
8
9/// A grouped avatars to display in a compact layout.
10#[derive(IntoElement)]
11pub struct AvatarGroup {
12    base: Div,
13    style: StyleRefinement,
14    avatars: Vec<Avatar>,
15    size: Size,
16    limit: usize,
17    ellipsis: bool,
18}
19
20impl AvatarGroup {
21    /// Create a new AvatarGroup.
22    pub fn new() -> Self {
23        Self {
24            base: div(),
25            style: StyleRefinement::default(),
26            avatars: Vec::new(),
27            size: Size::default(),
28            limit: 3,
29            ellipsis: false,
30        }
31    }
32
33    /// Add a child avatar to the group.
34    pub fn child(mut self, avatar: Avatar) -> Self {
35        self.avatars.push(avatar);
36        self
37    }
38
39    /// Add multiple child avatars to the group.
40    pub fn children(mut self, avatars: impl IntoIterator<Item = Avatar>) -> Self {
41        self.avatars.extend(avatars);
42        self
43    }
44
45    /// Set the maximum number of avatars to display before showing a "more" avatar.
46    pub fn limit(mut self, limit: usize) -> Self {
47        self.limit = limit;
48        self
49    }
50
51    /// Set whether to show an ellipsis when the limit is reached, default: false
52    pub fn ellipsis(mut self) -> Self {
53        self.ellipsis = true;
54        self
55    }
56}
57
58impl Sizable for AvatarGroup {
59    fn with_size(mut self, size: impl Into<Size>) -> Self {
60        self.size = size.into();
61        self
62    }
63}
64
65impl Styled for AvatarGroup {
66    fn style(&mut self) -> &mut StyleRefinement {
67        &mut self.style
68    }
69}
70
71impl InteractiveElement for AvatarGroup {
72    fn interactivity(&mut self) -> &mut Interactivity {
73        self.base.interactivity()
74    }
75}
76
77impl RenderOnce for AvatarGroup {
78    fn render(self, _: &mut gpui::Window, cx: &mut gpui::App) -> impl IntoElement {
79        let item_ml = -super::avatar_size(self.size) * 0.3;
80        let avatars_len = self.avatars.len();
81        // Children go in reverse so earlier avatars paint on top; the row is
82        // reversed back so the first avatar sits on the start edge.
83        let rtl = gpui_base::LayoutDirection::global(cx).is_rtl();
84
85        self.base
86            .h_flex()
87            .when(!rtl, |this| this.flex_row_reverse())
88            .refine_style(&self.style)
89            .children(if self.ellipsis && avatars_len > self.limit {
90                Some(
91                    Avatar::new()
92                        .name("⋯")
93                        .bg(cx.theme().tokens.secondary)
94                        .text_color(cx.theme().muted_foreground)
95                        .with_size(self.size)
96                        .ms(gpui::rems(0.25), cx),
97                )
98            } else {
99                None
100            })
101            .children(
102                self.avatars
103                    .into_iter()
104                    .take(self.limit)
105                    .enumerate()
106                    .rev()
107                    .map(|(ix, item)| {
108                        item.with_size(self.size)
109                            .when(ix > 0, |this| this.ms(item_ml, cx))
110                    }),
111            )
112    }
113}
114
115#[cfg(test)]
116mod tests {
117    use super::*;
118
119    #[gpui::test]
120    fn test_avatar_group_builder(_cx: &mut gpui::TestAppContext) {
121        let group = AvatarGroup::new()
122            .child(Avatar::new().name("Alice"))
123            .child(Avatar::new().name("Bob"))
124            .child(Avatar::new().name("Charlie"))
125            .child(Avatar::new().name("David"))
126            .large()
127            .limit(3)
128            .ellipsis();
129
130        assert_eq!(group.avatars.len(), 4);
131        assert_eq!(group.size, Size::Large);
132        assert_eq!(group.limit, 3);
133        assert!(group.ellipsis);
134    }
135}