WIP: Trait bounds

This commit is contained in:
Marshall Bowers 2023-10-26 10:46:02 +02:00
parent e31a9401a8
commit 4266ead958
2 changed files with 38 additions and 43 deletions

View file

@ -8,6 +8,8 @@ pub trait Element<V: 'static>: IntoAnyElement<V> {
fn id(&self) -> Option<ElementId>; fn id(&self) -> Option<ElementId>;
/// Called to initialize this element for the current frame. If this
/// element had state in a previous frame, it will be passed in for the 3rd argument.
fn initialize( fn initialize(
&mut self, &mut self,
view_state: &mut V, view_state: &mut V,
@ -41,7 +43,7 @@ pub trait Element<V: 'static>: IntoAnyElement<V> {
#[derive(Deref, DerefMut, Default, Clone, Debug, Eq, PartialEq, Hash)] #[derive(Deref, DerefMut, Default, Clone, Debug, Eq, PartialEq, Hash)]
pub struct GlobalElementId(SmallVec<[ElementId; 32]>); pub struct GlobalElementId(SmallVec<[ElementId; 32]>);
pub trait ParentElement<V: 'static>: Element<V> { pub trait ParentElement<V: 'static> {
fn children_mut(&mut self) -> &mut SmallVec<[AnyElement<V>; 2]>; fn children_mut(&mut self) -> &mut SmallVec<[AnyElement<V>; 2]>;
fn child(mut self, child: impl IntoAnyElement<V>) -> Self fn child(mut self, child: impl IntoAnyElement<V>) -> Self

View file

@ -23,56 +23,54 @@ pub struct Pane<S: 'static + Send + Sync> {
impl<V: 'static + Send + Sync> IntoAnyElement<V> for Pane<V> { impl<V: 'static + Send + Sync> IntoAnyElement<V> for Pane<V> {
fn into_any(self) -> AnyElement<V> { fn into_any(self) -> AnyElement<V> {
ElementRenderer { let render = move |view_state, cx| self.render(view_state, cx);
id: Some(self.id),
render: Some(move |view_state, cx| self.render(view_state, cx)), AnyElement::new(ElementRenderer {
render: Some(render),
view_type: PhantomData, view_type: PhantomData,
element_type: PhantomData, element_type: PhantomData,
} })
} }
} }
struct ElementRenderer<V, E, F> struct ElementRenderer<V, E, F>
where where
E: IntoAnyElement<V>, V: 'static + Send + Sync,
F: FnOnce(&mut V, &mut ViewContext<V>) -> E, E: 'static + IntoAnyElement<V> + Send + Sync,
F: FnOnce(&mut V, &mut ViewContext<V>) -> E + 'static + Send + Sync,
{ {
id: Option<ElementId>,
render: Option<F>, render: Option<F>,
view_type: PhantomData<V>, view_type: PhantomData<V>,
element_type: PhantomData<E>, element_type: PhantomData<E>,
} }
impl<V, E, F> Element for ElementRenderer<V, E, F> impl<V, E, F> Element<V> for ElementRenderer<V, E, F>
where where
V: 'static, V: 'static + Send + Sync,
E: IntoAnyElement<V>, E: 'static + IntoAnyElement<V> + Send + Sync,
F: FnOnce(&mut V, &mut ViewContext<V>) -> E, F: FnOnce(&mut V, &mut ViewContext<V>) -> E + 'static + Send + Sync,
{ {
type ViewState = V;
type ElementState = AnyElement<V>; type ElementState = AnyElement<V>;
fn id(&self) -> Option<ElementId> { fn id(&self) -> Option<ElementId> {
self.id None
} }
fn initialize( fn initialize(
&mut self, &mut self,
view_state: &mut Self::ViewState, view_state: &mut V,
rendered_element: Option<Self::ElementState>, _element_state: Option<Self::ElementState>,
cx: &mut ViewContext<Self::ViewState>, cx: &mut ViewContext<V>,
) -> Self::ElementState { ) -> Self::ElementState {
rendered_element.unwrap_or_else(|| { let render = self.render.take().unwrap();
let render = self.render.take().unwrap(); (render)(view_state, cx).into_any()
(render)(view_state, cx)
})
} }
fn layout( fn layout(
&mut self, &mut self,
view_state: &mut Self::ViewState, view_state: &mut V,
rendered_element: &mut Self::ElementState, rendered_element: &mut Self::ElementState,
cx: &mut ViewContext<Self::ViewState>, cx: &mut ViewContext<V>,
) -> gpui2::LayoutId { ) -> gpui2::LayoutId {
rendered_element.layout(view_state, cx) rendered_element.layout(view_state, cx)
} }
@ -80,9 +78,9 @@ where
fn paint( fn paint(
&mut self, &mut self,
bounds: gpui2::Bounds<gpui2::Pixels>, bounds: gpui2::Bounds<gpui2::Pixels>,
view_state: &mut Self::ViewState, view_state: &mut V,
rendered_element: &mut Self::ElementState, rendered_element: &mut Self::ElementState,
cx: &mut ViewContext<Self::ViewState>, cx: &mut ViewContext<V>,
) { ) {
rendered_element.paint(view_state, cx) rendered_element.paint(view_state, cx)
} }
@ -90,16 +88,16 @@ where
impl<V, E, F> IntoAnyElement<V> for ElementRenderer<V, E, F> impl<V, E, F> IntoAnyElement<V> for ElementRenderer<V, E, F>
where where
V: 'static, V: 'static + Send + Sync,
E: IntoAnyElement<V>, E: 'static + IntoAnyElement<V> + Send + Sync,
F: FnOnce(&mut V, &mut ViewContext<V>) -> E, F: FnOnce(&mut V, &mut ViewContext<V>) -> E + 'static + Send + Sync,
{ {
fn into_any(self) -> AnyElement<V> { fn into_any(self) -> AnyElement<V> {
self AnyElement::new(self)
} }
} }
impl<S: 'static + Send + Sync> Pane<S> { impl<V: 'static + Send + Sync> Pane<V> {
pub fn new(id: impl Into<ElementId>, size: Size<Length>) -> Self { pub fn new(id: impl Into<ElementId>, size: Size<Length>) -> Self {
// Fill is only here for debugging purposes, remove before release // Fill is only here for debugging purposes, remove before release
@ -118,7 +116,7 @@ impl<S: 'static + Send + Sync> Pane<S> {
self self
} }
fn render(&mut self, view: &mut S, cx: &mut ViewContext<S>) -> impl Element<S> { fn render(self, view: &mut V, cx: &mut ViewContext<V>) -> Div<V, StatefulInteraction<V>> IntoAnyElement<V> {
div() div()
.id(self.id.clone()) .id(self.id.clone())
.flex() .flex()
@ -127,12 +125,7 @@ impl<S: 'static + Send + Sync> Pane<S> {
.w(self.size.width) .w(self.size.width)
.h(self.size.height) .h(self.size.height)
.relative() .relative()
.child( .child(div().z_index(0).size_full().children(self.children))
div()
.z_index(0)
.size_full()
.children(self.children.drain(..)),
)
.child( .child(
// TODO kb! Figure out why we can't we see the red background when we drag a file over this div. // TODO kb! Figure out why we can't we see the red background when we drag a file over this div.
div() div()
@ -162,8 +155,8 @@ pub struct PaneGroup<S: 'static + Send + Sync> {
split_direction: SplitDirection, split_direction: SplitDirection,
} }
impl<S: 'static + Send + Sync> PaneGroup<S> { impl<V: 'static + Send + Sync> PaneGroup<V> {
pub fn new_groups(groups: Vec<PaneGroup<S>>, split_direction: SplitDirection) -> Self { pub fn new_groups(groups: Vec<PaneGroup<V>>, split_direction: SplitDirection) -> Self {
Self { Self {
state_type: PhantomData, state_type: PhantomData,
groups, groups,
@ -172,7 +165,7 @@ impl<S: 'static + Send + Sync> PaneGroup<S> {
} }
} }
pub fn new_panes(panes: Vec<Pane<S>>, split_direction: SplitDirection) -> Self { pub fn new_panes(panes: Vec<Pane<V>>, split_direction: SplitDirection) -> Self {
Self { Self {
state_type: PhantomData, state_type: PhantomData,
groups: Vec::new(), groups: Vec::new(),
@ -181,7 +174,7 @@ impl<S: 'static + Send + Sync> PaneGroup<S> {
} }
} }
fn render(&mut self, view: &mut S, cx: &mut ViewContext<S>) -> impl Element<S> { fn render(&mut self, view: &mut V, cx: &mut ViewContext<V>) -> impl Element<V> {
let theme = theme(cx); let theme = theme(cx);
if !self.panes.is_empty() { if !self.panes.is_empty() {
@ -191,7 +184,7 @@ impl<S: 'static + Send + Sync> PaneGroup<S> {
.gap_px() .gap_px()
.w_full() .w_full()
.h_full() .h_full()
.children(self.panes.iter_mut().map(|pane| pane.render(view, cx))); .children(self.panes.drain(..).map(|pane| pane.render(view, cx)));
if self.split_direction == SplitDirection::Horizontal { if self.split_direction == SplitDirection::Horizontal {
return el; return el;
@ -208,7 +201,7 @@ impl<S: 'static + Send + Sync> PaneGroup<S> {
.w_full() .w_full()
.h_full() .h_full()
.bg(theme.editor) .bg(theme.editor)
.children(self.groups.iter_mut().map(|group| group.render(view, cx))); .children(self.groups.iter_mut().map(| group| group.render(view, cx)));
if self.split_direction == SplitDirection::Horizontal { if self.split_direction == SplitDirection::Horizontal {
return el; return el;