facepile.rs

 1use crate::prelude::*;
 2use gpui::AnyElement;
 3use smallvec::SmallVec;
 4
 5/// A facepile is a collection of faces stacked horizontally–
 6/// always with the leftmost face on top and descending in z-index
 7///
 8/// Facepiles are used to display a group of people or things,
 9/// such as a list of participants in a collaboration session.
10#[derive(IntoElement)]
11pub struct Facepile {
12    base: Div,
13    faces: SmallVec<[AnyElement; 2]>,
14}
15
16impl Facepile {
17    pub fn empty() -> Self {
18        Self::new(SmallVec::new())
19    }
20
21    pub fn new(faces: SmallVec<[AnyElement; 2]>) -> Self {
22        Self { base: div(), faces }
23    }
24}
25
26impl RenderOnce for Facepile {
27    fn render(self, _cx: &mut WindowContext) -> impl IntoElement {
28        // Lay the faces out in reverse so they overlap in the desired order (left to right, front to back)
29        self.base
30            .flex()
31            .flex_row_reverse()
32            .items_center()
33            .justify_start()
34            .children(
35                self.faces
36                    .into_iter()
37                    .enumerate()
38                    .rev()
39                    .map(|(ix, player)| div().when(ix > 0, |div| div.ml_neg_1()).child(player)),
40            )
41    }
42}
43
44impl ParentElement for Facepile {
45    fn extend(&mut self, elements: impl IntoIterator<Item = AnyElement>) {
46        self.faces.extend(elements);
47    }
48}
49
50impl Styled for Facepile {
51    fn style(&mut self) -> &mut gpui::StyleRefinement {
52        self.base.style()
53    }
54}