nuit_core/compose/view/layout/
stack.rs

1use nuit_derive::Bind;
2
3use crate::{Context, Event, Id, IdPath, IdentifyExt, Node, View, DEFAULT_SPACING};
4
5macro_rules! impl_stack {
6    (#[doc = $doc:expr] $name:ident) => {
7        #[doc = $doc]
8        #[derive(Debug, Clone, PartialEq, Bind)]
9        pub struct $name<T> {
10            spacing: f64,
11            wrapped: T,
12        }
13
14        impl<T> $name<T> {
15            #[doc = concat!("Creates a new ", stringify!($name), " from given wrapped view.")]
16            pub const fn new(wrapped: T) -> Self {
17                Self {
18                    spacing: DEFAULT_SPACING,
19                    wrapped,
20                }
21            }
22
23            #[doc = concat!("Creates a new ", stringify!($name), " from given wrapped view with the given spacing.")]
24            pub fn with_spacing(spacing: impl Into<f64>, wrapped: T) -> Self {
25                Self {
26                    spacing: spacing.into(),
27                    wrapped,
28                }
29            }
30        }
31
32        impl<T> View for $name<T> where T: View {
33            fn fire(&self, event: &Event, event_path: &IdPath, context: &Context) {
34                if let Some(head) = event_path.head() {
35                    match head {
36                        Id::Index(0) => self.wrapped.fire(event, &event_path.tail(), &context.child(0)),
37                        i => panic!("Cannot fire event for child id {} on {} which only has one child", i, stringify!($name)),
38                    }
39                }
40            }
41
42            fn render(&self, context: &Context) -> Node {
43                Node::$name {
44                    spacing: self.spacing,
45                    wrapped: Box::new(self.wrapped.render(&context.child(0)).identify(0)),
46                }
47            }
48        }
49    };
50}
51
52impl_stack! {
53    /// A view that lays out its children horizontally.
54    HStack
55}
56
57impl_stack! {
58    /// A view that lays out its children vertically.
59    VStack
60}
61
62impl_stack! {
63    /// A view that lays out its children on top of each other.
64    ZStack
65}