zed/crates/gpui2/src/view.rs

279 lines
7.1 KiB
Rust
Raw Normal View History

2023-09-26 17:29:44 +00:00
use parking_lot::Mutex;
2023-09-22 14:33:51 +00:00
use crate::{
AnyBox, AnyElement, BorrowWindow, Bounds, Component, Element, ElementId, EntityId, Handle,
2023-10-17 19:37:09 +00:00
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
pub struct View<V> {
2023-10-18 12:12:50 +00:00
state: Handle<V>,
2023-10-26 14:40:44 +00:00
render: Arc<Mutex<dyn Fn(&mut V, &mut ViewContext<V>) -> AnyElement<V> + Send + 'static>>,
2023-09-22 14:33:51 +00:00
}
impl<V: 'static> View<V> {
2023-10-12 17:30:00 +00:00
pub fn into_any(self) -> AnyView {
2023-10-26 14:40:44 +00:00
AnyView(Arc::new(self))
2023-09-26 17:29:44 +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>,
2023-10-26 14:40:44 +00:00
render: impl Fn(&mut V, &mut ViewContext<'_, '_, V>) -> E + Send + 'static,
2023-10-18 12:12:50 +00:00
) -> View<V>
2023-09-28 07:16:42 +00:00
where
E: Component<V>,
2023-09-28 07:16:42 +00:00
{
2023-09-22 14:33:51 +00:00
View {
state,
2023-10-26 14:40:44 +00:00
render: Arc::new(Mutex::new(
move |state: &mut V, cx: &mut ViewContext<'_, '_, V>| render(state, cx).render(),
)),
2023-09-22 14:33:51 +00:00
}
}
impl<V: 'static, ParentViewState: 'static> Component<ParentViewState> for View<V> {
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-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| {
2023-10-26 14:40:44 +00:00
let mut any_element = (self.render.lock())(state, 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 {
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
}
}
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
}
unsafe impl<V, ParentV> Send for EraseViewState<V, ParentV> {}
impl<V: 'static, ParentV: 'static> Component<ParentV> for EraseViewState<V, ParentV> {
fn render(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)
}
}
trait ViewObject: Send + Sync {
2023-10-12 17:30:00 +00:00
fn entity_id(&self) -> EntityId;
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
}
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-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-18 14:43:20 +00:00
self.state.update(cx, |state, cx| {
2023-10-26 14:40:44 +00:00
let mut any_element = Box::new((self.render.lock())(state, cx));
2023-10-18 14:43:20 +00:00
any_element.initialize(state, cx);
any_element as AnyBox
})
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-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-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-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-26 14:40:44 +00:00
#[derive(Clone)]
pub struct AnyView(Arc<dyn ViewObject>);
2023-09-22 14:33:51 +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>,
}
unsafe impl<ParentV> Send for EraseAnyViewState<ParentV> {}
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-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
}
}