zed/crates/gpui3/src/view.rs

267 lines
7 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-12 17:30:00 +00:00
AnyBox, AnyElement, BorrowWindow, Bounds, Element, ElementId, EntityId, Handle,
IdentifiedElement, IntoAnyElement, LayoutId, Pixels, ViewContext, WindowContext,
2023-09-22 14:33:51 +00:00
};
2023-09-26 17:29:44 +00:00
use std::{any::Any, marker::PhantomData, sync::Arc};
2023-09-22 14:33:51 +00:00
2023-10-12 17:30:00 +00:00
pub struct View<S: Send + Sync> {
2023-09-22 14:33:51 +00:00
state: Handle<S>,
2023-10-02 18:47:45 +00:00
render: Arc<dyn Fn(&mut S, &mut ViewContext<S>) -> AnyElement<S> + Send + Sync + 'static>,
2023-09-22 14:33:51 +00:00
}
2023-10-12 17:30:00 +00:00
impl<S: 'static + Send + Sync> View<S> {
pub fn into_any(self) -> AnyView {
2023-09-26 17:29:44 +00:00
AnyView {
view: Arc::new(Mutex::new(self)),
}
}
}
2023-10-12 17:30:00 +00:00
impl<S: Send + Sync> Clone for View<S> {
2023-09-22 14:33:51 +00:00
fn clone(&self) -> Self {
Self {
state: self.state.clone(),
render: self.render.clone(),
}
}
}
2023-10-12 17:30:00 +00:00
pub fn view<S, E>(
2023-09-22 14:33:51 +00:00
state: Handle<S>,
render: impl Fn(&mut S, &mut ViewContext<S>) -> E + Send + Sync + 'static,
2023-10-12 17:30:00 +00:00
) -> View<S>
2023-09-28 07:16:42 +00:00
where
2023-10-12 19:27:46 +00:00
E: IntoAnyElement<S>,
2023-09-28 07:16:42 +00:00
S: 'static + Send + Sync,
{
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-12 17:30:00 +00:00
impl<S: 'static + Send + Sync, ParentViewState: 'static + Send + Sync>
IntoAnyElement<ParentViewState> for View<S>
{
fn into_any(self) -> AnyElement<ParentViewState> {
AnyElement::new(EraseViewState {
view: self,
parent_view_state_type: PhantomData,
})
}
}
impl<S: 'static + Send + Sync> Element for View<S> {
type ViewState = ();
2023-10-11 04:14:47 +00:00
type ElementState = AnyElement<S>;
2023-09-22 14:33:51 +00:00
2023-10-11 17:40:42 +00:00
fn element_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-09-22 14:33:51 +00:00
fn layout(
&mut self,
2023-10-11 04:14:47 +00:00
_: &mut Self::ViewState,
_: Option<Self::ElementState>,
cx: &mut ViewContext<Self::ViewState>,
) -> (LayoutId, Self::ElementState) {
2023-09-22 14:33:51 +00:00
self.state.update(cx, |state, cx| {
let mut element = (self.render)(state, cx);
2023-10-11 04:14:47 +00:00
let layout_id = element.layout(state, cx);
(layout_id, element)
2023-09-22 14:33:51 +00:00
})
}
fn paint(
&mut self,
2023-10-06 03:02:26 +00:00
_: Bounds<Pixels>,
2023-10-11 04:14:47 +00:00
_: &mut Self::ViewState,
element: &mut Self::ElementState,
cx: &mut ViewContext<Self::ViewState>,
) {
2023-09-22 14:33:51 +00:00
self.state
.update(cx, |state, cx| element.paint(state, None, cx))
}
}
2023-10-12 19:27:46 +00:00
impl<S: Send + Sync + 'static> IdentifiedElement for View<S> {}
2023-10-12 17:30:00 +00:00
struct EraseViewState<ViewState: 'static + Send + Sync, ParentViewState> {
view: View<ViewState>,
parent_view_state_type: PhantomData<ParentViewState>,
}
impl<ViewState, ParentViewState> IntoAnyElement<ParentViewState>
for EraseViewState<ViewState, ParentViewState>
where
ViewState: 'static + Send + Sync,
ParentViewState: 'static + Send + Sync,
{
fn into_any(self) -> AnyElement<ParentViewState> {
AnyElement::new(self)
}
}
impl<ViewState, ParentViewState> Element for EraseViewState<ViewState, ParentViewState>
where
ViewState: 'static + Send + Sync,
ParentViewState: 'static + Send + Sync,
{
type ViewState = ParentViewState;
type ElementState = AnyBox;
fn element_id(&self) -> Option<crate::ElementId> {
Element::element_id(&self.view)
}
fn layout(
&mut self,
_: &mut Self::ViewState,
_: Option<Self::ElementState>,
cx: &mut ViewContext<Self::ViewState>,
) -> (LayoutId, Self::ElementState) {
ViewObject::layout(&mut self.view, cx)
}
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-11 04:14:47 +00:00
fn layout(&mut self, cx: &mut WindowContext) -> (LayoutId, AnyBox);
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-12 17:30:00 +00:00
impl<S: Send + Sync + 'static> ViewObject for View<S> {
fn entity_id(&self) -> EntityId {
self.state.id
}
2023-10-11 19:22:40 +00:00
2023-10-11 04:14:47 +00:00
fn layout(&mut self, cx: &mut WindowContext) -> (LayoutId, AnyBox) {
2023-10-11 19:22:40 +00:00
cx.with_element_id(IdentifiedElement::element_id(self), |cx| {
self.state.update(cx, |state, cx| {
let mut element = (self.render)(state, cx);
let layout_id = element.layout(state, cx);
let element = Box::new(element) as AnyBox;
(layout_id, element)
})
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-11 19:22:40 +00:00
cx.with_element_id(IdentifiedElement::element_id(self), |cx| {
self.state.update(cx, |state, cx| {
let element = element.downcast_mut::<AnyElement<S>>().unwrap();
element.paint(state, None, cx);
});
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-12 17:30:00 +00:00
impl<ParentViewState> IntoAnyElement<ParentViewState> for AnyView
where
ParentViewState: 'static + Send + Sync,
{
fn into_any(self) -> AnyElement<ParentViewState> {
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-11 17:40:42 +00:00
fn element_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-09-22 14:33:51 +00:00
fn layout(
&mut self,
2023-10-11 04:14:47 +00:00
_: &mut Self::ViewState,
_: Option<Self::ElementState>,
cx: &mut ViewContext<Self::ViewState>,
) -> (LayoutId, Self::ElementState) {
2023-09-26 17:29:44 +00:00
self.view.lock().layout(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>,
}
impl<ParentViewState> IntoAnyElement<ParentViewState> for EraseAnyViewState<ParentViewState>
where
ParentViewState: 'static + Send + Sync,
{
fn into_any(self) -> AnyElement<ParentViewState> {
AnyElement::new(self)
}
}
impl<ParentViewState> Element for EraseAnyViewState<ParentViewState>
where
ParentViewState: 'static + Send + Sync,
{
type ViewState = ParentViewState;
type ElementState = AnyBox;
fn element_id(&self) -> Option<crate::ElementId> {
Element::element_id(&self.view)
}
fn layout(
&mut self,
_: &mut Self::ViewState,
_: Option<Self::ElementState>,
cx: &mut ViewContext<Self::ViewState>,
) -> (LayoutId, Self::ElementState) {
self.view.view.lock().layout(cx)
}
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(),
}
}
}