iced_core/
border.rs

1//! Draw lines around containers.
2use crate::{Color, Pixels};
3
4/// A border.
5#[derive(Debug, Clone, Copy, PartialEq, Default)]
6pub struct Border {
7    /// The color of the border.
8    pub color: Color,
9
10    /// The width of the border.
11    pub width: f32,
12
13    /// The [`Radius`] of the border.
14    pub radius: Radius,
15}
16
17/// Creates a new [`Border`] with the given [`Radius`].
18///
19/// ```
20/// # use iced_core::border::{self, Border};
21/// #
22/// assert_eq!(border::rounded(10), Border::default().rounded(10));
23/// ```
24pub fn rounded(radius: impl Into<Radius>) -> Border {
25    Border::default().rounded(radius)
26}
27
28/// Creates a new [`Border`] with the given [`Color`].
29///
30/// ```
31/// # use iced_core::border::{self, Border};
32/// # use iced_core::Color;
33/// #
34/// assert_eq!(border::color(Color::BLACK), Border::default().color(Color::BLACK));
35/// ```
36pub fn color(color: impl Into<Color>) -> Border {
37    Border::default().color(color)
38}
39
40/// Creates a new [`Border`] with the given `width`.
41///
42/// ```
43/// # use iced_core::border::{self, Border};
44/// # use iced_core::Color;
45/// #
46/// assert_eq!(border::width(10), Border::default().width(10));
47/// ```
48pub fn width(width: impl Into<Pixels>) -> Border {
49    Border::default().width(width)
50}
51
52impl Border {
53    /// Sets the [`Color`] of the [`Border`].
54    pub fn color(self, color: impl Into<Color>) -> Self {
55        Self {
56            color: color.into(),
57            ..self
58        }
59    }
60
61    /// Sets the [`Radius`] of the [`Border`].
62    pub fn rounded(self, radius: impl Into<Radius>) -> Self {
63        Self {
64            radius: radius.into(),
65            ..self
66        }
67    }
68
69    /// Sets the width of the [`Border`].
70    pub fn width(self, width: impl Into<Pixels>) -> Self {
71        Self {
72            width: width.into().0,
73            ..self
74        }
75    }
76}
77
78/// The border radii for the corners of a graphics primitive in the order:
79/// top-left, top-right, bottom-right, bottom-left.
80#[derive(Debug, Clone, Copy, PartialEq, Default)]
81pub struct Radius {
82    /// Top left radius
83    pub top_left: f32,
84    /// Top right radius
85    pub top_right: f32,
86    /// Bottom right radius
87    pub bottom_right: f32,
88    /// Bottom left radius
89    pub bottom_left: f32,
90}
91
92/// Creates a new [`Radius`] with the same value for each corner.
93pub fn radius(value: impl Into<Pixels>) -> Radius {
94    Radius::new(value)
95}
96
97/// Creates a new [`Radius`] with the given top left value.
98pub fn top_left(value: impl Into<Pixels>) -> Radius {
99    Radius::default().top_left(value)
100}
101
102/// Creates a new [`Radius`] with the given top right value.
103pub fn top_right(value: impl Into<Pixels>) -> Radius {
104    Radius::default().top_right(value)
105}
106
107/// Creates a new [`Radius`] with the given bottom right value.
108pub fn bottom_right(value: impl Into<Pixels>) -> Radius {
109    Radius::default().bottom_right(value)
110}
111
112/// Creates a new [`Radius`] with the given bottom left value.
113pub fn bottom_left(value: impl Into<Pixels>) -> Radius {
114    Radius::default().bottom_left(value)
115}
116
117/// Creates a new [`Radius`] with the given value as top left and top right.
118pub fn top(value: impl Into<Pixels>) -> Radius {
119    Radius::default().top(value)
120}
121
122/// Creates a new [`Radius`] with the given value as bottom left and bottom right.
123pub fn bottom(value: impl Into<Pixels>) -> Radius {
124    Radius::default().bottom(value)
125}
126
127/// Creates a new [`Radius`] with the given value as top left and bottom left.
128pub fn left(value: impl Into<Pixels>) -> Radius {
129    Radius::default().left(value)
130}
131
132/// Creates a new [`Radius`] with the given value as top right and bottom right.
133pub fn right(value: impl Into<Pixels>) -> Radius {
134    Radius::default().right(value)
135}
136
137impl Radius {
138    /// Creates a new [`Radius`] with the same value for each corner.
139    pub fn new(value: impl Into<Pixels>) -> Self {
140        let value = value.into().0;
141
142        Self {
143            top_left: value,
144            top_right: value,
145            bottom_right: value,
146            bottom_left: value,
147        }
148    }
149
150    /// Sets the top left value of the [`Radius`].
151    pub fn top_left(self, value: impl Into<Pixels>) -> Self {
152        Self {
153            top_left: value.into().0,
154            ..self
155        }
156    }
157
158    /// Sets the top right value of the [`Radius`].
159    pub fn top_right(self, value: impl Into<Pixels>) -> Self {
160        Self {
161            top_right: value.into().0,
162            ..self
163        }
164    }
165
166    /// Sets the bottom right value of the [`Radius`].
167    pub fn bottom_right(self, value: impl Into<Pixels>) -> Self {
168        Self {
169            bottom_right: value.into().0,
170            ..self
171        }
172    }
173
174    /// Sets the bottom left value of the [`Radius`].
175    pub fn bottom_left(self, value: impl Into<Pixels>) -> Self {
176        Self {
177            bottom_left: value.into().0,
178            ..self
179        }
180    }
181
182    /// Sets the top left and top right values of the [`Radius`].
183    pub fn top(self, value: impl Into<Pixels>) -> Self {
184        let value = value.into().0;
185
186        Self {
187            top_left: value,
188            top_right: value,
189            ..self
190        }
191    }
192
193    /// Sets the bottom left and bottom right values of the [`Radius`].
194    pub fn bottom(self, value: impl Into<Pixels>) -> Self {
195        let value = value.into().0;
196
197        Self {
198            bottom_left: value,
199            bottom_right: value,
200            ..self
201        }
202    }
203
204    /// Sets the top left and bottom left values of the [`Radius`].
205    pub fn left(self, value: impl Into<Pixels>) -> Self {
206        let value = value.into().0;
207
208        Self {
209            top_left: value,
210            bottom_left: value,
211            ..self
212        }
213    }
214
215    /// Sets the top right and bottom right values of the [`Radius`].
216    pub fn right(self, value: impl Into<Pixels>) -> Self {
217        let value = value.into().0;
218
219        Self {
220            top_right: value,
221            bottom_right: value,
222            ..self
223        }
224    }
225}
226
227impl From<f32> for Radius {
228    fn from(radius: f32) -> Self {
229        Self {
230            top_left: radius,
231            top_right: radius,
232            bottom_right: radius,
233            bottom_left: radius,
234        }
235    }
236}
237
238impl From<u8> for Radius {
239    fn from(w: u8) -> Self {
240        Self::from(f32::from(w))
241    }
242}
243
244impl From<u16> for Radius {
245    fn from(w: u16) -> Self {
246        Self::from(f32::from(w))
247    }
248}
249
250impl From<i32> for Radius {
251    fn from(w: i32) -> Self {
252        Self::from(w as f32)
253    }
254}
255
256impl From<Radius> for [f32; 4] {
257    fn from(radi: Radius) -> Self {
258        [
259            radi.top_left,
260            radi.top_right,
261            radi.bottom_right,
262            radi.bottom_left,
263        ]
264    }
265}