styled_ext.rs

  1use gpui::{App, Styled, hsla};
  2
  3use crate::ElevationIndex;
  4use crate::prelude::*;
  5
  6fn elevated<E: Styled>(this: E, cx: &App, index: ElevationIndex) -> E {
  7    this.bg(cx.theme().colors().elevated_surface_background)
  8        .rounded_lg()
  9        .border_1()
 10        .border_color(cx.theme().colors().border_variant)
 11        .shadow(index.shadow(cx))
 12}
 13
 14fn elevated_borderless<E: Styled>(this: E, cx: &mut App, index: ElevationIndex) -> E {
 15    this.bg(cx.theme().colors().elevated_surface_background)
 16        .rounded_lg()
 17        .shadow(index.shadow(cx))
 18}
 19
 20/// Extends [`gpui::Styled`] with Zed-specific styling methods.
 21pub trait StyledExt: Styled + Sized {
 22    /// Horizontally stacks elements.
 23    ///
 24    /// Sets `flex()`, `flex_row()`, `items_center()`
 25    fn h_flex(self) -> Self {
 26        self.flex().flex_row().items_center()
 27    }
 28
 29    /// Vertically stacks elements.
 30    ///
 31    /// Sets `flex()`, `flex_col()`
 32    fn v_flex(self) -> Self {
 33        self.flex().flex_col()
 34    }
 35
 36    /// The [`Surface`](ElevationIndex::Surface) elevation level, located above the app background, is the standard level for all elements
 37    ///
 38    /// Sets `bg()`, `rounded_lg()`, `border()`, `border_color()`, `shadow()`
 39    ///
 40    /// Example Elements: Title Bar, Panel, Tab Bar, Editor
 41    fn elevation_1(self, cx: &mut App) -> Self {
 42        elevated(self, cx, ElevationIndex::Surface)
 43    }
 44
 45    /// See [`elevation_1`](Self::elevation_1).
 46    ///
 47    /// Renders a borderless version [`elevation_1`](Self::elevation_1).
 48    fn elevation_1_borderless(self, cx: &mut App) -> Self {
 49        elevated_borderless(self, cx, ElevationIndex::Surface)
 50    }
 51
 52    /// Non-Modal Elevated Surfaces appear above the [`Surface`](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.
 53    ///
 54    /// Sets `bg()`, `rounded_lg()`, `border()`, `border_color()`, `shadow()`
 55    ///
 56    /// Examples: Notifications, Palettes, Detached/Floating Windows, Detached/Floating Panels
 57    fn elevation_2(self, cx: &App) -> Self {
 58        elevated(self, cx, ElevationIndex::ElevatedSurface)
 59    }
 60
 61    /// See [`elevation_2`](Self::elevation_2).
 62    ///
 63    /// Renders a borderless version [`elevation_2`](Self::elevation_2).
 64    fn elevation_2_borderless(self, cx: &mut App) -> Self {
 65        elevated_borderless(self, cx, ElevationIndex::ElevatedSurface)
 66    }
 67
 68    /// 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.
 69    ///
 70    /// 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.
 71    ///
 72    /// If the element does not have this behavior, it should be rendered at the [`Elevated Surface`](ElevationIndex::ElevatedSurface) layer.
 73    ///
 74    /// Sets `bg()`, `rounded_lg()`, `border()`, `border_color()`, `shadow()`
 75    ///
 76    /// Examples: Settings Modal, Channel Management, Wizards/Setup UI, Dialogs
 77    fn elevation_3(self, cx: &App) -> Self {
 78        elevated(self, cx, ElevationIndex::ModalSurface)
 79    }
 80
 81    /// See [`elevation_3`](Self::elevation_3).
 82    ///
 83    /// Renders a borderless version [`elevation_3`](Self::elevation_3).
 84    fn elevation_3_borderless(self, cx: &mut App) -> Self {
 85        elevated_borderless(self, cx, ElevationIndex::ModalSurface)
 86    }
 87
 88    /// The theme's primary border color.
 89    fn border_primary(self, cx: &mut App) -> Self {
 90        self.border_color(cx.theme().colors().border)
 91    }
 92
 93    /// The theme's secondary or muted border color.
 94    fn border_muted(self, cx: &mut App) -> Self {
 95        self.border_color(cx.theme().colors().border_variant)
 96    }
 97
 98    /// Sets the background color to red for debugging when building UI.
 99    fn debug_bg_red(self) -> Self {
100        self.bg(hsla(0. / 360., 1., 0.5, 1.))
101    }
102
103    /// Sets the background color to green for debugging when building UI.
104    fn debug_bg_green(self) -> Self {
105        self.bg(hsla(120. / 360., 1., 0.5, 1.))
106    }
107
108    /// Sets the background color to blue for debugging when building UI.
109    fn debug_bg_blue(self) -> Self {
110        self.bg(hsla(240. / 360., 1., 0.5, 1.))
111    }
112
113    /// Sets the background color to yellow for debugging when building UI.
114    fn debug_bg_yellow(self) -> Self {
115        self.bg(hsla(60. / 360., 1., 0.5, 1.))
116    }
117
118    /// Sets the background color to cyan for debugging when building UI.
119    fn debug_bg_cyan(self) -> Self {
120        self.bg(hsla(160. / 360., 1., 0.5, 1.))
121    }
122
123    /// Sets the background color to magenta for debugging when building UI.
124    fn debug_bg_magenta(self) -> Self {
125        self.bg(hsla(300. / 360., 1., 0.5, 1.))
126    }
127}
128
129impl<E: Styled> StyledExt for E {}