1use gpui::{hsla, px, Styled, WindowContext};
2use settings::Settings;
3use theme::ThemeSettings;
4
5use crate::prelude::*;
6use crate::{ElevationIndex, UiTextSize};
7
8fn elevated<E: Styled>(this: E, cx: &mut WindowContext, index: ElevationIndex) -> E {
9 this.bg(cx.theme().colors().elevated_surface_background)
10 .z_index(index.z_index())
11 .rounded(px(8.))
12 .border()
13 .border_color(cx.theme().colors().border_variant)
14 .shadow(index.shadow())
15}
16
17/// Extends [`Styled`](gpui::Styled) with Zed specific styling methods.
18pub trait StyledExt: Styled + Sized {
19 /// Horizontally stacks elements.
20 ///
21 /// Sets `flex()`, `flex_row()`, `items_center()`
22 fn h_flex(self) -> Self {
23 self.flex().flex_row().items_center()
24 }
25
26 /// Vertically stacks elements.
27 ///
28 /// Sets `flex()`, `flex_col()`
29 fn v_flex(self) -> Self {
30 self.flex().flex_col()
31 }
32
33 fn text_ui_size(self, size: UiTextSize) -> Self {
34 self.text_size(size.rems())
35 }
36
37 /// The default size for UI text.
38 ///
39 /// `0.825rem` or `14px` at the default scale of `1rem` = `16px`.
40 ///
41 /// Note: The absolute size of this text will change based on a user's `ui_scale` setting.
42 ///
43 /// Use [`text_ui_sm`] for regular-sized text.
44 fn text_ui(self) -> Self {
45 self.text_size(UiTextSize::default().rems())
46 }
47
48 /// The small size for UI text.
49 ///
50 /// `0.75rem` or `12px` at the default scale of `1rem` = `16px`.
51 ///
52 /// Note: The absolute size of this text will change based on a user's `ui_scale` setting.
53 ///
54 /// Use [`text_ui`] for regular-sized text.
55 fn text_ui_sm(self) -> Self {
56 self.text_size(UiTextSize::Small.rems())
57 }
58
59 /// The extra small size for UI text.
60 ///
61 /// `0.625rem` or `10px` at the default scale of `1rem` = `16px`.
62 ///
63 /// Note: The absolute size of this text will change based on a user's `ui_scale` setting.
64 ///
65 /// Use [`text_ui`] for regular-sized text.
66 fn text_ui_xs(self) -> Self {
67 self.text_size(UiTextSize::XSmall.rems())
68 }
69
70 /// The font size for buffer text.
71 ///
72 /// Retrieves the default font size, or the user's custom font size if set.
73 ///
74 /// This should only be used for text that is displayed in a buffer,
75 /// or other places that text needs to match the user's buffer font size.
76 fn text_buffer(self, cx: &mut WindowContext) -> Self {
77 let settings = ThemeSettings::get_global(cx);
78 self.text_size(settings.buffer_font_size(cx))
79 }
80
81 /// The [`Surface`](ui::ElevationIndex::Surface) elevation level, located above the app background, is the standard level for all elements
82 ///
83 /// Sets `bg()`, `rounded_lg()`, `border()`, `border_color()`, `shadow()`
84 ///
85 /// Example Elements: Title Bar, Panel, Tab Bar, Editor
86 fn elevation_1(self, cx: &mut WindowContext) -> Self {
87 elevated(self, cx, ElevationIndex::Surface)
88 }
89
90 /// Non-Modal Elevated Surfaces appear above the [`Surface`](ui::ElevationIndex::Surface) layer and is used for things that should appear above most UI elements like an editor or panel, but not elements like popovers, context menus, modals, etc.
91 ///
92 /// Sets `bg()`, `rounded_lg()`, `border()`, `border_color()`, `shadow()`
93 ///
94 /// Examples: Notifications, Palettes, Detached/Floating Windows, Detached/Floating Panels
95 fn elevation_2(self, cx: &mut WindowContext) -> Self {
96 elevated(self, cx, ElevationIndex::ElevatedSurface)
97 }
98
99 /// Modal Surfaces are used for elements that should appear above all other UI elements and are located above the wash layer. This is the maximum elevation at which UI elements can be rendered in their default state.
100 ///
101 /// Elements rendered at this layer should have an enforced behavior: Any interaction outside of the modal will either dismiss the modal or prompt an action (Save your progress, etc) then dismiss the modal.
102 ///
103 /// If the element does not have this behavior, it should be rendered at the [`Elevated Surface`](ui::ElevationIndex::ElevatedSurface) layer.
104 ///
105 /// Sets `bg()`, `rounded_lg()`, `border()`, `border_color()`, `shadow()`
106 ///
107 /// Examples: Settings Modal, Channel Management, Wizards/Setup UI, Dialogs
108 fn elevation_3(self, cx: &mut WindowContext) -> Self {
109 elevated(self, cx, ElevationIndex::ModalSurface)
110 }
111
112 /// The theme's primary border color.
113 fn border_primary(self, cx: &mut WindowContext) -> Self {
114 self.border_color(cx.theme().colors().border)
115 }
116
117 /// The theme's secondary or muted border color.
118 fn border_muted(self, cx: &mut WindowContext) -> Self {
119 self.border_color(cx.theme().colors().border_variant)
120 }
121
122 fn debug_bg_red(self) -> Self {
123 self.bg(hsla(0. / 360., 1., 0.5, 1.))
124 }
125
126 fn debug_bg_green(self) -> Self {
127 self.bg(hsla(120. / 360., 1., 0.5, 1.))
128 }
129
130 fn debug_bg_blue(self) -> Self {
131 self.bg(hsla(240. / 360., 1., 0.5, 1.))
132 }
133
134 fn debug_bg_yellow(self) -> Self {
135 self.bg(hsla(60. / 360., 1., 0.5, 1.))
136 }
137
138 fn debug_bg_cyan(self) -> Self {
139 self.bg(hsla(160. / 360., 1., 0.5, 1.))
140 }
141
142 fn debug_bg_magenta(self) -> Self {
143 self.bg(hsla(300. / 360., 1., 0.5, 1.))
144 }
145}
146
147impl<E: Styled> StyledExt for E {}