styled_ext.rs

  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        let size = size.rems();
 35
 36        self.text_size(size)
 37    }
 38
 39    /// The default size for UI text.
 40    ///
 41    /// `0.825rem` or `14px` at the default scale of `1rem` = `16px`.
 42    ///
 43    /// Note: The absolute size of this text will change based on a user's `ui_scale` setting.
 44    ///
 45    /// Use [`text_ui_sm`] for regular-sized text.
 46    fn text_ui(self) -> Self {
 47        let size = UITextSize::default().rems();
 48
 49        self.text_size(size)
 50    }
 51
 52    /// The small size for UI text.
 53    ///
 54    /// `0.75rem` or `12px` at the default scale of `1rem` = `16px`.
 55    ///
 56    /// Note: The absolute size of this text will change based on a user's `ui_scale` setting.
 57    ///
 58    /// Use [`text_ui`] for regular-sized text.
 59    fn text_ui_sm(self) -> Self {
 60        let size = UITextSize::Small.rems();
 61
 62        self.text_size(size)
 63    }
 64
 65    /// The font size for buffer text.
 66    ///
 67    /// Retrieves the default font size, or the user's custom font size if set.
 68    ///
 69    /// This should only be used for text that is displayed in a buffer,
 70    /// or other places that text needs to match the user's buffer font size.
 71    fn text_buffer(self, cx: &mut WindowContext) -> Self {
 72        let settings = ThemeSettings::get_global(cx);
 73        self.text_size(settings.buffer_font_size(cx))
 74    }
 75
 76    /// The [`Surface`](ui2::ElevationIndex::Surface) elevation level, located above the app background, is the standard level for all elements
 77    ///
 78    /// Sets `bg()`, `rounded_lg()`, `border()`, `border_color()`, `shadow()`
 79    ///
 80    /// Example Elements: Title Bar, Panel, Tab Bar, Editor
 81    fn elevation_1(self, cx: &mut WindowContext) -> Self {
 82        elevated(self, cx, ElevationIndex::Surface)
 83    }
 84
 85    /// Non-Modal Elevated Surfaces appear above the [`Surface`](ui2::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.
 86    ///
 87    /// Sets `bg()`, `rounded_lg()`, `border()`, `border_color()`, `shadow()`
 88    ///
 89    /// Examples: Notifications, Palettes, Detached/Floating Windows, Detached/Floating Panels
 90    fn elevation_2(self, cx: &mut WindowContext) -> Self {
 91        elevated(self, cx, ElevationIndex::ElevatedSurface)
 92    }
 93
 94    /// 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.
 95    ///
 96    /// 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.
 97    ///
 98    /// If the element does not have this behavior, it should be rendered at the [`Elevated Surface`](ui2::ElevationIndex::ElevatedSurface) layer.
 99    ///
100    /// Sets `bg()`, `rounded_lg()`, `border()`, `border_color()`, `shadow()`
101    ///
102    /// Examples: Settings Modal, Channel Management, Wizards/Setup UI, Dialogs
103    fn elevation_3(self, cx: &mut WindowContext) -> Self {
104        elevated(self, cx, ElevationIndex::ModalSurface)
105    }
106
107    fn debug_bg_red(self) -> Self {
108        self.bg(gpui::red())
109    }
110
111    fn debug_bg_green(self) -> Self {
112        self.bg(gpui::green())
113    }
114
115    fn debug_bg_blue(self) -> Self {
116        self.bg(gpui::blue())
117    }
118
119    fn debug_bg_yellow(self) -> Self {
120        self.bg(hsla(60. / 360., 1., 0.5, 1.))
121    }
122
123    fn debug_bg_cyan(self) -> Self {
124        self.bg(hsla(160. / 360., 1., 0.5, 1.))
125    }
126
127    fn debug_bg_magenta(self) -> Self {
128        self.bg(hsla(300. / 360., 1., 0.5, 1.))
129    }
130}
131
132impl<E: Styled> StyledExt for E {}