Reorganize element-related traits

This commit is contained in:
Nathan Sobo 2023-11-22 11:19:43 -07:00
parent ca1d9dd0e5
commit c23f17ee0b
42 changed files with 190 additions and 265 deletions

View file

@ -12,15 +12,15 @@ pub trait Render: 'static + Sized {
fn render(&mut self, cx: &mut ViewContext<Self>) -> Self::Element;
}
pub trait RenderOnce: Sized {
pub trait IntoElement: Sized {
type Element: Element + 'static;
fn element_id(&self) -> Option<ElementId>;
fn render_once(self) -> Self::Element;
fn into_element(self) -> Self::Element;
fn render_into_any(self) -> AnyElement {
self.render_once().into_any()
fn into_any_element(self) -> AnyElement {
self.into_element().into_any()
}
fn draw<T, R>(
@ -33,7 +33,7 @@ pub trait RenderOnce: Sized {
where
T: Clone + Default + Debug + Into<AvailableSpace>,
{
let element = self.render_once();
let element = self.into_element();
let element_id = element.element_id();
let element = DrawableElement {
element: Some(element),
@ -57,7 +57,7 @@ pub trait RenderOnce: Sized {
fn map<U>(self, f: impl FnOnce(Self) -> U) -> U
where
Self: Sized,
U: RenderOnce,
U: IntoElement,
{
f(self)
}
@ -83,7 +83,7 @@ pub trait RenderOnce: Sized {
}
}
pub trait Element: 'static + RenderOnce {
pub trait Element: 'static + IntoElement {
type State: 'static;
fn layout(
@ -99,30 +99,30 @@ pub trait Element: 'static + RenderOnce {
}
}
pub trait Component: 'static {
type Rendered: RenderOnce;
pub trait RenderOnce: 'static {
type Rendered: IntoElement;
fn render(self, cx: &mut WindowContext) -> Self::Rendered;
}
pub struct CompositeElement<C> {
pub struct Component<C> {
component: Option<C>,
}
pub struct CompositeElementState<C: Component> {
rendered_element: Option<<C::Rendered as RenderOnce>::Element>,
rendered_element_state: <<C::Rendered as RenderOnce>::Element as Element>::State,
pub struct CompositeElementState<C: RenderOnce> {
rendered_element: Option<<C::Rendered as IntoElement>::Element>,
rendered_element_state: <<C::Rendered as IntoElement>::Element as Element>::State,
}
impl<C> CompositeElement<C> {
impl<C> Component<C> {
pub fn new(component: C) -> Self {
CompositeElement {
Component {
component: Some(component),
}
}
}
impl<C: Component> Element for CompositeElement<C> {
impl<C: RenderOnce> Element for Component<C> {
type State = CompositeElementState<C>;
fn layout(
@ -130,7 +130,7 @@ impl<C: Component> Element for CompositeElement<C> {
state: Option<Self::State>,
cx: &mut WindowContext,
) -> (LayoutId, Self::State) {
let mut element = self.component.take().unwrap().render(cx).render_once();
let mut element = self.component.take().unwrap().render(cx).into_element();
let (layout_id, state) = element.layout(state.map(|s| s.rendered_element_state), cx);
let state = CompositeElementState {
rendered_element: Some(element),
@ -148,14 +148,14 @@ impl<C: Component> Element for CompositeElement<C> {
}
}
impl<C: Component> RenderOnce for CompositeElement<C> {
impl<C: RenderOnce> IntoElement for Component<C> {
type Element = Self;
fn element_id(&self) -> Option<ElementId> {
None
}
fn render_once(self) -> Self::Element {
fn into_element(self) -> Self::Element {
self
}
}
@ -166,23 +166,20 @@ pub struct GlobalElementId(SmallVec<[ElementId; 32]>);
pub trait ParentElement {
fn children_mut(&mut self) -> &mut SmallVec<[AnyElement; 2]>;
fn child(mut self, child: impl RenderOnce) -> Self
fn child(mut self, child: impl IntoElement) -> Self
where
Self: Sized,
{
self.children_mut().push(child.render_once().into_any());
self.children_mut().push(child.into_element().into_any());
self
}
fn children(mut self, children: impl IntoIterator<Item = impl RenderOnce>) -> Self
fn children(mut self, children: impl IntoIterator<Item = impl IntoElement>) -> Self
where
Self: Sized,
{
self.children_mut().extend(
children
.into_iter()
.map(|child| child.render_once().into_any()),
);
self.children_mut()
.extend(children.into_iter().map(|child| child.into_any_element()));
self
}
}
@ -486,14 +483,14 @@ impl Element for AnyElement {
}
}
impl RenderOnce for AnyElement {
impl IntoElement for AnyElement {
type Element = Self;
fn element_id(&self) -> Option<ElementId> {
AnyElement::element_id(self)
}
fn render_once(self) -> Self::Element {
fn into_element(self) -> Self::Element {
self
}
}

View file

@ -1,9 +1,9 @@
use crate::{
point, px, Action, AnyDrag, AnyElement, AnyTooltip, AnyView, AppContext, BorrowAppContext,
BorrowWindow, Bounds, ClickEvent, DispatchPhase, Element, ElementId, FocusEvent, FocusHandle,
KeyContext, KeyDownEvent, KeyUpEvent, LayoutId, MouseButton, MouseDownEvent, MouseMoveEvent,
MouseUpEvent, ParentElement, Pixels, Point, Render, RenderOnce, ScrollWheelEvent, SharedString,
Size, Style, StyleRefinement, Styled, Task, View, Visibility, WindowContext,
IntoElement, KeyContext, KeyDownEvent, KeyUpEvent, LayoutId, MouseButton, MouseDownEvent,
MouseMoveEvent, MouseUpEvent, ParentElement, Pixels, Point, Render, ScrollWheelEvent,
SharedString, Size, Style, StyleRefinement, Styled, Task, View, Visibility, WindowContext,
};
use collections::HashMap;
use refineable::Refineable;
@ -666,14 +666,14 @@ impl Element for Div {
}
}
impl RenderOnce for Div {
impl IntoElement for Div {
type Element = Self;
fn element_id(&self) -> Option<ElementId> {
self.interactivity.element_id.clone()
}
fn render_once(self) -> Self::Element {
fn into_element(self) -> Self::Element {
self
}
}
@ -1278,7 +1278,7 @@ where
}
}
impl<E> RenderOnce for Focusable<E>
impl<E> IntoElement for Focusable<E>
where
E: Element,
{
@ -1288,7 +1288,7 @@ where
self.element.element_id()
}
fn render_once(self) -> Self::Element {
fn into_element(self) -> Self::Element {
self.element
}
}
@ -1352,7 +1352,7 @@ where
}
}
impl<E> RenderOnce for Stateful<E>
impl<E> IntoElement for Stateful<E>
where
E: Element,
{
@ -1362,7 +1362,7 @@ where
self.element.element_id()
}
fn render_once(self) -> Self::Element {
fn into_element(self) -> Self::Element {
self
}
}

View file

@ -1,6 +1,6 @@
use crate::{
Bounds, Element, InteractiveElement, InteractiveElementState, Interactivity, LayoutId, Pixels,
RenderOnce, SharedString, StyleRefinement, Styled, WindowContext,
Bounds, Element, InteractiveElement, InteractiveElementState, Interactivity, IntoElement,
LayoutId, Pixels, SharedString, StyleRefinement, Styled, WindowContext,
};
use futures::FutureExt;
use util::ResultExt;
@ -86,14 +86,14 @@ impl Element for Img {
}
}
impl RenderOnce for Img {
impl IntoElement for Img {
type Element = Self;
fn element_id(&self) -> Option<crate::ElementId> {
self.interactivity.element_id.clone()
}
fn render_once(self) -> Self::Element {
fn into_element(self) -> Self::Element {
self
}
}

View file

@ -2,8 +2,8 @@ use smallvec::SmallVec;
use taffy::style::{Display, Position};
use crate::{
point, AnyElement, BorrowWindow, Bounds, Element, LayoutId, ParentElement, Pixels, Point,
RenderOnce, Size, Style, WindowContext,
point, AnyElement, BorrowWindow, Bounds, Element, IntoElement, LayoutId, ParentElement, Pixels,
Point, Size, Style, WindowContext,
};
pub struct OverlayState {
@ -151,14 +151,14 @@ impl Element for Overlay {
}
}
impl RenderOnce for Overlay {
impl IntoElement for Overlay {
type Element = Self;
fn element_id(&self) -> Option<crate::ElementId> {
None
}
fn render_once(self) -> Self::Element {
fn into_element(self) -> Self::Element {
self
}
}

View file

@ -1,6 +1,6 @@
use crate::{
Bounds, Element, ElementId, InteractiveElement, InteractiveElementState, Interactivity,
LayoutId, Pixels, RenderOnce, SharedString, StyleRefinement, Styled, WindowContext,
IntoElement, LayoutId, Pixels, SharedString, StyleRefinement, Styled, WindowContext,
};
use util::ResultExt;
@ -49,14 +49,14 @@ impl Element for Svg {
}
}
impl RenderOnce for Svg {
impl IntoElement for Svg {
type Element = Self;
fn element_id(&self) -> Option<ElementId> {
self.interactivity.element_id.clone()
}
fn render_once(self) -> Self::Element {
fn into_element(self) -> Self::Element {
self
}
}

View file

@ -1,5 +1,5 @@
use crate::{
Bounds, Element, ElementId, LayoutId, Pixels, RenderOnce, SharedString, Size, TextRun,
Bounds, Element, ElementId, IntoElement, LayoutId, Pixels, SharedString, Size, TextRun,
WhiteSpace, WindowContext, WrappedLine,
};
use anyhow::anyhow;
@ -26,14 +26,14 @@ impl Element for &'static str {
}
}
impl RenderOnce for &'static str {
impl IntoElement for &'static str {
type Element = Self;
fn element_id(&self) -> Option<ElementId> {
None
}
fn render_once(self) -> Self::Element {
fn into_element(self) -> Self::Element {
self
}
}
@ -57,14 +57,14 @@ impl Element for SharedString {
}
}
impl RenderOnce for SharedString {
impl IntoElement for SharedString {
type Element = Self;
fn element_id(&self) -> Option<ElementId> {
None
}
fn render_once(self) -> Self::Element {
fn into_element(self) -> Self::Element {
self
}
}
@ -106,14 +106,14 @@ impl Element for StyledText {
}
}
impl RenderOnce for StyledText {
impl IntoElement for StyledText {
type Element = Self;
fn element_id(&self) -> Option<crate::ElementId> {
None
}
fn render_once(self) -> Self::Element {
fn into_element(self) -> Self::Element {
self
}
}
@ -272,14 +272,14 @@ impl Element for InteractiveText {
}
}
impl RenderOnce for InteractiveText {
impl IntoElement for InteractiveText {
type Element = Self;
fn element_id(&self) -> Option<ElementId> {
Some(self.element_id.clone())
}
fn render_once(self) -> Self::Element {
fn into_element(self) -> Self::Element {
self
}
}

View file

@ -1,6 +1,6 @@
use crate::{
point, px, size, AnyElement, AvailableSpace, Bounds, Element, ElementId, InteractiveElement,
InteractiveElementState, Interactivity, LayoutId, Pixels, Point, Render, RenderOnce, Size,
InteractiveElementState, Interactivity, IntoElement, LayoutId, Pixels, Point, Render, Size,
StyleRefinement, Styled, View, ViewContext, WindowContext,
};
use smallvec::SmallVec;
@ -18,7 +18,7 @@ pub fn uniform_list<I, R, V>(
) -> UniformList
where
I: Into<ElementId>,
R: RenderOnce,
R: IntoElement,
V: Render,
{
let id = id.into();
@ -29,7 +29,7 @@ where
view.update(cx, |this, cx| {
f(this, range, cx)
.into_iter()
.map(|component| component.render_into_any())
.map(|component| component.into_any_element())
.collect()
})
};
@ -243,14 +243,14 @@ impl Element for UniformList {
}
}
impl RenderOnce for UniformList {
impl IntoElement for UniformList {
type Element = Self;
fn element_id(&self) -> Option<crate::ElementId> {
Some(self.id.clone())
}
fn render_once(self) -> Self::Element {
fn into_element(self) -> Self::Element {
self
}
}

View file

@ -1,6 +1,6 @@
use crate::{
div, point, Div, Element, FocusHandle, Keystroke, Modifiers, Pixels, Point, Render, RenderOnce,
ViewContext,
div, point, Div, Element, FocusHandle, IntoElement, Keystroke, Modifiers, Pixels, Point,
Render, ViewContext,
};
use smallvec::SmallVec;
use std::{any::Any, fmt::Debug, marker::PhantomData, ops::Deref, path::PathBuf};
@ -64,7 +64,7 @@ pub struct Drag<S, R, V, E>
where
R: Fn(&mut V, &mut ViewContext<V>) -> E,
V: 'static,
E: RenderOnce,
E: IntoElement,
{
pub state: S,
pub render_drag_handle: R,
@ -286,8 +286,8 @@ pub struct FocusEvent {
#[cfg(test)]
mod test {
use crate::{
self as gpui, div, Div, FocusHandle, InteractiveElement, KeyBinding, Keystroke,
ParentElement, Render, RenderOnce, Stateful, TestAppContext, VisualContext,
self as gpui, div, Div, FocusHandle, InteractiveElement, IntoElement, KeyBinding,
Keystroke, ParentElement, Render, Stateful, TestAppContext, VisualContext,
};
struct TestView {
@ -315,7 +315,7 @@ mod test {
div()
.key_context("nested")
.track_focus(&self.focus_handle)
.render_once(),
.into_element(),
),
)
}

View file

@ -1,5 +1,5 @@
pub use crate::{
BorrowAppContext, BorrowWindow, Component, Context, Element, FocusableElement,
InteractiveElement, ParentElement, Refineable, Render, RenderOnce, StatefulInteractiveElement,
Styled, VisualContext,
BorrowAppContext, BorrowWindow, Context, Element, FocusableElement, InteractiveElement,
IntoElement, ParentElement, Refineable, Render, RenderOnce, StatefulInteractiveElement, Styled,
VisualContext,
};

View file

@ -1,7 +1,7 @@
use crate::{
private::Sealed, AnyElement, AnyModel, AnyWeakModel, AppContext, AvailableSpace, BorrowWindow,
Bounds, Element, ElementId, Entity, EntityId, Flatten, FocusHandle, FocusableView, LayoutId,
Model, Pixels, Point, Render, RenderOnce, Size, ViewContext, VisualContext, WeakModel,
Bounds, Element, ElementId, Entity, EntityId, Flatten, FocusHandle, FocusableView, IntoElement,
LayoutId, Model, Pixels, Point, Render, Size, ViewContext, VisualContext, WeakModel,
WindowContext,
};
use anyhow::{Context, Result};
@ -244,26 +244,26 @@ impl Element for AnyView {
}
}
impl<V: 'static + Render> RenderOnce for View<V> {
impl<V: 'static + Render> IntoElement for View<V> {
type Element = View<V>;
fn element_id(&self) -> Option<ElementId> {
Some(self.model.entity_id.into())
}
fn render_once(self) -> Self::Element {
fn into_element(self) -> Self::Element {
self
}
}
impl RenderOnce for AnyView {
impl IntoElement for AnyView {
type Element = Self;
fn element_id(&self) -> Option<ElementId> {
Some(self.model.entity_id.into())
}
fn render_once(self) -> Self::Element {
fn into_element(self) -> Self::Element {
self
}
}