Skip to main content

euv_core/style/layout_prop/
impl.rs

1use super::*;
2
3/// Default layout style matching standard Flexbox defaults.
4impl Default for LayoutStyle {
5    /// Creates default layout properties (`Flex`, `Row`, `flex_shrink: 1.0`).
6    ///
7    /// # Returns
8    ///
9    /// - `Self` - Default `LayoutStyle`.
10    fn default() -> Self {
11        Self {
12            display: Display::Flex,
13            position: Position::Relative,
14            flex_direction: FlexDirection::Row,
15            flex_wrap: FlexWrap::NoWrap,
16            justify_content: JustifyContent::Start,
17            align_items: AlignItems::Stretch,
18            align_self: AlignSelf::Auto,
19            overflow: Overflow::Visible,
20            flex_grow: 0.0,
21            flex_shrink: 1.0,
22            flex_basis: Length::Auto,
23            grid_columns: 1,
24            width: Length::Auto,
25            height: Length::Auto,
26            min_width: Length::Auto,
27            min_height: Length::Auto,
28            max_width: Length::Auto,
29            max_height: Length::Auto,
30            margin: Edges::default(),
31            padding: Edges::default(),
32            row_gap: 0.0,
33            column_gap: 0.0,
34            top: None,
35            right: None,
36            bottom: None,
37            left: None,
38            z_index: 0,
39            aspect_ratio: None,
40        }
41    }
42}
43
44/// Breakpoint classification helpers.
45impl Breakpoint {
46    /// Classifies a logical viewport width into its canonical responsive breakpoint.
47    ///
48    /// # Arguments
49    ///
50    /// - `f32` - Logical viewport width in pixels.
51    ///
52    /// # Returns
53    ///
54    /// - `Self` - Matching `Breakpoint`.
55    #[must_use]
56    pub fn from_width(width: f32) -> Self {
57        if width < 768.0 {
58            Self::Compact
59        } else if width < 1024.0 {
60            Self::Medium
61        } else {
62            Self::Expanded
63        }
64    }
65
66    /// Returns true when the viewport is compact mobile width (`< 768px`).
67    ///
68    /// # Arguments
69    ///
70    /// - `&Self` - Breakpoint value.
71    ///
72    /// # Returns
73    ///
74    /// - `bool` - True for `Compact`.
75    #[must_use]
76    pub fn is_compact(&self) -> bool {
77        *self == Self::Compact
78    }
79}
80
81/// Non-panicking optional offset readers for `LayoutStyle`.
82impl LayoutStyle {
83    /// Returns the optional `top` offset.
84    ///
85    /// # Arguments
86    ///
87    /// - `&Self` - Layout style.
88    ///
89    /// # Returns
90    ///
91    /// - `Option<f32>` - Top offset if specified.
92    #[must_use]
93    pub fn top_offset(&self) -> Option<f32> {
94        let Self { top, .. } = *self;
95        top
96    }
97
98    /// Returns the optional `right` offset.
99    ///
100    /// # Arguments
101    ///
102    /// - `&Self` - Layout style.
103    ///
104    /// # Returns
105    ///
106    /// - `Option<f32>` - Right offset if specified.
107    #[must_use]
108    pub fn right_offset(&self) -> Option<f32> {
109        let Self { right, .. } = *self;
110        right
111    }
112
113    /// Returns the optional `bottom` offset.
114    ///
115    /// # Arguments
116    ///
117    /// - `&Self` - Layout style.
118    ///
119    /// # Returns
120    ///
121    /// - `Option<f32>` - Bottom offset if specified.
122    #[must_use]
123    pub fn bottom_offset(&self) -> Option<f32> {
124        let Self { bottom, .. } = *self;
125        bottom
126    }
127
128    /// Returns the optional `left` offset.
129    ///
130    /// # Arguments
131    ///
132    /// - `&Self` - Layout style.
133    ///
134    /// # Returns
135    ///
136    /// - `Option<f32>` - Left offset if specified.
137    #[must_use]
138    pub fn left_offset(&self) -> Option<f32> {
139        let Self { left, .. } = *self;
140        left
141    }
142
143    /// Returns the optional aspect ratio (`width / height`).
144    ///
145    /// # Arguments
146    ///
147    /// - `&Self` - Layout style.
148    ///
149    /// # Returns
150    ///
151    /// - `Option<f32>` - Aspect ratio if specified.
152    #[must_use]
153    pub fn aspect_ratio_value(&self) -> Option<f32> {
154        let Self { aspect_ratio, .. } = *self;
155        aspect_ratio
156    }
157}