WIP
This commit is contained in:
parent
98d03f6e7a
commit
637cff3ebd
22 changed files with 206 additions and 267 deletions
|
@ -8,14 +8,12 @@ use std::{marker::PhantomData, sync::Arc};
|
|||
|
||||
pub struct View<V> {
|
||||
state: Handle<V>,
|
||||
render: Arc<dyn Fn(&mut V, &mut ViewContext<V>) -> AnyElement<V> + Send + Sync + 'static>,
|
||||
render: Arc<Mutex<dyn Fn(&mut V, &mut ViewContext<V>) -> AnyElement<V> + Send + 'static>>,
|
||||
}
|
||||
|
||||
impl<V: 'static> View<V> {
|
||||
pub fn into_any(self) -> AnyView {
|
||||
AnyView {
|
||||
view: Arc::new(Mutex::new(self)),
|
||||
}
|
||||
AnyView(Arc::new(self))
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -30,14 +28,16 @@ impl<V> Clone for View<V> {
|
|||
|
||||
pub fn view<V, E>(
|
||||
state: Handle<V>,
|
||||
render: impl Fn(&mut V, &mut ViewContext<V>) -> E + Send + Sync + 'static,
|
||||
render: impl Fn(&mut V, &mut ViewContext<'_, '_, V>) -> E + Send + 'static,
|
||||
) -> View<V>
|
||||
where
|
||||
E: Component<V>,
|
||||
{
|
||||
View {
|
||||
state,
|
||||
render: Arc::new(move |state, cx| render(state, cx).render()),
|
||||
render: Arc::new(Mutex::new(
|
||||
move |state: &mut V, cx: &mut ViewContext<'_, '_, V>| render(state, cx).render(),
|
||||
)),
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -64,7 +64,7 @@ impl<V: 'static> Element<()> for View<V> {
|
|||
cx: &mut ViewContext<()>,
|
||||
) -> Self::ElementState {
|
||||
self.state.update(cx, |state, cx| {
|
||||
let mut any_element = (self.render)(state, cx);
|
||||
let mut any_element = (self.render.lock())(state, cx);
|
||||
any_element.initialize(state, cx);
|
||||
any_element
|
||||
})
|
||||
|
@ -96,7 +96,6 @@ struct EraseViewState<V, ParentV> {
|
|||
}
|
||||
|
||||
unsafe impl<V, ParentV> Send for EraseViewState<V, ParentV> {}
|
||||
unsafe impl<V, ParentV> Sync for EraseViewState<V, ParentV> {}
|
||||
|
||||
impl<V: 'static, ParentV: 'static> Component<ParentV> for EraseViewState<V, ParentV> {
|
||||
fn render(self) -> AnyElement<ParentV> {
|
||||
|
@ -142,9 +141,9 @@ impl<V: 'static, ParentV: 'static> Element<ParentV> for EraseViewState<V, Parent
|
|||
|
||||
trait ViewObject: Send + Sync {
|
||||
fn entity_id(&self) -> EntityId;
|
||||
fn initialize(&mut self, cx: &mut WindowContext) -> AnyBox;
|
||||
fn layout(&mut self, element: &mut AnyBox, cx: &mut WindowContext) -> LayoutId;
|
||||
fn paint(&mut self, bounds: Bounds<Pixels>, element: &mut AnyBox, cx: &mut WindowContext);
|
||||
fn initialize(&self, cx: &mut WindowContext) -> AnyBox;
|
||||
fn layout(&self, element: &mut AnyBox, cx: &mut WindowContext) -> LayoutId;
|
||||
fn paint(&self, bounds: Bounds<Pixels>, element: &mut AnyBox, cx: &mut WindowContext);
|
||||
}
|
||||
|
||||
impl<V: 'static> ViewObject for View<V> {
|
||||
|
@ -152,17 +151,17 @@ impl<V: 'static> ViewObject for View<V> {
|
|||
self.state.entity_id
|
||||
}
|
||||
|
||||
fn initialize(&mut self, cx: &mut WindowContext) -> AnyBox {
|
||||
fn initialize(&self, cx: &mut WindowContext) -> AnyBox {
|
||||
cx.with_element_id(self.entity_id(), |_global_id, cx| {
|
||||
self.state.update(cx, |state, cx| {
|
||||
let mut any_element = Box::new((self.render)(state, cx));
|
||||
let mut any_element = Box::new((self.render.lock())(state, cx));
|
||||
any_element.initialize(state, cx);
|
||||
any_element as AnyBox
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
fn layout(&mut self, element: &mut AnyBox, cx: &mut WindowContext) -> LayoutId {
|
||||
fn layout(&self, element: &mut AnyBox, cx: &mut WindowContext) -> LayoutId {
|
||||
cx.with_element_id(self.entity_id(), |_global_id, cx| {
|
||||
self.state.update(cx, |state, cx| {
|
||||
let element = element.downcast_mut::<AnyElement<V>>().unwrap();
|
||||
|
@ -171,7 +170,7 @@ impl<V: 'static> ViewObject for View<V> {
|
|||
})
|
||||
}
|
||||
|
||||
fn paint(&mut self, _: Bounds<Pixels>, element: &mut AnyBox, cx: &mut WindowContext) {
|
||||
fn paint(&self, _: Bounds<Pixels>, element: &mut AnyBox, cx: &mut WindowContext) {
|
||||
cx.with_element_id(self.entity_id(), |_global_id, cx| {
|
||||
self.state.update(cx, |state, cx| {
|
||||
let element = element.downcast_mut::<AnyElement<V>>().unwrap();
|
||||
|
@ -181,9 +180,8 @@ impl<V: 'static> ViewObject for View<V> {
|
|||
}
|
||||
}
|
||||
|
||||
pub struct AnyView {
|
||||
view: Arc<Mutex<dyn ViewObject>>,
|
||||
}
|
||||
#[derive(Clone)]
|
||||
pub struct AnyView(Arc<dyn ViewObject>);
|
||||
|
||||
impl<ParentV: 'static> Component<ParentV> for AnyView {
|
||||
fn render(self) -> AnyElement<ParentV> {
|
||||
|
@ -198,7 +196,7 @@ impl Element<()> for AnyView {
|
|||
type ElementState = AnyBox;
|
||||
|
||||
fn id(&self) -> Option<crate::ElementId> {
|
||||
Some(ElementId::View(self.view.lock().entity_id()))
|
||||
Some(ElementId::View(self.0.entity_id()))
|
||||
}
|
||||
|
||||
fn initialize(
|
||||
|
@ -207,7 +205,7 @@ impl Element<()> for AnyView {
|
|||
_: Option<Self::ElementState>,
|
||||
cx: &mut ViewContext<()>,
|
||||
) -> Self::ElementState {
|
||||
self.view.lock().initialize(cx)
|
||||
self.0.initialize(cx)
|
||||
}
|
||||
|
||||
fn layout(
|
||||
|
@ -216,7 +214,7 @@ impl Element<()> for AnyView {
|
|||
element: &mut Self::ElementState,
|
||||
cx: &mut ViewContext<()>,
|
||||
) -> LayoutId {
|
||||
self.view.lock().layout(element, cx)
|
||||
self.0.layout(element, cx)
|
||||
}
|
||||
|
||||
fn paint(
|
||||
|
@ -226,7 +224,7 @@ impl Element<()> for AnyView {
|
|||
element: &mut AnyBox,
|
||||
cx: &mut ViewContext<()>,
|
||||
) {
|
||||
self.view.lock().paint(bounds, element, cx)
|
||||
self.0.paint(bounds, element, cx)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -236,7 +234,6 @@ struct EraseAnyViewState<ParentViewState> {
|
|||
}
|
||||
|
||||
unsafe impl<ParentV> Send for EraseAnyViewState<ParentV> {}
|
||||
unsafe impl<ParentV> Sync for EraseAnyViewState<ParentV> {}
|
||||
|
||||
impl<ParentV: 'static> Component<ParentV> for EraseAnyViewState<ParentV> {
|
||||
fn render(self) -> AnyElement<ParentV> {
|
||||
|
@ -257,7 +254,7 @@ impl<ParentV: 'static> Element<ParentV> for EraseAnyViewState<ParentV> {
|
|||
_: Option<Self::ElementState>,
|
||||
cx: &mut ViewContext<ParentV>,
|
||||
) -> Self::ElementState {
|
||||
self.view.view.lock().initialize(cx)
|
||||
self.view.0.initialize(cx)
|
||||
}
|
||||
|
||||
fn layout(
|
||||
|
@ -266,7 +263,7 @@ impl<ParentV: 'static> Element<ParentV> for EraseAnyViewState<ParentV> {
|
|||
element: &mut Self::ElementState,
|
||||
cx: &mut ViewContext<ParentV>,
|
||||
) -> LayoutId {
|
||||
self.view.view.lock().layout(element, cx)
|
||||
self.view.0.layout(element, cx)
|
||||
}
|
||||
|
||||
fn paint(
|
||||
|
@ -276,14 +273,6 @@ impl<ParentV: 'static> Element<ParentV> for EraseAnyViewState<ParentV> {
|
|||
element: &mut Self::ElementState,
|
||||
cx: &mut ViewContext<ParentV>,
|
||||
) {
|
||||
self.view.view.lock().paint(bounds, element, cx)
|
||||
}
|
||||
}
|
||||
|
||||
impl Clone for AnyView {
|
||||
fn clone(&self) -> Self {
|
||||
Self {
|
||||
view: self.view.clone(),
|
||||
}
|
||||
self.view.0.paint(bounds, element, cx)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue