From 3ba88574917ccc00b6badcf71dbf2b1cfb96c203 Mon Sep 17 00:00:00 2001 From: Nathan Sobo Date: Mon, 11 Sep 2023 12:58:55 -0600 Subject: [PATCH 001/700] Checkpoint --- crates/gpui/src/geometry.rs | 9 +++++ crates/gpui2/src/elements/text.rs | 14 ++++++- crates/gpui2/src/gpui2.rs | 1 + crates/gpui2/src/view_context.rs | 37 ++++++++++++++++- crates/gpui2/src/view_handle.rs | 60 ++++++++++++++++++++++++++++ crates/storybook/src/collab_panel.rs | 4 +- 6 files changed, 120 insertions(+), 5 deletions(-) create mode 100644 crates/gpui2/src/view_handle.rs diff --git a/crates/gpui/src/geometry.rs b/crates/gpui/src/geometry.rs index c83ad73b5db16a457a94d0494743e7ee3f352f4a..b4e57df4e68151c1a9b7919b8041d65b64456dcf 100644 --- a/crates/gpui/src/geometry.rs +++ b/crates/gpui/src/geometry.rs @@ -167,6 +167,15 @@ pub struct Size { pub height: T, } +impl Size { + pub fn full() -> Self { + Self { + width: relative(1.), + height: relative(1.), + } + } +} + impl From> for Size where S: Into, diff --git a/crates/gpui2/src/elements/text.rs b/crates/gpui2/src/elements/text.rs index 6f89375df09085e828350d85274d9681f5f0aaa7..ce12e449ec51d4232055ab4329e0daa6067c1b17 100644 --- a/crates/gpui2/src/elements/text.rs +++ b/crates/gpui2/src/elements/text.rs @@ -12,11 +12,21 @@ use parking_lot::Mutex; use std::sync::Arc; use util::arc_cow::ArcCow; -impl>> IntoElement for S { +impl IntoElement for ArcCow<'static, str> { type Element = Text; fn into_element(self) -> Self::Element { - Text { text: self.into() } + Text { text: self } + } +} + +impl IntoElement for &'static str { + type Element = Text; + + fn into_element(self) -> Self::Element { + Text { + text: ArcCow::from(self), + } } } diff --git a/crates/gpui2/src/gpui2.rs b/crates/gpui2/src/gpui2.rs index 355697595ff2fc6e6d610b8ce7ce4d4cd8126c4c..b10b6629b592c9ddbe798bace1fac119f7961fcd 100644 --- a/crates/gpui2/src/gpui2.rs +++ b/crates/gpui2/src/gpui2.rs @@ -6,6 +6,7 @@ pub mod interactive; pub mod style; pub mod view; pub mod view_context; +pub mod view_handle; pub use color::*; pub use element::{AnyElement, Element, IntoElement, Layout, ParentElement}; diff --git a/crates/gpui2/src/view_context.rs b/crates/gpui2/src/view_context.rs index d6c0960a348bfef2dfff8adeafd24e7f7019549f..b0f6426ef84be1266b5b5b7c3b9cdb6caf43e3ae 100644 --- a/crates/gpui2/src/view_context.rs +++ b/crates/gpui2/src/view_context.rs @@ -3,7 +3,10 @@ use std::{any::TypeId, rc::Rc}; use crate::{element::LayoutId, style::Style}; use anyhow::{anyhow, Result}; use derive_more::{Deref, DerefMut}; -use gpui::{geometry::Size, scene::EventHandler, EventContext, Layout, MeasureParams}; +use gpui::{ + geometry::Size, scene::EventHandler, AnyWindowHandle, BorrowWindowContext, EventContext, + Layout, MeasureParams, WindowContext, +}; pub use gpui::{taffy::tree::NodeId, ViewContext as LegacyViewContext}; #[derive(Deref, DerefMut)] @@ -77,3 +80,35 @@ impl<'a, 'b, 'c, V: 'static> ViewContext<'a, 'b, 'c, V> { .computed_layout(layout_id) } } + +impl<'a, 'b, 'c, V: 'static> BorrowWindowContext for ViewContext<'a, 'b, 'c, V> { + type Result = T; + + fn read_window(&self, window: AnyWindowHandle, f: F) -> Self::Result + where + F: FnOnce(&WindowContext) -> T, + { + self.legacy_cx.read_window(window, f) + } + + fn read_window_optional(&self, window: AnyWindowHandle, f: F) -> Option + where + F: FnOnce(&WindowContext) -> Option, + { + self.legacy_cx.read_window_optional(window, f) + } + + fn update_window(&mut self, window: AnyWindowHandle, f: F) -> Self::Result + where + F: FnOnce(&mut WindowContext) -> T, + { + self.legacy_cx.update_window(window, f) + } + + fn update_window_optional(&mut self, window: AnyWindowHandle, f: F) -> Option + where + F: FnOnce(&mut WindowContext) -> Option, + { + self.legacy_cx.update_window_optional(window, f) + } +} diff --git a/crates/gpui2/src/view_handle.rs b/crates/gpui2/src/view_handle.rs new file mode 100644 index 0000000000000000000000000000000000000000..dfbe76be2e0e7ade4305305c0aeb47f88f30d3b4 --- /dev/null +++ b/crates/gpui2/src/view_handle.rs @@ -0,0 +1,60 @@ +use crate::{style::Style, Element, IntoElement, ViewContext}; +use gpui::{ + geometry::{Point, Size}, + taffy::style::Overflow, + AnyElement, View, ViewHandle, +}; + +impl Element for ViewHandle { + type PaintState = AnyElement; + + fn layout( + &mut self, + _: &mut ParentView, + cx: &mut ViewContext, + ) -> anyhow::Result<(gpui::LayoutId, Self::PaintState)> + where + Self: Sized, + { + let layout_id = cx.add_layout_node( + Style { + overflow: Point { + x: Overflow::Hidden, + y: Overflow::Hidden, + }, + size: Size::full(), + ..Default::default() + }, + None, + )?; + let element = self.update(cx, |view, cx| view.render(cx)); + Ok((layout_id, element)) + } + + fn paint( + &mut self, + _: &mut ParentView, + parent_origin: gpui::geometry::vector::Vector2F, + layout: &gpui::Layout, + element: &mut AnyElement, + cx: &mut ViewContext, + ) where + Self: Sized, + { + self.update(cx, |view, cx| { + let bounds = layout.bounds + parent_origin; + element.layout(gpui::SizeConstraint::strict(bounds.size()), view, cx); + cx.paint_layer(Some(layout.bounds), |cx| { + element.paint(bounds.origin(), bounds, view, cx); + }); + }) + } +} + +impl IntoElement for ViewHandle { + type Element = Self; + + fn into_element(self) -> Self::Element { + self + } +} diff --git a/crates/storybook/src/collab_panel.rs b/crates/storybook/src/collab_panel.rs index 7bf08febe3a5980b27d5a8446dc0dab3405a652b..26e7f77871894510abb99699f09ac53e4efb7a96 100644 --- a/crates/storybook/src/collab_panel.rs +++ b/crates/storybook/src/collab_panel.rs @@ -117,7 +117,7 @@ impl CollabPanelElement { fn list_section_header( &self, - label: impl Into>, + label: impl IntoElement, expanded: bool, theme: &Theme, ) -> impl Element { @@ -146,7 +146,7 @@ impl CollabPanelElement { fn list_item( &self, avatar_uri: impl Into>, - label: impl Into>, + label: impl IntoElement, theme: &Theme, ) -> impl Element { div() From 5dad97779afaad0176d1b46ed70d03d6e9ca54df Mon Sep 17 00:00:00 2001 From: Nathan Sobo Date: Tue, 12 Sep 2023 07:34:42 -0600 Subject: [PATCH 002/700] WIP --- Cargo.lock | 1 + crates/gpui2/src/style.rs | 2 ++ crates/refineable/src/refineable.rs | 6 ++++++ crates/storybook/Cargo.toml | 1 + crates/storybook/build.rs | 5 +++++ 5 files changed, 15 insertions(+) create mode 100644 crates/storybook/build.rs diff --git a/Cargo.lock b/Cargo.lock index 775e1d2b8e3139b32e9b712f813977e93ebebefd..6cf5d23d579804150e5323d9db8e4125649f18ed 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -7378,6 +7378,7 @@ dependencies = [ "anyhow", "gpui2", "log", + "refineable", "rust-embed", "serde", "settings", diff --git a/crates/gpui2/src/style.rs b/crates/gpui2/src/style.rs index e3e0f5b0c4655fb75d76aaf15935b6de271d3ff1..7cd497e85c4175128cf7a0aa3765e2c3a84f199f 100644 --- a/crates/gpui2/src/style.rs +++ b/crates/gpui2/src/style.rs @@ -22,6 +22,8 @@ use gpui2_macros::styleable_helpers; use refineable::{Refineable, RefinementCascade}; use std::sync::Arc; +pub type StyleCascade = RefinementCascade