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}