gpui_material_theme/
layout.rs1use crate::{TypeStyle, generated::*};
2
3#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
4#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
5pub enum Direction {
6 #[default]
7 Ltr,
8 Rtl,
9}
10
11#[derive(Clone, Copy, Debug, PartialEq, Eq)]
13pub enum WidthClass {
14 Compact,
15 Medium,
16 Expanded,
17 Large,
18 ExtraLarge,
19}
20
21impl WidthClass {
22 pub fn from_width(width: f32) -> Self {
24 if !width.is_finite() {
25 return Self::Compact;
26 }
27 if width >= MD_SYS_LAYOUT_WINDOW_WIDTH_EXTRA_LARGE {
28 Self::ExtraLarge
29 } else if width >= MD_SYS_LAYOUT_WINDOW_WIDTH_LARGE {
30 Self::Large
31 } else if width >= MD_SYS_LAYOUT_WINDOW_WIDTH_EXPANDED {
32 Self::Expanded
33 } else if width >= MD_SYS_LAYOUT_WINDOW_WIDTH_MEDIUM {
34 Self::Medium
35 } else {
36 Self::Compact
37 }
38 }
39}
40
41#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
43#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
44pub enum Density {
45 Compact,
46 #[default]
47 Standard,
48 Comfortable,
49}
50
51impl Density {
52 pub fn visual_size(self, baseline: f32) -> f32 {
53 let delta = match self {
54 Self::Compact => MD_SYS_LAYOUT_DENSITY_COMPACT,
55 Self::Standard => MD_SYS_LAYOUT_DENSITY_STANDARD,
56 Self::Comfortable => MD_SYS_LAYOUT_DENSITY_COMFORTABLE,
57 };
58 if !baseline.is_finite() || baseline <= 0.0 {
59 return 0.0;
60 }
61 (baseline + delta).max(0.0)
62 }
63
64 pub fn interactive_size(self, baseline: f32) -> f32 {
66 self.visual_size(baseline)
67 .max(MD_SYS_LAYOUT_MINIMUM_INTERACTIVE_SIZE)
68 }
69}
70
71#[derive(Clone, Copy, Debug, PartialEq, Eq, Default)]
73#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
74pub struct LayoutPolicy {
75 pub density: Density,
76 pub direction: Direction,
77}
78
79impl LayoutPolicy {
80 pub fn scale_text(style: TypeStyle, font_scale: f32) -> TypeStyle {
83 if !font_scale.is_finite() || font_scale <= 0.0 {
84 return style;
85 }
86 let scaled = TypeStyle {
87 size: style.size * font_scale,
88 line_height: style.line_height * font_scale,
89 tracking: style.tracking * font_scale,
90 ..style
91 };
92 if [scaled.size, scaled.line_height, scaled.tracking]
93 .into_iter()
94 .all(f32::is_finite)
95 && scaled.size > 0.0
96 && scaled.line_height > 0.0
97 {
98 scaled
99 } else {
100 style
101 }
102 }
103
104 pub const fn horizontal<T: Copy>(self, start: T, end: T) -> (T, T) {
106 match self.direction {
107 Direction::Ltr => (start, end),
108 Direction::Rtl => (end, start),
109 }
110 }
111}