gpui_material_theme/
layout.rs1use crate::{TypeStyle, generated::*};
2
3#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
5#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
6pub enum Direction {
7 #[default]
9 Ltr,
10 Rtl,
12}
13
14#[derive(Clone, Copy, Debug, PartialEq, Eq)]
16pub enum WidthClass {
17 Compact,
19 Medium,
21 Expanded,
23 Large,
25 ExtraLarge,
27}
28
29impl WidthClass {
30 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#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
51#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
52pub enum Density {
53 Compact,
55 #[default]
57 Standard,
58 Comfortable,
60}
61
62impl Density {
63 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 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#[derive(Clone, Copy, Debug, PartialEq, Eq, Default)]
87#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
88pub struct LayoutPolicy {
89 pub density: Density,
91 pub direction: Direction,
93}
94
95impl LayoutPolicy {
96 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 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}