Skip to content

Commit 5dca5ca

Browse files
Add elevation to StyledExt
Co-Authored-By: Marshall Bowers <1486634+maxdeviant@users.noreply.github.com>
1 parent 1c02690 commit 5dca5ca

File tree

4 files changed

+82
-19
lines changed

4 files changed

+82
-19
lines changed

crates/ui2/src/components/elevated_surface.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,5 @@ pub fn elevated_surface<V: 'static>(level: ElevationIndex, cx: &mut ViewContext<
2424
}
2525

2626
pub fn modal<V>(cx: &mut ViewContext<V>) -> Div<V> {
27-
elevated_surface(ElevationIndex::ModalSurfaces, cx)
27+
elevated_surface(ElevationIndex::ModalSurface, cx)
2828
}

crates/ui2/src/elevation.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@ Material Design 3 has a some great visualizations of elevation that may be helpf
3434

3535
The app background constitutes the lowest elevation layer, appearing behind all other surfaces and components. It is predominantly used for the background color of the app.
3636

37-
### UI Surface
37+
### Surface
3838

39-
The UI Surface, located above the app background, is the standard level for all elements
39+
The Surface elevation level, located above the app background, is the standard level for all elements
4040

4141
Example Elements: Title Bar, Panel, Tab Bar, Editor
4242

crates/ui2/src/elevation.rs

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,43 +11,53 @@ pub enum Elevation {
1111

1212
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
1313
pub enum ElevationIndex {
14-
AppBackground,
15-
UISurface,
14+
Background,
15+
Surface,
1616
ElevatedSurface,
1717
Wash,
18-
ModalSurfaces,
18+
ModalSurface,
1919
DraggedElement,
2020
}
2121

2222
impl ElevationIndex {
2323
pub fn z_index(self) -> u32 {
2424
match self {
25-
ElevationIndex::AppBackground => 0,
26-
ElevationIndex::UISurface => 100,
25+
ElevationIndex::Background => 0,
26+
ElevationIndex::Surface => 100,
2727
ElevationIndex::ElevatedSurface => 200,
2828
ElevationIndex::Wash => 300,
29-
ElevationIndex::ModalSurfaces => 400,
29+
ElevationIndex::ModalSurface => 400,
3030
ElevationIndex::DraggedElement => 900,
3131
}
3232
}
3333

3434
pub fn shadow(self) -> SmallVec<[BoxShadow; 2]> {
3535
match self {
36-
ElevationIndex::AppBackground => smallvec![],
36+
ElevationIndex::Surface => smallvec![],
3737

38-
ElevationIndex::UISurface => smallvec![BoxShadow {
38+
ElevationIndex::ElevatedSurface => smallvec![BoxShadow {
3939
color: hsla(0., 0., 0., 0.12),
4040
offset: point(px(0.), px(1.)),
4141
blur_radius: px(3.),
4242
spread_radius: px(0.),
4343
}],
4444

45-
_ => smallvec![BoxShadow {
46-
color: hsla(0., 0., 0., 0.32),
47-
offset: point(px(1.), px(3.)),
48-
blur_radius: px(12.),
49-
spread_radius: px(0.),
50-
}],
45+
ElevationIndex::ModalSurface => smallvec![
46+
BoxShadow {
47+
color: hsla(0., 0., 0., 0.12),
48+
offset: point(px(0.), px(1.)),
49+
blur_radius: px(3.),
50+
spread_radius: px(0.),
51+
},
52+
BoxShadow {
53+
color: hsla(0., 0., 0., 0.16),
54+
offset: point(px(3.), px(1.)),
55+
blur_radius: px(12.),
56+
spread_radius: px(0.),
57+
},
58+
],
59+
60+
_ => smallvec![],
5161
}
5262
}
5363
}

crates/ui2/src/styled_ext.rs

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
1-
use gpui::{Div, ElementFocus, ElementInteractivity, Styled};
1+
use gpui::{Div, ElementFocus, ElementInteractivity, Styled, UniformList, ViewContext};
2+
use theme2::ActiveTheme;
23

3-
use crate::UITextSize;
4+
use crate::{ElevationIndex, UITextSize};
5+
6+
fn elevated<E: Styled, V: 'static>(this: E, cx: &mut ViewContext<V>, index: ElevationIndex) -> E {
7+
this.bg(cx.theme().colors().elevated_surface_background)
8+
.rounded_lg()
9+
.border()
10+
.border_color(cx.theme().colors().border_variant)
11+
.shadow(index.shadow())
12+
}
413

514
/// Extends [`Styled`](gpui::Styled) with Zed specific styling methods.
615
pub trait StyledExt: Styled {
@@ -64,6 +73,48 @@ pub trait StyledExt: Styled {
6473

6574
self.text_size(size)
6675
}
76+
77+
/// The [`Surface`](ui2::ElevationIndex::Surface) elevation level, located above the app background, is the standard level for all elements
78+
///
79+
/// Sets `bg()`, `rounded_lg()`, `border()`, `border_color()`, `shadow()`
80+
///
81+
/// Example Elements: Title Bar, Panel, Tab Bar, Editor
82+
fn elevation_1<V: 'static>(self, cx: &mut ViewContext<V>) -> Self
83+
where
84+
Self: Styled + Sized,
85+
{
86+
elevated(self, cx, ElevationIndex::Surface)
87+
}
88+
89+
/// 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.
90+
///
91+
/// Sets `bg()`, `rounded_lg()`, `border()`, `border_color()`, `shadow()`
92+
///
93+
/// Examples: Notifications, Palettes, Detached/Floating Windows, Detached/Floating Panels
94+
fn elevation_2<V: 'static>(self, cx: &mut ViewContext<V>) -> Self
95+
where
96+
Self: Styled + Sized,
97+
{
98+
elevated(self, cx, ElevationIndex::ElevatedSurface)
99+
}
100+
101+
// There is no elevation 3, as the third elevation level is reserved for wash layers. See [`Elevation`](ui2::Elevation).
102+
103+
/// 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.
104+
///
105+
/// 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.
106+
///
107+
/// If the element does not have this behavior, it should be rendered at the [`Elevated Surface`](ui2::ElevationIndex::ElevatedSurface) layer.
108+
///
109+
/// Sets `bg()`, `rounded_lg()`, `border()`, `border_color()`, `shadow()`
110+
///
111+
/// Examples: Settings Modal, Channel Management, Wizards/Setup UI, Dialogs
112+
fn elevation_4<V: 'static>(self, cx: &mut ViewContext<V>) -> Self
113+
where
114+
Self: Styled + Sized,
115+
{
116+
elevated(self, cx, ElevationIndex::ModalSurface)
117+
}
67118
}
68119

69120
impl<V, I, F> StyledExt for Div<V, I, F>
@@ -72,3 +123,5 @@ where
72123
F: ElementFocus<V>,
73124
{
74125
}
126+
127+
impl<V> StyledExt for UniformList<V> {}

0 commit comments

Comments
 (0)