2023-09-22 14:33:51 +00:00
|
|
|
use crate::{
|
2023-10-30 22:19:40 +00:00
|
|
|
AnyBox, AnyElement, AnyModel, AppContext, AvailableSpace, BorrowWindow, Bounds, Component,
|
|
|
|
Element, ElementId, EntityId, LayoutId, Model, Pixels, Size, ViewContext, VisualContext,
|
|
|
|
WeakModel, WindowContext,
|
2023-10-26 16:17:45 +00:00
|
|
|
};
|
2023-10-26 17:41:42 +00:00
|
|
|
use anyhow::{Context, Result};
|
2023-10-30 22:19:40 +00:00
|
|
|
use std::{any::TypeId, marker::PhantomData, sync::Arc};
|
2023-09-22 14:33:51 +00:00
|
|
|
|
2023-10-30 19:36:48 +00:00
|
|
|
pub trait Render: 'static + Sized {
|
|
|
|
type Element: Element<Self> + 'static + Send;
|
|
|
|
|
|
|
|
fn render(&mut self, cx: &mut ViewContext<Self>) -> Self::Element;
|
2023-09-22 14:33:51 +00:00
|
|
|
}
|
|
|
|
|
2023-10-30 19:36:48 +00:00
|
|
|
pub struct View<V> {
|
|
|
|
pub(crate) model: Model<V>,
|
2023-09-22 14:33:51 +00:00
|
|
|
}
|
|
|
|
|
2023-10-30 19:36:48 +00:00
|
|
|
impl<V: Render> View<V> {
|
2023-10-26 16:17:45 +00:00
|
|
|
pub fn into_any(self) -> AnyView {
|
|
|
|
AnyView(Arc::new(self))
|
|
|
|
}
|
2023-10-30 19:36:48 +00:00
|
|
|
}
|
2023-10-26 16:17:45 +00:00
|
|
|
|
2023-10-30 19:36:48 +00:00
|
|
|
impl<V: 'static> View<V> {
|
2023-10-26 16:17:45 +00:00
|
|
|
pub fn downgrade(&self) -> WeakView<V> {
|
|
|
|
WeakView {
|
2023-10-30 19:36:48 +00:00
|
|
|
model: self.model.downgrade(),
|
2023-10-26 16:17:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-26 17:41:42 +00:00
|
|
|
pub fn update<C, R>(
|
2023-10-26 16:17:45 +00:00
|
|
|
&self,
|
2023-10-26 17:41:42 +00:00
|
|
|
cx: &mut C,
|
|
|
|
f: impl FnOnce(&mut V, &mut C::ViewContext<'_, '_, V>) -> R,
|
|
|
|
) -> C::Result<R>
|
|
|
|
where
|
|
|
|
C: VisualContext,
|
|
|
|
{
|
|
|
|
cx.update_view(self, f)
|
2023-10-26 16:17:45 +00:00
|
|
|
}
|
2023-10-30 22:19:40 +00:00
|
|
|
|
|
|
|
pub fn read<'a>(&self, cx: &'a AppContext) -> &'a V {
|
|
|
|
self.model.read(cx)
|
|
|
|
}
|
2023-10-26 16:17:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<V> Clone for View<V> {
|
|
|
|
fn clone(&self) -> Self {
|
|
|
|
Self {
|
2023-10-30 19:36:48 +00:00
|
|
|
model: self.model.clone(),
|
2023-10-26 16:17:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-30 19:36:48 +00:00
|
|
|
impl<V: Render, ParentViewState: 'static> Component<ParentViewState> for View<V> {
|
2023-10-26 10:46:52 +00:00
|
|
|
fn render(self) -> AnyElement<ParentViewState> {
|
2023-10-12 17:30:00 +00:00
|
|
|
AnyElement::new(EraseViewState {
|
|
|
|
view: self,
|
|
|
|
parent_view_state_type: PhantomData,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-30 19:36:48 +00:00
|
|
|
impl<V> Element<()> for View<V>
|
|
|
|
where
|
|
|
|
V: Render,
|
|
|
|
{
|
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-30 19:36:48 +00:00
|
|
|
Some(ElementId::View(self.model.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-26 16:17:45 +00:00
|
|
|
self.update(cx, |state, cx| {
|
2023-10-30 19:36:48 +00:00
|
|
|
let mut any_element = AnyElement::new(state.render(cx));
|
2023-10-18 14:43:20 +00:00
|
|
|
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 {
|
2023-10-26 16:17:45 +00:00
|
|
|
self.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-26 16:17:45 +00:00
|
|
|
self.update(cx, |state, cx| element.paint(state, cx))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct WeakView<V> {
|
2023-10-30 19:36:48 +00:00
|
|
|
pub(crate) model: WeakModel<V>,
|
2023-10-26 16:17:45 +00:00
|
|
|
}
|
|
|
|
|
2023-10-26 17:41:42 +00:00
|
|
|
impl<V: 'static> WeakView<V> {
|
2023-10-26 16:17:45 +00:00
|
|
|
pub fn upgrade(&self) -> Option<View<V>> {
|
2023-10-30 19:36:48 +00:00
|
|
|
let model = self.model.upgrade()?;
|
|
|
|
Some(View { model })
|
2023-09-22 14:33:51 +00:00
|
|
|
}
|
2023-10-26 17:41:42 +00:00
|
|
|
|
|
|
|
pub fn update<R>(
|
|
|
|
&self,
|
|
|
|
cx: &mut WindowContext,
|
|
|
|
f: impl FnOnce(&mut V, &mut ViewContext<V>) -> R,
|
|
|
|
) -> Result<R> {
|
|
|
|
let view = self.upgrade().context("error upgrading view")?;
|
|
|
|
Ok(view.update(cx, f))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<V> Clone for WeakView<V> {
|
|
|
|
fn clone(&self) -> Self {
|
|
|
|
Self {
|
2023-10-30 19:36:48 +00:00
|
|
|
model: self.model.clone(),
|
2023-10-26 17:41:42 +00:00
|
|
|
}
|
|
|
|
}
|
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> {}
|
|
|
|
|
2023-10-30 19:36:48 +00:00
|
|
|
impl<V: Render, ParentV: 'static> Component<ParentV> for EraseViewState<V, ParentV> {
|
2023-10-26 10:46:52 +00:00
|
|
|
fn render(self) -> AnyElement<ParentV> {
|
2023-10-12 17:30:00 +00:00
|
|
|
AnyElement::new(self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-30 19:36:48 +00:00
|
|
|
impl<V: Render, 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-30 22:19:40 +00:00
|
|
|
fn entity_type(&self) -> TypeId;
|
2023-10-12 17:30:00 +00:00
|
|
|
fn entity_id(&self) -> EntityId;
|
2023-10-30 22:19:40 +00:00
|
|
|
fn model(&self) -> AnyModel;
|
2023-10-26 14:40:44 +00:00
|
|
|
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);
|
2023-09-22 14:33:51 +00:00
|
|
|
}
|
|
|
|
|
2023-10-30 19:36:48 +00:00
|
|
|
impl<V> ViewObject for View<V>
|
|
|
|
where
|
|
|
|
V: Render,
|
|
|
|
{
|
2023-10-30 22:19:40 +00:00
|
|
|
fn entity_type(&self) -> TypeId {
|
|
|
|
TypeId::of::<V>()
|
|
|
|
}
|
|
|
|
|
2023-10-12 17:30:00 +00:00
|
|
|
fn entity_id(&self) -> EntityId {
|
2023-10-30 19:36:48 +00:00
|
|
|
self.model.entity_id
|
2023-10-12 17:30:00 +00:00
|
|
|
}
|
2023-10-11 19:22:40 +00:00
|
|
|
|
2023-10-30 22:19:40 +00:00
|
|
|
fn model(&self) -> AnyModel {
|
|
|
|
self.model.clone().into_any()
|
|
|
|
}
|
|
|
|
|
2023-10-26 14:40:44 +00:00
|
|
|
fn initialize(&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-26 16:17:45 +00:00
|
|
|
self.update(cx, |state, cx| {
|
2023-10-30 19:36:48 +00:00
|
|
|
let mut any_element = Box::new(AnyElement::new(state.render(cx)));
|
2023-10-18 14:43:20 +00:00
|
|
|
any_element.initialize(state, cx);
|
2023-10-30 19:36:48 +00:00
|
|
|
any_element
|
2023-10-18 14:43:20 +00:00
|
|
|
})
|
2023-10-18 12:12:50 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-10-26 14:40:44 +00:00
|
|
|
fn layout(&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-26 16:17:45 +00:00
|
|
|
self.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-26 14:40:44 +00:00
|
|
|
fn paint(&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-26 16:17:45 +00:00
|
|
|
self.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-26 14:40:44 +00:00
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct AnyView(Arc<dyn ViewObject>);
|
2023-09-22 14:33:51 +00:00
|
|
|
|
2023-10-26 17:41:42 +00:00
|
|
|
impl AnyView {
|
2023-10-30 22:19:40 +00:00
|
|
|
pub fn downcast<V: 'static + Send>(self) -> Option<View<V>> {
|
|
|
|
self.0.model().downcast().map(|model| View { model })
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn entity_type(&self) -> TypeId {
|
|
|
|
self.0.entity_type()
|
|
|
|
}
|
|
|
|
|
2023-10-26 17:41:42 +00:00
|
|
|
pub(crate) fn draw(&self, available_space: Size<AvailableSpace>, cx: &mut WindowContext) {
|
|
|
|
let mut rendered_element = self.0.initialize(cx);
|
|
|
|
let layout_id = self.0.layout(&mut rendered_element, cx);
|
|
|
|
cx.window
|
|
|
|
.layout_engine
|
|
|
|
.compute_layout(layout_id, available_space);
|
|
|
|
let bounds = cx.window.layout_engine.layout_bounds(layout_id);
|
|
|
|
self.0.paint(bounds, &mut rendered_element, cx);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-26 10:46:52 +00:00
|
|
|
impl<ParentV: 'static> Component<ParentV> for AnyView {
|
|
|
|
fn render(self) -> AnyElement<ParentV> {
|
2023-10-12 17:30:00 +00:00
|
|
|
AnyElement::new(EraseAnyViewState {
|
|
|
|
view: self,
|
|
|
|
parent_view_state_type: PhantomData,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-26 07:51:33 +00:00
|
|
|
impl Element<()> for AnyView {
|
2023-10-11 04:14:47 +00:00
|
|
|
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-26 14:40:44 +00:00
|
|
|
Some(ElementId::View(self.0.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-26 07:51:33 +00:00
|
|
|
_: &mut (),
|
2023-10-11 04:14:47 +00:00
|
|
|
_: Option<Self::ElementState>,
|
2023-10-26 07:51:33 +00:00
|
|
|
cx: &mut ViewContext<()>,
|
2023-10-18 12:12:50 +00:00
|
|
|
) -> Self::ElementState {
|
2023-10-26 14:40:44 +00:00
|
|
|
self.0.initialize(cx)
|
2023-10-18 12:12:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn layout(
|
|
|
|
&mut self,
|
2023-10-26 07:51:33 +00:00
|
|
|
_: &mut (),
|
2023-10-18 12:12:50 +00:00
|
|
|
element: &mut Self::ElementState,
|
2023-10-26 07:51:33 +00:00
|
|
|
cx: &mut ViewContext<()>,
|
2023-10-18 12:12:50 +00:00
|
|
|
) -> LayoutId {
|
2023-10-26 14:40:44 +00:00
|
|
|
self.0.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,
|
2023-10-26 07:51:33 +00:00
|
|
|
cx: &mut ViewContext<()>,
|
2023-10-11 04:14:47 +00:00
|
|
|
) {
|
2023-10-26 14:40:44 +00:00
|
|
|
self.0.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> {}
|
|
|
|
|
2023-10-26 10:46:52 +00:00
|
|
|
impl<ParentV: 'static> Component<ParentV> for EraseAnyViewState<ParentV> {
|
|
|
|
fn render(self) -> AnyElement<ParentV> {
|
2023-10-12 17:30:00 +00:00
|
|
|
AnyElement::new(self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-30 22:19:40 +00:00
|
|
|
impl<T, E> Render for T
|
|
|
|
where
|
|
|
|
T: 'static + FnMut(&mut WindowContext) -> E,
|
|
|
|
E: 'static + Send + Element<T>,
|
|
|
|
{
|
|
|
|
type Element = E;
|
|
|
|
|
|
|
|
fn render(&mut self, cx: &mut ViewContext<Self>) -> Self::Element {
|
|
|
|
(self)(cx)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 {
|
2023-10-26 14:40:44 +00:00
|
|
|
self.view.0.initialize(cx)
|
2023-10-18 12:12:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2023-10-26 14:40:44 +00:00
|
|
|
self.view.0.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
|
|
|
) {
|
2023-10-26 14:40:44 +00:00
|
|
|
self.view.0.paint(bounds, element, cx)
|
2023-09-22 14:33:51 +00:00
|
|
|
}
|
|
|
|
}
|