From a0416e9c6d9029f7566a07ad3a4ab93ba0c9aa59 Mon Sep 17 00:00:00 2001 From: Nathan Sobo Date: Thu, 21 Sep 2023 13:46:31 -0600 Subject: [PATCH] WIP --- crates/gpui3/src/element.rs | 128 +---------------------------- crates/gpui3/src/gpui3.rs | 2 + crates/storybook2/src/workspace.rs | 20 ++--- 3 files changed, 13 insertions(+), 137 deletions(-) diff --git a/crates/gpui3/src/element.rs b/crates/gpui3/src/element.rs index 95d305d670..df88d6af9b 100644 --- a/crates/gpui3/src/element.rs +++ b/crates/gpui3/src/element.rs @@ -1,6 +1,5 @@ -use smallvec::SmallVec; - use super::{Handle, Layout, LayoutId, Pixels, Point, Result, ViewContext, WindowContext}; +pub(crate) use smallvec::SmallVec; use std::{any::Any, cell::RefCell, marker::PhantomData, rc::Rc}; pub trait Element: 'static { @@ -166,128 +165,3 @@ impl IntoAnyElement for AnyElement { self } } - -#[derive(Clone)] -pub struct View { - state: Handle, - render: Rc) -> AnyElement>, -} - -pub fn view>( - state: Handle, - render: impl 'static + Fn(&mut S, &mut ViewContext) -> E, -) -> View { - View { - state, - render: Rc::new(move |state, cx| render(state, cx).into_any()), - } -} - -impl View { - pub fn into_any(self) -> AnyView { - AnyView { - view: Rc::new(RefCell::new(self)), - parent_state_type: PhantomData, - } - } -} - -impl Element for View { - type State = (); - type FrameState = AnyElement; - - fn layout( - &mut self, - _: &mut Self::State, - cx: &mut ViewContext, - ) -> Result<(LayoutId, Self::FrameState)> { - self.state.update(cx, |state, cx| { - let mut element = (self.render)(state, cx); - let layout_id = element.layout(state, cx)?; - Ok((layout_id, element)) - }) - } - - fn paint( - &mut self, - layout: Layout, - _: &mut Self::State, - element: &mut Self::FrameState, - cx: &mut ViewContext, - ) -> Result<()> { - self.state - .update(cx, |state, cx| element.paint(state, None, cx)) - } -} - -trait ViewObject { - fn layout(&mut self, cx: &mut WindowContext) -> Result<(LayoutId, Box)>; - fn paint( - &mut self, - layout: Layout, - element: &mut dyn Any, - cx: &mut WindowContext, - ) -> Result<()>; -} - -impl ViewObject for View { - fn layout(&mut self, cx: &mut WindowContext) -> Result<(LayoutId, Box)> { - self.state.update(cx, |state, cx| { - let mut element = (self.render)(state, cx); - let layout_id = element.layout(state, cx)?; - let element = Box::new(element) as Box; - Ok((layout_id, element)) - }) - } - - fn paint( - &mut self, - layout: Layout, - element: &mut dyn Any, - cx: &mut WindowContext, - ) -> Result<()> { - self.state.update(cx, |state, cx| { - element - .downcast_mut::>() - .unwrap() - .paint(state, None, cx) - }) - } -} - -pub struct AnyView { - view: Rc>, - parent_state_type: PhantomData, -} - -impl Element for AnyView { - type State = S; - type FrameState = Box; - - fn layout( - &mut self, - _: &mut Self::State, - cx: &mut ViewContext, - ) -> Result<(LayoutId, Self::FrameState)> { - self.view.borrow_mut().layout(cx) - } - - fn paint( - &mut self, - layout: Layout, - _: &mut Self::State, - element: &mut Self::FrameState, - cx: &mut ViewContext, - ) -> Result<()> { - self.view.borrow_mut().paint(layout, element, cx) - } -} - -impl Clone for AnyView { - fn clone(&self) -> Self { - Self { - view: self.view.clone(), - parent_state_type: PhantomData, - } - } -} diff --git a/crates/gpui3/src/gpui3.rs b/crates/gpui3/src/gpui3.rs index 7de72cec5a..73fbc825ee 100644 --- a/crates/gpui3/src/gpui3.rs +++ b/crates/gpui3/src/gpui3.rs @@ -13,6 +13,7 @@ mod styled; mod taffy; mod text_system; mod util; +mod view; mod window; pub use anyhow::Result; @@ -38,6 +39,7 @@ use taffy::TaffyLayoutEngine; pub use taffy::{AvailableSpace, LayoutId}; pub use text_system::*; pub use util::arc_cow::ArcCow; +pub use view::*; pub use window::*; pub trait Context { diff --git a/crates/storybook2/src/workspace.rs b/crates/storybook2/src/workspace.rs index b600e77426..1e3df3bac5 100644 --- a/crates/storybook2/src/workspace.rs +++ b/crates/storybook2/src/workspace.rs @@ -1,22 +1,22 @@ use crate::{collab_panel::collab_panel, theme::theme}; -use gpui3::{div, img, svg, Element, ParentElement, ScrollState, StyleHelpers, ViewContext}; +use gpui3::{ + div, img, svg, view, Element, ParentElement, ScrollState, StyleHelpers, View, ViewContext, + WindowAppearance, WindowContext, +}; #[derive(Default)] -struct WorkspaceElement { +struct Workspace { left_scroll_state: ScrollState, right_scroll_state: ScrollState, } -pub fn workspace() -> impl Element { - WorkspaceElement::default() +pub fn workspace(cx: &mut WindowContext) -> View { + let workspace = cx.entity(|_| Workspace::default()); + view(workspace, |workspace, cx| workspace.render(cx)) } -impl WorkspaceElement { - fn render( - &mut self, - _: &mut V, - cx: &mut ViewContext, - ) -> impl Element { +impl Workspace { + fn render(&mut self, cx: &mut ViewContext) -> impl Element { let theme = theme(cx); div()