
This PR adds `w_vw` and `h_vh` methods to `StyledExt`. These methods are the same as their `w` and `h` counterparts, but operate in viewport units, giving us the equivalent of `vw` and `vh` in CSS. You can see them in action in this story: ``` cargo run -p storybook2 -- components/viewport_units ``` Release Notes: - N/A
146 lines
5.1 KiB
Rust
146 lines
5.1 KiB
Rust
use gpui::{hsla, px, Styled, WindowContext};
|
|
use settings::Settings;
|
|
use theme::ThemeSettings;
|
|
|
|
use crate::prelude::*;
|
|
use crate::{ElevationIndex, UITextSize};
|
|
|
|
fn elevated<E: Styled>(this: E, cx: &mut WindowContext, index: ElevationIndex) -> E {
|
|
this.bg(cx.theme().colors().elevated_surface_background)
|
|
.z_index(index.z_index())
|
|
.rounded(px(8.))
|
|
.border()
|
|
.border_color(cx.theme().colors().border_variant)
|
|
.shadow(index.shadow())
|
|
}
|
|
|
|
/// Extends [`Styled`](gpui::Styled) with Zed specific styling methods.
|
|
pub trait StyledExt: Styled + Sized {
|
|
/// Sets the width of the element as a percentage of the viewport's width.
|
|
///
|
|
/// `percent` should be a value between `0.0` and `1.0`.
|
|
fn w_vw(self, percent: f32, cx: &mut WindowContext) -> Self {
|
|
self.w(cx.viewport_size().width * percent)
|
|
}
|
|
|
|
/// Sets the height of the element as a percentage of the viewport's height.
|
|
///
|
|
/// `percent` should be a value between `0.0` and `1.0`.
|
|
fn h_vh(self, percent: f32, cx: &mut WindowContext) -> Self {
|
|
self.h(cx.viewport_size().height * percent)
|
|
}
|
|
|
|
/// Horizontally stacks elements.
|
|
///
|
|
/// Sets `flex()`, `flex_row()`, `items_center()`
|
|
fn h_flex(self) -> Self {
|
|
self.flex().flex_row().items_center()
|
|
}
|
|
|
|
/// Vertically stacks elements.
|
|
///
|
|
/// Sets `flex()`, `flex_col()`
|
|
fn v_flex(self) -> Self {
|
|
self.flex().flex_col()
|
|
}
|
|
|
|
fn text_ui_size(self, size: UITextSize) -> Self {
|
|
let size = size.rems();
|
|
|
|
self.text_size(size)
|
|
}
|
|
|
|
/// The default size for UI text.
|
|
///
|
|
/// `0.825rem` or `14px` at the default scale of `1rem` = `16px`.
|
|
///
|
|
/// Note: The absolute size of this text will change based on a user's `ui_scale` setting.
|
|
///
|
|
/// Use [`text_ui_sm`] for regular-sized text.
|
|
fn text_ui(self) -> Self {
|
|
let size = UITextSize::default().rems();
|
|
|
|
self.text_size(size)
|
|
}
|
|
|
|
/// The small size for UI text.
|
|
///
|
|
/// `0.75rem` or `12px` at the default scale of `1rem` = `16px`.
|
|
///
|
|
/// Note: The absolute size of this text will change based on a user's `ui_scale` setting.
|
|
///
|
|
/// Use [`text_ui`] for regular-sized text.
|
|
fn text_ui_sm(self) -> Self {
|
|
let size = UITextSize::Small.rems();
|
|
|
|
self.text_size(size)
|
|
}
|
|
|
|
/// The font size for buffer text.
|
|
///
|
|
/// Retrieves the default font size, or the user's custom font size if set.
|
|
///
|
|
/// This should only be used for text that is displayed in a buffer,
|
|
/// or other places that text needs to match the user's buffer font size.
|
|
fn text_buffer(self, cx: &mut WindowContext) -> Self {
|
|
let settings = ThemeSettings::get_global(cx);
|
|
self.text_size(settings.buffer_font_size(cx))
|
|
}
|
|
|
|
/// The [`Surface`](ui2::ElevationIndex::Surface) elevation level, located above the app background, is the standard level for all elements
|
|
///
|
|
/// Sets `bg()`, `rounded_lg()`, `border()`, `border_color()`, `shadow()`
|
|
///
|
|
/// Example Elements: Title Bar, Panel, Tab Bar, Editor
|
|
fn elevation_1(self, cx: &mut WindowContext) -> Self {
|
|
elevated(self, cx, ElevationIndex::Surface)
|
|
}
|
|
|
|
/// 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.
|
|
///
|
|
/// Sets `bg()`, `rounded_lg()`, `border()`, `border_color()`, `shadow()`
|
|
///
|
|
/// Examples: Notifications, Palettes, Detached/Floating Windows, Detached/Floating Panels
|
|
fn elevation_2(self, cx: &mut WindowContext) -> Self {
|
|
elevated(self, cx, ElevationIndex::ElevatedSurface)
|
|
}
|
|
|
|
/// 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.
|
|
///
|
|
/// 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.
|
|
///
|
|
/// If the element does not have this behavior, it should be rendered at the [`Elevated Surface`](ui2::ElevationIndex::ElevatedSurface) layer.
|
|
///
|
|
/// Sets `bg()`, `rounded_lg()`, `border()`, `border_color()`, `shadow()`
|
|
///
|
|
/// Examples: Settings Modal, Channel Management, Wizards/Setup UI, Dialogs
|
|
fn elevation_3(self, cx: &mut WindowContext) -> Self {
|
|
elevated(self, cx, ElevationIndex::ModalSurface)
|
|
}
|
|
|
|
fn debug_bg_red(self) -> Self {
|
|
self.bg(gpui::red())
|
|
}
|
|
|
|
fn debug_bg_green(self) -> Self {
|
|
self.bg(gpui::green())
|
|
}
|
|
|
|
fn debug_bg_blue(self) -> Self {
|
|
self.bg(gpui::blue())
|
|
}
|
|
|
|
fn debug_bg_yellow(self) -> Self {
|
|
self.bg(hsla(60. / 360., 1., 0.5, 1.))
|
|
}
|
|
|
|
fn debug_bg_cyan(self) -> Self {
|
|
self.bg(hsla(160. / 360., 1., 0.5, 1.))
|
|
}
|
|
|
|
fn debug_bg_magenta(self) -> Self {
|
|
self.bg(hsla(300. / 360., 1., 0.5, 1.))
|
|
}
|
|
}
|
|
|
|
impl<E: Styled> StyledExt for E {}
|