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