facepile.rs

 1use std::marker::PhantomData;
 2
 3use crate::prelude::*;
 4use crate::{theme, Avatar, Player};
 5
 6#[derive(Element)]
 7pub struct Facepile<S: 'static + Send + Sync> {
 8    state_type: PhantomData<S>,
 9    players: Vec<Player>,
10}
11
12impl<S: 'static + Send + Sync> Facepile<S> {
13    pub fn new<P: Iterator<Item = Player>>(players: P) -> Self {
14        Self {
15            state_type: PhantomData,
16            players: players.collect(),
17        }
18    }
19
20    fn render(&mut self, _view: &mut S, cx: &mut ViewContext<S>) -> impl Element<ViewState = S> {
21        let theme = theme(cx);
22        let player_count = self.players.len();
23        let player_list = self.players.iter().enumerate().map(|(ix, player)| {
24            let isnt_last = ix < player_count - 1;
25
26            div()
27                .when(isnt_last, |div| div.neg_mr_1())
28                .child(Avatar::new(player.avatar_src().to_string()))
29        });
30        div().p_1().flex().items_center().children(player_list)
31    }
32}
33
34#[cfg(feature = "stories")]
35pub use stories::*;
36
37#[cfg(feature = "stories")]
38mod stories {
39    use crate::{static_players, Story};
40
41    use super::*;
42
43    #[derive(Element)]
44    pub struct FacepileStory<S: 'static + Send + Sync> {
45        state_type: PhantomData<S>,
46    }
47
48    impl<S: 'static + Send + Sync> FacepileStory<S> {
49        pub fn new() -> Self {
50            Self {
51                state_type: PhantomData,
52            }
53        }
54
55        fn render(&mut self, _view: &mut S, cx: &mut ViewContext<S>) -> impl Element<ViewState = S> {
56            let players = static_players();
57
58            Story::container(cx)
59                .child(Story::title_for::<_, Facepile<S>>(cx))
60                .child(Story::label(cx, "Default"))
61                .child(
62                    div()
63                        .flex()
64                        .gap_3()
65                        .child(Facepile::new(players.clone().into_iter().take(1)))
66                        .child(Facepile::new(players.clone().into_iter().take(2)))
67                        .child(Facepile::new(players.clone().into_iter().take(3))),
68                )
69        }
70    }
71}