Split story into a crate.

This commit is contained in:
Jason Lee 2024-06-26 14:38:06 +08:00
parent 4945be5a36
commit 1f21469e7b
15 changed files with 144 additions and 47 deletions

10
Cargo.lock generated
View file

@ -4171,6 +4171,7 @@ dependencies = [
name = "ui" name = "ui"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"anyhow",
"catppuccin", "catppuccin",
"gpui", "gpui",
"log", "log",
@ -4180,6 +4181,14 @@ dependencies = [
"windows", "windows",
] ]
[[package]]
name = "ui-story"
version = "0.1.0"
dependencies = [
"gpui",
"ui",
]
[[package]] [[package]]
name = "unicase" name = "unicase"
version = "2.7.0" version = "2.7.0"
@ -4866,6 +4875,7 @@ dependencies = [
"anyhow", "anyhow",
"gpui", "gpui",
"ui", "ui",
"ui-story",
"util 0.1.0", "util 0.1.0",
] ]

View file

@ -2,8 +2,9 @@
members = [ members = [
"crates/app", "crates/app",
"crates/ui", "crates/ui",
"crates/ui-story",
"crates/workspace", "crates/workspace",
"crates/util" "crates/util",
] ]
default-members = ["crates/app"] default-members = ["crates/app"]
@ -12,6 +13,7 @@ resolver = "2"
[workspace.dependencies] [workspace.dependencies]
gpui = { git = "https://github.com/zed-industries/zed.git" } gpui = { git = "https://github.com/zed-industries/zed.git" }
ui = { path = "crates/ui" } ui = { path = "crates/ui" }
ui-story = { path = "crates/ui-story" }
workspace = { path = "crates/workspace" } workspace = { path = "crates/workspace" }
util = { path = "crates/util" } util = { path = "crates/util" }
anyhow = "1" anyhow = "1"

View file

@ -0,0 +1,8 @@
[package]
name = "ui-story"
version = "0.1.0"
edition = "2021"
[dependencies]
ui.workspace = true
gpui.workspace = true

View file

@ -3,7 +3,7 @@ use gpui::{
WindowContext, WindowContext,
}; };
use crate::{ use ui::{
button::{Button, ButtonSize, ButtonStyle}, button::{Button, ButtonSize, ButtonStyle},
Clickable, Disableable as _, Clickable, Disableable as _,
}; };

View file

@ -3,12 +3,7 @@ use gpui::{
WindowContext, WindowContext,
}; };
use crate::{ use ui::{checkbox::Checkbox, h_flex, v_flex, Disableable as _, Selection};
checkbox::Checkbox,
disableable::Disableable as _,
selectable::Selection,
stock::{h_flex, v_flex},
};
use super::story_case; use super::story_case;

View file

@ -3,7 +3,7 @@ use gpui::{
VisualContext, WindowContext, VisualContext, WindowContext,
}; };
use crate::text_field::TextField; use ui::text_field::TextField;
use super::story_case; use super::story_case;
@ -16,22 +16,32 @@ pub struct InputStory {
impl InputStory { impl InputStory {
pub(crate) fn new(cx: &mut WindowContext) -> Self { pub(crate) fn new(cx: &mut WindowContext) -> Self {
let input1 = cx.new_view(|cx| TextField::new(cx).set_text("Hello 世界", cx)); let input1 = cx.new_view(|cx| {
let mut input = TextField::new(cx);
input.set_text("Hello 世界", cx);
input
});
let mask_input = cx.new_view(|cx| { let mask_input = cx.new_view(|cx| {
TextField::new(cx) let mut input = TextField::new(cx);
.set_masked(true, cx) input.set_masked(true, cx);
.set_text("this-is-password", cx) input.set_text("this-is-password", cx);
input
}); });
Self { Self {
input1, input1,
input2: cx.new_view(|cx| TextField::new(cx).set_placeholder("Enter text here...", cx)), input2: cx.new_view(|cx| {
let mut input = TextField::new(cx);
input.set_placeholder("Enter text here...", cx);
input
}),
mash_input: mask_input, mash_input: mask_input,
disabled_input: cx.new_view(|cx| { disabled_input: cx.new_view(|cx| {
TextField::new(cx) let mut input = TextField::new(cx);
.set_text("This is disabled input", cx) input.set_text("This is disabled input", cx);
.set_disabled(true, cx) input.set_disabled(true, cx);
input
}), }),
} }
} }

View file

@ -1,8 +1,4 @@
use core::fmt; use std::fmt::{self, Display, Formatter};
use std::{
fmt::{Display, Formatter},
sync::Arc,
};
use checkbox_story::CheckboxStory; use checkbox_story::CheckboxStory;
use gpui::{ use gpui::{
@ -15,10 +11,10 @@ use switch_story::SwitchStory;
mod button_story; mod button_story;
mod checkbox_story; mod checkbox_story;
mod input_story; mod input_story;
mod picker_story;
mod switch_story; mod switch_story;
use crate::{ use ui::{
button::Button,
label::Label, label::Label,
tab::{Tab, TabBar}, tab::{Tab, TabBar},
Selectable, Selectable,

View file

View file

@ -1,17 +1,13 @@
use gpui::{ use gpui::{
div, ClickEvent, Div, IntoElement, ParentElement, Render, RenderOnce, SharedString, Div, IntoElement, ParentElement, Render, SharedString, Styled, ViewContext, WindowContext,
StatefulInteractiveElement as _, Styled, ViewContext, VisualContext as _, WindowContext,
}; };
use crate::{ use ui::{
checkbox::Checkbox, h_flex,
disableable::Disableable as _,
label::Label, label::Label,
selectable::Selection,
stock::{h_flex, v_flex},
switch::{LabelSide, Switch}, switch::{LabelSide, Switch},
theme::ActiveTheme, theme::ActiveTheme,
StyledExt, v_flex, Disableable as _, StyledExt,
}; };
use super::story_case; use super::story_case;

View file

@ -11,3 +11,4 @@ serde.workspace = true
serde_json.workspace = true serde_json.workspace = true
smallvec = "1.13.2" smallvec = "1.13.2"
windows = "0.57.0" windows = "0.57.0"
anyhow = "1"

View file

@ -9,17 +9,21 @@ mod styled_ext;
pub mod button; pub mod button;
pub mod checkbox; pub mod checkbox;
pub mod empty;
pub mod label; pub mod label;
pub mod story;
pub mod text_field; pub mod text_field;
pub mod theme; pub mod theme;
pub mod title_bar; pub mod title_bar;
pub use styled_ext::StyledExt; pub use styled_ext::StyledExt;
pub mod divider;
pub mod dropdown;
pub mod picker;
pub mod switch; pub mod switch;
pub mod tab; pub mod tab;
pub use clickable::Clickable; pub use clickable::Clickable;
pub use disableable::Disableable; pub use disableable::Disableable;
pub use selectable::Selectable; pub use selectable::*;
pub use icon::*; pub use icon::*;
pub use stock::*;

View file

@ -1,4 +1,63 @@
use gpui::Styled; use crate::theme::ActiveTheme;
use gpui::{hsla, point, px, BoxShadow, Styled, WindowContext};
use smallvec::{smallvec, SmallVec};
pub enum ElevationIndex {
Background,
Surface,
ElevatedSurface,
Wash,
ModalSurface,
DraggedElement,
}
impl ElevationIndex {
pub fn shadow(self) -> SmallVec<[BoxShadow; 2]> {
match self {
ElevationIndex::Surface => smallvec![],
ElevationIndex::ElevatedSurface => smallvec![BoxShadow {
color: hsla(0., 0., 0., 0.12),
offset: point(px(0.), px(2.)),
blur_radius: px(3.),
spread_radius: px(0.),
}],
ElevationIndex::ModalSurface => smallvec![
BoxShadow {
color: hsla(0., 0., 0., 0.12),
offset: point(px(0.), px(2.)),
blur_radius: px(3.),
spread_radius: px(0.),
},
BoxShadow {
color: hsla(0., 0., 0., 0.08),
offset: point(px(0.), px(3.)),
blur_radius: px(6.),
spread_radius: px(0.),
},
BoxShadow {
color: hsla(0., 0., 0., 0.04),
offset: point(px(0.), px(6.)),
blur_radius: px(12.),
spread_radius: px(0.),
},
],
_ => smallvec![],
}
}
}
fn elevated<E: Styled>(this: E, cx: &mut WindowContext, index: ElevationIndex) -> E {
let theme = cx.theme();
this.bg(theme.popover)
.rounded(px(8.))
.border_1()
.border_color(theme.border)
.shadow(index.shadow())
}
/// Extends [`gpui::Styled`] with specific styling methods. /// Extends [`gpui::Styled`] with specific styling methods.
pub trait StyledExt: Styled + Sized { pub trait StyledExt: Styled + Sized {
@ -15,6 +74,21 @@ pub trait StyledExt: Styled + Sized {
fn v_flex(self) -> Self { fn v_flex(self) -> Self {
self.flex().flex_col() self.flex().flex_col()
} }
/// Located above the app background
fn elevation_1(self, cx: &mut WindowContext) -> Self {
elevated(self, cx, ElevationIndex::Surface)
}
/// Appear above most UI elements
fn elevation_2(self, cx: &mut WindowContext) -> Self {
elevated(self, cx, ElevationIndex::ElevatedSurface)
}
// Above all other UI elements and are located above the wash layer
fn elevation_3(self, cx: &mut WindowContext) -> Self {
elevated(self, cx, ElevationIndex::ModalSurface)
}
} }
impl<E: Styled> StyledExt for E {} impl<E: Styled> StyledExt for E {}

View file

@ -4,9 +4,9 @@ mod text_view;
use crate::theme::ActiveTheme; use crate::theme::ActiveTheme;
use gpui::{ use gpui::{
div, prelude::FluentBuilder as _, ClipboardItem, EventEmitter, FocusHandle, FocusableView, div, prelude::FluentBuilder as _, AppContext, ClipboardItem, EventEmitter, FocusHandle,
InteractiveElement, IntoElement, KeyDownEvent, MouseButton, ParentElement, Render, RenderOnce, FocusableView, InteractiveElement, IntoElement, KeyDownEvent, MouseButton, ParentElement,
Styled, View, WindowContext, Render, RenderOnce, Styled, View, ViewContext, WindowContext,
}; };
use std::time::Duration; use std::time::Duration;
use text_view::TextView; use text_view::TextView;
@ -31,29 +31,29 @@ impl TextField {
cx.focus(&self.focus_handle); cx.focus(&self.focus_handle);
} }
pub fn set_placeholder(self, placeholder: &str, cx: &mut WindowContext) -> Self { pub fn set_placeholder(&mut self, placeholder: &str, cx: &mut WindowContext) {
self.view.update(cx, |text_view, cx| { self.view.update(cx, |text_view, cx| {
text_view.set_placeholder(placeholder, cx) text_view.set_placeholder(placeholder, cx)
}); });
self
} }
pub fn set_disabled(self, disabled: bool, cx: &mut WindowContext) -> Self { pub fn set_disabled(&mut self, disabled: bool, cx: &mut WindowContext) {
self.view self.view
.update(cx, |text_view, cx| text_view.set_disabled(disabled, cx)); .update(cx, |text_view, cx| text_view.set_disabled(disabled, cx));
self
} }
pub fn set_text(self, text: &str, cx: &mut WindowContext) -> Self { pub fn set_text(&mut self, text: &str, cx: &mut WindowContext) {
self.view self.view
.update(cx, |text_view, cx| text_view.set_text(text, cx)); .update(cx, |text_view, cx| text_view.set_text(text, cx));
self
} }
pub fn set_masked(self, masked: bool, cx: &mut WindowContext) -> Self { pub fn text(&self, cx: &AppContext) -> String {
self.view.read(cx).text.clone()
}
pub fn set_masked(&mut self, masked: bool, cx: &mut WindowContext) {
self.view self.view
.update(cx, |text_view, cx| text_view.set_masked(masked, cx)); .update(cx, |text_view, cx| text_view.set_masked(masked, cx));
self
} }
} }

View file

@ -6,5 +6,6 @@ edition = "2021"
[dependencies] [dependencies]
gpui.workspace = true gpui.workspace = true
ui.workspace = true ui.workspace = true
ui-story.workspace = true
anyhow.workspace = true anyhow.workspace = true
util.workspace = true util.workspace = true

View file

@ -5,11 +5,11 @@ use std::sync::Arc;
use ui::{ use ui::{
button::Button, button::Button,
label::Label, label::Label,
story::Stories,
theme::{ActiveTheme, Theme}, theme::{ActiveTheme, Theme},
title_bar::TitleBar, title_bar::TitleBar,
Clickable as _, StyledExt as _, Clickable as _, StyledExt as _,
}; };
use ui_story::Stories;
use util::ResultExt as _; use util::ResultExt as _;
mod app_state; mod app_state;