elevation.rs

  1use gpui::{hsla, point, px, BoxShadow};
  2use smallvec::{smallvec, SmallVec};
  3
  4#[doc = include_str!("docs/elevation.md")]
  5#[derive(Debug, Clone, Copy, PartialEq, Eq)]
  6pub enum Elevation {
  7    ElevationIndex(ElevationIndex),
  8    LayerIndex(LayerIndex),
  9    ElementIndex(ElementIndex),
 10}
 11
 12#[derive(Debug, Clone, Copy, PartialEq, Eq)]
 13pub enum ElevationIndex {
 14    Background,
 15    Surface,
 16    ElevatedSurface,
 17    Wash,
 18    ModalSurface,
 19    DraggedElement,
 20}
 21
 22impl ElevationIndex {
 23    pub fn z_index(self) -> u16 {
 24        match self {
 25            ElevationIndex::Background => 0,
 26            ElevationIndex::Surface => 42,
 27            ElevationIndex::ElevatedSurface => 84,
 28            ElevationIndex::Wash => 126,
 29            ElevationIndex::ModalSurface => 168,
 30            ElevationIndex::DraggedElement => 210,
 31        }
 32    }
 33
 34    pub fn shadow(self) -> SmallVec<[BoxShadow; 2]> {
 35        match self {
 36            ElevationIndex::Surface => smallvec![],
 37
 38            ElevationIndex::ElevatedSurface => smallvec![BoxShadow {
 39                color: hsla(0., 0., 0., 0.12),
 40                offset: point(px(0.), px(2.)),
 41                blur_radius: px(3.),
 42                spread_radius: px(0.),
 43            }],
 44
 45            ElevationIndex::ModalSurface => smallvec![
 46                BoxShadow {
 47                    color: hsla(0., 0., 0., 0.12),
 48                    offset: point(px(0.), px(2.)),
 49                    blur_radius: px(3.),
 50                    spread_radius: px(0.),
 51                },
 52                BoxShadow {
 53                    color: hsla(0., 0., 0., 0.08),
 54                    offset: point(px(0.), px(3.)),
 55                    blur_radius: px(6.),
 56                    spread_radius: px(0.),
 57                },
 58                BoxShadow {
 59                    color: hsla(0., 0., 0., 0.04),
 60                    offset: point(px(0.), px(6.)),
 61                    blur_radius: px(12.),
 62                    spread_radius: px(0.),
 63                },
 64            ],
 65
 66            _ => smallvec![],
 67        }
 68    }
 69}
 70
 71#[derive(Debug, Clone, Copy, PartialEq, Eq)]
 72pub enum LayerIndex {
 73    BehindElement,
 74    Element,
 75    ElevatedElement,
 76}
 77
 78impl LayerIndex {
 79    pub fn usize(&self) -> usize {
 80        match *self {
 81            LayerIndex::BehindElement => 0,
 82            LayerIndex::Element => 100,
 83            LayerIndex::ElevatedElement => 200,
 84        }
 85    }
 86}
 87
 88/// An appropriate z-index for the given layer based on its intended usage.
 89#[derive(Debug, Clone, Copy, PartialEq, Eq)]
 90pub enum ElementIndex {
 91    Effect,
 92    Background,
 93    Tint,
 94    Highlight,
 95    Content,
 96    Overlay,
 97}
 98
 99impl ElementIndex {
100    pub fn usize(&self) -> usize {
101        match *self {
102            ElementIndex::Effect => 0,
103            ElementIndex::Background => 100,
104            ElementIndex::Tint => 200,
105            ElementIndex::Highlight => 300,
106            ElementIndex::Content => 400,
107            ElementIndex::Overlay => 500,
108        }
109    }
110}