diff --git a/crates/gpui/playground/src/adapter.rs b/crates/gpui/playground/src/adapter.rs index 44d830be8f..8e1be3b5f9 100644 --- a/crates/gpui/playground/src/adapter.rs +++ b/crates/gpui/playground/src/adapter.rs @@ -5,25 +5,22 @@ use util::ResultExt; use crate::element::AnyElement; #[derive(Clone)] -pub struct Adapter { - view: V, - element: AnyElement, -} +pub struct Adapter(pub(crate) AnyElement); -impl gpui::Element> for Adapter { +impl gpui::Element for Adapter { type LayoutState = (); type PaintState = (); fn layout( &mut self, constraint: gpui::SizeConstraint, - view: &mut Self, - legacy_cx: &mut gpui::LayoutContext, + view: &mut V, + legacy_cx: &mut gpui::LayoutContext, ) -> (gpui::geometry::vector::Vector2F, Self::LayoutState) { legacy_cx.push_layout_engine(); let node = self - .element - .layout(&mut self.view, &mut LayoutContext { legacy_cx }) + .0 + .layout(view, &mut LayoutContext { legacy_cx }) .log_err(); if let Some(node) = node { @@ -41,11 +38,11 @@ impl gpui::Element> for Adapter { bounds: RectF, visible_bounds: RectF, layout: &mut (), - adapter: &mut Self, - legacy_cx: &mut gpui::PaintContext, + view: &mut V, + legacy_cx: &mut gpui::PaintContext, ) -> Self::PaintState { let mut cx = PaintContext { legacy_cx, scene }; - self.element.paint(&mut adapter.view, &mut cx).log_err(); + self.0.paint(view, &mut cx).log_err(); } fn rect_for_text_range( @@ -55,8 +52,8 @@ impl gpui::Element> for Adapter { visible_bounds: RectF, layout: &Self::LayoutState, paint: &Self::PaintState, - view: &Adapter, - cx: &gpui::ViewContext>, + view: &V, + cx: &gpui::ViewContext, ) -> Option { todo!() } @@ -66,22 +63,9 @@ impl gpui::Element> for Adapter { bounds: RectF, layout: &Self::LayoutState, paint: &Self::PaintState, - view: &Adapter, - cx: &gpui::ViewContext>, + view: &V, + cx: &gpui::ViewContext, ) -> gpui::serde_json::Value { todo!() } } - -impl gpui::Entity for Adapter { - type Event = (); -} - -impl gpui::View for Adapter -where - V: Clone, -{ - fn render(&mut self, cx: &mut gpui::ViewContext<'_, '_, Self>) -> gpui::AnyElement { - gpui::Element::into_any(self.clone()) - } -} diff --git a/crates/gpui/playground/src/element.rs b/crates/gpui/playground/src/element.rs index 8b5fdf2d7c..7198404919 100644 --- a/crates/gpui/playground/src/element.rs +++ b/crates/gpui/playground/src/element.rs @@ -10,14 +10,14 @@ pub use taffy::tree::NodeId; #[derive(Deref, DerefMut)] pub struct LayoutContext<'a, 'b, 'c, 'd, V> { - pub(crate) legacy_cx: &'d mut LegacyLayoutContext<'a, 'b, 'c, Adapter>, + pub(crate) legacy_cx: &'d mut LegacyLayoutContext<'a, 'b, 'c, V>, } #[derive(Deref, DerefMut)] pub struct PaintContext<'a, 'b, 'c, 'd, V> { #[deref] #[deref_mut] - pub(crate) legacy_cx: &'d mut LegacyPaintContext<'a, 'b, 'c, Adapter>, + pub(crate) legacy_cx: &'d mut LegacyPaintContext<'a, 'b, 'c, V>, pub(crate) scene: &'d mut gpui::SceneBuilder, } @@ -37,6 +37,14 @@ pub trait Element: 'static + Clone { } } + fn adapt(self) -> Adapter + where + Self: Sized, + Self: Element, + { + Adapter(self.into_any()) + } + // Display //////////////////// fn block(mut self) -> Self @@ -200,14 +208,15 @@ pub trait Element: 'static + Clone { } pub trait ElementObject { - fn clone_object(&self) -> Box>; + fn style_mut(&mut self) -> &mut Style; fn layout(&mut self, view: &mut V, cx: &mut LayoutContext) -> Result; fn paint(&mut self, layout: Layout, view: &mut V, cx: &mut PaintContext) -> Result<()>; + fn clone_object(&self) -> Box>; } impl> ElementObject for E { - fn clone_object(&self) -> Box> { - Box::new(Clone::clone(self)) + fn style_mut(&mut self) -> &mut Style { + self.style_mut() } fn layout(&mut self, view: &mut V, cx: &mut LayoutContext) -> Result { @@ -217,6 +226,10 @@ impl> ElementObject for E { fn paint(&mut self, layout: Layout, view: &mut V, cx: &mut PaintContext) -> Result<()> { self.paint(layout, view, cx) } + + fn clone_object(&self) -> Box> { + Box::new(Clone::clone(self)) + } } pub struct AnyElement { @@ -250,3 +263,17 @@ impl Clone for AnyElement { } } } + +impl Element for AnyElement { + fn style_mut(&mut self) -> &mut Style { + self.element.style_mut() + } + + fn layout(&mut self, view: &mut V, cx: &mut LayoutContext) -> Result { + self.layout(view, cx) + } + + fn paint(&mut self, layout: Layout, view: &mut V, cx: &mut PaintContext) -> Result<()> { + self.paint(view, cx) + } +} diff --git a/crates/gpui/playground/src/playground.rs b/crates/gpui/playground/src/playground.rs index 07371d71d0..4c06dbb59e 100644 --- a/crates/gpui/playground/src/playground.rs +++ b/crates/gpui/playground/src/playground.rs @@ -3,22 +3,20 @@ use element::{AnyElement, Element}; use frame::frame; use log::LevelFilter; use simplelog::SimpleLogger; -use taffy::tree::NodeId; fn main() { SimpleLogger::init(LevelFilter::Info, Default::default()).expect("could not initialize logger"); gpui::App::new(()).unwrap().run(|cx| { + cx.add_window(Default::default(), |_| { + view(|_| workspace(&rose_pine::moon())) + }); cx.platform().activate(true); - - // cx.add_window( - // Default::default(), - // // |_| view(|_| Playground::new()), - // ); }); } use themes::{rose_pine, ThemeColors}; +use view::view; mod adapter; mod color; @@ -26,60 +24,10 @@ mod element; mod frame; mod style; mod themes; +mod view; pub struct Playground(AnyElement); -impl gpui::Element for Playground { - type LayoutState = NodeId; - - type PaintState = (); - - fn layout( - &mut self, - constraint: gpui::SizeConstraint, - view: &mut V, - cx: &mut gpui::LayoutContext, - ) -> (gpui::geometry::vector::Vector2F, Self::LayoutState) { - todo!() - } - - fn paint( - &mut self, - scene: &mut gpui::SceneBuilder, - bounds: gpui::geometry::rect::RectF, - visible_bounds: gpui::geometry::rect::RectF, - layout: &mut Self::LayoutState, - view: &mut V, - cx: &mut gpui::PaintContext, - ) -> Self::PaintState { - todo!() - } - - fn rect_for_text_range( - &self, - range_utf16: std::ops::Range, - bounds: gpui::geometry::rect::RectF, - visible_bounds: gpui::geometry::rect::RectF, - layout: &Self::LayoutState, - paint: &Self::PaintState, - view: &V, - cx: &gpui::ViewContext, - ) -> Option { - todo!() - } - - fn debug( - &self, - bounds: gpui::geometry::rect::RectF, - layout: &Self::LayoutState, - paint: &Self::PaintState, - view: &V, - cx: &gpui::ViewContext, - ) -> gpui::serde_json::Value { - todo!() - } -} - impl Playground { pub fn new() -> Self { Self(workspace(&rose_pine::moon()).into_any()) diff --git a/crates/gpui/playground/src/view.rs b/crates/gpui/playground/src/view.rs new file mode 100644 index 0000000000..952ff5c00d --- /dev/null +++ b/crates/gpui/playground/src/view.rs @@ -0,0 +1,22 @@ +use crate::element::{AnyElement, Element}; +use gpui::{Element as _, ViewContext}; + +pub fn view(mut render: F) -> ViewFn +where + F: 'static + FnMut(&mut ViewContext) -> E, + E: Element, +{ + ViewFn(Box::new(move |cx| (render)(cx).into_any())) +} + +pub struct ViewFn(Box) -> AnyElement>); + +impl gpui::Entity for ViewFn { + type Event = (); +} + +impl gpui::View for ViewFn { + fn render(&mut self, cx: &mut ViewContext) -> gpui::AnyElement { + (self.0)(cx).adapt().into_any() + } +} diff --git a/crates/gpui/src/app/window.rs b/crates/gpui/src/app/window.rs index 9ff155b760..0941203ee4 100644 --- a/crates/gpui/src/app/window.rs +++ b/crates/gpui/src/app/window.rs @@ -1461,6 +1461,7 @@ impl ToJson for SizeConstraint { } } +#[derive(Clone)] pub struct ChildView { view_id: usize, view_name: &'static str, diff --git a/crates/gpui/src/elements.rs b/crates/gpui/src/elements.rs index 0b600a02b9..7246968809 100644 --- a/crates/gpui/src/elements.rs +++ b/crates/gpui/src/elements.rs @@ -33,8 +33,8 @@ use crate::{ rect::RectF, vector::{vec2f, Vector2F}, }, - json, Action, LayoutContext, PaintContext, SceneBuilder, SizeConstraint, View, ViewContext, - WeakViewHandle, WindowContext, + json, Action, Entity, LayoutContext, PaintContext, SceneBuilder, SizeConstraint, View, + ViewContext, WeakViewHandle, WindowContext, }; use anyhow::{anyhow, Result}; use collections::HashMap; @@ -564,6 +564,12 @@ impl Element for AnyElement { } } +impl Entity for AnyElement<()> { + type Event = (); +} + +// impl View for AnyElement<()> {} + pub struct RootElement { element: AnyElement, view: WeakViewHandle,