ui crate docs & spring cleaning (#18768)
Similar to https://github.com/zed-industries/zed/pull/18690 & https://github.com/zed-industries/zed/pull/18695, this PR enables required docs for `ui` and does some cleanup. Changes: - Enables the `deny(missing_docs)` crate-wide. - Adds `allow(missing_docs)` on many modules until folks pick them up to document them - Documents some modules (all in `ui/src/styles`) - Crate root-level organization: Traits move to `traits`, other misc organization - Cleaned out a bunch of unused code. Note: I'd like to remove `utils/format_distance` but the assistant panel uses it. To move it over to use the `time_format` crate we may need to update it to use `time` instead of `chrono`. Needs more investigation. Release Notes: - N/A
This commit is contained in:
@@ -1,14 +1,8 @@
|
||||
use crate::prelude::*;
|
||||
use gpui::{WindowBackgroundAppearance, WindowContext};
|
||||
use theme::Appearance;
|
||||
|
||||
/// Returns the current [Appearance].
|
||||
pub fn appearance(cx: &WindowContext) -> Appearance {
|
||||
cx.theme().appearance
|
||||
}
|
||||
|
||||
/// Returns the [WindowBackgroundAppearance].
|
||||
pub fn window_appearance(cx: &WindowContext) -> WindowBackgroundAppearance {
|
||||
fn window_appearance(cx: &WindowContext) -> WindowBackgroundAppearance {
|
||||
cx.theme().styles.window_background_appearance
|
||||
}
|
||||
|
||||
|
||||
@@ -5,28 +5,63 @@ use theme::ActiveTheme;
|
||||
#[derive(Debug, Default, PartialEq, Copy, Clone)]
|
||||
pub enum Color {
|
||||
#[default]
|
||||
/// The default text color. Might be known as "foreground" or "primary" in
|
||||
/// some theme systems.
|
||||
///
|
||||
/// For less emphasis, consider using [`Color::Muted`] or [`Color::Hidden`].
|
||||
Default,
|
||||
/// A text color used for accents, such as links or highlights.
|
||||
Accent,
|
||||
Created,
|
||||
Deleted,
|
||||
Disabled,
|
||||
Error,
|
||||
Hidden,
|
||||
Hint,
|
||||
Info,
|
||||
Modified,
|
||||
/// A color used to indicate a conflict, such as a version control merge conflict, or a conflict between a file in the editor and the file system.
|
||||
Conflict,
|
||||
Ignored,
|
||||
Muted,
|
||||
Placeholder,
|
||||
Player(u32),
|
||||
Selected,
|
||||
Success,
|
||||
Warning,
|
||||
/// A color used to indicate a newly created item, such as a new file in
|
||||
/// version control, or a new file on disk.
|
||||
Created,
|
||||
/// It is highly, HIGHLY recommended not to use this! Using this color
|
||||
/// means detaching it from any semantic meaning across themes.
|
||||
///
|
||||
/// A custom color specified by an HSLA value.
|
||||
Custom(Hsla),
|
||||
/// A color used to indicate a deleted item, such as a file removed from version control.
|
||||
Deleted,
|
||||
/// A color used for disabled UI elements or text, like a disabled button or menu item.
|
||||
Disabled,
|
||||
/// A color used to indicate an error condition, or something the user
|
||||
/// cannot do. In very rare cases, it might be used to indicate dangerous or
|
||||
/// destructive action.
|
||||
Error,
|
||||
/// A color used for elements that represent something that is hidden, like
|
||||
/// a hidden file, or an element that should be visually de-emphasized.
|
||||
Hidden,
|
||||
/// A color used for hint or suggestion text, often a blue color. Use this
|
||||
/// color to represent helpful, or semantically neutral information.
|
||||
Hint,
|
||||
/// A color used for items that are intentionally ignored, such as files ignored by version control.
|
||||
Ignored,
|
||||
/// A color used for informational messages or status indicators, often a blue color.
|
||||
Info,
|
||||
/// A color used to indicate a modified item, such as an edited file, or a modified entry in version control.
|
||||
Modified,
|
||||
/// A color used for text or UI elements that should be visually muted or de-emphasized.
|
||||
///
|
||||
/// For more emphasis, consider using [`Color::Default`].
|
||||
///
|
||||
/// For less emphasis, consider using [`Color::Hidden`].
|
||||
Muted,
|
||||
/// A color used for placeholder text in input fields.
|
||||
Placeholder,
|
||||
/// A color associated with a specific player number.
|
||||
Player(u32),
|
||||
/// A color used to indicate selected text or UI elements.
|
||||
Selected,
|
||||
/// A color used to indicate a successful operation or status.
|
||||
Success,
|
||||
/// A color used to indicate a warning condition.
|
||||
Warning,
|
||||
}
|
||||
|
||||
impl Color {
|
||||
/// Returns the Color's HSLA value.
|
||||
pub fn color(&self, cx: &WindowContext) -> Hsla {
|
||||
match self {
|
||||
Color::Default => cx.theme().colors().text,
|
||||
|
||||
@@ -1,44 +0,0 @@
|
||||
# Elevation
|
||||
|
||||
Elevation can be thought of as the physical closeness of an element to the user. Elements with lower elevations are physically further away from the user on the z-axis and appear to be underneath elements with higher elevations.
|
||||
|
||||
Material Design 3 has a some great visualizations of elevation that may be helpful to understanding the mental modal of elevation. [Material Design – Elevation](https://m3.material.io/styles/elevation/overview)
|
||||
|
||||
## Elevation Levels
|
||||
|
||||
1. App Background (e.x.: Workspace, system window)
|
||||
1. UI Surface (e.x.: Title Bar, Panel, Tab Bar)
|
||||
1. Elevated Surface (e.x.: Palette, Notification, Floating Window)
|
||||
1. Wash
|
||||
1. Modal Surfaces (e.x.: Modal)
|
||||
1. Dragged Element (This is a special case, see Layer section below)
|
||||
|
||||
### App Background
|
||||
|
||||
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.
|
||||
|
||||
### Surface
|
||||
|
||||
The Surface elevation level, located above the app background, is the standard level for all elements
|
||||
|
||||
Example Elements: Title Bar, Panel, Tab Bar, Editor
|
||||
|
||||
### Elevated Surface
|
||||
|
||||
Non-Modal Elevated Surfaces appear above the UI 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.
|
||||
|
||||
Examples: Notifications, Palettes, Detached/Floating Windows, Detached/Floating Panels
|
||||
|
||||
You could imagine a variant of the assistant that floats in a window above the editor on this elevation, or a floating terminal window that becomes less opaque when not focused.
|
||||
|
||||
### Wash
|
||||
|
||||
Wash denotes a distinct elevation reserved to isolate app UI layers from high elevation components such as modals, notifications, and overlaid panels. The wash may not consistently be visible when these components are active. This layer is often referred to as a scrim or overlay and the background color of the wash is typically deployed in its design.
|
||||
|
||||
### Modal Surfaces
|
||||
|
||||
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
|
||||
|
||||
Elements rendered at this layer 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 layer.
|
||||
@@ -1,31 +1,32 @@
|
||||
use gpui::{hsla, point, px, BoxShadow};
|
||||
use smallvec::{smallvec, SmallVec};
|
||||
|
||||
#[doc = include_str!("docs/elevation.md")]
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum Elevation {
|
||||
ElevationIndex(ElevationIndex),
|
||||
LayerIndex(LayerIndex),
|
||||
ElementIndex(ElementIndex),
|
||||
}
|
||||
|
||||
impl From<ElevationIndex> for Elevation {
|
||||
fn from(val: ElevationIndex) -> Self {
|
||||
Elevation::ElevationIndex(val)
|
||||
}
|
||||
}
|
||||
|
||||
/// Today, elevation is primarily used to add shadows to elements, and set the correct background for elements like buttons.
|
||||
///
|
||||
/// Elevation can be thought of as the physical closeness of an element to the
|
||||
/// user. Elements with lower elevations are physically further away on the
|
||||
/// z-axis and appear to be underneath elements with higher elevations.
|
||||
///
|
||||
/// In the future, a more complete approach to elevation may be added.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum ElevationIndex {
|
||||
/// On the layer of the app background. This is under panels, panes, and
|
||||
/// other surfaces.
|
||||
Background,
|
||||
/// The primary surface – Contains panels, panes, containers, etc.
|
||||
Surface,
|
||||
/// A surface that is elevated above the primary surface. but below washes, models, and dragged elements.
|
||||
ElevatedSurface,
|
||||
/// A surface that is above all non-modal surfaces, and separates the app from focused intents, like dialogs, alerts, modals, etc.
|
||||
Wash,
|
||||
/// A surface above the [ElevationIndex::Wash] that is used for dialogs, alerts, modals, etc.
|
||||
ModalSurface,
|
||||
/// A surface above all other surfaces, reserved exclusively for dragged elements, like a dragged file, tab or other draggable element.
|
||||
DraggedElement,
|
||||
}
|
||||
|
||||
impl ElevationIndex {
|
||||
/// Returns an appropriate shadow for the given elevation index.
|
||||
pub fn shadow(self) -> SmallVec<[BoxShadow; 2]> {
|
||||
match self {
|
||||
ElevationIndex::Surface => smallvec![],
|
||||
@@ -62,21 +63,3 @@ impl ElevationIndex {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum LayerIndex {
|
||||
BehindElement,
|
||||
Element,
|
||||
ElevatedElement,
|
||||
}
|
||||
|
||||
/// An appropriate z-index for the given layer based on its intended usage.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum ElementIndex {
|
||||
Effect,
|
||||
Background,
|
||||
Tint,
|
||||
Highlight,
|
||||
Content,
|
||||
Overlay,
|
||||
}
|
||||
|
||||
@@ -4,6 +4,11 @@ use theme::{ThemeSettings, UiDensity};
|
||||
|
||||
use crate::{rems_from_px, BASE_REM_SIZE_IN_PX};
|
||||
|
||||
/// A dynamic spacing system that adjusts spacing based on
|
||||
/// [UiDensity].
|
||||
///
|
||||
/// When possible, [Spacing] should be used over manual
|
||||
/// or built-in spacing values in places dynamic spacing is needed.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
||||
pub enum Spacing {
|
||||
/// No spacing
|
||||
@@ -38,6 +43,7 @@ pub enum Spacing {
|
||||
}
|
||||
|
||||
impl Spacing {
|
||||
/// Returns the spacing's scaling ratio in pixels.
|
||||
pub fn spacing_ratio(self, cx: &WindowContext) -> f32 {
|
||||
match ThemeSettings::get_global(cx).ui_density {
|
||||
UiDensity::Compact => match self {
|
||||
@@ -73,10 +79,12 @@ impl Spacing {
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns the spacing's value in rems.
|
||||
pub fn rems(self, cx: &WindowContext) -> Rems {
|
||||
rems(self.spacing_ratio(cx))
|
||||
}
|
||||
|
||||
/// Returns the spacing's value in pixels.
|
||||
pub fn px(self, cx: &WindowContext) -> Pixels {
|
||||
let ui_font_size_f32: f32 = ThemeSettings::get_global(cx).ui_font_size.into();
|
||||
|
||||
@@ -84,10 +92,14 @@ impl Spacing {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn user_spacing_style(cx: &WindowContext) -> UiDensity {
|
||||
fn user_spacing_style(cx: &WindowContext) -> UiDensity {
|
||||
ThemeSettings::get_global(cx).ui_density
|
||||
}
|
||||
|
||||
/// Returns a custom spacing value based on the current [`UiDensity`].
|
||||
///
|
||||
/// If you use this, talk to @iamnbutler and let me know what you're doing
|
||||
/// that needs custom spacing– I'd love to understand so we can extend the system further and remove the need for this.
|
||||
pub fn custom_spacing(cx: &WindowContext, size: f32) -> Rems {
|
||||
rems_from_px(size * user_spacing_style(cx).spacing_ratio())
|
||||
}
|
||||
|
||||
@@ -87,6 +87,7 @@ pub trait StyledTypography: Styled + Sized {
|
||||
|
||||
impl<E: Styled> StyledTypography for E {}
|
||||
|
||||
/// A utility for getting the size of various semantic text sizes.
|
||||
#[derive(Debug, Default, Clone)]
|
||||
pub enum TextSize {
|
||||
/// The default size for UI text.
|
||||
@@ -128,6 +129,7 @@ pub enum TextSize {
|
||||
}
|
||||
|
||||
impl TextSize {
|
||||
/// Returns the text size in rems.
|
||||
pub fn rems(self, cx: &WindowContext) -> Rems {
|
||||
let theme_settings = ThemeSettings::get_global(cx);
|
||||
|
||||
@@ -143,20 +145,27 @@ impl TextSize {
|
||||
}
|
||||
|
||||
/// The size of a [`Headline`] element
|
||||
///
|
||||
/// Defaults to a Major Second scale.
|
||||
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Copy, Default)]
|
||||
pub enum HeadlineSize {
|
||||
/// An extra small headline - `~14px` @16px/rem
|
||||
XSmall,
|
||||
/// A small headline - `16px` @16px/rem
|
||||
Small,
|
||||
#[default]
|
||||
/// A medium headline - `~18px` @16px/rem
|
||||
Medium,
|
||||
/// A large headline - `~20px` @16px/rem
|
||||
Large,
|
||||
/// An extra large headline - `~22px` @16px/rem
|
||||
XLarge,
|
||||
}
|
||||
|
||||
impl HeadlineSize {
|
||||
pub fn size(self) -> Rems {
|
||||
/// Returns the headline size in rems.
|
||||
pub fn rems(self) -> Rems {
|
||||
match self {
|
||||
// Based on the Major Second scale
|
||||
Self::XSmall => rems(0.88),
|
||||
Self::Small => rems(1.0),
|
||||
Self::Medium => rems(1.125),
|
||||
@@ -165,6 +174,7 @@ impl HeadlineSize {
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns the line height for the headline size.
|
||||
pub fn line_height(self) -> Rems {
|
||||
match self {
|
||||
Self::XSmall => rems(1.6),
|
||||
@@ -176,6 +186,8 @@ impl HeadlineSize {
|
||||
}
|
||||
}
|
||||
|
||||
/// A headline element, used to emphasize some text and
|
||||
/// create a visual hierarchy.
|
||||
#[derive(IntoElement)]
|
||||
pub struct Headline {
|
||||
size: HeadlineSize,
|
||||
@@ -190,13 +202,14 @@ impl RenderOnce for Headline {
|
||||
div()
|
||||
.font(ui_font)
|
||||
.line_height(self.size.line_height())
|
||||
.text_size(self.size.size())
|
||||
.text_size(self.size.rems())
|
||||
.text_color(cx.theme().colors().text)
|
||||
.child(self.text)
|
||||
}
|
||||
}
|
||||
|
||||
impl Headline {
|
||||
/// Create a new headline element.
|
||||
pub fn new(text: impl Into<SharedString>) -> Self {
|
||||
Self {
|
||||
size: HeadlineSize::default(),
|
||||
@@ -205,11 +218,13 @@ impl Headline {
|
||||
}
|
||||
}
|
||||
|
||||
/// Set the size of the headline.
|
||||
pub fn size(mut self, size: HeadlineSize) -> Self {
|
||||
self.size = size;
|
||||
self
|
||||
}
|
||||
|
||||
/// Set the color of the headline.
|
||||
pub fn color(mut self, color: Color) -> Self {
|
||||
self.color = color;
|
||||
self
|
||||
|
||||
Reference in New Issue
Block a user