bevy_ui_builders/separator/
builder.rs

1//! SeparatorBuilder implementation
2
3use bevy::prelude::*;
4use crate::dimensions;
5use super::types::*;
6
7/// Builder for creating separators with consistent styling
8pub struct SeparatorBuilder {
9    orientation: Orientation,
10    style: SeparatorStyle,
11    color: Option<Color>,
12    thickness: Option<f32>,
13    margin: UiRect,
14    length: Val,
15}
16
17impl SeparatorBuilder {
18    pub fn new() -> Self {
19        Self {
20            orientation: Orientation::Horizontal,
21            style: SeparatorStyle::Solid,
22            color: None,
23            thickness: None,
24            margin: UiRect::vertical(Val::Px(dimensions::SEPARATOR_MARGIN)),
25            length: Val::Percent(100.0),
26        }
27    }
28
29    pub fn orientation(mut self, orientation: Orientation) -> Self {
30        self.orientation = orientation;
31        self
32    }
33
34    pub fn style(mut self, style: SeparatorStyle) -> Self {
35        self.style = style;
36        self
37    }
38
39    /// Override the color
40    pub fn color(mut self, color: Color) -> Self {
41        self.color = Some(color);
42        self
43    }
44
45    /// Override the thickness
46    pub fn thickness(mut self, thickness: f32) -> Self {
47        self.thickness = Some(thickness);
48        self
49    }
50
51    pub fn margin(mut self, margin: UiRect) -> Self {
52        self.margin = margin;
53        self
54    }
55
56    pub fn length(mut self, length: Val) -> Self {
57        self.length = length;
58        self
59    }
60
61    pub fn build(self, parent: &mut ChildSpawnerCommands) -> Entity {
62        let color = self.color.unwrap_or_else(|| self.style.color());
63        let thickness = self.thickness.unwrap_or_else(|| self.style.thickness());
64
65        let (width, height) = match self.orientation {
66            Orientation::Horizontal => (self.length, Val::Px(thickness)),
67            Orientation::Vertical => (Val::Px(thickness), self.length),
68        };
69
70        parent
71            .spawn((
72                Node {
73                    width,
74                    height,
75                    margin: self.margin,
76                    ..default()
77                },
78                BackgroundColor(color),
79                Separator {
80                    orientation: self.orientation,
81                    style: self.style,
82                },
83            ))
84            .id()
85    }
86}
87
88/// Convenience function for creating a horizontal separator
89pub fn separator() -> SeparatorBuilder {
90    SeparatorBuilder::new()
91}
92
93/// Convenience function for creating a vertical separator
94pub fn separator_vertical() -> SeparatorBuilder {
95    SeparatorBuilder::new().orientation(Orientation::Vertical)
96}