swoop_ui/container/
stack.rs

1use bevy_ui::prelude::*;
2
3use crate::UiBase;
4
5/// Horizontal Flex Layout
6pub mod h_stack;
7/// Vertical Flex Layout
8pub mod v_stack;
9
10pub mod prelude {
11    pub use super::StackContainer;
12    pub use super::h_stack::*;
13    pub use super::v_stack::*;
14}
15
16/// Provides a flexible layout interface for stack-style containers (e.g. HStack, VStack)
17pub trait StackContainer: UiBase + Default {
18    /// Creates a new stack container with specified alignment and spacing
19    fn new(align_items: AlignItems, gap: Val) -> Self {
20        let mut stack = Self::default();
21        stack.node_node().align_items = align_items;
22        stack.spacing(gap)
23    }
24
25    /// Reverses the current flex direction (e.g. Row ⇄ RowReverse, Column ⇄ ColumnReverse)
26    fn reverse(mut self) -> Self {
27        let node = self.node_node();
28        let direction = match node.flex_direction {
29            FlexDirection::Row => FlexDirection::RowReverse,
30            FlexDirection::Column => FlexDirection::ColumnReverse,
31            FlexDirection::RowReverse => FlexDirection::Row,
32            FlexDirection::ColumnReverse => FlexDirection::Column,
33        };
34        node.flex_direction = direction;
35
36        self
37    }
38
39    /// Sets the wrapping behavior of the flex container
40    fn wrap(mut self, wrap: FlexWrap) -> Self {
41        self.node_node().flex_wrap = wrap;
42        self
43    }
44
45    /// Defines how extra space along the main axis is distributed
46    fn justify_content(mut self, justify: JustifyContent) -> Self {
47        self.node_node().justify_content = justify;
48        self
49    }
50
51    /// Defines how children are aligned along the cross axis
52    fn align_items(mut self, align_items: AlignItems) -> Self {
53        self.node_node().align_items = align_items;
54        self
55    }
56
57    /// Sets vertical spacing between rows
58    fn row_gap(mut self, gap: Val) -> Self {
59        self.node_node().row_gap = gap;
60        self
61    }
62
63    /// Sets horizontal spacing between columns
64    fn column_gap(mut self, gap: Val) -> Self {
65        self.node_node().column_gap = gap;
66        self
67    }
68
69    /// Automatically applies spacing based on current flex direction
70    fn spacing(mut self, gap: Val) -> Self {
71        let node = self.node_node();
72        match node.flex_direction {
73            FlexDirection::Row | FlexDirection::RowReverse => node.column_gap = gap,
74            FlexDirection::Column | FlexDirection::ColumnReverse => node.row_gap = gap,
75        }
76
77        self
78    }
79}