zed/crates/gpui2/src/view.rs

308 lines
7.8 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::{
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-18 12:12:50 +00:00
pub struct View<V: Send + Sync> {
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-18 12:12:50 +00:00
impl<V: 'static + Send + Sync> 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-18 12:12:50 +00:00
impl<V: Send + Sync> 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>,
V: 'static + Send + Sync,
2023-09-28 07:16:42 +00:00
{
2023-09-22 14:33:51 +00:00
View {
state,
render: Arc::new(move |state, cx| render(state, cx).into_any()),
2023-09-22 14:33:51 +00:00
}
}
2023-10-18 12:12:50 +00:00
impl<V: 'static + Send + Sync, ParentViewState: 'static + Send + Sync>
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-18 12:12:50 +00:00
impl<V: 'static + Send + Sync> Element for View<V> {
2023-10-12 17:30:00 +00:00
type ViewState = ();
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-11 19:22:40 +00:00
Some(ElementId::View(self.state.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-18 12:12:50 +00:00
struct EraseViewState<V: 'static + Send + Sync, ParentV> {
view: View<V>,
parent_view_state_type: PhantomData<ParentV>,
2023-10-12 17:30:00 +00:00
}
2023-10-18 12:12:50 +00:00
impl<V, ParentV> IntoAnyElement<ParentV> for EraseViewState<V, ParentV>
2023-10-12 17:30:00 +00:00
where
2023-10-18 12:12:50 +00:00
V: 'static + Send + Sync,
ParentV: 'static + Send + Sync,
2023-10-12 17:30:00 +00:00
{
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-18 12:12:50 +00:00
impl<V, ParentV> Element for EraseViewState<V, ParentV>
2023-10-12 17:30:00 +00:00
where
2023-10-18 12:12:50 +00:00
V: 'static + Send + Sync,
ParentV: 'static + Send + Sync,
2023-10-12 17:30:00 +00:00
{
2023-10-18 12:12:50 +00:00
type ViewState = 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,
_: &mut Self::ViewState,
_: Option<Self::ElementState>,
cx: &mut ViewContext<Self::ViewState>,
2023-10-18 12:12:50 +00:00
) -> Self::ElementState {
ViewObject::initialize(&mut self.view, cx)
}
fn layout(
&mut self,
_: &mut Self::ViewState,
element: &mut Self::ElementState,
cx: &mut ViewContext<Self::ViewState>,
) -> LayoutId {
ViewObject::layout(&mut self.view, element, cx)
2023-10-12 17:30:00 +00:00
}
fn paint(
&mut self,
bounds: Bounds<Pixels>,
_: &mut Self::ViewState,
element: &mut Self::ElementState,
cx: &mut ViewContext<Self::ViewState>,
) {
ViewObject::paint(&mut self.view, bounds, element, cx)
}
}
trait ViewObject: 'static + Send + Sync {
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-18 12:12:50 +00:00
impl<V: Send + Sync + 'static> ViewObject for View<V> {
2023-10-12 17:30:00 +00:00
fn entity_id(&self) -> EntityId {
self.state.id
}
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-18 12:12:50 +00:00
impl<ParentV> IntoAnyElement<ParentV> for AnyView
2023-10-12 17:30:00 +00:00
where
2023-10-18 12:12:50 +00:00
ParentV: 'static + Send + Sync,
2023-10-12 17:30:00 +00:00
{
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-18 12:12:50 +00:00
impl<ParentV> IntoAnyElement<ParentV> for EraseAnyViewState<ParentV>
2023-10-12 17:30:00 +00:00
where
2023-10-18 12:12:50 +00:00
ParentV: 'static + Send + Sync,
2023-10-12 17:30:00 +00:00
{
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-18 12:12:50 +00:00
impl<ParentV> Element for EraseAnyViewState<ParentV>
2023-10-12 17:30:00 +00:00
where
2023-10-18 12:12:50 +00:00
ParentV: 'static + Send + Sync,
2023-10-12 17:30:00 +00:00
{
2023-10-18 12:12:50 +00:00
type ViewState = 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,
_: &mut Self::ViewState,
_: Option<Self::ElementState>,
cx: &mut ViewContext<Self::ViewState>,
2023-10-18 12:12:50 +00:00
) -> Self::ElementState {
self.view.view.lock().initialize(cx)
}
fn layout(
&mut self,
_: &mut Self::ViewState,
element: &mut Self::ElementState,
cx: &mut ViewContext<Self::ViewState>,
) -> LayoutId {
self.view.view.lock().layout(element, cx)
2023-10-12 17:30:00 +00:00
}
fn paint(
&mut self,
bounds: Bounds<Pixels>,
_: &mut Self::ViewState,
element: &mut Self::ElementState,
cx: &mut ViewContext<Self::ViewState>,
) {
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(),
}
}
}