This commit is contained in:
Nathan Sobo 2023-09-21 13:46:31 -06:00
parent a53c0b9472
commit a0416e9c6d
3 changed files with 13 additions and 137 deletions

View file

@ -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,
}
}
}

View file

@ -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 {

View file

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