ailloli_ui_core/style/
box_model.rs

1//! Composable logical-pixel margin and padding values.
2
3use crate::geometry::EdgeInsets;
4
5/// Composable margin + padding (also mirrored on [`super::LayoutStyle`]).
6///
7/// # Examples
8///
9/// ```
10/// use ailloli_ui_core::style::BoxModel;
11/// assert_eq!(BoxModel::new().padding(8.0).padding.left, 8.0);
12/// ```
13#[derive(Debug, Clone, Copy, PartialEq)]
14pub struct BoxModel {
15    /// Space outside the decorated widget border, in logical pixels.
16    pub margin: EdgeInsets,
17    /// Space between the border and content, in logical pixels.
18    pub padding: EdgeInsets,
19}
20
21impl BoxModel {
22    /// Creates zero margin and zero padding.
23    ///
24    /// # Examples
25    ///
26    /// ```
27    /// use ailloli_ui_core::style::BoxModel;
28    /// assert_eq!(BoxModel::new().margin.left, 0.0);
29    /// ```
30    pub fn new() -> Self {
31        Self {
32            margin: EdgeInsets::default(),
33            padding: EdgeInsets::default(),
34        }
35    }
36
37    /// Creates zero margin with the supplied per-edge padding.
38    ///
39    /// # Examples
40    ///
41    /// ```
42    /// use ailloli_ui_core::EdgeInsets;
43    /// use ailloli_ui_core::style::BoxModel;
44    /// assert_eq!(BoxModel::with_padding(EdgeInsets::all(4.0)).padding.top, 4.0);
45    /// ```
46    pub fn with_padding(padding: EdgeInsets) -> Self {
47        Self {
48            margin: EdgeInsets::default(),
49            padding,
50        }
51    }
52
53    /// Creates the supplied per-edge margin with zero padding.
54    ///
55    /// # Examples
56    ///
57    /// ```
58    /// use ailloli_ui_core::EdgeInsets;
59    /// use ailloli_ui_core::style::BoxModel;
60    /// assert_eq!(BoxModel::with_margin(EdgeInsets::all(4.0)).margin.top, 4.0);
61    /// ```
62    pub fn with_margin(margin: EdgeInsets) -> Self {
63        Self {
64            margin,
65            padding: EdgeInsets::default(),
66        }
67    }
68
69    /// Replaces all four margin edges with `value` logical pixels.
70    ///
71    /// Negative and non-finite values are stored for the layout layer to handle.
72    ///
73    /// # Examples
74    ///
75    /// ```
76    /// use ailloli_ui_core::style::BoxModel;
77    /// assert_eq!(BoxModel::new().margin(6.0).margin.right, 6.0);
78    /// ```
79    pub fn margin(mut self, value: f32) -> Self {
80        self.margin = EdgeInsets::all(value);
81        self
82    }
83
84    /// Replaces all four padding edges with `value` logical pixels.
85    ///
86    /// Negative and non-finite values are stored for the layout layer to handle.
87    ///
88    /// # Examples
89    ///
90    /// ```
91    /// use ailloli_ui_core::style::BoxModel;
92    /// assert_eq!(BoxModel::new().padding(6.0).padding.bottom, 6.0);
93    /// ```
94    pub fn padding(mut self, value: f32) -> Self {
95        self.padding = EdgeInsets::all(value);
96        self
97    }
98}
99
100impl Default for BoxModel {
101    fn default() -> Self {
102        Self::new()
103    }
104}