Skip to main content

rlvgl_core/
style.rs

1//! Visual appearance attributes applied to widgets.
2
3/// Collection of styling properties for a widget.
4#[derive(Debug, Clone, Copy, PartialEq, Eq)]
5pub struct Style {
6    /// Background color of the widget.
7    pub bg_color: crate::widget::Color,
8    /// Border color of the widget.
9    pub border_color: crate::widget::Color,
10    /// Border width in pixels.
11    pub border_width: u8,
12    /// Widget-level opacity (`255` = fully opaque, `0` = fully transparent).
13    ///
14    /// Applied as a multiplier to all colors when the widget draws itself.
15    pub alpha: u8,
16    /// Corner radius in pixels (`0` = sharp corners).
17    pub radius: u8,
18}
19
20impl Default for Style {
21    fn default() -> Self {
22        Self {
23            bg_color: crate::widget::Color(255, 255, 255, 255),
24            border_color: crate::widget::Color(0, 0, 0, 255),
25            border_width: 0,
26            alpha: 255,
27            radius: 0,
28        }
29    }
30}
31
32/// Builder pattern for constructing [`Style`] instances.
33pub struct StyleBuilder {
34    style: Style,
35}
36
37impl Default for StyleBuilder {
38    fn default() -> Self {
39        Self::new()
40    }
41}
42
43impl StyleBuilder {
44    /// Create a new builder with [`Style::default`] values.
45    pub fn new() -> Self {
46        Self {
47            style: Style::default(),
48        }
49    }
50
51    /// Set the background color.
52    pub fn bg_color(mut self, color: crate::widget::Color) -> Self {
53        self.style.bg_color = color;
54        self
55    }
56
57    /// Set the border color.
58    pub fn border_color(mut self, color: crate::widget::Color) -> Self {
59        self.style.border_color = color;
60        self
61    }
62
63    /// Set the border width in pixels.
64    pub fn border_width(mut self, width: u8) -> Self {
65        self.style.border_width = width;
66        self
67    }
68
69    /// Set the widget-level opacity (`255` = opaque, `0` = transparent).
70    pub fn alpha(mut self, alpha: u8) -> Self {
71        self.style.alpha = alpha;
72        self
73    }
74
75    /// Set the corner radius in pixels (`0` = sharp corners).
76    pub fn radius(mut self, radius: u8) -> Self {
77        self.style.radius = radius;
78        self
79    }
80
81    /// Consume the builder and return the constructed [`Style`].
82    pub fn build(self) -> Style {
83        self.style
84    }
85}