orbtk_utils/
border.rs

1use super::{Brush, Thickness};
2
3/// Used to build a border, specifying additional details.
4#[derive(Default)]
5pub struct BorderBuilder {
6    brush: Brush,
7    thickness: Thickness,
8    radius: f64,
9}
10
11impl BorderBuilder {
12    /// Creates a new border builder with default values.
13    pub fn new() -> BorderBuilder {
14        BorderBuilder::default()
15    }
16
17    /// Inserts a border brush.
18    pub fn brush<B: Into<Brush>>(mut self, brush: B) -> Self {
19        self.brush = brush.into();
20        self
21    }
22
23    /// Inserts a border thickness.
24    pub fn thickness(mut self, thickness: Thickness) -> Self {
25        self.thickness = thickness;
26        self
27    }
28
29    /// Inserts a border radius.
30    pub fn radius(mut self, radius: f64) -> Self {
31        self.radius = radius;
32        self
33    }
34
35    /// Builds the border.
36    pub fn build(self) -> Border {
37        Border {
38            brush: self.brush,
39            thickness: self.thickness,
40            radius: self.radius,
41        }
42    }
43}
44
45/// Describes a border of a shape with border `brush`, `thickness` and `radius`.
46#[derive(Clone, Default, Debug, PartialEq)]
47pub struct Border {
48    brush: Brush,
49    thickness: Thickness,
50    radius: f64,
51}
52
53impl Border {
54    /// Creates a new `BorderBuilder` with default values.
55    pub fn create() -> BorderBuilder {
56        BorderBuilder::new()
57    }
58
59    /// Gets the border brush.
60    pub fn brush(&self) -> &Brush {
61        &self.brush
62    }
63
64    /// Sets the border brush.
65    pub fn set_brush<B: Into<Brush>>(&mut self, brush: B) {
66        self.brush = brush.into();
67    }
68
69    /// Gets the thickness.
70    pub fn thickness(&self) -> Thickness {
71        self.thickness
72    }
73
74    /// Sets the thickness.
75    pub fn set_thickness(&mut self, thickness: Thickness) {
76        self.thickness = thickness;
77    }
78
79    /// Gets the radius.
80    pub fn radius(&self) -> f64 {
81        self.radius
82    }
83
84    /// Sets the radius.
85    pub fn set_radius(&mut self, radius: f64) {
86        self.radius = radius
87    }
88}
89
90/// Contains a set of getters and setters to read and write to a border.
91pub trait Bordered {
92    /// Gets the thickness.
93    fn border_thickness(&self) -> Thickness;
94
95    /// Sets the border thickness.
96    fn set_border_thickness(&mut self, thickness: Thickness);
97
98    /// Gets the border brush.
99    fn border_brush(&self) -> &Brush;
100
101    /// Sets the border brush.
102    fn set_border_brush(&mut self, brush: Brush);
103
104    /// Gets the border radius.
105    fn border_radius(&self) -> f64;
106
107    /// Sets the border radius.
108    fn set_border_radius(&mut self, radius: f64);
109
110    /// Gets the complete border.
111    fn border(&self) -> &Border;
112
113    /// Sets the complete border.
114    fn set_border(&mut self, border: Border);
115}
116
117#[cfg(test)]
118mod tests {
119    use crate::prelude::*;
120
121    #[test]
122    fn test_brush() {
123        let brush = Brush::from("#000000");
124
125        let builder = BorderBuilder::new();
126        let border = builder.brush(brush).build();
127
128        let test_brush = Brush::from("#000000");
129        assert_eq!(border.brush(), &test_brush);
130    }
131
132    #[test]
133    fn test_thickness() {
134        let thickness = Thickness::new(0.0, 0.0, 0.0, 0.0);
135
136        let builder = BorderBuilder::new();
137        let border = builder.thickness(thickness).build();
138        assert_eq!(border.thickness(), thickness);
139    }
140
141    #[test]
142    fn test_radius() {
143        let radius = 0.0;
144
145        let builder = BorderBuilder::new();
146        let border = builder.radius(radius).build();
147        assert!(crate::f64_cmp(border.radius(), radius));
148    }
149
150    #[test]
151    fn test_set_brush() {
152        let brush = Brush::from("#000000");
153
154        let mut border = Border::default();
155        border.set_brush(brush);
156
157        let test_brush = Brush::from("#000000");
158        assert_eq!(border.brush(), &test_brush);
159    }
160
161    #[test]
162    fn test_set_thickness() {
163        let thickness = Thickness::new(0.0, 0.0, 0.0, 0.0);
164
165        let mut border = Border::default();
166        border.set_thickness(thickness);
167        assert_eq!(border.thickness(), thickness);
168    }
169
170    #[test]
171    fn test_set_radius() {
172        let radius = 0.0;
173
174        let mut border = Border::default();
175        border.set_radius(radius);
176        assert!(crate::f64_cmp(border.radius(), radius));
177    }
178}