elvis_core/style/
border.rs

1use crate::{
2    style::Style,
3    value::{BorderRadius, BorderStyle, BoxBorder, Color, Unit},
4};
5use elvis_core_support::Setter;
6
7/// Border Style
8#[derive(Clone, Setter, Eq, PartialEq, Ord, PartialOrd)]
9pub struct Border {
10    top_color: Color,
11    top_style: BorderStyle,
12    top_width: Unit,
13
14    right_color: Color,
15    right_style: BorderStyle,
16    right_width: Unit,
17
18    bottom_color: Color,
19    bottom_style: BorderStyle,
20    bottom_width: Unit,
21
22    left_color: Color,
23    left_style: BorderStyle,
24    left_width: Unit,
25
26    top_left_radius: Unit,
27    top_right_radius: Unit,
28    bottom_right_radius: Unit,
29    bottom_left_radius: Unit,
30
31    second_top_left_radius: Unit,
32    second_top_right_radius: Unit,
33    second_bottom_right_radius: Unit,
34    second_bottom_left_radius: Unit,
35}
36
37impl Default for Border {
38    fn default() -> Border {
39        Border {
40            top_color: Color::Black,
41            top_style: BorderStyle::default(),
42            top_width: Unit::Px(1.0),
43
44            right_color: Color::Black,
45            right_style: BorderStyle::default(),
46            right_width: Unit::Px(1.0),
47
48            bottom_color: Color::Black,
49            bottom_style: BorderStyle::default(),
50            bottom_width: Unit::Px(1.0),
51
52            left_color: Color::Black,
53            left_style: BorderStyle::default(),
54            left_width: Unit::Px(1.0),
55
56            top_left_radius: Unit::None(0.0),
57            top_right_radius: Unit::None(0.0),
58            bottom_right_radius: Unit::None(0.0),
59            bottom_left_radius: Unit::None(0.0),
60
61            second_top_left_radius: Unit::None(0.0),
62            second_top_right_radius: Unit::None(0.0),
63            second_bottom_right_radius: Unit::None(0.0),
64            second_bottom_left_radius: Unit::None(0.0),
65        }
66    }
67}
68
69impl Border {
70    /// Set color for all borders
71    pub fn color(mut self, color: Color) -> Self {
72        self.top_color = color;
73        self.right_color = color;
74        self.bottom_color = color;
75        self.left_color = color;
76        self
77    }
78
79    /// Set radius for all borders
80    pub fn radius(mut self, radius: Unit) -> Self {
81        self.top_left_radius = radius;
82        self.top_right_radius = radius;
83        self.bottom_right_radius = radius;
84        self.bottom_left_radius = radius;
85        self
86    }
87
88    /// Set width for all borders
89    pub fn width(mut self, width: Unit) -> Self {
90        self.top_width = width;
91        self.right_width = width;
92        self.bottom_width = width;
93        self.left_width = width;
94        self
95    }
96
97    /// Set style for all borders
98    pub fn style(mut self, style: BorderStyle) -> Self {
99        self.top_style = style.clone();
100        self.right_style = style.clone();
101        self.bottom_style = style.clone();
102        self.left_style = style;
103        self
104    }
105}
106
107impl Into<Vec<Style>> for Border {
108    fn into(self) -> Vec<Style> {
109        let top_border = BoxBorder {
110            width: self.top_width,
111            style: self.top_style,
112            color: self.top_color,
113        };
114        let right_border = BoxBorder {
115            width: self.right_width,
116            style: self.right_style,
117            color: self.right_color,
118        };
119        let bottom_border = BoxBorder {
120            width: self.bottom_width,
121            style: self.bottom_style,
122            color: self.bottom_color,
123        };
124        let left_border = BoxBorder {
125            width: self.left_width,
126            style: self.left_style,
127            color: self.left_color,
128        };
129
130        let mut styles = vec![Style::BorderRadius(BorderRadius {
131            top_left: self.top_left_radius,
132            top_right: self.top_right_radius,
133            bottom_right: self.bottom_right_radius,
134            bottom_left: self.bottom_left_radius,
135            second_top_left: self.second_top_left_radius,
136            second_top_right: self.second_top_right_radius,
137            second_bottom_right: self.second_bottom_left_radius,
138            second_bottom_left: self.second_bottom_right_radius,
139        })];
140
141        if top_border == right_border
142            && right_border == bottom_border
143            && bottom_border == left_border
144        {
145            styles.append(&mut vec![
146                Style::BorderTop(top_border),
147                Style::BorderRight(right_border),
148                Style::BorderBottom(bottom_border),
149                Style::BorderLeft(left_border),
150            ]);
151        } else {
152            styles.append(&mut vec![Style::Border(top_border)]);
153        }
154
155        styles
156    }
157}