spacing.rs

 1use gpui::*;
 2use settings::Settings;
 3use theme::{ThemeSettings, UiDensity};
 4
 5#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
 6pub enum Spacing {
 7    /// No spacing
 8    None,
 9    /// Usually a one pixel spacing. Grows to 2px in comfortable density.
10    /// @16px/rem: `1px`|`1px`|`2px`
11    XXSmall,
12    /// Extra small spacing - @16px/rem: `1px`|`2px`|`4px`
13    ///
14    /// Relative to the user's `ui_font_size` and [UiDensity] setting.
15    XSmall,
16    /// Small spacing - @16px/rem: `2px`|`4px`|`6px`
17    ///
18    /// Relative to the user's `ui_font_size` and [UiDensity] setting.
19    Small,
20    /// Medium spacing - @16px/rem: `3px`|`6px`|`8px`
21    ///
22    /// Relative to the user's `ui_font_size` and [UiDensity] setting.
23    Medium,
24    /// Large spacing - @16px/rem: `4px`|`8px`|`10px`
25    ///
26    /// Relative to the user's `ui_font_size` and [UiDensity] setting.
27    Large,
28    XLarge,
29    XXLarge,
30}
31
32impl Spacing {
33    pub fn spacing_ratio(self, cx: &WindowContext) -> f32 {
34        match ThemeSettings::get_global(cx).ui_density {
35            UiDensity::Compact => match self {
36                Spacing::None => 0.0,
37                Spacing::XXSmall => 1. / 16.,
38                Spacing::XSmall => 1. / 16.,
39                Spacing::Small => 2. / 16.,
40                Spacing::Medium => 3. / 16.,
41                Spacing::Large => 4. / 16.,
42                Spacing::XLarge => 8. / 16.,
43                Spacing::XXLarge => 12. / 16.,
44            },
45            UiDensity::Default => match self {
46                Spacing::None => 0.0,
47                Spacing::XXSmall => 1. / 16.,
48                Spacing::XSmall => 2. / 16.,
49                Spacing::Small => 4. / 16.,
50                Spacing::Medium => 6. / 16.,
51                Spacing::Large => 8. / 16.,
52                Spacing::XLarge => 12. / 16.,
53                #[allow(clippy::eq_op)]
54                Spacing::XXLarge => 16. / 16.,
55            },
56            UiDensity::Comfortable => match self {
57                Spacing::None => 0.0,
58                Spacing::XXSmall => 2. / 16.,
59                Spacing::XSmall => 3. / 16.,
60                Spacing::Small => 6. / 16.,
61                Spacing::Medium => 8. / 16.,
62                Spacing::Large => 10. / 16.,
63                #[allow(clippy::eq_op)]
64                Spacing::XLarge => 16. / 16.,
65                Spacing::XXLarge => 20. / 16.,
66            },
67        }
68    }
69
70    pub fn rems(self, cx: &WindowContext) -> Rems {
71        rems(self.spacing_ratio(cx))
72    }
73
74    pub fn px(self, cx: &WindowContext) -> Pixels {
75        let ui_font_size_f32: f32 = ThemeSettings::get_global(cx).ui_font_size.into();
76
77        px(ui_font_size_f32 * self.spacing_ratio(cx))
78    }
79}
80
81pub fn user_spacing_style(cx: &WindowContext) -> UiDensity {
82    ThemeSettings::get_global(cx).ui_density
83}
84
85pub fn custom_spacing(cx: &WindowContext, size: f32) -> Rems {
86    crate::rems_from_px(size * user_spacing_style(cx).spacing_ratio())
87}