ailloli_ui_core/geometry/edge_insets.rs
1//! Four-sided logical-pixel padding or margin values.
2
3/// Padding or margin insets on all four sides (logical pixels).
4///
5/// # Examples
6///
7/// ```
8/// use ailloli_ui_core::EdgeInsets;
9/// assert_eq!(EdgeInsets::all(4.0).horizontal(), 8.0);
10/// ```
11#[derive(Debug, Clone, Copy, Default, PartialEq)]
12pub struct EdgeInsets {
13 /// Left inset in logical pixels.
14 pub left: f32,
15 /// Top inset in logical pixels.
16 pub top: f32,
17 /// Right inset in logical pixels.
18 pub right: f32,
19 /// Bottom inset in logical pixels.
20 pub bottom: f32,
21}
22
23impl EdgeInsets {
24 /// Creates per-side insets without clamping negative or non-finite values.
25 ///
26 /// # Examples
27 ///
28 /// ```
29 /// use ailloli_ui_core::EdgeInsets;
30 /// assert_eq!(EdgeInsets::new(1.0, 2.0, 3.0, 4.0).right, 3.0);
31 /// ```
32 pub const fn new(left: f32, top: f32, right: f32, bottom: f32) -> Self {
33 Self {
34 left,
35 top,
36 right,
37 bottom,
38 }
39 }
40
41 /// Creates equal insets on all four sides.
42 ///
43 /// # Examples
44 ///
45 /// ```
46 /// use ailloli_ui_core::EdgeInsets;
47 /// assert_eq!(EdgeInsets::all(3.0).bottom, 3.0);
48 /// ```
49 pub const fn all(v: f32) -> Self {
50 Self::new(v, v, v, v)
51 }
52
53 /// `left + right`.
54 ///
55 /// # Examples
56 ///
57 /// ```
58 /// use ailloli_ui_core::EdgeInsets;
59 /// assert_eq!(EdgeInsets::new(1.0, 2.0, 3.0, 4.0).horizontal(), 4.0);
60 /// ```
61 pub const fn horizontal(&self) -> f32 {
62 self.left + self.right
63 }
64
65 /// `top + bottom`.
66 ///
67 /// # Examples
68 ///
69 /// ```
70 /// use ailloli_ui_core::EdgeInsets;
71 /// assert_eq!(EdgeInsets::new(1.0, 2.0, 3.0, 4.0).vertical(), 6.0);
72 /// ```
73 pub const fn vertical(&self) -> f32 {
74 self.top + self.bottom
75 }
76}