swoop_ui/container/
stack.rs

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