styled_ext.rs

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