use crate::{ AnyBox, AnyElement, BorrowWindow, Bounds, Component, Element, ElementId, EntityId, Handle, LayoutId, Pixels, ViewContext, WeakHandle, WindowContext, }; use parking_lot::Mutex; use std::{ marker::PhantomData, sync::{Arc, Weak}, }; pub struct View { pub(crate) state: Handle, render: Arc) -> AnyElement + Send + 'static>>, } pub fn view( state: Handle, render: impl Fn(&mut V, &mut ViewContext<'_, '_, V>) -> E + Send + 'static, ) -> View where E: Component, { View { state, render: Arc::new(Mutex::new( move |state: &mut V, cx: &mut ViewContext<'_, '_, V>| render(state, cx).render(), )), } } impl View { pub fn into_any(self) -> AnyView { AnyView(Arc::new(self)) } pub fn downgrade(&self) -> WeakView { WeakView { state: self.state.downgrade(), render: Arc::downgrade(&self.render), } } } impl View { pub fn update( &self, cx: &mut WindowContext, f: impl FnOnce(&mut V, &mut ViewContext) -> R, ) -> R { let this = self.clone(); let mut lease = cx.app.entities.lease(&self.state); let mut cx = ViewContext::mutable(&mut *cx.app, &mut *cx.window, this); let result = f(&mut *lease, &mut cx); cx.app.entities.end_lease(lease); result } } impl Clone for View { fn clone(&self) -> Self { Self { state: self.state.clone(), render: self.render.clone(), } } } impl Component for View { fn render(self) -> AnyElement { AnyElement::new(EraseViewState { view: self, parent_view_state_type: PhantomData, }) } } impl Element<()> for View { type ElementState = AnyElement; fn id(&self) -> Option { Some(ElementId::View(self.state.entity_id)) } fn initialize( &mut self, _: &mut (), _: Option, cx: &mut ViewContext<()>, ) -> Self::ElementState { self.update(cx, |state, cx| { let mut any_element = (self.render.lock())(state, cx); any_element.initialize(state, cx); any_element }) } fn layout( &mut self, _: &mut (), element: &mut Self::ElementState, cx: &mut ViewContext<()>, ) -> LayoutId { self.update(cx, |state, cx| element.layout(state, cx)) } fn paint( &mut self, _: Bounds, _: &mut (), element: &mut Self::ElementState, cx: &mut ViewContext<()>, ) { self.update(cx, |state, cx| element.paint(state, cx)) } } pub struct WeakView { state: WeakHandle, render: Weak) -> AnyElement + Send + 'static>>, } impl WeakView { pub fn upgrade(&self) -> Option> { let state = self.state.upgrade()?; let render = self.render.upgrade()?; Some(View { state, render }) } } struct EraseViewState { view: View, parent_view_state_type: PhantomData, } unsafe impl Send for EraseViewState {} impl Component for EraseViewState { fn render(self) -> AnyElement { AnyElement::new(self) } } impl Element for EraseViewState { type ElementState = AnyBox; fn id(&self) -> Option { Element::id(&self.view) } fn initialize( &mut self, _: &mut ParentV, _: Option, cx: &mut ViewContext, ) -> Self::ElementState { ViewObject::initialize(&mut self.view, cx) } fn layout( &mut self, _: &mut ParentV, element: &mut Self::ElementState, cx: &mut ViewContext, ) -> LayoutId { ViewObject::layout(&mut self.view, element, cx) } fn paint( &mut self, bounds: Bounds, _: &mut ParentV, element: &mut Self::ElementState, cx: &mut ViewContext, ) { ViewObject::paint(&mut self.view, bounds, element, cx) } } trait ViewObject: Send + Sync { fn entity_id(&self) -> EntityId; fn initialize(&self, cx: &mut WindowContext) -> AnyBox; fn layout(&self, element: &mut AnyBox, cx: &mut WindowContext) -> LayoutId; fn paint(&self, bounds: Bounds, element: &mut AnyBox, cx: &mut WindowContext); } impl ViewObject for View { fn entity_id(&self) -> EntityId { self.state.entity_id } fn initialize(&self, cx: &mut WindowContext) -> AnyBox { cx.with_element_id(self.entity_id(), |_global_id, cx| { self.update(cx, |state, cx| { let mut any_element = Box::new((self.render.lock())(state, cx)); any_element.initialize(state, cx); any_element as AnyBox }) }) } fn layout(&self, element: &mut AnyBox, cx: &mut WindowContext) -> LayoutId { cx.with_element_id(self.entity_id(), |_global_id, cx| { self.update(cx, |state, cx| { let element = element.downcast_mut::>().unwrap(); element.layout(state, cx) }) }) } fn paint(&self, _: Bounds, element: &mut AnyBox, cx: &mut WindowContext) { cx.with_element_id(self.entity_id(), |_global_id, cx| { self.update(cx, |state, cx| { let element = element.downcast_mut::>().unwrap(); element.paint(state, cx); }); }); } } #[derive(Clone)] pub struct AnyView(Arc); impl Component for AnyView { fn render(self) -> AnyElement { AnyElement::new(EraseAnyViewState { view: self, parent_view_state_type: PhantomData, }) } } impl Element<()> for AnyView { type ElementState = AnyBox; fn id(&self) -> Option { Some(ElementId::View(self.0.entity_id())) } fn initialize( &mut self, _: &mut (), _: Option, cx: &mut ViewContext<()>, ) -> Self::ElementState { self.0.initialize(cx) } fn layout( &mut self, _: &mut (), element: &mut Self::ElementState, cx: &mut ViewContext<()>, ) -> LayoutId { self.0.layout(element, cx) } fn paint( &mut self, bounds: Bounds, _: &mut (), element: &mut AnyBox, cx: &mut ViewContext<()>, ) { self.0.paint(bounds, element, cx) } } struct EraseAnyViewState { view: AnyView, parent_view_state_type: PhantomData, } unsafe impl Send for EraseAnyViewState {} impl Component for EraseAnyViewState { fn render(self) -> AnyElement { AnyElement::new(self) } } impl Element for EraseAnyViewState { type ElementState = AnyBox; fn id(&self) -> Option { Element::id(&self.view) } fn initialize( &mut self, _: &mut ParentV, _: Option, cx: &mut ViewContext, ) -> Self::ElementState { self.view.0.initialize(cx) } fn layout( &mut self, _: &mut ParentV, element: &mut Self::ElementState, cx: &mut ViewContext, ) -> LayoutId { self.view.0.layout(element, cx) } fn paint( &mut self, bounds: Bounds, _: &mut ParentV, element: &mut Self::ElementState, cx: &mut ViewContext, ) { self.view.0.paint(bounds, element, cx) } }