swoop_ui/container/
stack.rs

1use bevy_ui::prelude::*;
2
3use crate::View;
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::StackView;
12    pub use super::h_stack::HStack;
13    pub use super::v_stack::VStack;
14}
15
16/// Provides a flexible layout interface for stack-style containers (e.g. HStack, VStack).
17///
18/// This trait is intended for views that manage their children using a `FlexDirection`-based
19/// layout model. It offers a fluent builder-style interface for configuring core layout
20/// properties like spacing, alignment, and wrapping.
21pub trait StackView: View {
22    /// Creates a new instance with a specified `JustifyContent` alignment along the main axis.
23    ///
24    /// # Arguments
25    /// * `justify_content` - Defines how free space is distributed along the main axis.
26    fn from_justify_content(justify_content: JustifyContent) -> Self {
27        let mut stack = Self::default();
28        stack.node_node().justify_content = justify_content;
29        stack
30    }
31
32    /// Creates a new instance with a specified `AlignItems` alignment along the cross axis.
33    ///
34    /// # Arguments
35    /// * `align_items` - Defines how child elements are aligned perpendicular to the main axis.
36    fn from_align_items(align_items: AlignItems) -> Self {
37        let mut stack = Self::default();
38        stack.node_node().align_items = align_items;
39        stack
40    }
41
42    /// Sets the direction of content flow within the stack (e.g. horizontal or vertical).
43    ///
44    /// # Arguments
45    /// * `direction` - The flex direction, such as `Row` or `Column`.
46    fn flex_direction(mut self, direction: FlexDirection) -> Self {
47        self.node_node().flex_direction = direction;
48        self
49    }
50
51    /// Sets the wrapping behavior of the flex container.
52    ///
53    /// Determines whether children wrap to the next line or stay on a single line.
54    ///
55    /// # Arguments
56    /// * `wrap` - The wrapping strategy, such as `NoWrap` or `Wrap`.
57    fn wrap(mut self, wrap: FlexWrap) -> Self {
58        self.node_node().flex_wrap = wrap;
59        self
60    }
61
62    /// Defines how extra space along the main axis is distributed.
63    ///
64    /// # Arguments
65    /// * `justify` - The justification mode (e.g. `Center`, `SpaceBetween`).
66    fn justify_content(mut self, justify: JustifyContent) -> Self {
67        self.node_node().justify_content = justify;
68        self
69    }
70
71    /// Defines how children are aligned along the cross axis.
72    ///
73    /// # Arguments
74    /// * `align_items` - The alignment mode (e.g. `Start`, `Stretch`).
75    fn align_items(mut self, align_items: AlignItems) -> Self {
76        self.node_node().align_items = align_items;
77        self
78    }
79
80    /// Sets the vertical spacing between rows when wrapping is enabled.
81    ///
82    /// # Arguments
83    /// * `gap` - The spacing between rows (`Val::Px`, `Val::Percent`, etc.).
84    fn row_gap(mut self, gap: Val) -> Self {
85        self.node_node().row_gap = gap;
86        self
87    }
88
89    /// Sets the horizontal spacing between columns when wrapping is enabled.
90    ///
91    /// # Arguments
92    /// * `gap` - The spacing between columns (`Val::Px`, `Val::Percent`, etc.).
93    fn column_gap(mut self, gap: Val) -> Self {
94        self.node_node().column_gap = gap;
95        self
96    }
97}