diff --git a/crates/gpui2/src/element.rs b/crates/gpui2/src/element.rs index 3b53884e354b93486332197e3fe04680d1865b55..4b5e61be4a50e230fe3cd78a90b7b5ca3f34486a 100644 --- a/crates/gpui2/src/element.rs +++ b/crates/gpui2/src/element.rs @@ -180,6 +180,9 @@ where pub struct AnyElement(Box + Send + Sync>); +unsafe impl Send for AnyElement {} +unsafe impl Sync for AnyElement {} + impl AnyElement { pub fn new(element: E) -> Self where @@ -213,3 +216,67 @@ impl IntoAnyElement for AnyElement { self } } + +impl Element for Option +where + V: 'static, + E: 'static + IntoAnyElement + Send + Sync, + F: FnOnce(&mut V, &mut ViewContext<'_, '_, V>) -> E + Send + Sync + 'static, +{ + type ElementState = AnyElement; + + fn id(&self) -> Option { + None + } + + fn initialize( + &mut self, + view_state: &mut V, + _rendered_element: Option, + cx: &mut ViewContext, + ) -> Self::ElementState { + let render = self.take().unwrap(); + (render)(view_state, cx).into_any() + } + + fn layout( + &mut self, + view_state: &mut V, + rendered_element: &mut Self::ElementState, + cx: &mut ViewContext, + ) -> LayoutId { + rendered_element.layout(view_state, cx) + } + + fn paint( + &mut self, + _bounds: Bounds, + view_state: &mut V, + rendered_element: &mut Self::ElementState, + cx: &mut ViewContext, + ) { + rendered_element.paint(view_state, cx) + } +} + +impl IntoAnyElement for Option +where + V: 'static, + E: 'static + IntoAnyElement + Send + Sync, + F: FnOnce(&mut V, &mut ViewContext<'_, '_, V>) -> E + Send + Sync + 'static, +{ + fn into_any(self) -> AnyElement { + AnyElement::new(self) + } +} + +impl IntoAnyElement for F +where + V: 'static, + E: 'static + IntoAnyElement + Send + Sync, + F: FnOnce(&mut V, &mut ViewContext<'_, '_, V>) -> E + Send + Sync + 'static, +{ + fn into_any(self) -> AnyElement { + AnyElement::new(Some(self)) + } +} diff --git a/crates/ui2/src/components/panes.rs b/crates/ui2/src/components/panes.rs index 8ad1d4091df31cb5774b0ad8f42ac9c5aa4099b1..229de3caa434ede1fb227e09e0df9db88f7e0588 100644 --- a/crates/ui2/src/components/panes.rs +++ b/crates/ui2/src/components/panes.rs @@ -12,105 +12,17 @@ pub enum SplitDirection { Vertical, } -// #[derive(Element)] pub struct Pane { id: ElementId, - state_type: PhantomData, size: Size, fill: Hsla, children: SmallVec<[AnyElement; 2]>, } -impl IntoAnyElement for Pane { - fn into_any(self) -> AnyElement { - let render = - move |view_state: &mut V, cx: &mut ViewContext<'_, '_, V>| self.render(view_state, cx); - - AnyElement::new(ElementRenderer { - render: Some(render), - view_type: PhantomData, - element_type: PhantomData, - }) - } -} - -struct ElementRenderer -where - V: 'static, - E: 'static + IntoAnyElement + Send + Sync, - F: FnOnce(&mut V, &mut ViewContext<'_, '_, V>) -> E + 'static + Send + Sync, -{ - render: Option, - view_type: PhantomData, - element_type: PhantomData, -} - -unsafe impl Send for ElementRenderer -where - V: 'static, - E: 'static + IntoAnyElement + Send + Sync, - F: FnOnce(&mut V, &mut ViewContext<'_, '_, V>) -> E + 'static + Send + Sync, -{ -} - -unsafe impl Sync for ElementRenderer -where - V: 'static, - E: 'static + IntoAnyElement + Send + Sync, - F: FnOnce(&mut V, &mut ViewContext<'_, '_, V>) -> E + 'static + Send + Sync, -{ -} - -impl Element for ElementRenderer -where - V: 'static, - E: 'static + IntoAnyElement + Send + Sync, - F: FnOnce(&mut V, &mut ViewContext<'_, '_, V>) -> E + 'static + Send + Sync, -{ - type ElementState = AnyElement; - - fn id(&self) -> Option { - None - } - - fn initialize( - &mut self, - view_state: &mut V, - _element_state: Option, - cx: &mut ViewContext, - ) -> Self::ElementState { - let render = self.render.take().unwrap(); - (render)(view_state, cx).into_any() - } - - fn layout( - &mut self, - view_state: &mut V, - rendered_element: &mut Self::ElementState, - cx: &mut ViewContext, - ) -> gpui2::LayoutId { - rendered_element.layout(view_state, cx) - } - - fn paint( - &mut self, - bounds: gpui2::Bounds, - view_state: &mut V, - rendered_element: &mut Self::ElementState, - cx: &mut ViewContext, - ) { - rendered_element.paint(view_state, cx) - } -} - -impl IntoAnyElement for ElementRenderer -where - V: 'static, - E: 'static + IntoAnyElement + Send + Sync, - F: FnOnce(&mut V, &mut ViewContext) -> E + 'static + Send + Sync, -{ +impl IntoAnyElement for Pane { fn into_any(self) -> AnyElement { - AnyElement::new(self) + (move |view_state: &mut V, cx: &mut ViewContext<'_, '_, V>| self.render(view_state, cx)) + .into_any() } } @@ -120,10 +32,8 @@ impl Pane { Self { id: id.into(), - state_type: PhantomData, size, fill: hsla(0.3, 0.3, 0.3, 1.), - // fill: system_color.transparent, children: SmallVec::new(), } }