zed/crates/gpui2/src/element.rs

203 lines
6.2 KiB
Rust
Raw Normal View History

2023-10-20 15:31:42 +00:00
use crate::{BorrowWindow, Bounds, ElementId, LayoutId, Pixels, ViewContext};
2023-10-10 18:41:28 +00:00
use derive_more::{Deref, DerefMut};
2023-09-21 19:46:31 +00:00
pub(crate) use smallvec::SmallVec;
2023-10-20 09:00:52 +00:00
use std::mem;
2023-09-19 19:19:22 +00:00
2023-10-12 17:30:00 +00:00
pub trait Element: 'static + Send + Sync + IntoAnyElement<Self::ViewState> {
2023-10-11 04:14:47 +00:00
type ViewState: 'static + Send + Sync;
type ElementState: 'static + Send + Sync;
2023-09-19 19:19:22 +00:00
2023-10-17 09:08:48 +00:00
fn id(&self) -> Option<ElementId>;
2023-10-10 18:41:28 +00:00
2023-10-18 12:12:50 +00:00
fn initialize(
2023-09-19 19:19:22 +00:00
&mut self,
2023-10-18 12:12:50 +00:00
view_state: &mut Self::ViewState,
2023-10-11 04:14:47 +00:00
element_state: Option<Self::ElementState>,
cx: &mut ViewContext<Self::ViewState>,
2023-10-18 12:12:50 +00:00
) -> Self::ElementState;
fn layout(
&mut self,
view_state: &mut Self::ViewState,
element_state: &mut Self::ElementState,
cx: &mut ViewContext<Self::ViewState>,
) -> LayoutId;
2023-09-19 19:19:22 +00:00
fn paint(
2023-09-19 19:19:22 +00:00
&mut self,
2023-10-06 03:02:26 +00:00
bounds: Bounds<Pixels>,
2023-10-18 12:12:50 +00:00
view_state: &mut Self::ViewState,
2023-10-11 04:14:47 +00:00
element_state: &mut Self::ElementState,
cx: &mut ViewContext<Self::ViewState>,
);
2023-09-19 19:19:22 +00:00
}
2023-10-11 04:14:47 +00:00
#[derive(Deref, DerefMut, Default, Clone, Debug, Eq, PartialEq, Hash)]
2023-10-19 17:43:28 +00:00
pub struct GlobalElementId(SmallVec<[ElementId; 32]>);
2023-10-10 18:41:28 +00:00
2023-10-17 11:32:49 +00:00
pub trait ParentElement: Element {
fn children_mut(&mut self) -> &mut SmallVec<[AnyElement<Self::ViewState>; 2]>;
2023-10-10 14:47:09 +00:00
2023-10-17 11:32:49 +00:00
fn child(mut self, child: impl IntoAnyElement<Self::ViewState>) -> Self
2023-09-20 03:55:49 +00:00
where
Self: Sized,
{
self.children_mut().push(child.into_any());
self
}
2023-10-17 11:32:49 +00:00
fn children(
mut self,
iter: impl IntoIterator<Item = impl IntoAnyElement<Self::ViewState>>,
) -> Self
where
Self: Sized,
{
self.children_mut()
.extend(iter.into_iter().map(|item| item.into_any()));
self
}
2023-09-19 19:19:22 +00:00
}
2023-10-18 12:12:50 +00:00
trait ElementObject<V>: 'static + Send + Sync {
fn initialize(&mut self, view_state: &mut V, cx: &mut ViewContext<V>);
fn layout(&mut self, view_state: &mut V, cx: &mut ViewContext<V>) -> LayoutId;
2023-10-20 15:31:42 +00:00
fn paint(&mut self, view_state: &mut V, cx: &mut ViewContext<V>);
2023-09-19 19:19:22 +00:00
}
struct RenderedElement<E: Element> {
element: E,
2023-10-11 04:14:47 +00:00
phase: ElementRenderPhase<E::ElementState>,
2023-09-19 19:19:22 +00:00
}
#[derive(Default)]
2023-10-18 12:12:50 +00:00
enum ElementRenderPhase<V> {
2023-09-19 19:19:22 +00:00
#[default]
2023-10-18 12:12:50 +00:00
Start,
Initialized {
frame_state: Option<V>,
},
2023-09-19 19:19:22 +00:00
LayoutRequested {
layout_id: LayoutId,
2023-10-18 12:12:50 +00:00
frame_state: Option<V>,
2023-09-19 19:19:22 +00:00
},
2023-10-18 12:12:50 +00:00
Painted,
2023-09-19 19:19:22 +00:00
}
2023-10-11 04:14:47 +00:00
/// Internal struct that wraps an element to store Layout and ElementState after the element is rendered.
2023-09-19 19:19:22 +00:00
/// It's allocated as a trait object to erase the element type and wrapped in AnyElement<E::State> for
/// improved usability.
impl<E: Element> RenderedElement<E> {
fn new(element: E) -> Self {
RenderedElement {
element,
2023-10-18 12:12:50 +00:00
phase: ElementRenderPhase::Start,
2023-09-19 19:19:22 +00:00
}
}
2023-10-18 12:12:50 +00:00
}
2023-10-11 04:14:47 +00:00
2023-10-18 12:12:50 +00:00
impl<E> ElementObject<E::ViewState> for RenderedElement<E>
where
E: Element,
{
fn initialize(&mut self, view_state: &mut E::ViewState, cx: &mut ViewContext<E::ViewState>) {
let frame_state = if let Some(id) = self.element.id() {
2023-10-11 04:14:47 +00:00
cx.with_element_state(id, |element_state, cx| {
2023-10-18 12:12:50 +00:00
let element_state = self.element.initialize(view_state, element_state, cx);
2023-10-11 04:14:47 +00:00
((), element_state)
});
2023-10-18 12:12:50 +00:00
None
2023-10-11 04:14:47 +00:00
} else {
2023-10-18 12:12:50 +00:00
let frame_state = self.element.initialize(view_state, None, cx);
Some(frame_state)
};
self.phase = ElementRenderPhase::Initialized { frame_state };
2023-10-11 04:14:47 +00:00
}
2023-09-19 19:19:22 +00:00
2023-10-11 04:14:47 +00:00
fn layout(&mut self, state: &mut E::ViewState, cx: &mut ViewContext<E::ViewState>) -> LayoutId {
2023-10-18 12:12:50 +00:00
let layout_id;
let mut frame_state;
match mem::take(&mut self.phase) {
ElementRenderPhase::Initialized {
frame_state: initial_frame_state,
} => {
frame_state = initial_frame_state;
if let Some(id) = self.element.id() {
layout_id = cx.with_element_state(id, |element_state, cx| {
let mut element_state = element_state.unwrap();
let layout_id = self.element.layout(state, &mut element_state, cx);
(layout_id, element_state)
});
} else {
layout_id = self
.element
.layout(state, frame_state.as_mut().unwrap(), cx);
}
}
_ => panic!("must call initialize before layout"),
2023-10-11 04:14:47 +00:00
};
2023-09-19 19:19:22 +00:00
self.phase = ElementRenderPhase::LayoutRequested {
layout_id,
frame_state,
};
2023-10-11 04:14:47 +00:00
layout_id
2023-09-19 19:19:22 +00:00
}
2023-10-20 15:31:42 +00:00
fn paint(&mut self, view_state: &mut E::ViewState, cx: &mut ViewContext<E::ViewState>) {
2023-10-18 12:12:50 +00:00
self.phase = match mem::take(&mut self.phase) {
2023-09-19 19:19:22 +00:00
ElementRenderPhase::LayoutRequested {
layout_id,
mut frame_state,
} => {
2023-10-20 15:31:42 +00:00
let bounds = cx.layout_bounds(layout_id);
2023-10-18 12:12:50 +00:00
if let Some(id) = self.element.id() {
cx.with_element_state(id, |element_state, cx| {
let mut element_state = element_state.unwrap();
self.element
.paint(bounds, view_state, &mut element_state, cx);
((), element_state)
});
} else {
self.element
.paint(bounds, view_state, frame_state.as_mut().unwrap(), cx);
2023-09-19 19:19:22 +00:00
}
2023-10-18 12:12:50 +00:00
ElementRenderPhase::Painted
2023-09-19 19:19:22 +00:00
}
2023-10-18 12:12:50 +00:00
_ => panic!("must call layout before paint"),
2023-09-19 19:19:22 +00:00
};
}
}
2023-10-18 12:12:50 +00:00
pub struct AnyElement<V>(Box<dyn ElementObject<V>>);
2023-09-19 19:19:22 +00:00
2023-10-18 12:12:50 +00:00
impl<V: 'static + Send + Sync> AnyElement<V> {
pub fn new<E: Element<ViewState = V>>(element: E) -> Self {
2023-10-12 17:30:00 +00:00
AnyElement(Box::new(RenderedElement::new(element)))
}
2023-10-18 12:12:50 +00:00
pub fn initialize(&mut self, view_state: &mut V, cx: &mut ViewContext<V>) {
self.0.initialize(view_state, cx);
2023-09-19 19:19:22 +00:00
}
2023-10-18 12:12:50 +00:00
pub fn layout(&mut self, view_state: &mut V, cx: &mut ViewContext<V>) -> LayoutId {
self.0.layout(view_state, cx)
}
2023-10-20 15:31:42 +00:00
pub fn paint(&mut self, view_state: &mut V, cx: &mut ViewContext<V>) {
self.0.paint(view_state, cx)
2023-09-19 19:19:22 +00:00
}
}
2023-10-18 12:12:50 +00:00
pub trait IntoAnyElement<V> {
fn into_any(self) -> AnyElement<V>;
2023-09-19 19:19:22 +00:00
}
2023-10-18 12:12:50 +00:00
impl<V> IntoAnyElement<V> for AnyElement<V> {
fn into_any(self) -> AnyElement<V> {
2023-09-19 19:19:22 +00:00
self
}
}