swoop_ui/container/grid/
h_grid.rs

1use bevy_ecs::prelude::*;
2use bevy_ui::prelude::*;
3
4use crate::container::{BackgroundContainer, BackgroundStyle, BorderContainer, BorderStyle};
5use crate::{UiBase, UiToBundle};
6
7use super::GridContainer;
8
9/// A horizontal grid layout container that arranges children using `GridAutoFlow::Row`.
10/// Supports background and border styling as well as custom grid track configuration.
11pub struct HGrid {
12    /// The name component used to identify the UI node
13    name: Name,
14    /// The layout node controlling grid behavior and spacing
15    node: Node,
16    /// Border rendering style (color and radius)
17    border: BorderStyle,
18    /// Background rendering style (color or image)
19    background: BackgroundStyle,
20}
21
22impl Default for HGrid {
23    fn default() -> Self {
24        Self {
25            name: Name::new("HGrid"),
26            node: Node {
27                display: Display::Grid,
28                grid_auto_flow: GridAutoFlow::Column,
29                justify_content: JustifyContent::Start,
30                align_items: AlignItems::Center,
31                column_gap: Val::Px(0.0),
32                ..Default::default()
33            },
34            border: BorderStyle::default(),
35            background: BackgroundStyle::default(),
36        }
37    }
38}
39
40impl UiBase for HGrid {
41    fn name_node(&mut self) -> &mut Name {
42        &mut self.name
43    }
44
45    fn node_node(&mut self) -> &mut Node {
46        &mut self.node
47    }
48}
49
50impl GridContainer for HGrid {
51    fn grid_auto_track(mut self, tracks: Vec<GridTrack>) -> Self {
52        self.node_node().grid_auto_columns = tracks;
53        self
54    }
55
56    fn grid_template_track(mut self, tracks: Vec<RepeatedGridTrack>) -> Self {
57        self.node_node().grid_template_columns = tracks;
58        self
59    }
60}
61
62impl BackgroundContainer for HGrid {
63    fn background_node(&mut self) -> &mut BackgroundStyle {
64        &mut self.background
65    }
66}
67
68impl BorderContainer for HGrid {
69    fn border_node(&mut self) -> &mut BorderStyle {
70        &mut self.border
71    }
72}
73
74impl UiToBundle for HGrid {
75    fn pack(self) -> impl Bundle {
76        let name = self.name;
77        let border = self.border.pack();
78        let background = self.background.pack();
79        (name, self.node, border, background)
80    }
81}