view.rs

  1use parking_lot::Mutex;
  2
  3use crate::{
  4    AnyBox, AnyElement, Bounds, Element, Handle, IntoAnyElement, LayoutId, Pixels, ViewContext,
  5    WindowContext,
  6};
  7use std::{any::Any, marker::PhantomData, sync::Arc};
  8
  9pub struct View<S: Send + Sync, P> {
 10    state: Handle<S>,
 11    render: Arc<dyn Fn(&mut S, &mut ViewContext<S>) -> AnyElement<S> + Send + Sync + 'static>,
 12    parent_state_type: PhantomData<P>,
 13}
 14
 15impl<S: 'static + Send + Sync, P: 'static + Send> View<S, P> {
 16    pub fn into_any(self) -> AnyView<P> {
 17        AnyView {
 18            view: Arc::new(Mutex::new(self)),
 19            parent_state_type: PhantomData,
 20        }
 21    }
 22}
 23
 24impl<S: Send + Sync, P> Clone for View<S, P> {
 25    fn clone(&self) -> Self {
 26        Self {
 27            state: self.state.clone(),
 28            render: self.render.clone(),
 29            parent_state_type: PhantomData,
 30        }
 31    }
 32}
 33
 34pub type RootView<S> = View<S, ()>;
 35
 36pub fn view<S, P, E>(
 37    state: Handle<S>,
 38    render: impl Fn(&mut S, &mut ViewContext<S>) -> E + Send + Sync + 'static,
 39) -> View<S, P>
 40where
 41    S: 'static + Send + Sync,
 42    P: 'static,
 43    E: Element<ViewState = S>,
 44{
 45    View {
 46        state,
 47        render: Arc::new(move |state, cx| render(state, cx).into_any()),
 48        parent_state_type: PhantomData,
 49    }
 50}
 51
 52impl<S: 'static + Send + Sync, P: 'static + Send + Sync> Element for View<S, P> {
 53    type ViewState = P;
 54    type ElementState = AnyElement<S>;
 55
 56    fn layout(
 57        &mut self,
 58        _: &mut Self::ViewState,
 59        _: Option<Self::ElementState>,
 60        cx: &mut ViewContext<Self::ViewState>,
 61    ) -> (LayoutId, Self::ElementState) {
 62        self.state.update(cx, |state, cx| {
 63            let mut element = (self.render)(state, cx);
 64            let layout_id = element.layout(state, cx);
 65            (layout_id, element)
 66        })
 67    }
 68
 69    fn paint(
 70        &mut self,
 71        _: Bounds<Pixels>,
 72        _: &mut Self::ViewState,
 73        element: &mut Self::ElementState,
 74        cx: &mut ViewContext<Self::ViewState>,
 75    ) {
 76        self.state
 77            .update(cx, |state, cx| element.paint(state, None, cx))
 78    }
 79}
 80
 81trait ViewObject: Send + 'static {
 82    fn layout(&mut self, cx: &mut WindowContext) -> (LayoutId, AnyBox);
 83    fn paint(&mut self, bounds: Bounds<Pixels>, element: &mut dyn Any, cx: &mut WindowContext);
 84}
 85
 86impl<S: Send + Sync + 'static, P: Send + 'static> ViewObject for View<S, P> {
 87    fn layout(&mut self, cx: &mut WindowContext) -> (LayoutId, AnyBox) {
 88        self.state.update(cx, |state, cx| {
 89            let mut element = (self.render)(state, cx);
 90            let layout_id = element.layout(state, cx);
 91            let element = Box::new(element) as AnyBox;
 92            (layout_id, element)
 93        })
 94    }
 95
 96    fn paint(&mut self, _: Bounds<Pixels>, element: &mut dyn Any, cx: &mut WindowContext) {
 97        self.state.update(cx, |state, cx| {
 98            let element = element.downcast_mut::<AnyElement<S>>().unwrap();
 99            element.paint(state, None, cx);
100        });
101    }
102}
103
104pub struct AnyView<S> {
105    view: Arc<Mutex<dyn ViewObject>>,
106    parent_state_type: PhantomData<S>,
107}
108
109impl<S: 'static + Send + Sync> Element for AnyView<S> {
110    type ViewState = ();
111    type ElementState = AnyBox;
112
113    fn layout(
114        &mut self,
115        _: &mut Self::ViewState,
116        _: Option<Self::ElementState>,
117        cx: &mut ViewContext<Self::ViewState>,
118    ) -> (LayoutId, Self::ElementState) {
119        self.view.lock().layout(cx)
120    }
121
122    fn paint(
123        &mut self,
124        bounds: Bounds<Pixels>,
125        _: &mut (),
126        element: &mut AnyBox,
127        cx: &mut ViewContext<Self::ViewState>,
128    ) {
129        self.view.lock().paint(bounds, element.as_mut(), cx)
130    }
131}
132
133impl<S> Clone for AnyView<S> {
134    fn clone(&self) -> Self {
135        Self {
136            view: self.view.clone(),
137            parent_state_type: PhantomData,
138        }
139    }
140}