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// Shadow UI Trait
41pub mod shadow;
42
43/// Layouts and containers
44pub mod container;
45
46pub mod prelude {
47    pub use super::background::*;
48    pub use super::border::*;
49    pub use super::container::prelude::*;
50    pub use super::shadow::*;
51    pub use super::{SwoopUiPlugin, View, ViewPack};
52}
53
54/// Reserved for future addition of system functions
55pub struct SwoopUiPlugin;
56
57/// Provides common builder-style methods for UI configuration
58pub trait View: Debug + Clone + PartialEq + Default {
59    /// Returns a mutable reference to the entity's Name component
60    fn name_node(&mut self) -> &mut Name;
61
62    /// Returns a mutable reference to the entity's Node component
63    fn node_node(&mut self) -> &mut Node;
64
65    /// Sets the Name component
66    fn name(mut self, name: impl Into<Cow<'static, str>>) -> Self {
67        self.name_node().set(name);
68        self
69    }
70
71    /// Applies padding to the Node
72    fn padding(mut self, padding: UiRect) -> Self {
73        self.node_node().padding = padding;
74        self
75    }
76
77    /// Sets width and height of the Node
78    fn frame(mut self, width: Val, height: Val) -> Self {
79        let node = self.node_node();
80        node.width = width;
81        node.height = height;
82        self
83    }
84
85    /// Sets the width of the Node
86    fn width(mut self, width: Val) -> Self {
87        self.node_node().width = width;
88        self
89    }
90
91    /// Sets the height of the Node
92    fn height(mut self, height: Val) -> Self {
93        self.node_node().height = height;
94        self
95    }
96}
97
98/// Converts a custom UI builder into a Bevy-compatible Bundle
99pub trait ViewPack {
100    /// Consumes the UI builder and produces a bundle
101    fn pack(self) -> impl Bundle;
102}
103
104impl Plugin for SwoopUiPlugin {
105    fn build(&self, _: &mut App) {}
106}