zed/crates/gpui3/src/element.rs

177 lines
4.5 KiB
Rust
Raw Normal View History

2023-10-10 17:48:32 +00:00
use crate::{Bounds, ElementId, ElementWithId};
2023-10-06 03:02:26 +00:00
use super::{LayoutId, Pixels, Point, Result, ViewContext};
2023-09-21 19:46:31 +00:00
pub(crate) use smallvec::SmallVec;
2023-09-19 19:19:22 +00:00
pub trait Element: 'static {
type State;
type FrameState;
fn layout(
2023-09-19 19:19:22 +00:00
&mut self,
state: &mut Self::State,
cx: &mut ViewContext<Self::State>,
2023-09-19 19:19:22 +00:00
) -> Result<(LayoutId, Self::FrameState)>;
fn paint(
2023-09-19 19:19:22 +00:00
&mut self,
2023-10-06 03:02:26 +00:00
bounds: Bounds<Pixels>,
2023-09-19 19:19:22 +00:00
state: &mut Self::State,
frame_state: &mut Self::FrameState,
cx: &mut ViewContext<Self::State>,
2023-09-19 19:19:22 +00:00
) -> Result<()>;
2023-10-10 17:48:32 +00:00
fn id(self, id: ElementId) -> ElementWithId<Self>
where
Self: Sized,
{
ElementWithId { element: self, id }
}
2023-09-19 19:19:22 +00:00
}
2023-10-10 14:47:09 +00:00
pub trait ParentElement {
type State;
fn children_mut(&mut self) -> &mut SmallVec<[AnyElement<Self::State>; 2]>;
2023-09-20 03:55:49 +00:00
2023-10-10 14:47:09 +00:00
fn child(mut self, child: impl IntoAnyElement<Self::State>) -> Self
2023-09-20 03:55:49 +00:00
where
Self: Sized,
{
self.children_mut().push(child.into_any());
self
}
2023-10-10 14:47:09 +00:00
fn children(mut self, iter: impl IntoIterator<Item = impl IntoAnyElement<Self::State>>) -> 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
}
trait ElementObject<S> {
fn layout(&mut self, state: &mut S, cx: &mut ViewContext<S>) -> Result<LayoutId>;
fn paint(
2023-09-19 19:19:22 +00:00
&mut self,
state: &mut S,
2023-09-21 18:18:09 +00:00
offset: Option<Point<Pixels>>,
cx: &mut ViewContext<S>,
2023-09-19 19:19:22 +00:00
) -> Result<()>;
}
struct RenderedElement<E: Element> {
element: E,
phase: ElementRenderPhase<E::FrameState>,
}
#[derive(Default)]
enum ElementRenderPhase<S> {
#[default]
Rendered,
LayoutRequested {
layout_id: LayoutId,
frame_state: S,
},
Painted {
2023-10-06 03:02:26 +00:00
bounds: Bounds<Pixels>,
2023-09-19 19:19:22 +00:00
frame_state: S,
},
}
/// Internal struct that wraps an element to store Layout and FrameState after the element is rendered.
/// 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,
phase: ElementRenderPhase::Rendered,
}
}
}
impl<E: Element> ElementObject<E::State> for RenderedElement<E> {
fn layout(&mut self, state: &mut E::State, cx: &mut ViewContext<E::State>) -> Result<LayoutId> {
let (layout_id, frame_state) = self.element.layout(state, cx)?;
self.phase = ElementRenderPhase::LayoutRequested {
layout_id,
frame_state,
};
Ok(layout_id)
}
fn paint(
&mut self,
state: &mut E::State,
2023-09-21 18:18:09 +00:00
offset: Option<Point<Pixels>>,
2023-09-19 19:19:22 +00:00
cx: &mut ViewContext<E::State>,
) -> Result<()> {
self.phase = match std::mem::take(&mut self.phase) {
ElementRenderPhase::Rendered => panic!("must call layout before paint"),
ElementRenderPhase::LayoutRequested {
layout_id,
mut frame_state,
} => {
2023-10-06 03:02:26 +00:00
let mut bounds = cx.layout_bounds(layout_id)?.clone();
offset.map(|offset| bounds.origin += offset);
self.element.paint(bounds, state, &mut frame_state, cx)?;
2023-09-19 19:19:22 +00:00
ElementRenderPhase::Painted {
2023-10-06 03:02:26 +00:00
bounds,
2023-09-19 19:19:22 +00:00
frame_state,
}
}
ElementRenderPhase::Painted {
2023-10-06 03:02:26 +00:00
bounds,
2023-09-19 19:19:22 +00:00
mut frame_state,
} => {
self.element
2023-10-06 03:02:26 +00:00
.paint(bounds.clone(), state, &mut frame_state, cx)?;
2023-09-19 19:19:22 +00:00
ElementRenderPhase::Painted {
2023-10-06 03:02:26 +00:00
bounds,
2023-09-19 19:19:22 +00:00
frame_state,
}
}
};
Ok(())
}
}
pub struct AnyElement<S>(Box<dyn ElementObject<S>>);
impl<S> AnyElement<S> {
pub fn layout(&mut self, state: &mut S, cx: &mut ViewContext<S>) -> Result<LayoutId> {
2023-09-19 19:19:22 +00:00
self.0.layout(state, cx)
}
pub fn paint(
2023-09-19 19:19:22 +00:00
&mut self,
state: &mut S,
2023-09-21 18:18:09 +00:00
offset: Option<Point<Pixels>>,
cx: &mut ViewContext<S>,
2023-09-19 19:19:22 +00:00
) -> Result<()> {
2023-09-21 18:18:09 +00:00
self.0.paint(state, offset, cx)
2023-09-19 19:19:22 +00:00
}
}
pub trait IntoAnyElement<S> {
fn into_any(self) -> AnyElement<S>;
}
impl<E: Element> IntoAnyElement<E::State> for E {
fn into_any(self) -> AnyElement<E::State> {
AnyElement(Box::new(RenderedElement::new(self)))
}
}
impl<S> IntoAnyElement<S> for AnyElement<S> {
fn into_any(self) -> AnyElement<S> {
self
}
}