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//! Now only some packaged candies are generated, no additional functions, maybe they will be added later,
5//! a plugin is reserved, but it has not been used yet
6//!
7//! Most methods implement Bundle and can be generated directly.
8//! However, some packaging requires multiple levels,
9//! so ViewToBundle is implemented and the pack() method is called to convert it into impl Bundle.
10//!
11//! # UI Layout Overview
12//!
13//! This crate defines a menu bar container using a horizontal stack layout:
14//!
15//! ```rust
16//! use bevy::prelude::*;
17//! use swoop_ui::prelude::*;
18//!
19//! fn setup(mut commands: Commands) {
20//! commands.spawn((
21//! HStack::new(AlignItems::Start, Val::Auto)
22//! .background_color(Srgba::WHITE.into())
23//! .justify_content(JustifyContent::Start)
24//! .pack(),
25//! ));
26//! }
27//! ```
28//!
29//! - `HStack` creates a horizontal layout with children aligned at the top.
30//! - The background color is set using the impl Into<Color> value.
31//! - Contents are left-aligned using `JustifyContent::Start`.
32//!
33//! Because the impl Bundle is implemented, it can be directly generated using commands.spawn
34
35use std::borrow::Cow;
36use std::fmt::Debug;
37
38use bevy_app::prelude::*;
39use bevy_ecs::prelude::*;
40use bevy_ui::prelude::*;
41
42// Background UI trait
43pub mod background;
44// Border UI trait
45pub mod border;
46// Shadow UI Trait
47pub mod shadow;
48// Absolute positioning
49pub mod position;
50
51/// Button
52pub mod button;
53/// Layouts and containers
54pub mod container;
55/// Text
56pub mod text;
57
58pub mod prelude {
59 pub use super::background::BackgroundView;
60 pub use super::border::BorderView;
61 pub use super::position::PositionView;
62 pub use super::shadow::{BoxShadowView, TextShadowView};
63 pub use super::{SwoopUiPlugin, View, ViewToBundle};
64
65 pub use super::button::prelude::*;
66 pub use super::container::prelude::*;
67 pub use super::text::prelude::*;
68}
69
70/// Reserved for future addition of system functions
71pub struct SwoopUiPlugin;
72
73/// Provides common builder-style methods for UI configuration
74pub trait View: Debug + Clone + Default {
75 /// Short Default Method
76 fn new() -> Self {
77 Self::default()
78 }
79
80 fn from_name(name: impl Into<Cow<'static, str>>) -> Self {
81 let mut view = Self::default();
82 view.name_node().set(name);
83 view
84 }
85
86 /// Returns a mutable reference to the entity's Name component
87 fn name_node(&mut self) -> &mut Name;
88
89 /// Returns a mutable reference to the entity's Node component
90 fn node_node(&mut self) -> &mut Node;
91
92 /// Sets the Name component
93 fn name(mut self, name: impl Into<Cow<'static, str>>) -> Self {
94 self.name_node().set(name);
95 self
96 }
97
98 /// Applies padding to the Node
99 fn padding(mut self, padding: UiRect) -> Self {
100 self.node_node().padding = padding;
101 self
102 }
103
104 /// Sets width and height of the Node
105 fn frame(mut self, width: Val, height: Val) -> Self {
106 let node = self.node_node();
107 node.width = width;
108 node.height = height;
109 self
110 }
111
112 /// Sets the width of the Node
113 fn width(mut self, width: Val) -> Self {
114 self.node_node().width = width;
115 self
116 }
117
118 /// Sets the height of the Node
119 fn height(mut self, height: Val) -> Self {
120 self.node_node().height = height;
121 self
122 }
123}
124
125pub trait ViewToBundle: View {
126 fn pack(self) -> impl Bundle;
127}
128
129impl Plugin for SwoopUiPlugin {
130 fn build(&self, _: &mut App) {}
131}