Skip to main content

floem/
view_tuple.rs

1use taffy::FlexDirection;
2
3use crate::{
4    view::{IntoView, View},
5    views::{create_stack, Stack},
6};
7
8pub trait ViewTuple {
9    fn into_views(self) -> Vec<Box<dyn View>>;
10    fn stack(self, direction: FlexDirection) -> Stack;
11    fn v_stack(self) -> Stack
12    where
13        Self: Sized,
14    {
15        ViewTuple::stack(self, FlexDirection::Column)
16    }
17    fn h_stack(self) -> Stack
18    where
19        Self: Sized,
20    {
21        ViewTuple::stack(self, FlexDirection::Row)
22    }
23}
24
25// Macro to implement ViewTuple for tuples of Views and Vec<Box<dyn View>>
26macro_rules! impl_view_tuple {
27    ($capacity:expr, $($t:ident),+) => {
28        impl<$($t: IntoView + 'static),+> ViewTuple for ($($t,)+) {
29            fn into_views(self) -> Vec<Box<dyn View>> {
30                #[allow(non_snake_case)]
31                let ($($t,)+) = self;
32                vec![
33                    $($t.into_any(),)+
34                ]
35            }
36            fn stack(self, direction: FlexDirection) -> Stack {
37                create_stack(self.into_views(), Some(direction))
38            }
39        }
40
41        impl<$($t: IntoView + 'static),+> IntoView for ($($t,)+) {
42            type V = crate::views::Stack;
43
44            fn into_view(self) -> Self::V {
45                #[allow(non_snake_case)]
46                let ($($t,)+) = self;
47                let views = vec![ $($t.into_any(),)+ ];
48                crate::views::create_stack(views, None)
49            }
50        }
51    };
52}
53
54impl_view_tuple!(1, A);
55impl_view_tuple!(2, A, B);
56impl_view_tuple!(3, A, B, C);
57impl_view_tuple!(4, A, B, C, D);
58impl_view_tuple!(5, A, B, C, D, E);
59impl_view_tuple!(6, A, B, C, D, E, F);
60impl_view_tuple!(7, A, B, C, D, E, F, G);
61impl_view_tuple!(8, A, B, C, D, E, F, G, H);
62impl_view_tuple!(9, A, B, C, D, E, F, G, H, I);
63impl_view_tuple!(10, A, B, C, D, E, F, G, H, I, J);
64impl_view_tuple!(11, A, B, C, D, E, F, G, H, I, J, K);
65impl_view_tuple!(12, A, B, C, D, E, F, G, H, I, J, K, L);
66impl_view_tuple!(13, A, B, C, D, E, F, G, H, I, J, K, L, M);
67impl_view_tuple!(14, A, B, C, D, E, F, G, H, I, J, K, L, M, N);
68impl_view_tuple!(15, A, B, C, D, E, F, G, H, I, J, K, L, M, N, O);
69impl_view_tuple!(16, A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P);