2023-09-22 14:33:51 +00:00
|
|
|
use crate::{
|
2023-10-31 15:19:46 +00:00
|
|
|
private::Sealed, AnyBox, AnyElement, AnyModel, AnyWeakModel, AppContext, AvailableSpace,
|
2023-11-03 10:36:18 +00:00
|
|
|
Bounds, Component, Element, ElementId, Entity, EntityId, Flatten, 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-11-01 18:31:23 +00:00
|
|
|
use std::{
|
|
|
|
any::{Any, TypeId},
|
2023-11-02 08:41:49 +00:00
|
|
|
hash::{Hash, Hasher},
|
2023-11-01 18:31:23 +00:00
|
|
|
};
|
2023-09-22 14:33:51 +00:00
|
|
|
|
2023-10-30 19:36:48 +00:00
|
|
|
pub trait Render: 'static + Sized {
|
2023-11-01 18:31:23 +00:00
|
|
|
type Element: Element<Self> + 'static;
|
2023-10-30 19:36:48 +00:00
|
|
|
|
|
|
|
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-31 00:43:07 +00:00
|
|
|
impl<V> Sealed for View<V> {}
|
|
|
|
|
|
|
|
impl<V: 'static> Entity<V> for View<V> {
|
|
|
|
type Weak = WeakView<V>;
|
|
|
|
|
|
|
|
fn entity_id(&self) -> EntityId {
|
|
|
|
self.model.entity_id
|
|
|
|
}
|
|
|
|
|
|
|
|
fn downgrade(&self) -> Self::Weak {
|
2023-10-26 16:17:45 +00:00
|
|
|
WeakView {
|
2023-10-30 19:36:48 +00:00
|
|
|
model: self.model.downgrade(),
|
2023-10-26 16:17:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-31 00:43:07 +00:00
|
|
|
fn upgrade_from(weak: &Self::Weak) -> Option<Self>
|
|
|
|
where
|
|
|
|
Self: Sized,
|
|
|
|
{
|
|
|
|
let model = weak.model.upgrade()?;
|
|
|
|
Some(View { model })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<V: 'static> View<V> {
|
|
|
|
/// Convert this strong view reference into a weak view reference.
|
|
|
|
pub fn downgrade(&self) -> WeakView<V> {
|
|
|
|
Entity::downgrade(self)
|
|
|
|
}
|
|
|
|
|
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,
|
2023-11-02 08:41:49 +00:00
|
|
|
f: impl FnOnce(&mut V, &mut ViewContext<'_, V>) -> R,
|
2023-10-26 17:41:42 +00:00
|
|
|
) -> 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-11-02 08:41:49 +00:00
|
|
|
impl<V> Hash for View<V> {
|
|
|
|
fn hash<H: Hasher>(&self, state: &mut H) {
|
|
|
|
self.model.hash(state);
|
2023-10-12 17:30:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-02 08:41:49 +00:00
|
|
|
impl<V> PartialEq for View<V> {
|
|
|
|
fn eq(&self, other: &Self) -> bool {
|
|
|
|
self.model == other.model
|
2023-10-11 17:40:42 +00:00
|
|
|
}
|
2023-11-02 08:41:49 +00:00
|
|
|
}
|
2023-10-11 17:40:42 +00:00
|
|
|
|
2023-11-02 08:41:49 +00:00
|
|
|
impl<V> Eq for View<V> {}
|
2023-10-18 12:12:50 +00:00
|
|
|
|
2023-11-02 08:41:49 +00:00
|
|
|
impl<V: Render, ParentViewState: 'static> Component<ParentViewState> for View<V> {
|
|
|
|
fn render(self) -> AnyElement<ParentViewState> {
|
|
|
|
AnyElement::new(AnyView::from(self))
|
2023-10-26 16:17:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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-11-02 09:53:28 +00:00
|
|
|
pub fn entity_id(&self) -> EntityId {
|
|
|
|
self.model.entity_id
|
|
|
|
}
|
|
|
|
|
2023-10-26 16:17:45 +00:00
|
|
|
pub fn upgrade(&self) -> Option<View<V>> {
|
2023-10-31 00:43:07 +00:00
|
|
|
Entity::upgrade_from(self)
|
2023-09-22 14:33:51 +00:00
|
|
|
}
|
2023-10-26 17:41:42 +00:00
|
|
|
|
2023-11-02 08:41:49 +00:00
|
|
|
pub fn update<C, R>(
|
2023-10-26 17:41:42 +00:00
|
|
|
&self,
|
2023-11-02 08:41:49 +00:00
|
|
|
cx: &mut C,
|
|
|
|
f: impl FnOnce(&mut V, &mut ViewContext<'_, V>) -> R,
|
|
|
|
) -> Result<R>
|
|
|
|
where
|
|
|
|
C: VisualContext,
|
|
|
|
Result<C::Result<R>>: Flatten<R>,
|
|
|
|
{
|
2023-10-26 17:41:42 +00:00
|
|
|
let view = self.upgrade().context("error upgrading view")?;
|
2023-11-02 08:41:49 +00:00
|
|
|
Ok(view.update(cx, f)).flatten()
|
2023-10-26 17:41:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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-11-02 08:41:49 +00:00
|
|
|
impl<V> Hash for WeakView<V> {
|
|
|
|
fn hash<H: Hasher>(&self, state: &mut H) {
|
|
|
|
self.model.hash(state);
|
2023-10-12 17:30:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-02 08:41:49 +00:00
|
|
|
impl<V> PartialEq for WeakView<V> {
|
|
|
|
fn eq(&self, other: &Self) -> bool {
|
|
|
|
self.model == other.model
|
2023-10-18 12:12:50 +00:00
|
|
|
}
|
2023-09-22 14:33:51 +00:00
|
|
|
}
|
|
|
|
|
2023-11-02 08:41:49 +00:00
|
|
|
impl<V> Eq for WeakView<V> {}
|
2023-09-22 14:33:51 +00:00
|
|
|
|
2023-10-31 15:16:30 +00:00
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
pub struct AnyView {
|
|
|
|
model: AnyModel,
|
2023-11-09 23:36:36 +00:00
|
|
|
initialize: fn(&AnyView, &mut WindowContext) -> AnyBox,
|
2023-10-31 15:19:46 +00:00
|
|
|
layout: fn(&AnyView, &mut AnyBox, &mut WindowContext) -> LayoutId,
|
|
|
|
paint: fn(&AnyView, &mut AnyBox, &mut WindowContext),
|
2023-10-31 15:16:30 +00:00
|
|
|
}
|
2023-09-22 14:33:51 +00:00
|
|
|
|
2023-10-26 17:41:42 +00:00
|
|
|
impl AnyView {
|
2023-10-31 15:19:46 +00:00
|
|
|
pub fn downgrade(&self) -> AnyWeakView {
|
|
|
|
AnyWeakView {
|
|
|
|
model: self.model.downgrade(),
|
|
|
|
initialize: self.initialize,
|
|
|
|
layout: self.layout,
|
|
|
|
paint: self.paint,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-31 15:16:30 +00:00
|
|
|
pub fn downcast<T: 'static>(self) -> Result<View<T>, Self> {
|
|
|
|
match self.model.downcast() {
|
|
|
|
Ok(model) => Ok(View { model }),
|
|
|
|
Err(model) => Err(Self {
|
|
|
|
model,
|
|
|
|
initialize: self.initialize,
|
|
|
|
layout: self.layout,
|
|
|
|
paint: self.paint,
|
|
|
|
}),
|
|
|
|
}
|
2023-10-30 22:19:40 +00:00
|
|
|
}
|
|
|
|
|
2023-11-02 08:41:49 +00:00
|
|
|
pub fn entity_type(&self) -> TypeId {
|
2023-10-31 15:16:30 +00:00
|
|
|
self.model.entity_type
|
2023-10-30 22:19:40 +00:00
|
|
|
}
|
|
|
|
|
2023-10-26 17:41:42 +00:00
|
|
|
pub(crate) fn draw(&self, available_space: Size<AvailableSpace>, cx: &mut WindowContext) {
|
2023-10-31 15:16:30 +00:00
|
|
|
let mut rendered_element = (self.initialize)(self, cx);
|
|
|
|
let layout_id = (self.layout)(self, &mut rendered_element, cx);
|
2023-10-26 17:41:42 +00:00
|
|
|
cx.window
|
|
|
|
.layout_engine
|
|
|
|
.compute_layout(layout_id, available_space);
|
2023-10-31 15:16:30 +00:00
|
|
|
(self.paint)(self, &mut rendered_element, cx);
|
2023-10-26 17:41:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-31 15:16:30 +00:00
|
|
|
impl<V: 'static> Component<V> for AnyView {
|
|
|
|
fn render(self) -> AnyElement<V> {
|
|
|
|
AnyElement::new(self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<V: Render> From<View<V>> for AnyView {
|
|
|
|
fn from(value: View<V>) -> Self {
|
|
|
|
AnyView {
|
|
|
|
model: value.model.into_any(),
|
2023-11-03 10:36:18 +00:00
|
|
|
initialize: any_view::initialize::<V>,
|
|
|
|
layout: any_view::layout::<V>,
|
|
|
|
paint: any_view::paint::<V>,
|
2023-10-31 15:16:30 +00:00
|
|
|
}
|
2023-10-12 17:30:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-31 15:16:30 +00:00
|
|
|
impl<ParentViewState: 'static> Element<ParentViewState> for AnyView {
|
2023-11-01 18:31:23 +00:00
|
|
|
type ElementState = Box<dyn Any>;
|
2023-09-22 14:33:51 +00:00
|
|
|
|
2023-10-31 15:16:30 +00:00
|
|
|
fn id(&self) -> Option<ElementId> {
|
|
|
|
Some(self.model.entity_id.into())
|
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-31 15:16:30 +00:00
|
|
|
_view_state: &mut ParentViewState,
|
|
|
|
_element_state: Option<Self::ElementState>,
|
|
|
|
cx: &mut ViewContext<ParentViewState>,
|
2023-10-18 12:12:50 +00:00
|
|
|
) -> Self::ElementState {
|
2023-10-31 15:16:30 +00:00
|
|
|
(self.initialize)(self, cx)
|
2023-10-18 12:12:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn layout(
|
|
|
|
&mut self,
|
2023-10-31 15:16:30 +00:00
|
|
|
_view_state: &mut ParentViewState,
|
|
|
|
rendered_element: &mut Self::ElementState,
|
|
|
|
cx: &mut ViewContext<ParentViewState>,
|
2023-10-18 12:12:50 +00:00
|
|
|
) -> LayoutId {
|
2023-10-31 15:16:30 +00:00
|
|
|
(self.layout)(self, rendered_element, cx)
|
2023-09-22 14:33:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn paint(
|
|
|
|
&mut self,
|
2023-10-31 15:16:30 +00:00
|
|
|
_bounds: Bounds<Pixels>,
|
|
|
|
_view_state: &mut ParentViewState,
|
|
|
|
rendered_element: &mut Self::ElementState,
|
|
|
|
cx: &mut ViewContext<ParentViewState>,
|
2023-10-11 04:14:47 +00:00
|
|
|
) {
|
2023-10-31 15:16:30 +00:00
|
|
|
(self.paint)(self, rendered_element, cx)
|
2023-10-12 17:30:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-31 15:19:46 +00:00
|
|
|
pub struct AnyWeakView {
|
|
|
|
model: AnyWeakModel,
|
|
|
|
initialize: fn(&AnyView, &mut WindowContext) -> AnyBox,
|
|
|
|
layout: fn(&AnyView, &mut AnyBox, &mut WindowContext) -> LayoutId,
|
|
|
|
paint: fn(&AnyView, &mut AnyBox, &mut WindowContext),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl AnyWeakView {
|
|
|
|
pub fn upgrade(&self) -> Option<AnyView> {
|
|
|
|
let model = self.model.upgrade()?;
|
|
|
|
Some(AnyView {
|
|
|
|
model,
|
|
|
|
initialize: self.initialize,
|
|
|
|
layout: self.layout,
|
|
|
|
paint: self.paint,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-03 10:36:18 +00:00
|
|
|
impl<V: Render> From<WeakView<V>> for AnyWeakView {
|
|
|
|
fn from(view: WeakView<V>) -> Self {
|
|
|
|
Self {
|
|
|
|
model: view.model.into(),
|
|
|
|
initialize: any_view::initialize::<V>,
|
|
|
|
layout: any_view::layout::<V>,
|
|
|
|
paint: any_view::paint::<V>,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-11-03 10:36:18 +00:00
|
|
|
|
|
|
|
mod any_view {
|
|
|
|
use crate::{AnyElement, AnyView, BorrowWindow, LayoutId, Render, WindowContext};
|
|
|
|
use std::any::Any;
|
|
|
|
|
|
|
|
pub(crate) fn initialize<V: Render>(view: &AnyView, cx: &mut WindowContext) -> Box<dyn Any> {
|
2023-11-14 05:42:19 +00:00
|
|
|
cx.with_element_id(Some(view.model.entity_id), |cx| {
|
2023-11-03 10:36:18 +00:00
|
|
|
let view = view.clone().downcast::<V>().unwrap();
|
|
|
|
let element = view.update(cx, |view, cx| {
|
|
|
|
let mut element = AnyElement::new(view.render(cx));
|
|
|
|
element.initialize(view, cx);
|
|
|
|
element
|
|
|
|
});
|
|
|
|
Box::new(element)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn layout<V: Render>(
|
|
|
|
view: &AnyView,
|
|
|
|
element: &mut Box<dyn Any>,
|
|
|
|
cx: &mut WindowContext,
|
|
|
|
) -> LayoutId {
|
2023-11-14 05:42:19 +00:00
|
|
|
cx.with_element_id(Some(view.model.entity_id), |cx| {
|
2023-11-03 10:36:18 +00:00
|
|
|
let view = view.clone().downcast::<V>().unwrap();
|
|
|
|
let element = element.downcast_mut::<AnyElement<V>>().unwrap();
|
|
|
|
view.update(cx, |view, cx| element.layout(view, cx))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn paint<V: Render>(
|
|
|
|
view: &AnyView,
|
|
|
|
element: &mut Box<dyn Any>,
|
|
|
|
cx: &mut WindowContext,
|
|
|
|
) {
|
2023-11-14 05:42:19 +00:00
|
|
|
cx.with_element_id(Some(view.model.entity_id), |cx| {
|
2023-11-03 10:36:18 +00:00
|
|
|
let view = view.clone().downcast::<V>().unwrap();
|
|
|
|
let element = element.downcast_mut::<AnyElement<V>>().unwrap();
|
|
|
|
view.update(cx, |view, cx| element.paint(view, cx))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|