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