zed/crates/gpui2/src/element.rs

386 lines
10 KiB
Rust
Raw Normal View History

use crate::{
2023-11-20 02:32:31 +00:00
AvailableSpace, BorrowWindow, Bounds, ElementId, LayoutId, Pixels, Point, Size, WindowContext,
};
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;
use std::{any::Any, fmt::Debug, mem};
2023-09-19 19:19:22 +00:00
2023-11-20 02:32:31 +00:00
pub trait Element {
type ElementState: 'static;
2023-09-19 19:19:22 +00:00
2023-11-14 08:15:48 +00:00
fn element_id(&self) -> Option<ElementId>;
2023-10-10 18:41:28 +00:00
2023-10-18 12:12:50 +00:00
fn layout(
&mut self,
element_state: Option<Self::ElementState>,
2023-11-20 02:32:31 +00:00
cx: &mut WindowContext,
2023-11-15 19:41:09 +00:00
) -> (LayoutId, Self::ElementState);
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-11 04:14:47 +00:00
element_state: &mut Self::ElementState,
2023-11-20 02:32:31 +00:00
cx: &mut WindowContext,
2023-10-11 04:14:47 +00:00
);
fn draw<T, R>(
self,
origin: Point<Pixels>,
available_space: Size<T>,
2023-11-20 02:32:31 +00:00
cx: &mut WindowContext,
f: impl FnOnce(&Self::ElementState, &mut WindowContext) -> R,
) -> R
where
Self: Sized,
T: Clone + Default + Debug + Into<AvailableSpace>,
{
let mut element = RenderedElement {
element: self,
phase: ElementRenderPhase::Start,
};
2023-11-20 02:32:31 +00:00
element.draw(origin, available_space.map(Into::into), cx);
if let ElementRenderPhase::Painted { frame_state } = &element.phase {
if let Some(frame_state) = frame_state.as_ref() {
f(&frame_state, cx)
} else {
let element_id = element
.element
.element_id()
.expect("we either have some frame_state or some element_id");
cx.with_element_state(element_id, |element_state, cx| {
let element_state = element_state.unwrap();
let result = f(&element_state, cx);
(result, element_state)
})
}
} else {
unreachable!()
}
}
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-11-20 02:32:31 +00:00
pub trait ParentComponent {
fn children_mut(&mut self) -> &mut SmallVec<[AnyElement; 2]>;
2023-10-10 14:47:09 +00:00
2023-11-20 02:32:31 +00:00
fn child(mut self, child: impl Component) -> Self
2023-09-20 03:55:49 +00:00
where
Self: Sized,
{
self.children_mut().push(child.render());
2023-09-20 03:55:49 +00:00
self
}
2023-11-20 02:32:31 +00:00
fn children(mut self, iter: impl IntoIterator<Item = impl Component>) -> Self
where
Self: Sized,
{
self.children_mut()
.extend(iter.into_iter().map(|item| item.render()));
self
}
2023-09-19 19:19:22 +00:00
}
2023-11-20 02:32:31 +00:00
trait ElementObject {
fn element_id(&self) -> Option<ElementId>;
2023-11-20 02:32:31 +00:00
fn layout(&mut self, cx: &mut WindowContext) -> LayoutId;
fn paint(&mut self, cx: &mut WindowContext);
fn measure(
&mut self,
available_space: Size<AvailableSpace>,
2023-11-20 02:32:31 +00:00
cx: &mut WindowContext,
) -> Size<Pixels>;
fn draw(
&mut self,
origin: Point<Pixels>,
available_space: Size<AvailableSpace>,
2023-11-20 02:32:31 +00:00
cx: &mut WindowContext,
);
2023-09-19 19:19:22 +00:00
}
2023-11-20 02:32:31 +00:00
struct RenderedElement<E: Element> {
2023-09-19 19:19:22 +00:00
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,
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
},
LayoutComputed {
layout_id: LayoutId,
available_space: Size<AvailableSpace>,
frame_state: Option<V>,
},
Painted {
frame_state: Option<V>,
},
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.
2023-11-20 02:32:31 +00:00
impl<E: Element> RenderedElement<E> {
2023-09-19 19:19:22 +00:00
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-11-20 02:32:31 +00:00
impl<E> ElementObject for RenderedElement<E>
2023-10-18 12:12:50 +00:00
where
2023-11-20 02:32:31 +00:00
E: Element,
E::ElementState: 'static,
2023-10-18 12:12:50 +00:00
{
fn element_id(&self) -> Option<ElementId> {
self.element.element_id()
}
2023-11-20 02:32:31 +00:00
fn layout(&mut self, cx: &mut WindowContext) -> LayoutId {
2023-11-15 19:41:09 +00:00
let (layout_id, frame_state) = match mem::take(&mut self.phase) {
ElementRenderPhase::Start => {
2023-11-14 08:15:48 +00:00
if let Some(id) = self.element.element_id() {
2023-11-15 19:41:09 +00:00
let layout_id = cx.with_element_state(id, |element_state, cx| {
2023-11-20 02:32:31 +00:00
self.element.layout(element_state, cx)
2023-10-18 12:12:50 +00:00
});
2023-11-15 19:41:09 +00:00
(layout_id, None)
2023-10-18 12:12:50 +00:00
} else {
2023-11-20 02:32:31 +00:00
let (layout_id, frame_state) = self.element.layout(None, cx);
2023-11-15 19:41:09 +00:00
(layout_id, Some(frame_state))
2023-10-18 12:12:50 +00:00
}
}
ElementRenderPhase::LayoutRequested { .. }
| ElementRenderPhase::LayoutComputed { .. }
| ElementRenderPhase::Painted { .. } => {
2023-11-07 19:00:05 +00:00
panic!("element rendered twice")
}
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-11-20 02:32:31 +00:00
fn paint(&mut self, cx: &mut WindowContext) {
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,
}
| ElementRenderPhase::LayoutComputed {
layout_id,
mut frame_state,
..
2023-09-19 19:19:22 +00:00
} => {
2023-10-20 15:31:42 +00:00
let bounds = cx.layout_bounds(layout_id);
2023-11-14 08:15:48 +00:00
if let Some(id) = self.element.element_id() {
2023-10-18 12:12:50 +00:00
cx.with_element_state(id, |element_state, cx| {
let mut element_state = element_state.unwrap();
2023-11-20 02:32:31 +00:00
self.element.paint(bounds, &mut element_state, cx);
2023-10-18 12:12:50 +00:00
((), element_state)
});
} else {
self.element
2023-11-20 02:32:31 +00:00
.paint(bounds, frame_state.as_mut().unwrap(), cx);
2023-09-19 19:19:22 +00:00
}
ElementRenderPhase::Painted { frame_state }
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
};
}
fn measure(
&mut self,
available_space: Size<AvailableSpace>,
2023-11-20 02:32:31 +00:00
cx: &mut WindowContext,
) -> Size<Pixels> {
if matches!(&self.phase, ElementRenderPhase::Start) {
2023-11-20 02:32:31 +00:00
self.layout(cx);
}
let layout_id = match &mut self.phase {
ElementRenderPhase::LayoutRequested {
layout_id,
frame_state,
} => {
cx.compute_layout(*layout_id, available_space);
let layout_id = *layout_id;
self.phase = ElementRenderPhase::LayoutComputed {
layout_id,
available_space,
frame_state: frame_state.take(),
};
layout_id
}
ElementRenderPhase::LayoutComputed {
layout_id,
available_space: prev_available_space,
..
} => {
if available_space != *prev_available_space {
cx.compute_layout(*layout_id, available_space);
*prev_available_space = available_space;
}
*layout_id
}
_ => panic!("cannot measure after painting"),
};
cx.layout_bounds(layout_id).size
}
fn draw(
&mut self,
origin: Point<Pixels>,
available_space: Size<AvailableSpace>,
2023-11-20 02:32:31 +00:00
cx: &mut WindowContext,
) {
2023-11-20 02:32:31 +00:00
self.measure(available_space, cx);
cx.with_absolute_element_offset(origin, |cx| self.paint(cx))
}
2023-09-19 19:19:22 +00:00
}
2023-11-20 02:32:31 +00:00
pub struct AnyElement(Box<dyn ElementObject>);
2023-11-20 02:32:31 +00:00
impl AnyElement {
pub fn new<E>(element: E) -> Self
where
2023-11-20 02:32:31 +00:00
E: 'static + Element,
E::ElementState: Any,
{
2023-10-12 17:30:00 +00:00
AnyElement(Box::new(RenderedElement::new(element)))
}
pub fn element_id(&self) -> Option<ElementId> {
self.0.element_id()
}
2023-11-20 02:32:31 +00:00
pub fn layout(&mut self, cx: &mut WindowContext) -> LayoutId {
self.0.layout(cx)
2023-10-18 12:12:50 +00:00
}
2023-11-20 02:32:31 +00:00
pub fn paint(&mut self, cx: &mut WindowContext) {
self.0.paint(cx)
2023-09-19 19:19:22 +00:00
}
/// Initializes this element and performs layout within the given available space to determine its size.
pub fn measure(
&mut self,
available_space: Size<AvailableSpace>,
2023-11-20 02:32:31 +00:00
cx: &mut WindowContext,
) -> Size<Pixels> {
2023-11-20 02:32:31 +00:00
self.0.measure(available_space, cx)
}
/// Initializes this element and performs layout in the available space, then paints it at the given origin.
pub fn draw(
&mut self,
origin: Point<Pixels>,
available_space: Size<AvailableSpace>,
2023-11-20 02:32:31 +00:00
cx: &mut WindowContext,
) {
2023-11-20 02:32:31 +00:00
self.0.draw(origin, available_space, cx)
}
2023-09-19 19:19:22 +00:00
}
2023-11-20 02:32:31 +00:00
pub trait Component {
fn render(self) -> AnyElement;
fn map<U>(self, f: impl FnOnce(Self) -> U) -> U
where
Self: Sized,
2023-11-20 02:32:31 +00:00
U: Component,
{
f(self)
}
fn when(self, condition: bool, then: impl FnOnce(Self) -> Self) -> Self
where
Self: Sized,
{
self.map(|this| if condition { then(this) } else { this })
}
fn when_some<T>(self, option: Option<T>, then: impl FnOnce(Self, T) -> Self) -> Self
where
Self: Sized,
{
self.map(|this| {
if let Some(value) = option {
then(this, value)
} else {
this
}
})
}
2023-09-19 19:19:22 +00:00
}
2023-11-20 02:32:31 +00:00
impl Component for AnyElement {
fn render(self) -> AnyElement {
2023-09-19 19:19:22 +00:00
self
}
}
2023-11-20 02:32:31 +00:00
impl<E, F> Element for Option<F>
where
2023-11-20 02:32:31 +00:00
E: 'static + Component,
F: FnOnce(&mut WindowContext) -> E + 'static,
{
2023-11-20 02:32:31 +00:00
type ElementState = AnyElement;
2023-11-14 08:15:48 +00:00
fn element_id(&self) -> Option<ElementId> {
None
}
2023-11-15 19:41:09 +00:00
fn layout(
&mut self,
2023-11-15 19:41:09 +00:00
_: Option<Self::ElementState>,
2023-11-20 02:32:31 +00:00
cx: &mut WindowContext,
2023-11-15 19:41:09 +00:00
) -> (LayoutId, Self::ElementState) {
let render = self.take().unwrap();
2023-11-20 02:32:31 +00:00
let mut rendered_element = (render)(cx).render();
let layout_id = rendered_element.layout(cx);
2023-11-15 19:41:09 +00:00
(layout_id, rendered_element)
}
fn paint(
&mut self,
_bounds: Bounds<Pixels>,
rendered_element: &mut Self::ElementState,
2023-11-20 02:32:31 +00:00
cx: &mut WindowContext,
) {
2023-11-20 02:32:31 +00:00
rendered_element.paint(cx)
}
}
2023-11-20 02:32:31 +00:00
impl<E, F> Component for Option<F>
where
2023-11-20 02:32:31 +00:00
E: 'static + Component,
F: FnOnce(&mut WindowContext) -> E + 'static,
{
2023-11-20 02:32:31 +00:00
fn render(self) -> AnyElement {
AnyElement::new(self)
}
}
2023-11-20 02:32:31 +00:00
impl<E, F> Component for F
where
2023-11-20 02:32:31 +00:00
E: 'static + Component,
F: FnOnce(&mut WindowContext) -> E + 'static,
{
2023-11-20 02:32:31 +00:00
fn render(self) -> AnyElement {
AnyElement::new(Some(self))
}
}