Skip to main content

cranpose_ui/modifier/
background.rs

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