1//! Draw lines around containers.
2use crate::{Color, Pixels};
34/// A border.
5#[derive(Debug, Clone, Copy, PartialEq, Default)]
6pub struct Border {
7/// The color of the border.
8pub color: Color,
910/// The width of the border.
11pub width: f32,
1213/// The [`Radius`] of the border.
14pub radius: Radius,
15}
1617/// 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}
2728/// 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}
3940/// 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}
5152impl Border {
53/// Sets the [`Color`] of the [`Border`].
54pub fn color(self, color: impl Into<Color>) -> Self {
55Self {
56 color: color.into(),
57 ..self
58}
59 }
6061/// Sets the [`Radius`] of the [`Border`].
62pub fn rounded(self, radius: impl Into<Radius>) -> Self {
63Self {
64 radius: radius.into(),
65 ..self
66}
67 }
6869/// Sets the width of the [`Border`].
70pub fn width(self, width: impl Into<Pixels>) -> Self {
71Self {
72 width: width.into().0,
73 ..self
74}
75 }
76}
7778/// 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
83pub top_left: f32,
84/// Top right radius
85pub top_right: f32,
86/// Bottom right radius
87pub bottom_right: f32,
88/// Bottom left radius
89pub bottom_left: f32,
90}
9192/// Creates a new [`Radius`] with the same value for each corner.
93pub fn radius(value: impl Into<Pixels>) -> Radius {
94 Radius::new(value)
95}
9697/// 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}
101102/// 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}
106107/// 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}
111112/// 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}
116117/// 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}
121122/// 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}
126127/// 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}
131132/// 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}
136137impl Radius {
138/// Creates a new [`Radius`] with the same value for each corner.
139pub fn new(value: impl Into<Pixels>) -> Self {
140let value = value.into().0;
141142Self {
143 top_left: value,
144 top_right: value,
145 bottom_right: value,
146 bottom_left: value,
147 }
148 }
149150/// Sets the top left value of the [`Radius`].
151pub fn top_left(self, value: impl Into<Pixels>) -> Self {
152Self {
153 top_left: value.into().0,
154 ..self
155}
156 }
157158/// Sets the top right value of the [`Radius`].
159pub fn top_right(self, value: impl Into<Pixels>) -> Self {
160Self {
161 top_right: value.into().0,
162 ..self
163}
164 }
165166/// Sets the bottom right value of the [`Radius`].
167pub fn bottom_right(self, value: impl Into<Pixels>) -> Self {
168Self {
169 bottom_right: value.into().0,
170 ..self
171}
172 }
173174/// Sets the bottom left value of the [`Radius`].
175pub fn bottom_left(self, value: impl Into<Pixels>) -> Self {
176Self {
177 bottom_left: value.into().0,
178 ..self
179}
180 }
181182/// Sets the top left and top right values of the [`Radius`].
183pub fn top(self, value: impl Into<Pixels>) -> Self {
184let value = value.into().0;
185186Self {
187 top_left: value,
188 top_right: value,
189 ..self
190}
191 }
192193/// Sets the bottom left and bottom right values of the [`Radius`].
194pub fn bottom(self, value: impl Into<Pixels>) -> Self {
195let value = value.into().0;
196197Self {
198 bottom_left: value,
199 bottom_right: value,
200 ..self
201}
202 }
203204/// Sets the top left and bottom left values of the [`Radius`].
205pub fn left(self, value: impl Into<Pixels>) -> Self {
206let value = value.into().0;
207208Self {
209 top_left: value,
210 bottom_left: value,
211 ..self
212}
213 }
214215/// Sets the top right and bottom right values of the [`Radius`].
216pub fn right(self, value: impl Into<Pixels>) -> Self {
217let value = value.into().0;
218219Self {
220 top_right: value,
221 bottom_right: value,
222 ..self
223}
224 }
225}
226227impl From<f32> for Radius {
228fn from(radius: f32) -> Self {
229Self {
230 top_left: radius,
231 top_right: radius,
232 bottom_right: radius,
233 bottom_left: radius,
234 }
235 }
236}
237238impl From<u8> for Radius {
239fn from(w: u8) -> Self {
240Self::from(f32::from(w))
241 }
242}
243244impl From<u16> for Radius {
245fn from(w: u16) -> Self {
246Self::from(f32::from(w))
247 }
248}
249250impl From<i32> for Radius {
251fn from(w: i32) -> Self {
252Self::from(w as f32)
253 }
254}
255256impl From<Radius> for [f32; 4] {
257fn from(radi: Radius) -> Self {
258 [
259 radi.top_left,
260 radi.top_right,
261 radi.bottom_right,
262 radi.bottom_left,
263 ]
264 }
265}