orbtk_utils/
border.rs

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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
use super::{Brush, Thickness};

/// Used to build a border, specifying additional details.
#[derive(Default)]
pub struct BorderBuilder {
    brush: Brush,
    thickness: Thickness,
    radius: f64,
}

impl BorderBuilder {
    /// Creates a new border builder with default values.
    pub fn new() -> BorderBuilder {
        BorderBuilder::default()
    }

    /// Inserts a border brush.
    pub fn brush<B: Into<Brush>>(mut self, brush: B) -> Self {
        self.brush = brush.into();
        self
    }

    /// Inserts a border thickness.
    pub fn thickness(mut self, thickness: Thickness) -> Self {
        self.thickness = thickness;
        self
    }

    /// Inserts a border radius.
    pub fn radius(mut self, radius: f64) -> Self {
        self.radius = radius;
        self
    }

    /// Builds the border.
    pub fn build(self) -> Border {
        Border {
            brush: self.brush,
            thickness: self.thickness,
            radius: self.radius,
        }
    }
}

/// Describes a border of a shape with border `brush`, `thickness` and `radius`.
#[derive(Clone, Default, Debug, PartialEq)]
pub struct Border {
    brush: Brush,
    thickness: Thickness,
    radius: f64,
}

impl Border {
    /// Creates a new `BorderBuilder` with default values.
    pub fn create() -> BorderBuilder {
        BorderBuilder::new()
    }

    /// Gets the border brush.
    pub fn brush(&self) -> &Brush {
        &self.brush
    }

    /// Sets the border brush.
    pub fn set_brush<B: Into<Brush>>(&mut self, brush: B) {
        self.brush = brush.into();
    }

    /// Gets the thickness.
    pub fn thickness(&self) -> Thickness {
        self.thickness
    }

    /// Sets the thickness.
    pub fn set_thickness(&mut self, thickness: Thickness) {
        self.thickness = thickness;
    }

    /// Gets the radius.
    pub fn radius(&self) -> f64 {
        self.radius
    }

    /// Sets the radius.
    pub fn set_radius(&mut self, radius: f64) {
        self.radius = radius
    }
}

/// Contains a set of getters and setters to read and write to a border.
pub trait Bordered {
    /// Gets the thickness.
    fn border_thickness(&self) -> Thickness;

    /// Sets the border thickness.
    fn set_border_thickness(&mut self, thickness: Thickness);

    /// Gets the border brush.
    fn border_brush(&self) -> &Brush;

    /// Sets the border brush.
    fn set_border_brush(&mut self, brush: Brush);

    /// Gets the border radius.
    fn border_radius(&self) -> f64;

    /// Sets the border radius.
    fn set_border_radius(&mut self, radius: f64);

    /// Gets the complete border.
    fn border(&self) -> &Border;

    /// Sets the complete border.
    fn set_border(&mut self, border: Border);
}

#[cfg(test)]
mod tests {
    use crate::prelude::*;

    #[test]
    fn test_brush() {
        let brush = Brush::from("#000000");

        let builder = BorderBuilder::new();
        let border = builder.brush(brush).build();

        let test_brush = Brush::from("#000000");
        assert_eq!(border.brush(), &test_brush);
    }

    #[test]
    fn test_thickness() {
        let thickness = Thickness::new(0.0, 0.0, 0.0, 0.0);

        let builder = BorderBuilder::new();
        let border = builder.thickness(thickness).build();
        assert_eq!(border.thickness(), thickness);
    }

    #[test]
    fn test_radius() {
        let radius = 0.0;

        let builder = BorderBuilder::new();
        let border = builder.radius(radius).build();
        assert!(crate::f64_cmp(border.radius(), radius));
    }

    #[test]
    fn test_set_brush() {
        let brush = Brush::from("#000000");

        let mut border = Border::default();
        border.set_brush(brush);

        let test_brush = Brush::from("#000000");
        assert_eq!(border.brush(), &test_brush);
    }

    #[test]
    fn test_set_thickness() {
        let thickness = Thickness::new(0.0, 0.0, 0.0, 0.0);

        let mut border = Border::default();
        border.set_thickness(thickness);
        assert_eq!(border.thickness(), thickness);
    }

    #[test]
    fn test_set_radius() {
        let radius = 0.0;

        let mut border = Border::default();
        border.set_radius(radius);
        assert!(crate::f64_cmp(border.radius(), radius));
    }
}