swoop_ui/
lib.rs

1//! **Swoop UI** is a modular, ergonomic layout toolkit built on top of Bevy UI.
2//! It introduces expressive layout containers like `HStack`, `VStack`, `HGrid`, and `VGrid`,
3//! supporting fluent syntax for padding, spacing, border, and background styling.
4//!
5//! # UI Layout Overview
6//!
7//! This crate defines a menu bar container using a horizontal stack layout:
8//!
9//! ```rust
10//! use bevy::prelude::*;
11//! use swoop_ui::prelude::*;
12//!
13//! fn setup(mut commands: Commands) {
14//!     commands.spawn((
15//!         HStack::new(AlignItems::Start, Val::Auto)
16//!             .background_color(Srgba::WHITE.into())
17//!             .justify_content(JustifyContent::Start)
18//!             .pack(),
19//!     ));
20//! }
21//! ```
22//!
23//! - `HStack` creates a horizontal layout with children aligned at the top.
24//! - The background color is set using the impl Into<Color> value.
25//! - Contents are left-aligned using `JustifyContent::Start`.
26//!
27//! The layout is finalized with `.pack()` and passed into `commands.spawn` to be spawn in the entity world.
28
29use std::borrow::Cow;
30use std::fmt::Debug;
31
32use bevy_app::prelude::*;
33use bevy_ecs::prelude::*;
34use bevy_ui::prelude::*;
35
36// Background UI trait
37pub mod background;
38// Border UI trait
39pub mod border;
40
41/// Layouts and containers
42pub mod container;
43
44pub mod prelude {
45    pub use super::container::prelude::*;
46    pub use super::{SwoopUiPlugin, View, ViewPack};
47}
48
49/// Reserved for future addition of system functions
50pub struct SwoopUiPlugin;
51
52/// Provides common builder-style methods for UI configuration
53pub trait View: Debug + Clone + PartialEq + Default {
54    /// Returns a mutable reference to the entity's Name component
55    fn name_node(&mut self) -> &mut Name;
56
57    /// Returns a mutable reference to the entity's Node component
58    fn node_node(&mut self) -> &mut Node;
59
60    /// Sets the Name component
61    fn name(mut self, name: impl Into<Cow<'static, str>>) -> Self {
62        self.name_node().set(name);
63        self
64    }
65
66    /// Applies padding to the Node
67    fn padding(mut self, padding: UiRect) -> Self {
68        self.node_node().padding = padding;
69        self
70    }
71
72    /// Sets width and height of the Node
73    fn frame(mut self, width: Val, height: Val) -> Self {
74        let node = self.node_node();
75        node.width = width;
76        node.height = height;
77        self
78    }
79
80    /// Sets the width of the Node
81    fn width(mut self, width: Val) -> Self {
82        self.node_node().width = width;
83        self
84    }
85
86    /// Sets the height of the Node
87    fn height(mut self, height: Val) -> Self {
88        self.node_node().height = height;
89        self
90    }
91}
92
93/// Converts a custom UI builder into a Bevy-compatible Bundle
94pub trait ViewPack {
95    /// Consumes the UI builder and produces a bundle
96    fn pack(self) -> impl Bundle;
97}
98
99impl Plugin for SwoopUiPlugin {
100    fn build(&self, _: &mut App) {}
101}