From 3b5ee59273b0f6d36e95678311896936a9f107a8 Mon Sep 17 00:00:00 2001 From: Nathan Sobo Date: Wed, 30 Aug 2023 16:12:14 -0600 Subject: [PATCH] Split playground into gpui2 and storybook --- Cargo.lock | 35 +- Cargo.toml | 2 +- crates/gpui/playground/Cargo.toml | 28 -- crates/gpui/playground/src/adapter.rs | 78 ---- crates/gpui/playground/src/components.rs | 101 ----- crates/gpui/playground/src/div.rs | 116 ------ crates/gpui/playground/src/element.rs | 170 -------- crates/gpui/playground/src/hoverable.rs | 104 ----- crates/gpui/playground/src/interactive.rs | 147 ------- crates/gpui/playground/src/layout_context.rs | 64 --- crates/gpui/playground/src/paint_context.rs | 54 --- crates/gpui/playground/src/playground.rs | 99 ----- crates/gpui/playground/src/style.rs | 379 ------------------ crates/gpui/playground/src/themes.rs | 175 -------- .../gpui/playground/src/themes/rose_pine.rs | 133 ------ crates/gpui/playground/src/view.rs | 26 -- crates/gpui/src/app.rs | 2 +- crates/gpui/src/scene.rs | 2 +- crates/gpui2/Cargo.toml | 3 + crates/gpui2/src/adapter.rs | 3 +- crates/gpui2/src/element.rs | 12 - crates/gpui2/src/elements.rs | 6 + crates/gpui2/src/{ => elements}/div.rs | 4 +- crates/gpui2/src/{ => elements}/hoverable.rs | 2 +- .../src => gpui2/src/elements}/pressable.rs | 2 +- .../src => gpui2/src/elements}/text.rs | 5 +- crates/gpui2/src/gpui2.rs | 56 +-- crates/gpui2/src/interactive.rs | 26 +- crates/gpui2/src/pressable.rs | 107 ----- crates/gpui2/src/style.rs | 4 +- crates/gpui2/src/text.rs | 148 ------- crates/gpui2/src/themes/rose_pine.rs | 133 ------ .../{gpui/playground => storybook}/Cargo.lock | 2 +- crates/storybook/Cargo.toml | 23 ++ .../playground => storybook}/docs/thoughts.md | 0 crates/{gpui2 => storybook}/src/components.rs | 13 +- crates/storybook/src/element_ext.rs | 22 + .../src/storybook.rs} | 48 +-- .../src/themes.rs => storybook/src/theme.rs} | 33 +- .../playground => storybook}/src/workspace.rs | 12 +- test.rs | 6 +- 41 files changed, 175 insertions(+), 2210 deletions(-) delete mode 100644 crates/gpui/playground/Cargo.toml delete mode 100644 crates/gpui/playground/src/adapter.rs delete mode 100644 crates/gpui/playground/src/components.rs delete mode 100644 crates/gpui/playground/src/div.rs delete mode 100644 crates/gpui/playground/src/element.rs delete mode 100644 crates/gpui/playground/src/hoverable.rs delete mode 100644 crates/gpui/playground/src/interactive.rs delete mode 100644 crates/gpui/playground/src/layout_context.rs delete mode 100644 crates/gpui/playground/src/paint_context.rs delete mode 100644 crates/gpui/playground/src/playground.rs delete mode 100644 crates/gpui/playground/src/style.rs delete mode 100644 crates/gpui/playground/src/themes.rs delete mode 100644 crates/gpui/playground/src/themes/rose_pine.rs delete mode 100644 crates/gpui/playground/src/view.rs create mode 100644 crates/gpui2/src/elements.rs rename crates/gpui2/src/{ => elements}/div.rs (96%) rename crates/gpui2/src/{ => elements}/hoverable.rs (97%) rename crates/{gpui/playground/src => gpui2/src/elements}/pressable.rs (97%) rename crates/{gpui/playground/src => gpui2/src/elements}/text.rs (97%) delete mode 100644 crates/gpui2/src/pressable.rs delete mode 100644 crates/gpui2/src/text.rs delete mode 100644 crates/gpui2/src/themes/rose_pine.rs rename crates/{gpui/playground => storybook}/Cargo.lock (99%) create mode 100644 crates/storybook/Cargo.toml rename crates/{gpui/playground => storybook}/docs/thoughts.md (100%) rename crates/{gpui2 => storybook}/src/components.rs (90%) create mode 100644 crates/storybook/src/element_ext.rs rename crates/{gpui2/src/playground.rs => storybook/src/storybook.rs} (68%) rename crates/{gpui2/src/themes.rs => storybook/src/theme.rs} (87%) rename crates/{gpui/playground => storybook}/src/workspace.rs (83%) diff --git a/Cargo.lock b/Cargo.lock index bb0c39372d..496567459a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5194,26 +5194,6 @@ version = "0.3.27" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "26072860ba924cbfa98ea39c8c19b4dd6a4a25423dbdf219c1eca91aa0cf6964" -[[package]] -name = "playground" -version = "0.1.0" -dependencies = [ - "anyhow", - "derive_more", - "gpui", - "gpui2_macros", - "log", - "parking_lot 0.11.2", - "refineable", - "rust-embed", - "serde", - "settings", - "simplelog", - "smallvec", - "theme", - "util", -] - [[package]] name = "plist" version = "1.5.0" @@ -7320,6 +7300,21 @@ version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a2eb9349b6444b326872e140eb1cf5e7c522154d69e7a0ffb0fb81c06b37543f" +[[package]] +name = "storybook" +version = "0.1.0" +dependencies = [ + "anyhow", + "gpui2", + "log", + "rust-embed", + "serde", + "settings", + "simplelog", + "theme", + "util", +] + [[package]] name = "stringprep" version = "0.1.3" diff --git a/Cargo.toml b/Cargo.toml index 119d0c8947..789652f379 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -32,7 +32,6 @@ members = [ "crates/git", "crates/go_to_line", "crates/gpui", - "crates/gpui/playground", "crates/gpui_macros", "crates/gpui2", "crates/gpui2_macros", @@ -64,6 +63,7 @@ members = [ "crates/sqlez", "crates/sqlez_macros", "crates/feature_flags", + "crates/storybook", "crates/sum_tree", "crates/terminal", "crates/text", diff --git a/crates/gpui/playground/Cargo.toml b/crates/gpui/playground/Cargo.toml deleted file mode 100644 index a817387dc0..0000000000 --- a/crates/gpui/playground/Cargo.toml +++ /dev/null @@ -1,28 +0,0 @@ -[package] -name = "playground" -version = "0.1.0" -edition = "2021" -publish = false - -[[bin]] -name = "playground" -path = "src/playground.rs" - -[dependencies] -anyhow.workspace = true -derive_more.workspace = true -gpui = { path = ".." } -log.workspace = true -gpui2_macros = { path = "../../gpui2_macros" } -parking_lot.workspace = true -refineable.workspace = true -rust-embed.workspace = true -serde.workspace = true -settings = { path = "../../settings" } -simplelog = "0.9" -smallvec.workspace = true -theme = { path = "../../theme" } -util = { path = "../../util" } - -[dev-dependencies] -gpui = { path = "..", features = ["test-support"] } diff --git a/crates/gpui/playground/src/adapter.rs b/crates/gpui/playground/src/adapter.rs deleted file mode 100644 index eff2356c94..0000000000 --- a/crates/gpui/playground/src/adapter.rs +++ /dev/null @@ -1,78 +0,0 @@ -use crate::{layout_context::LayoutContext, paint_context::PaintContext}; -use gpui::{geometry::rect::RectF, LayoutEngine, LayoutId}; -use util::ResultExt; - -/// Makes a new, playground-style element into a legacy element. -pub struct AdapterElement(pub(crate) crate::element::AnyElement); - -impl gpui::Element for AdapterElement { - type LayoutState = Option<(LayoutEngine, LayoutId)>; - type PaintState = (); - - fn layout( - &mut self, - constraint: gpui::SizeConstraint, - view: &mut V, - cx: &mut gpui::LayoutContext, - ) -> (gpui::geometry::vector::Vector2F, Self::LayoutState) { - cx.push_layout_engine(LayoutEngine::new()); - - let size = constraint.max; - let mut cx = LayoutContext::new(cx); - let layout_id = self.0.layout(view, &mut cx).log_err(); - if let Some(layout_id) = layout_id { - cx.layout_engine() - .unwrap() - .compute_layout(layout_id, constraint.max) - .log_err(); - } - - let layout_engine = cx.pop_layout_engine(); - debug_assert!(layout_engine.is_some(), - "unexpected layout stack state. is there an unmatched pop_layout_engine in the called code?" - ); - - (constraint.max, layout_engine.zip(layout_id)) - } - - fn paint( - &mut self, - scene: &mut gpui::SceneBuilder, - bounds: RectF, - visible_bounds: RectF, - layout_data: &mut Option<(LayoutEngine, LayoutId)>, - view: &mut V, - legacy_cx: &mut gpui::PaintContext, - ) -> Self::PaintState { - let (layout_engine, layout_id) = layout_data.take().unwrap(); - legacy_cx.push_layout_engine(layout_engine); - let mut cx = PaintContext::new(legacy_cx, scene); - self.0.paint(view, bounds.origin(), &mut cx); - *layout_data = legacy_cx.pop_layout_engine().zip(Some(layout_id)); - debug_assert!(layout_data.is_some()); - } - - fn rect_for_text_range( - &self, - range_utf16: std::ops::Range, - bounds: RectF, - visible_bounds: RectF, - layout: &Self::LayoutState, - paint: &Self::PaintState, - view: &V, - cx: &gpui::ViewContext, - ) -> Option { - todo!("implement before merging to main") - } - - fn debug( - &self, - bounds: RectF, - layout: &Self::LayoutState, - paint: &Self::PaintState, - view: &V, - cx: &gpui::ViewContext, - ) -> gpui::serde_json::Value { - todo!("implement before merging to main") - } -} diff --git a/crates/gpui/playground/src/components.rs b/crates/gpui/playground/src/components.rs deleted file mode 100644 index a98fea3383..0000000000 --- a/crates/gpui/playground/src/components.rs +++ /dev/null @@ -1,101 +0,0 @@ -use crate::{ - div::div, - element::{IntoElement, ParentElement}, - interactive::Interactive, - style::StyleHelpers, - text::ArcCow, - // themes::Theme, -}; -use gpui::{platform::MouseButton, ViewContext}; -use gpui2_macros::Element; -use std::{marker::PhantomData, rc::Rc}; - -struct ButtonHandlers { - click: Option)>>, -} - -impl Default for ButtonHandlers { - fn default() -> Self { - Self { click: None } - } -} - -use crate as gpui2; -#[derive(Element)] -pub struct Button { - handlers: ButtonHandlers, - label: Option>, - icon: Option>, - data: Rc, - view_type: PhantomData, -} - -// Impl block for buttons without data. -// See below for an impl block for any button. -impl Button { - fn new() -> Self { - Self { - handlers: ButtonHandlers::default(), - label: None, - icon: None, - data: Rc::new(()), - view_type: PhantomData, - } - } - - pub fn data(self, data: D) -> Button { - Button { - handlers: ButtonHandlers::default(), - label: self.label, - icon: self.icon, - data: Rc::new(data), - view_type: PhantomData, - } - } -} - -// Impl block for button regardless of its data type. -impl Button { - pub fn label(mut self, label: impl Into>) -> Self { - self.label = Some(label.into()); - self - } - - pub fn icon(mut self, icon: impl Into>) -> Self { - self.icon = Some(icon.into()); - self - } - - pub fn on_click(mut self, handler: impl Fn(&mut V, &D, &mut ViewContext) + 'static) -> Self { - self.handlers.click = Some(Rc::new(handler)); - self - } -} - -pub fn button() -> Button { - Button::new() -} - -impl Button { - fn render( - &mut self, - view: &mut V, - cx: &mut ViewContext, - ) -> impl IntoElement + Interactive { - // let colors = &cx.theme::().colors; - - let button = div() - // .fill(colors.error(0.5)) - .h_4() - .children(self.label.clone()); - - if let Some(handler) = self.handlers.click.clone() { - let data = self.data.clone(); - button.on_mouse_down(MouseButton::Left, move |view, event, cx| { - handler(view, data.as_ref(), cx) - }) - } else { - button - } - } -} diff --git a/crates/gpui/playground/src/div.rs b/crates/gpui/playground/src/div.rs deleted file mode 100644 index 79d539b221..0000000000 --- a/crates/gpui/playground/src/div.rs +++ /dev/null @@ -1,116 +0,0 @@ -use crate::{ - element::{AnyElement, Element, IntoElement, Layout, ParentElement}, - interactive::{InteractionHandlers, Interactive}, - layout_context::LayoutContext, - paint_context::PaintContext, - style::{Style, StyleHelpers, Styleable}, -}; -use anyhow::Result; -use gpui::{LayoutId, RenderContext}; -use refineable::{Refineable, RefinementCascade}; -use smallvec::SmallVec; - -pub struct Div { - styles: RefinementCascade