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)
17pub trait StackView: View {
18    fn from_justify_content(justify_content: JustifyContent) -> Self {
19        let mut stack = Self::default();
20        stack.node_node().justify_content = justify_content;
21        stack
22    }
23
24    fn from_align_items(align_items: AlignItems) -> Self {
25        let mut stack = Self::default();
26        stack.node_node().align_items = align_items;
27        stack
28    }
29
30    /// Reverses the current flex direction (e.g. Row ⇄ RowReverse, Column ⇄ ColumnReverse)
31    fn reverse(mut self) -> Self {
32        let node = self.node_node();
33        let direction = match node.flex_direction {
34            FlexDirection::Row => FlexDirection::RowReverse,
35            FlexDirection::Column => FlexDirection::ColumnReverse,
36            FlexDirection::RowReverse => FlexDirection::Row,
37            FlexDirection::ColumnReverse => FlexDirection::Column,
38        };
39        node.flex_direction = direction;
40
41        self
42    }
43
44    /// Sets the wrapping behavior of the flex container
45    fn wrap(mut self, wrap: FlexWrap) -> Self {
46        self.node_node().flex_wrap = wrap;
47        self
48    }
49
50    /// Defines how extra space along the main axis is distributed
51    fn justify_content(mut self, justify: JustifyContent) -> Self {
52        self.node_node().justify_content = justify;
53        self
54    }
55
56    /// Defines how children are aligned along the cross axis
57    fn align_items(mut self, align_items: AlignItems) -> Self {
58        self.node_node().align_items = align_items;
59        self
60    }
61
62    /// Sets vertical spacing between rows
63    fn row_gap(mut self, gap: Val) -> Self {
64        self.node_node().row_gap = gap;
65        self
66    }
67
68    /// Sets horizontal spacing between columns
69    fn column_gap(mut self, gap: Val) -> Self {
70        self.node_node().column_gap = gap;
71        self
72    }
73
74    /// Automatically applies spacing based on current flex direction
75    fn spacing(mut self, gap: Val) -> Self {
76        let node = self.node_node();
77        match node.flex_direction {
78            FlexDirection::Row | FlexDirection::RowReverse => node.column_gap = gap,
79            FlexDirection::Column | FlexDirection::ColumnReverse => node.row_gap = gap,
80        }
81
82        self
83    }
84}