Skip to main content

gpui_material_theme/
layout.rs

1use crate::{TypeStyle, generated::*};
2
3/// Logical horizontal reading and layout direction.
4#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
5#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
6pub enum Direction {
7    /// Left-to-right layout direction (default).
8    #[default]
9    Ltr,
10    /// Right-to-left layout direction.
11    Rtl,
12}
13
14/// Material window width classes, based on available logical width.
15#[derive(Clone, Copy, Debug, PartialEq, Eq)]
16pub enum WidthClass {
17    /// Compact width (`< 600 dp`).
18    Compact,
19    /// Medium width (`600..840 dp`).
20    Medium,
21    /// Expanded width (`840..1200 dp`).
22    Expanded,
23    /// Large desktop width (`1200..1600 dp`).
24    Large,
25    /// Extra-large desktop width (`>= 1600 dp`).
26    ExtraLarge,
27}
28
29impl WidthClass {
30    /// Thresholds are inclusive. Non-finite and negative widths resolve to Compact.
31    pub fn from_width(width: f32) -> Self {
32        if !width.is_finite() {
33            return Self::Compact;
34        }
35        if width >= MD_SYS_LAYOUT_WINDOW_WIDTH_EXTRA_LARGE {
36            Self::ExtraLarge
37        } else if width >= MD_SYS_LAYOUT_WINDOW_WIDTH_LARGE {
38            Self::Large
39        } else if width >= MD_SYS_LAYOUT_WINDOW_WIDTH_EXPANDED {
40            Self::Expanded
41        } else if width >= MD_SYS_LAYOUT_WINDOW_WIDTH_MEDIUM {
42            Self::Medium
43        } else {
44            Self::Compact
45        }
46    }
47}
48
49/// Desktop density adjustment for a component's visual size.
50#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
51#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
52pub enum Density {
53    /// Tighter visual spacing for dense desktop layouts.
54    Compact,
55    /// Default Material visual density.
56    #[default]
57    Standard,
58    /// Roomier visual spacing.
59    Comfortable,
60}
61
62impl Density {
63    /// Applies this density's delta to a baseline visual size in logical units.
64    ///
65    /// Non-finite or non-positive baselines return `0.0`.
66    pub fn visual_size(self, baseline: f32) -> f32 {
67        let delta = match self {
68            Self::Compact => MD_SYS_LAYOUT_DENSITY_COMPACT,
69            Self::Standard => MD_SYS_LAYOUT_DENSITY_STANDARD,
70            Self::Comfortable => MD_SYS_LAYOUT_DENSITY_COMFORTABLE,
71        };
72        if !baseline.is_finite() || baseline <= 0.0 {
73            return 0.0;
74        }
75        (baseline + delta).max(0.0)
76    }
77
78    /// The target stays at least 48 logical units even for dense visuals.
79    pub fn interactive_size(self, baseline: f32) -> f32 {
80        self.visual_size(baseline)
81            .max(MD_SYS_LAYOUT_MINIMUM_INTERACTIVE_SIZE)
82    }
83}
84
85/// Pure layout inputs. Widths and geometry are already in logical units.
86#[derive(Clone, Copy, Debug, PartialEq, Eq, Default)]
87#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
88pub struct LayoutPolicy {
89    /// Visual density setting for component sizing.
90    pub density: Density,
91    /// Logical horizontal layout direction (`Ltr` or `Rtl`).
92    pub direction: Direction,
93}
94
95impl LayoutPolicy {
96    /// Converts text metrics using the host's user font scale. Invalid or
97    /// overflowing scale factors use the baseline metrics.
98    pub fn scale_text(style: TypeStyle, font_scale: f32) -> TypeStyle {
99        if !font_scale.is_finite() || font_scale <= 0.0 {
100            return style;
101        }
102        let scaled = TypeStyle {
103            size: style.size * font_scale,
104            line_height: style.line_height * font_scale,
105            tracking: style.tracking * font_scale,
106            ..style
107        };
108        if [scaled.size, scaled.line_height, scaled.tracking]
109            .into_iter()
110            .all(f32::is_finite)
111            && scaled.size > 0.0
112            && scaled.line_height > 0.0
113        {
114            scaled
115        } else {
116            style
117        }
118    }
119
120    /// Maps logical start and end to physical left and right.
121    pub const fn horizontal<T: Copy>(self, start: T, end: T) -> (T, T) {
122        match self.direction {
123            Direction::Ltr => (start, end),
124            Direction::Rtl => (end, start),
125        }
126    }
127}
128
129/// Sanitizes a logical scalar dimension to a finite non-negative value.
130///
131/// `NaN`, infinities, negative values, and `-0.0` normalize to `0.0`.
132pub const fn non_negative_f32(value: f32) -> f32 {
133    if !value.is_finite() || value <= 0.0 {
134        0.0
135    } else {
136        value
137    }
138}
139
140/// Direction-aware logical edge insets in density-independent units (`dp`).
141///
142/// Shared across components to avoid duplicating per-component padding structs.
143#[derive(Clone, Copy, Debug, Default, PartialEq)]
144#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
145pub struct LogicalEdges {
146    /// Top edge inset in logical units.
147    pub top: f32,
148    /// Bottom edge inset in logical units.
149    pub bottom: f32,
150    /// Inline-start edge inset (left in LTR, right in RTL).
151    pub start: f32,
152    /// Inline-end edge inset (right in LTR, left in RTL).
153    pub end: f32,
154}
155
156impl LogicalEdges {
157    /// Zero insets on all four sides.
158    pub const ZERO: Self = Self {
159        top: 0.0,
160        bottom: 0.0,
161        start: 0.0,
162        end: 0.0,
163    };
164
165    /// Creates uniform insets on all four sides, clamping invalid or negative values to `0.0`.
166    pub const fn all(value: f32) -> Self {
167        let value = non_negative_f32(value);
168        Self {
169            top: value,
170            bottom: value,
171            start: value,
172            end: value,
173        }
174    }
175
176    /// Creates symmetric insets from `horizontal` (start/end) and `vertical` (top/bottom).
177    pub const fn symmetric(horizontal: f32, vertical: f32) -> Self {
178        let horizontal = non_negative_f32(horizontal);
179        let vertical = non_negative_f32(vertical);
180        Self {
181            top: vertical,
182            bottom: vertical,
183            start: horizontal,
184            end: horizontal,
185        }
186    }
187
188    /// Creates explicit logical insets in CSS-like `(top, end, bottom, start)` order.
189    pub const fn new(top: f32, end: f32, bottom: f32, start: f32) -> Self {
190        Self {
191            top: non_negative_f32(top),
192            bottom: non_negative_f32(bottom),
193            start: non_negative_f32(start),
194            end: non_negative_f32(end),
195        }
196    }
197
198    /// Sets the top inset.
199    pub const fn top(mut self, value: f32) -> Self {
200        self.top = non_negative_f32(value);
201        self
202    }
203
204    /// Sets the bottom inset.
205    pub const fn bottom(mut self, value: f32) -> Self {
206        self.bottom = non_negative_f32(value);
207        self
208    }
209
210    /// Sets the inline-start inset.
211    pub const fn start(mut self, value: f32) -> Self {
212        self.start = non_negative_f32(value);
213        self
214    }
215
216    /// Sets the inline-end inset.
217    pub const fn end(mut self, value: f32) -> Self {
218        self.end = non_negative_f32(value);
219        self
220    }
221
222    /// Sets both `start` and `end` insets.
223    pub const fn horizontal(mut self, value: f32) -> Self {
224        let value = non_negative_f32(value);
225        self.start = value;
226        self.end = value;
227        self
228    }
229
230    /// Sets both `top` and `bottom` insets.
231    pub const fn vertical(mut self, value: f32) -> Self {
232        let value = non_negative_f32(value);
233        self.top = value;
234        self.bottom = value;
235        self
236    }
237
238    /// Returns a copy with every side sanitized via [`non_negative_f32`].
239    pub const fn sanitize(self) -> Self {
240        Self {
241            top: non_negative_f32(self.top),
242            bottom: non_negative_f32(self.bottom),
243            start: non_negative_f32(self.start),
244            end: non_negative_f32(self.end),
245        }
246    }
247
248    /// Resolves sanitized insets to physical `[top, right, bottom, left]` order for `direction`.
249    pub const fn resolve(self, direction: Direction) -> [f32; 4] {
250        let clean = self.sanitize();
251        let (left, right) = match direction {
252            Direction::Ltr => (clean.start, clean.end),
253            Direction::Rtl => (clean.end, clean.start),
254        };
255        [clean.top, right, clean.bottom, left]
256    }
257}