swoop_ui/container/grid/
h_grid.rs

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