Skip to main content

cranpose_ui/modifier/
background.rs

1use super::{inspector_metadata, Color, Modifier, RoundedCornerShape};
2use crate::modifier_nodes::{BackgroundElement, CornerShapeElement};
3
4impl Modifier {
5    /// Set the background color.
6    ///
7    /// Example: `Modifier::empty().background(Color::rgb(1.0, 0.0, 0.0))`
8    pub fn background(self, color: Color) -> Self {
9        let modifier = Self::with_element(BackgroundElement::new(color))
10            .with_inspector_metadata(background_metadata(color));
11        self.then(modifier)
12    }
13
14    /// Add rounded corners with uniform radius.
15    ///
16    /// Example: `Modifier::empty().rounded_corners(8.0)`
17    pub fn rounded_corners(self, radius: f32) -> Self {
18        let shape = RoundedCornerShape::uniform(radius);
19        let modifier = Self::with_element(CornerShapeElement::new(shape));
20        self.then(modifier)
21    }
22
23    /// Add rounded corners with a custom shape.
24    ///
25    /// Example: `Modifier::empty().rounded_corner_shape(shape)`
26    pub fn rounded_corner_shape(self, shape: RoundedCornerShape) -> Self {
27        let modifier = Self::with_element(CornerShapeElement::new(shape));
28        self.then(modifier)
29    }
30}
31
32fn background_metadata(color: Color) -> super::InspectorMetadata {
33    inspector_metadata("background", |info| {
34        info.add_property("backgroundColor", format!("{color:?}"));
35    })
36}