2023-09-26 17:29:44 +00:00
|
|
|
use parking_lot::Mutex;
|
|
|
|
|
2023-09-22 14:33:51 +00:00
|
|
|
use crate::{
|
2023-10-17 19:37:09 +00:00
|
|
|
AnyBox, AnyElement, BorrowWindow, Bounds, Element, ElementId, EntityId, Handle, IntoAnyElement,
|
|
|
|
LayoutId, Pixels, ViewContext, WindowContext,
|
2023-09-22 14:33:51 +00:00
|
|
|
};
|
2023-10-13 16:01:15 +00:00
|
|
|
use std::{marker::PhantomData, sync::Arc};
|
2023-09-22 14:33:51 +00:00
|
|
|
|
2023-10-24 12:20:01 +00:00
|
|
|
pub struct View<V> {
|
2023-10-18 12:12:50 +00:00
|
|
|
state: Handle<V>,
|
|
|
|
render: Arc<dyn Fn(&mut V, &mut ViewContext<V>) -> AnyElement<V> + Send + Sync + 'static>,
|
2023-09-22 14:33:51 +00:00
|
|
|
}
|
|
|
|
|
2023-10-24 12:20:01 +00:00
|
|
|
impl<V: 'static> View<V> {
|
2023-10-12 17:30:00 +00:00
|
|
|
pub fn into_any(self) -> AnyView {
|
2023-09-26 17:29:44 +00:00
|
|
|
AnyView {
|
|
|
|
view: Arc::new(Mutex::new(self)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-24 12:20:01 +00:00
|
|
|
impl<V> Clone for View<V> {
|
2023-09-22 14:33:51 +00:00
|
|
|
fn clone(&self) -> Self {
|
|
|
|
Self {
|
|
|
|
state: self.state.clone(),
|
|
|
|
render: self.render.clone(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-18 12:12:50 +00:00
|
|
|
pub fn view<V, E>(
|
|
|
|
state: Handle<V>,
|
|
|
|
render: impl Fn(&mut V, &mut ViewContext<V>) -> E + Send + Sync + 'static,
|
|
|
|
) -> View<V>
|
2023-09-28 07:16:42 +00:00
|
|
|
where
|
2023-10-18 12:12:50 +00:00
|
|
|
E: IntoAnyElement<V>,
|
2023-09-28 07:16:42 +00:00
|
|
|
{
|
2023-09-22 14:33:51 +00:00
|
|
|
View {
|
|
|
|
state,
|
2023-09-22 18:44:37 +00:00
|
|
|
render: Arc::new(move |state, cx| render(state, cx).into_any()),
|
2023-09-22 14:33:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-24 12:20:01 +00:00
|
|
|
impl<V: 'static, ParentViewState: 'static> IntoAnyElement<ParentViewState> for View<V> {
|
2023-10-12 17:30:00 +00:00
|
|
|
fn into_any(self) -> AnyElement<ParentViewState> {
|
|
|
|
AnyElement::new(EraseViewState {
|
|
|
|
view: self,
|
|
|
|
parent_view_state_type: PhantomData,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-26 07:45:26 +00:00
|
|
|
impl<V: 'static> Element<()> for View<V> {
|
2023-10-18 12:12:50 +00:00
|
|
|
type ElementState = AnyElement<V>;
|
2023-09-22 14:33:51 +00:00
|
|
|
|
2023-10-17 09:08:48 +00:00
|
|
|
fn id(&self) -> Option<crate::ElementId> {
|
2023-10-23 12:59:57 +00:00
|
|
|
Some(ElementId::View(self.state.entity_id))
|
2023-10-11 17:40:42 +00:00
|
|
|
}
|
|
|
|
|
2023-10-18 12:12:50 +00:00
|
|
|
fn initialize(
|
2023-09-22 14:33:51 +00:00
|
|
|
&mut self,
|
2023-10-18 12:12:50 +00:00
|
|
|
_: &mut (),
|
2023-10-11 04:14:47 +00:00
|
|
|
_: Option<Self::ElementState>,
|
2023-10-18 12:12:50 +00:00
|
|
|
cx: &mut ViewContext<()>,
|
|
|
|
) -> Self::ElementState {
|
2023-10-18 14:43:20 +00:00
|
|
|
self.state.update(cx, |state, cx| {
|
|
|
|
let mut any_element = (self.render)(state, cx);
|
|
|
|
any_element.initialize(state, cx);
|
|
|
|
any_element
|
|
|
|
})
|
2023-10-18 12:12:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn layout(
|
|
|
|
&mut self,
|
|
|
|
_: &mut (),
|
|
|
|
element: &mut Self::ElementState,
|
|
|
|
cx: &mut ViewContext<()>,
|
|
|
|
) -> LayoutId {
|
|
|
|
self.state.update(cx, |state, cx| element.layout(state, cx))
|
2023-09-22 14:33:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn paint(
|
|
|
|
&mut self,
|
2023-10-06 03:02:26 +00:00
|
|
|
_: Bounds<Pixels>,
|
2023-10-18 12:12:50 +00:00
|
|
|
_: &mut (),
|
2023-10-11 04:14:47 +00:00
|
|
|
element: &mut Self::ElementState,
|
2023-10-18 12:12:50 +00:00
|
|
|
cx: &mut ViewContext<()>,
|
2023-10-11 04:14:47 +00:00
|
|
|
) {
|
2023-10-20 15:31:42 +00:00
|
|
|
self.state.update(cx, |state, cx| element.paint(state, cx))
|
2023-09-22 14:33:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-24 12:20:01 +00:00
|
|
|
struct EraseViewState<V, ParentV> {
|
2023-10-18 12:12:50 +00:00
|
|
|
view: View<V>,
|
|
|
|
parent_view_state_type: PhantomData<ParentV>,
|
2023-10-12 17:30:00 +00:00
|
|
|
}
|
|
|
|
|
2023-10-24 12:20:01 +00:00
|
|
|
unsafe impl<V, ParentV> Send for EraseViewState<V, ParentV> {}
|
|
|
|
unsafe impl<V, ParentV> Sync for EraseViewState<V, ParentV> {}
|
|
|
|
|
|
|
|
impl<V: 'static, ParentV: 'static> IntoAnyElement<ParentV> for EraseViewState<V, ParentV> {
|
2023-10-18 12:12:50 +00:00
|
|
|
fn into_any(self) -> AnyElement<ParentV> {
|
2023-10-12 17:30:00 +00:00
|
|
|
AnyElement::new(self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-26 07:45:26 +00:00
|
|
|
impl<V: 'static, ParentV: 'static> Element<ParentV> for EraseViewState<V, ParentV> {
|
2023-10-12 17:30:00 +00:00
|
|
|
type ElementState = AnyBox;
|
|
|
|
|
2023-10-17 09:08:48 +00:00
|
|
|
fn id(&self) -> Option<crate::ElementId> {
|
|
|
|
Element::id(&self.view)
|
2023-10-12 17:30:00 +00:00
|
|
|
}
|
|
|
|
|
2023-10-18 12:12:50 +00:00
|
|
|
fn initialize(
|
2023-10-12 17:30:00 +00:00
|
|
|
&mut self,
|
2023-10-26 07:45:26 +00:00
|
|
|
_: &mut ParentV,
|
2023-10-12 17:30:00 +00:00
|
|
|
_: Option<Self::ElementState>,
|
2023-10-26 07:45:26 +00:00
|
|
|
cx: &mut ViewContext<ParentV>,
|
2023-10-18 12:12:50 +00:00
|
|
|
) -> Self::ElementState {
|
|
|
|
ViewObject::initialize(&mut self.view, cx)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn layout(
|
|
|
|
&mut self,
|
2023-10-26 07:45:26 +00:00
|
|
|
_: &mut ParentV,
|
2023-10-18 12:12:50 +00:00
|
|
|
element: &mut Self::ElementState,
|
2023-10-26 07:45:26 +00:00
|
|
|
cx: &mut ViewContext<ParentV>,
|
2023-10-18 12:12:50 +00:00
|
|
|
) -> LayoutId {
|
|
|
|
ViewObject::layout(&mut self.view, element, cx)
|
2023-10-12 17:30:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn paint(
|
|
|
|
&mut self,
|
|
|
|
bounds: Bounds<Pixels>,
|
2023-10-26 07:45:26 +00:00
|
|
|
_: &mut ParentV,
|
2023-10-12 17:30:00 +00:00
|
|
|
element: &mut Self::ElementState,
|
2023-10-26 07:45:26 +00:00
|
|
|
cx: &mut ViewContext<ParentV>,
|
2023-10-12 17:30:00 +00:00
|
|
|
) {
|
|
|
|
ViewObject::paint(&mut self.view, bounds, element, cx)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-24 12:20:01 +00:00
|
|
|
trait ViewObject: Send + Sync {
|
2023-10-12 17:30:00 +00:00
|
|
|
fn entity_id(&self) -> EntityId;
|
2023-10-18 12:12:50 +00:00
|
|
|
fn initialize(&mut self, cx: &mut WindowContext) -> AnyBox;
|
|
|
|
fn layout(&mut self, element: &mut AnyBox, cx: &mut WindowContext) -> LayoutId;
|
2023-10-12 19:27:46 +00:00
|
|
|
fn paint(&mut self, bounds: Bounds<Pixels>, element: &mut AnyBox, cx: &mut WindowContext);
|
2023-09-22 14:33:51 +00:00
|
|
|
}
|
|
|
|
|
2023-10-24 12:20:01 +00:00
|
|
|
impl<V: 'static> ViewObject for View<V> {
|
2023-10-12 17:30:00 +00:00
|
|
|
fn entity_id(&self) -> EntityId {
|
2023-10-23 12:59:57 +00:00
|
|
|
self.state.entity_id
|
2023-10-12 17:30:00 +00:00
|
|
|
}
|
2023-10-11 19:22:40 +00:00
|
|
|
|
2023-10-18 12:12:50 +00:00
|
|
|
fn initialize(&mut self, cx: &mut WindowContext) -> AnyBox {
|
2023-10-19 17:03:10 +00:00
|
|
|
cx.with_element_id(self.entity_id(), |_global_id, cx| {
|
2023-10-18 14:43:20 +00:00
|
|
|
self.state.update(cx, |state, cx| {
|
|
|
|
let mut any_element = Box::new((self.render)(state, cx));
|
|
|
|
any_element.initialize(state, cx);
|
|
|
|
any_element as AnyBox
|
|
|
|
})
|
2023-10-18 12:12:50 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
fn layout(&mut self, element: &mut AnyBox, cx: &mut WindowContext) -> LayoutId {
|
2023-10-19 17:03:10 +00:00
|
|
|
cx.with_element_id(self.entity_id(), |_global_id, cx| {
|
2023-10-11 19:22:40 +00:00
|
|
|
self.state.update(cx, |state, cx| {
|
2023-10-18 12:12:50 +00:00
|
|
|
let element = element.downcast_mut::<AnyElement<V>>().unwrap();
|
|
|
|
element.layout(state, cx)
|
2023-10-11 19:22:40 +00:00
|
|
|
})
|
2023-09-22 14:33:51 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-10-12 19:27:46 +00:00
|
|
|
fn paint(&mut self, _: Bounds<Pixels>, element: &mut AnyBox, cx: &mut WindowContext) {
|
2023-10-19 17:03:10 +00:00
|
|
|
cx.with_element_id(self.entity_id(), |_global_id, cx| {
|
2023-10-11 19:22:40 +00:00
|
|
|
self.state.update(cx, |state, cx| {
|
2023-10-18 12:12:50 +00:00
|
|
|
let element = element.downcast_mut::<AnyElement<V>>().unwrap();
|
2023-10-20 15:31:42 +00:00
|
|
|
element.paint(state, cx);
|
2023-10-11 19:22:40 +00:00
|
|
|
});
|
2023-10-11 04:14:47 +00:00
|
|
|
});
|
2023-09-22 14:33:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-12 17:30:00 +00:00
|
|
|
pub struct AnyView {
|
2023-09-26 17:29:44 +00:00
|
|
|
view: Arc<Mutex<dyn ViewObject>>,
|
2023-09-22 14:33:51 +00:00
|
|
|
}
|
|
|
|
|
2023-10-24 12:20:01 +00:00
|
|
|
impl<ParentV: 'static> IntoAnyElement<ParentV> for AnyView {
|
2023-10-18 12:12:50 +00:00
|
|
|
fn into_any(self) -> AnyElement<ParentV> {
|
2023-10-12 17:30:00 +00:00
|
|
|
AnyElement::new(EraseAnyViewState {
|
|
|
|
view: self,
|
|
|
|
parent_view_state_type: PhantomData,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Element for AnyView {
|
2023-10-11 04:14:47 +00:00
|
|
|
type ViewState = ();
|
|
|
|
type ElementState = AnyBox;
|
2023-09-22 14:33:51 +00:00
|
|
|
|
2023-10-17 09:08:48 +00:00
|
|
|
fn id(&self) -> Option<crate::ElementId> {
|
2023-10-12 17:30:00 +00:00
|
|
|
Some(ElementId::View(self.view.lock().entity_id()))
|
2023-10-11 17:40:42 +00:00
|
|
|
}
|
|
|
|
|
2023-10-18 12:12:50 +00:00
|
|
|
fn initialize(
|
2023-09-22 14:33:51 +00:00
|
|
|
&mut self,
|
2023-10-11 04:14:47 +00:00
|
|
|
_: &mut Self::ViewState,
|
|
|
|
_: Option<Self::ElementState>,
|
|
|
|
cx: &mut ViewContext<Self::ViewState>,
|
2023-10-18 12:12:50 +00:00
|
|
|
) -> Self::ElementState {
|
|
|
|
self.view.lock().initialize(cx)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn layout(
|
|
|
|
&mut self,
|
|
|
|
_: &mut Self::ViewState,
|
|
|
|
element: &mut Self::ElementState,
|
|
|
|
cx: &mut ViewContext<Self::ViewState>,
|
|
|
|
) -> LayoutId {
|
|
|
|
self.view.lock().layout(element, cx)
|
2023-09-22 14:33:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn paint(
|
|
|
|
&mut self,
|
2023-10-06 03:02:26 +00:00
|
|
|
bounds: Bounds<Pixels>,
|
2023-09-30 16:01:59 +00:00
|
|
|
_: &mut (),
|
2023-10-11 04:14:47 +00:00
|
|
|
element: &mut AnyBox,
|
|
|
|
cx: &mut ViewContext<Self::ViewState>,
|
|
|
|
) {
|
2023-10-12 19:27:46 +00:00
|
|
|
self.view.lock().paint(bounds, element, cx)
|
2023-09-22 14:33:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-12 17:30:00 +00:00
|
|
|
struct EraseAnyViewState<ParentViewState> {
|
|
|
|
view: AnyView,
|
|
|
|
parent_view_state_type: PhantomData<ParentViewState>,
|
|
|
|
}
|
|
|
|
|
2023-10-24 12:20:01 +00:00
|
|
|
unsafe impl<ParentV> Send for EraseAnyViewState<ParentV> {}
|
|
|
|
unsafe impl<ParentV> Sync for EraseAnyViewState<ParentV> {}
|
|
|
|
|
|
|
|
impl<ParentV: 'static> IntoAnyElement<ParentV> for EraseAnyViewState<ParentV> {
|
2023-10-18 12:12:50 +00:00
|
|
|
fn into_any(self) -> AnyElement<ParentV> {
|
2023-10-12 17:30:00 +00:00
|
|
|
AnyElement::new(self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-26 07:45:26 +00:00
|
|
|
impl<ParentV: 'static> Element<ParentV> for EraseAnyViewState<ParentV> {
|
2023-10-12 17:30:00 +00:00
|
|
|
type ElementState = AnyBox;
|
|
|
|
|
2023-10-17 09:08:48 +00:00
|
|
|
fn id(&self) -> Option<crate::ElementId> {
|
|
|
|
Element::id(&self.view)
|
2023-10-12 17:30:00 +00:00
|
|
|
}
|
|
|
|
|
2023-10-18 12:12:50 +00:00
|
|
|
fn initialize(
|
2023-10-12 17:30:00 +00:00
|
|
|
&mut self,
|
2023-10-26 07:45:26 +00:00
|
|
|
_: &mut ParentV,
|
2023-10-12 17:30:00 +00:00
|
|
|
_: Option<Self::ElementState>,
|
2023-10-26 07:45:26 +00:00
|
|
|
cx: &mut ViewContext<ParentV>,
|
2023-10-18 12:12:50 +00:00
|
|
|
) -> Self::ElementState {
|
|
|
|
self.view.view.lock().initialize(cx)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn layout(
|
|
|
|
&mut self,
|
2023-10-26 07:45:26 +00:00
|
|
|
_: &mut ParentV,
|
2023-10-18 12:12:50 +00:00
|
|
|
element: &mut Self::ElementState,
|
2023-10-26 07:45:26 +00:00
|
|
|
cx: &mut ViewContext<ParentV>,
|
2023-10-18 12:12:50 +00:00
|
|
|
) -> LayoutId {
|
|
|
|
self.view.view.lock().layout(element, cx)
|
2023-10-12 17:30:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn paint(
|
|
|
|
&mut self,
|
|
|
|
bounds: Bounds<Pixels>,
|
2023-10-26 07:45:26 +00:00
|
|
|
_: &mut ParentV,
|
2023-10-12 17:30:00 +00:00
|
|
|
element: &mut Self::ElementState,
|
2023-10-26 07:45:26 +00:00
|
|
|
cx: &mut ViewContext<ParentV>,
|
2023-10-12 17:30:00 +00:00
|
|
|
) {
|
|
|
|
self.view.view.lock().paint(bounds, element, cx)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Clone for AnyView {
|
2023-09-22 14:33:51 +00:00
|
|
|
fn clone(&self) -> Self {
|
|
|
|
Self {
|
|
|
|
view: self.view.clone(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|