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