swoop_ui/container/grid.rs
1use bevy_ui::prelude::*;
2
3use crate::View;
4
5/// Horizontal Grid Layout
6pub mod h_grid;
7/// Vertical Grid Layout
8pub mod v_grid;
9
10pub mod prelude {
11 pub use super::GridView;
12 pub use super::h_grid::HGrid;
13 pub use super::v_grid::VGrid;
14}
15
16/// Provides a fluent interface for configuring grid layout containers.
17///
18/// This trait is intended for views that participate in CSS-like grid layouts.
19/// It enables precise definition of tracks (rows and columns), gaps, flow behavior,
20/// and alignment along both axes, using a builder-style API.
21pub trait GridView: View {
22 /// Sets the flow algorithm used to place items in the grid.
23 ///
24 /// # Arguments
25 /// * `flow` – Determines item placement strategy, such as `Row`, `Column`, `RowDense`, or `ColumnDense`.
26 fn grid_auto_flow(mut self, flow: GridAutoFlow) -> Self {
27 self.node_node().grid_auto_flow = flow;
28 self
29 }
30
31 /// Specifies the sizing behavior for automatically created columns.
32 ///
33 /// Applies when children overflow the defined template or no template columns are set.
34 ///
35 /// # Arguments
36 /// * `tracks` – A vector of `GridTrack` values that define implicit column sizing.
37 fn grid_auto_columns(mut self, tracks: Vec<GridTrack>) -> Self {
38 self.node_node().grid_auto_columns = tracks;
39 self
40 }
41
42 /// Specifies the sizing behavior for automatically created rows.
43 ///
44 /// Applies when content exceeds the defined row template.
45 ///
46 /// # Arguments
47 /// * `tracks` – A vector of `GridTrack` values that define implicit row sizing.
48 fn grid_auto_rows(mut self, tracks: Vec<GridTrack>) -> Self {
49 self.node_node().grid_auto_rows = tracks;
50 self
51 }
52
53 /// Defines the column track template for this grid.
54 ///
55 /// Explicitly sets the layout behavior of each column.
56 ///
57 /// # Arguments
58 /// * `tracks` – A vector of `RepeatedGridTrack` values to control each column's size and repeat behavior.
59 fn grid_template_columns(mut self, tracks: Vec<RepeatedGridTrack>) -> Self {
60 self.node_node().grid_template_columns = tracks;
61 self
62 }
63
64 /// Defines the row track template for this grid.
65 ///
66 /// Explicitly sets the layout behavior of each row.
67 ///
68 /// # Arguments
69 /// * `tracks` – A vector of `RepeatedGridTrack` values to control each row's size and repeat behavior.
70 fn grid_template_rows(mut self, tracks: Vec<RepeatedGridTrack>) -> Self {
71 self.node_node().grid_template_rows = tracks;
72 self
73 }
74
75 /// Aligns all grid content along the main axis (horizontal in LTR layouts).
76 ///
77 /// # Arguments
78 /// * `justify` – Justification mode (e.g. `Start`, `Center`, `SpaceBetween`).
79 fn justify_content(mut self, justify: JustifyContent) -> Self {
80 self.node_node().justify_content = justify;
81 self
82 }
83
84 /// Aligns grid items along the cross axis (vertical in LTR layouts).
85 ///
86 /// # Arguments
87 /// * `align_items` – Alignment mode for items within their cells.
88 fn align_items(mut self, align_items: AlignItems) -> Self {
89 self.node_node().align_items = align_items;
90 self
91 }
92
93 /// Sets the vertical spacing between grid rows.
94 ///
95 /// # Arguments
96 /// * `gap` – The space to insert between rows, using `Val::Px`, `Val::Percent`, etc.
97 fn row_gap(mut self, gap: Val) -> Self {
98 self.node_node().row_gap = gap;
99 self
100 }
101
102 /// Sets the horizontal spacing between grid columns.
103 ///
104 /// # Arguments
105 /// * `gap` – The space to insert between columns, using `Val::Px`, `Val::Percent`, etc.
106 fn column_gap(mut self, gap: Val) -> Self {
107 self.node_node().column_gap = gap;
108 self
109 }
110}