WIP

Nathan Sobo created

Change summary

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(-)

Detailed changes

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<S> IntoAnyElement<S> for AnyElement<S> {
         self
     }
 }
-
-#[derive(Clone)]
-pub struct View<S> {
-    state: Handle<S>,
-    render: Rc<dyn Fn(&mut S, &mut ViewContext<S>) -> AnyElement<S>>,
-}
-
-pub fn view<S: 'static, E: Element<State = S>>(
-    state: Handle<S>,
-    render: impl 'static + Fn(&mut S, &mut ViewContext<S>) -> E,
-) -> View<S> {
-    View {
-        state,
-        render: Rc::new(move |state, cx| render(state, cx).into_any()),
-    }
-}
-
-impl<S: 'static> View<S> {
-    pub fn into_any<ParentState>(self) -> AnyView<ParentState> {
-        AnyView {
-            view: Rc::new(RefCell::new(self)),
-            parent_state_type: PhantomData,
-        }
-    }
-}
-
-impl<S: 'static> Element for View<S> {
-    type State = ();
-    type FrameState = AnyElement<S>;
-
-    fn layout(
-        &mut self,
-        _: &mut Self::State,
-        cx: &mut ViewContext<Self::State>,
-    ) -> 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<Self::State>,
-    ) -> Result<()> {
-        self.state
-            .update(cx, |state, cx| element.paint(state, None, cx))
-    }
-}
-
-trait ViewObject {
-    fn layout(&mut self, cx: &mut WindowContext) -> Result<(LayoutId, Box<dyn Any>)>;
-    fn paint(
-        &mut self,
-        layout: Layout,
-        element: &mut dyn Any,
-        cx: &mut WindowContext,
-    ) -> Result<()>;
-}
-
-impl<S: 'static> ViewObject for View<S> {
-    fn layout(&mut self, cx: &mut WindowContext) -> Result<(LayoutId, Box<dyn Any>)> {
-        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<dyn Any>;
-            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::<AnyElement<S>>()
-                .unwrap()
-                .paint(state, None, cx)
-        })
-    }
-}
-
-pub struct AnyView<S> {
-    view: Rc<RefCell<dyn ViewObject>>,
-    parent_state_type: PhantomData<S>,
-}
-
-impl<S: 'static> Element for AnyView<S> {
-    type State = S;
-    type FrameState = Box<dyn Any>;
-
-    fn layout(
-        &mut self,
-        _: &mut Self::State,
-        cx: &mut ViewContext<Self::State>,
-    ) -> 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<Self::State>,
-    ) -> Result<()> {
-        self.view.borrow_mut().paint(layout, element, cx)
-    }
-}
-
-impl<S> Clone for AnyView<S> {
-    fn clone(&self) -> Self {
-        Self {
-            view: self.view.clone(),
-            parent_state_type: PhantomData,
-        }
-    }
-}

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 {

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<Workspace> {
+    let workspace = cx.entity(|_| Workspace::default());
+    view(workspace, |workspace, cx| workspace.render(cx))
 }
 
-impl WorkspaceElement {
-    fn render<V: 'static>(
-        &mut self,
-        _: &mut V,
-        cx: &mut ViewContext<V>,
-    ) -> impl Element<State = V> {
+impl Workspace {
+    fn render<V: 'static>(&mut self, cx: &mut ViewContext<V>) -> impl Element<State = V> {
         let theme = theme(cx);
 
         div()