Skip to content

armgit / zed

Public Imported from GitHub
0

Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.

Merge branch 'main' into callback-handles

Conrad Irwind0dd44faad9d
diff --cc crates/gpui2/src/app.rs
index ca96ba210ec,ff601db3726..f6451297065
--- a/crates/gpui2/src/app.rs
+++ b/crates/gpui2/src/app.rs
@@@ -424,7 -424,7 +424,7 @@@ impl AppContext
/// Opens a new window with the given option and the root view returned by the given function.
/// The function is invoked with a `WindowContext`, which can be used to interact with window-specific
/// functionality.
- pub fn open_window<V: Render>(
- pub fn open_window<V: 'static + Render<V>>(
++ pub fn open_window<V: 'static + Render>(
&mut self,
options: crate::WindowOptions,
build_root_view: impl FnOnce(&mut WindowContext) -> View<V>,
diff --cc crates/gpui2/src/app/async_context.rs
index 65c006b0621,3fdff1102ad..cc3b0ace57b
--- a/crates/gpui2/src/app/async_context.rs
+++ b/crates/gpui2/src/app/async_context.rs
@@@ -115,7 -115,7 +115,7 @@@ impl AsyncAppContext
build_root_view: impl FnOnce(&mut WindowContext) -> View<V>,
) -> Result<WindowHandle<V>>
where
- V: Render,
- V: 'static + Render<V>,
++ V: 'static + Render,
{
let app = self
.app
@@@ -306,7 -306,7 +306,7 @@@ impl VisualContext for AsyncWindowConte
build_view: impl FnOnce(&mut ViewContext<'_, V>) -> V,
) -> Self::Result<View<V>>
where
- V: Render,
- V: 'static + Render<V>,
++ V: 'static + Render,
{
self.window
.update(self, |_, cx| cx.replace_root_view(build_view))
diff --cc crates/gpui2/src/app/test_context.rs
index 8687646b4b7,ee1c4602ee1..8fad38c1205
--- a/crates/gpui2/src/app/test_context.rs
+++ b/crates/gpui2/src/app/test_context.rs
@@@ -126,7 -126,7 +126,7 @@@ impl TestAppContext
pub fn add_window<F, V>(&mut self, build_window: F) -> WindowHandle<V>
where
F: FnOnce(&mut ViewContext<V>) -> V,
- V: Render,
- V: 'static + Render<V>,
++ V: 'static + Render,
{
let mut cx = self.app.borrow_mut();
cx.open_window(WindowOptions::default(), |cx| cx.build_view(build_window))
@@@ -143,7 -143,7 +143,7 @@@
pub fn add_window_view<F, V>(&mut self, build_window: F) -> (View<V>, &mut VisualTestContext)
where
F: FnOnce(&mut ViewContext<V>) -> V,
- V: Render,
- V: 'static + Render<V>,
++ V: 'static + Render,
{
let mut cx = self.app.borrow_mut();
let window = cx.open_window(WindowOptions::default(), |cx| cx.build_view(build_window));
@@@ -591,7 -591,7 +591,7 @@@ impl<'a> VisualContext for VisualTestCo
build_view: impl FnOnce(&mut ViewContext<'_, V>) -> V,
) -> Self::Result<View<V>>
where
- V: Render,
- V: 'static + Render<V>,
++ V: 'static + Render,
{
self.window
.update(self.cx, |_, cx| cx.replace_root_view(build_view))
diff --cc crates/gpui2/src/element.rs
index d36f5f1635f,990de68b063..5cd015503d2
--- a/crates/gpui2/src/element.rs
+++ b/crates/gpui2/src/element.rs
@@@ -1,71 -1,198 +1,172 @@@
use crate::{
- AvailableSpace, BorrowWindow, Bounds, ElementId, LayoutId, Pixels, Point, Size, WindowContext,
+ AvailableSpace, BorrowWindow, Bounds, ElementId, LayoutId, Pixels, Point, Size, ViewContext,
++ WindowContext,
};
use derive_more::{Deref, DerefMut};
pub(crate) use smallvec::SmallVec;
- use std::{any::Any, fmt::Debug, mem};
-use std::{any::Any, fmt::Debug, marker::PhantomData};
++use std::{any::Any, fmt::Debug};
- pub trait Element {
- type ElementState: 'static;
-pub trait Render<V: 'static>: 'static + Sized {
- type Element: Element<V> + 'static;
++pub trait Render: 'static + Sized {
++ type Element: Element + 'static;
+
- fn render(&mut self, cx: &mut ViewContext<V>) -> Self::Element;
++ fn render(&mut self, cx: &mut ViewContext<Self>) -> Self::Element;
+ }
+
-pub trait RenderOnce<V: 'static>: Sized {
- type Element: Element<V> + 'static;
++pub trait RenderOnce: Sized {
++ type Element: Element + 'static;
fn element_id(&self) -> Option<ElementId>;
- fn layout(
- &mut self,
- element_state: Option<Self::ElementState>,
- cx: &mut WindowContext,
- ) -> (LayoutId, Self::ElementState);
+ fn render_once(self) -> Self::Element;
- fn paint(
- &mut self,
- bounds: Bounds<Pixels>,
- element_state: &mut Self::ElementState,
- cx: &mut WindowContext,
- );
- fn render_into_any(self) -> AnyElement<V> {
++ fn render_into_any(self) -> AnyElement {
+ self.render_once().into_any()
+ }
fn draw<T, R>(
self,
origin: Point<Pixels>,
available_space: Size<T>,
- view_state: &mut V,
- cx: &mut ViewContext<V>,
- f: impl FnOnce(&mut <Self::Element as Element<V>>::State, &mut ViewContext<V>) -> R,
+ cx: &mut WindowContext,
- f: impl FnOnce(&Self::ElementState, &mut WindowContext) -> R,
++ f: impl FnOnce(&mut <Self::Element as Element>::State, &mut WindowContext) -> R,
) -> R
where
- Self: Sized,
T: Clone + Default + Debug + Into<AvailableSpace>,
{
- let mut element = RenderedElement {
- element: self,
- phase: ElementRenderPhase::Start,
+ let element = self.render_once();
+ let element_id = element.element_id();
+ let element = DrawableElement {
+ element: Some(element),
+ phase: ElementDrawPhase::Start,
};
- 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)
- let frame_state = DrawableElement::draw(
- element,
- origin,
- available_space.map(Into::into),
- view_state,
- cx,
- );
++
++ let frame_state =
++ DrawableElement::draw(element, origin, available_space.map(Into::into), cx);
+
+ if let Some(mut frame_state) = frame_state {
+ f(&mut frame_state, cx)
+ } else {
+ cx.with_element_state(element_id.unwrap(), |element_state, cx| {
+ let mut element_state = element_state.unwrap();
+ let result = f(&mut element_state, cx);
+ (result, element_state)
+ })
+ }
+ }
+
+ fn map<U>(self, f: impl FnOnce(Self) -> U) -> U
+ where
+ Self: Sized,
- U: RenderOnce<V>,
++ U: RenderOnce,
+ {
+ 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 {
- 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)
- })
+ this
}
- } else {
- unreachable!()
+ })
+ }
+ }
+
-pub trait Element<V: 'static>: 'static + RenderOnce<V> {
++pub trait Element: 'static + RenderOnce {
+ type State: 'static;
+
+ fn layout(
+ &mut self,
- view_state: &mut V,
- element_state: Option<Self::State>,
- cx: &mut ViewContext<V>,
++ state: Option<Self::State>,
++ cx: &mut WindowContext,
+ ) -> (LayoutId, Self::State);
+
- fn paint(
- self,
- bounds: Bounds<Pixels>,
- view_state: &mut V,
- element_state: &mut Self::State,
- cx: &mut ViewContext<V>,
- );
++ fn paint(self, bounds: Bounds<Pixels>, state: &mut Self::State, cx: &mut WindowContext);
+
- fn into_any(self) -> AnyElement<V> {
++ fn into_any(self) -> AnyElement {
+ AnyElement::new(self)
+ }
+ }
+
-pub trait Component<V: 'static>: 'static {
- type Rendered: RenderOnce<V>;
++pub trait Component: 'static {
++ type Rendered: RenderOnce;
+
- fn render(self, view: &mut V, cx: &mut ViewContext<V>) -> Self::Rendered;
++ fn render(self, cx: &mut WindowContext) -> Self::Rendered;
+ }
+
-pub struct CompositeElement<V, C> {
++pub struct CompositeElement<C> {
+ component: Option<C>,
- view_type: PhantomData<V>,
+ }
+
-pub struct CompositeElementState<V: 'static, C: Component<V>> {
- rendered_element: Option<<C::Rendered as RenderOnce<V>>::Element>,
- rendered_element_state: <<C::Rendered as RenderOnce<V>>::Element as Element<V>>::State,
++pub struct CompositeElementState<C: Component> {
++ rendered_element: Option<<C::Rendered as RenderOnce>::Element>,
++ rendered_element_state: <<C::Rendered as RenderOnce>::Element as Element>::State,
+ }
+
-impl<V, C> CompositeElement<V, C> {
++impl<C> CompositeElement<C> {
+ pub fn new(component: C) -> Self {
+ CompositeElement {
+ component: Some(component),
- view_type: PhantomData,
}
}
}
-impl<V: 'static, C: Component<V>> Element<V> for CompositeElement<V, C> {
- type State = CompositeElementState<V, C>;
++impl<C: Component> Element for CompositeElement<C> {
++ type State = CompositeElementState<C>;
+
+ fn layout(
+ &mut self,
- view: &mut V,
+ state: Option<Self::State>,
- cx: &mut ViewContext<V>,
++ cx: &mut WindowContext,
+ ) -> (LayoutId, Self::State) {
- let mut element = self
- .component
- .take()
- .unwrap()
- .render(view, cx)
- .render_once();
- let (layout_id, state) = element.layout(view, state.map(|s| s.rendered_element_state), cx);
++ let mut element = self.component.take().unwrap().render(cx).render_once();
++ let (layout_id, state) = element.layout(state.map(|s| s.rendered_element_state), cx);
+ let state = CompositeElementState {
+ rendered_element: Some(element),
+ rendered_element_state: state,
+ };
+ (layout_id, state)
+ }
+
- fn paint(
- self,
- bounds: Bounds<Pixels>,
- view: &mut V,
- state: &mut Self::State,
- cx: &mut ViewContext<V>,
- ) {
- state.rendered_element.take().unwrap().paint(
- bounds,
- view,
- &mut state.rendered_element_state,
- cx,
- );
++ fn paint(self, bounds: Bounds<Pixels>, state: &mut Self::State, cx: &mut WindowContext) {
++ state
++ .rendered_element
++ .take()
++ .unwrap()
++ .paint(bounds, &mut state.rendered_element_state, cx);
+ }
+ }
+
-impl<V: 'static, C: Component<V>> RenderOnce<V> for CompositeElement<V, C> {
++impl<C: Component> RenderOnce for CompositeElement<C> {
+ type Element = Self;
+
+ fn element_id(&self) -> Option<ElementId> {
+ None
+ }
+
+ fn render_once(self) -> Self::Element {
+ self
+ }
+ }
+
#[derive(Deref, DerefMut, Default, Clone, Debug, Eq, PartialEq, Hash)]
pub struct GlobalElementId(SmallVec<[ElementId; 32]>);
- pub trait ParentComponent {
-pub trait ParentElement<V: 'static> {
- fn children_mut(&mut self) -> &mut SmallVec<[AnyElement<V>; 2]>;
++pub trait ParentElement {
+ fn children_mut(&mut self) -> &mut SmallVec<[AnyElement; 2]>;
- fn child(mut self, child: impl Component) -> Self
- fn child(mut self, child: impl RenderOnce<V>) -> Self
++ fn child(mut self, child: impl RenderOnce) -> Self
where
Self: Sized,
{
@@@ -73,7 -200,7 +174,7 @@@
self
}
- fn children(mut self, iter: impl IntoIterator<Item = impl Component>) -> Self
- fn children(mut self, children: impl IntoIterator<Item = impl RenderOnce<V>>) -> Self
++ fn children(mut self, children: impl IntoIterator<Item = impl RenderOnce>) -> Self
where
Self: Sized,
{
@@@ -83,15 -213,16 +187,19 @@@
}
}
-trait ElementObject<V> {
+trait ElementObject {
fn element_id(&self) -> Option<ElementId>;
- fn layout(&mut self, view_state: &mut V, cx: &mut ViewContext<V>) -> LayoutId;
- fn paint(&mut self, view_state: &mut V, cx: &mut ViewContext<V>);
++
+ fn layout(&mut self, cx: &mut WindowContext) -> LayoutId;
++
+ fn paint(&mut self, cx: &mut WindowContext);
++
fn measure(
&mut self,
available_space: Size<AvailableSpace>,
- view_state: &mut V,
- cx: &mut ViewContext<V>,
+ cx: &mut WindowContext,
) -> Size<Pixels>;
++
fn draw(
&mut self,
origin: Point<Pixels>,
@@@ -100,9 -232,9 +208,9 @@@
);
}
- struct RenderedElement<E: Element> {
- element: E,
- phase: ElementRenderPhase<E::ElementState>,
-pub struct DrawableElement<V: 'static, E: Element<V>> {
++pub struct DrawableElement<E: Element> {
+ element: Option<E>,
+ phase: ElementDrawPhase<E::State>,
}
#[derive(Default)]
@@@ -123,77 -252,79 +228,74 @@@ enum ElementDrawPhase<S>
},
}
- /// Internal struct that wraps an element to store Layout and ElementState 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> {
+ /// A wrapper around an implementer of [Element] that allows it to be drawn in a window.
-impl<V, E: Element<V>> DrawableElement<V, E> {
++impl<E: Element> DrawableElement<E> {
fn new(element: E) -> Self {
- RenderedElement {
- element,
- phase: ElementRenderPhase::Start,
+ DrawableElement {
+ element: Some(element),
+ phase: ElementDrawPhase::Start,
}
}
- }
- impl<E> ElementObject for RenderedElement<E>
- where
- E: Element,
- E::ElementState: 'static,
- {
fn element_id(&self) -> Option<ElementId> {
- self.element.element_id()
+ self.element.as_ref()?.element_id()
}
- fn layout(&mut self, state: &mut V, cx: &mut ViewContext<V>) -> LayoutId {
+ fn layout(&mut self, cx: &mut WindowContext) -> LayoutId {
- let (layout_id, frame_state) = match mem::take(&mut self.phase) {
- ElementRenderPhase::Start => {
- if let Some(id) = self.element.element_id() {
- let layout_id = cx.with_element_state(id, |element_state, cx| {
- self.element.layout(element_state, cx)
- });
- (layout_id, None)
- } else {
- let (layout_id, frame_state) = self.element.layout(None, cx);
- (layout_id, Some(frame_state))
- }
- }
- ElementRenderPhase::LayoutRequested { .. }
- | ElementRenderPhase::LayoutComputed { .. }
- | ElementRenderPhase::Painted { .. } => {
- panic!("element rendered twice")
- }
+ let (layout_id, frame_state) = if let Some(id) = self.element.as_ref().unwrap().element_id()
+ {
+ let layout_id = cx.with_element_state(id, |element_state, cx| {
- self.element
- .as_mut()
- .unwrap()
- .layout(state, element_state, cx)
++ self.element.as_mut().unwrap().layout(element_state, cx)
+ });
+ (layout_id, None)
+ } else {
- let (layout_id, frame_state) = self.element.as_mut().unwrap().layout(state, None, cx);
++ let (layout_id, frame_state) = self.element.as_mut().unwrap().layout(None, cx);
+ (layout_id, Some(frame_state))
};
- self.phase = ElementRenderPhase::LayoutRequested {
+ self.phase = ElementDrawPhase::LayoutRequested {
layout_id,
frame_state,
};
layout_id
}
- fn paint(&mut self, cx: &mut WindowContext) {
- self.phase = match mem::take(&mut self.phase) {
- ElementRenderPhase::LayoutRequested {
- fn paint(mut self, view_state: &mut V, cx: &mut ViewContext<V>) -> Option<E::State> {
++ fn paint(mut self, cx: &mut WindowContext) -> Option<E::State> {
+ match self.phase {
+ ElementDrawPhase::LayoutRequested {
layout_id,
- mut frame_state,
+ frame_state,
}
- | ElementRenderPhase::LayoutComputed {
+ | ElementDrawPhase::LayoutComputed {
layout_id,
- mut frame_state,
+ frame_state,
..
} => {
let bounds = cx.layout_bounds(layout_id);
- if let Some(id) = self.element.element_id() {
- cx.with_element_state(id, |element_state, cx| {
+
+ if let Some(mut frame_state) = frame_state {
+ self.element
+ .take()
+ .unwrap()
- .paint(bounds, view_state, &mut frame_state, cx);
++ .paint(bounds, &mut frame_state, cx);
+ Some(frame_state)
+ } else {
+ let element_id = self
+ .element
+ .as_ref()
+ .unwrap()
+ .element_id()
+ .expect("if we don't have frame state, we should have element state");
+ cx.with_element_state(element_id, |element_state, cx| {
let mut element_state = element_state.unwrap();
- self.element.paint(bounds, &mut element_state, cx);
- self.element.take().unwrap().paint(
- bounds,
- view_state,
- &mut element_state,
- cx,
- );
++ self.element
++ .take()
++ .unwrap()
++ .paint(bounds, &mut element_state, cx);
((), element_state)
});
- } else {
- self.element
- .paint(bounds, frame_state.as_mut().unwrap(), cx);
+ None
}
- ElementRenderPhase::Painted { frame_state }
}
_ => panic!("must call layout before paint"),
@@@ -203,10 -334,11 +305,10 @@@
fn measure(
&mut self,
available_space: Size<AvailableSpace>,
- view_state: &mut V,
- cx: &mut ViewContext<V>,
+ cx: &mut WindowContext,
) -> Size<Pixels> {
- if matches!(&self.phase, ElementRenderPhase::Start) {
+ if matches!(&self.phase, ElementDrawPhase::Start) {
- self.layout(view_state, cx);
+ self.layout(cx);
}
let layout_id = match &mut self.phase {
@@@ -241,37 -373,117 +343,105 @@@
}
fn draw(
- &mut self,
+ mut self,
origin: Point<Pixels>,
available_space: Size<AvailableSpace>,
- view_state: &mut V,
- cx: &mut ViewContext<V>,
+ cx: &mut WindowContext,
- ) {
+ ) -> Option<E::State> {
- self.measure(available_space, view_state, cx);
- cx.with_absolute_element_offset(origin, |cx| self.paint(view_state, cx))
+ self.measure(available_space, cx);
+ cx.with_absolute_element_offset(origin, |cx| self.paint(cx))
}
}
-// impl<V: 'static, E: Element<V>> Element<V> for DrawableElement<V, E> {
-// type State = <E::Element as Element<V>>::State;
++// impl<V: 'static, E: Element> Element for DrawableElement<V, E> {
++// type State = <E::Element as Element>::State;
+
+ // fn layout(
+ // &mut self,
-// view_state: &mut V,
+ // element_state: Option<Self::State>,
-// cx: &mut ViewContext<V>,
++// cx: &mut WindowContext,
+ // ) -> (LayoutId, Self::State) {
+
+ // }
+
+ // fn paint(
+ // self,
+ // bounds: Bounds<Pixels>,
-// view_state: &mut V,
+ // element_state: &mut Self::State,
-// cx: &mut ViewContext<V>,
++// cx: &mut WindowContext,
+ // ) {
+ // todo!()
+ // }
+ // }
+
-// impl<V: 'static, E: 'static + Element<V>> RenderOnce<V> for DrawableElement<V, E> {
++// impl<V: 'static, E: 'static + Element> RenderOnce for DrawableElement<V, E> {
+ // type Element = Self;
+
+ // fn element_id(&self) -> Option<ElementId> {
+ // self.element.as_ref()?.element_id()
+ // }
+
+ // fn render_once(self) -> Self::Element {
+ // self
+ // }
+ // }
+
-impl<V, E> ElementObject<V> for Option<DrawableElement<V, E>>
++impl<E> ElementObject for Option<DrawableElement<E>>
+ where
- E: Element<V>,
++ E: Element,
+ E::State: 'static,
+ {
+ fn element_id(&self) -> Option<ElementId> {
+ self.as_ref().unwrap().element_id()
+ }
+
- fn layout(&mut self, view_state: &mut V, cx: &mut ViewContext<V>) -> LayoutId {
- DrawableElement::layout(self.as_mut().unwrap(), view_state, cx)
++ fn layout(&mut self, cx: &mut WindowContext) -> LayoutId {
++ DrawableElement::layout(self.as_mut().unwrap(), cx)
+ }
+
- fn paint(&mut self, view_state: &mut V, cx: &mut ViewContext<V>) {
- DrawableElement::paint(self.take().unwrap(), view_state, cx);
++ fn paint(&mut self, cx: &mut WindowContext) {
++ DrawableElement::paint(self.take().unwrap(), cx);
+ }
+
+ fn measure(
+ &mut self,
+ available_space: Size<AvailableSpace>,
- view_state: &mut V,
- cx: &mut ViewContext<V>,
++ cx: &mut WindowContext,
+ ) -> Size<Pixels> {
- DrawableElement::measure(self.as_mut().unwrap(), available_space, view_state, cx)
++ DrawableElement::measure(self.as_mut().unwrap(), available_space, cx)
+ }
+
+ fn draw(
+ &mut self,
+ origin: Point<Pixels>,
+ available_space: Size<AvailableSpace>,
- view_state: &mut V,
- cx: &mut ViewContext<V>,
++ cx: &mut WindowContext,
+ ) {
- DrawableElement::draw(
- self.take().unwrap(),
- origin,
- available_space,
- view_state,
- cx,
- );
++ DrawableElement::draw(self.take().unwrap(), origin, available_space, cx);
+ }
+ }
+
-pub struct AnyElement<V>(Box<dyn ElementObject<V>>);
+pub struct AnyElement(Box<dyn ElementObject>);
-impl<V: 'static> AnyElement<V> {
+impl AnyElement {
pub fn new<E>(element: E) -> Self
where
- V: 'static,
- E: 'static + Element<V>,
+ E: 'static + Element,
- E::ElementState: Any,
+ E::State: Any,
{
- AnyElement(Box::new(RenderedElement::new(element)))
- AnyElement(Box::new(Some(DrawableElement::new(element))) as Box<dyn ElementObject<V>>)
++ AnyElement(Box::new(Some(DrawableElement::new(element))) as Box<dyn ElementObject>)
}
pub fn element_id(&self) -> Option<ElementId> {
self.0.element_id()
}
- pub fn layout(&mut self, view_state: &mut V, cx: &mut ViewContext<V>) -> LayoutId {
- self.0.layout(view_state, cx)
+ pub fn layout(&mut self, cx: &mut WindowContext) -> LayoutId {
+ self.0.layout(cx)
}
- pub fn paint(&mut self, cx: &mut WindowContext) {
- pub fn paint(mut self, view_state: &mut V, cx: &mut ViewContext<V>) {
- self.0.paint(view_state, cx)
++ pub fn paint(mut self, cx: &mut WindowContext) {
+ self.0.paint(cx)
}
/// Initializes this element and performs layout within the given available space to determine its size.
@@@ -285,101 -498,101 +455,91 @@@
/// Initializes this element and performs layout in the available space, then paints it at the given origin.
pub fn draw(
- &mut self,
+ mut self,
origin: Point<Pixels>,
available_space: Size<AvailableSpace>,
- view_state: &mut V,
- cx: &mut ViewContext<V>,
+ cx: &mut WindowContext,
) {
- self.0.draw(origin, available_space, view_state, cx)
+ self.0.draw(origin, available_space, cx)
}
- }
-
- pub trait Component {
- fn render(self) -> AnyElement;
- fn map<U>(self, f: impl FnOnce(Self) -> U) -> U
- where
- Self: Sized,
- 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
- }
- })
- }
- }
-
- impl Component for AnyElement {
- fn render(self) -> AnyElement {
- self
+ /// Converts this `AnyElement` into a trait object that can be stored and manipulated.
- pub fn into_any(self) -> AnyElement<V> {
++ pub fn into_any(self) -> AnyElement {
+ AnyElement::new(self)
}
}
- impl<E, F> Element for Option<F>
- where
- E: 'static + Component,
- F: FnOnce(&mut WindowContext) -> E + 'static,
- {
- type ElementState = AnyElement;
-
- fn element_id(&self) -> Option<ElementId> {
- None
- }
-impl<V: 'static> Element<V> for AnyElement<V> {
++impl Element for AnyElement {
+ type State = ();
fn layout(
&mut self,
- _: Option<Self::ElementState>,
- view_state: &mut V,
+ _: Option<Self::State>,
- cx: &mut ViewContext<V>,
+ cx: &mut WindowContext,
- ) -> (LayoutId, Self::ElementState) {
- let render = self.take().unwrap();
- let mut rendered_element = (render)(cx).render();
- let layout_id = rendered_element.layout(cx);
- (layout_id, rendered_element)
+ ) -> (LayoutId, Self::State) {
- let layout_id = self.layout(view_state, cx);
++ let layout_id = self.layout(cx);
+ (layout_id, ())
}
-- fn paint(
- &mut self,
- self,
-- _bounds: Bounds<Pixels>,
- rendered_element: &mut Self::ElementState,
- cx: &mut WindowContext,
- view_state: &mut V,
- _: &mut Self::State,
- cx: &mut ViewContext<V>,
-- ) {
- rendered_element.paint(cx)
- self.paint(view_state, cx);
++ fn paint(self, _: Bounds<Pixels>, _: &mut Self::State, cx: &mut WindowContext) {
++ self.paint(cx);
}
}
- impl<E, F> Component for Option<F>
- where
- E: 'static + Component,
- F: FnOnce(&mut WindowContext) -> E + 'static,
- {
- fn render(self) -> AnyElement {
- AnyElement::new(self)
-impl<V: 'static> RenderOnce<V> for AnyElement<V> {
++impl RenderOnce for AnyElement {
+ type Element = Self;
+
+ fn element_id(&self) -> Option<ElementId> {
+ AnyElement::element_id(self)
}
- }
- impl<E, F> Component for F
- where
- E: 'static + Component,
- F: FnOnce(&mut WindowContext) -> E + 'static,
- {
- fn render(self) -> AnyElement {
- AnyElement::new(Some(self))
+ fn render_once(self) -> Self::Element {
+ self
}
}
+
-// impl<V, E, F> Element<V> for Option<F>
++// impl<V, E, F> Element for Option<F>
+ // where
+ // V: 'static,
-// E: Element<V>,
-// F: FnOnce(&mut V, &mut ViewContext<'_, V>) -> E + 'static,
++// E: Element,
++// F: FnOnce(&mut V, &mut WindowContext<'_, V>) -> E + 'static,
+ // {
-// type State = Option<AnyElement<V>>;
++// type State = Option<AnyElement>;
+
+ // fn element_id(&self) -> Option<ElementId> {
+ // None
+ // }
+
+ // fn layout(
+ // &mut self,
-// view_state: &mut V,
+ // _: Option<Self::State>,
-// cx: &mut ViewContext<V>,
++// cx: &mut WindowContext,
+ // ) -> (LayoutId, Self::State) {
+ // let render = self.take().unwrap();
+ // let mut element = (render)(view_state, cx).into_any();
+ // let layout_id = element.layout(view_state, cx);
+ // (layout_id, Some(element))
+ // }
+
+ // fn paint(
+ // self,
+ // _bounds: Bounds<Pixels>,
-// view_state: &mut V,
+ // rendered_element: &mut Self::State,
-// cx: &mut ViewContext<V>,
++// cx: &mut WindowContext,
+ // ) {
+ // rendered_element.take().unwrap().paint(view_state, cx);
+ // }
+ // }
+
-// impl<V, E, F> RenderOnce<V> for Option<F>
++// impl<V, E, F> RenderOnce for Option<F>
+ // where
+ // V: 'static,
-// E: Element<V>,
-// F: FnOnce(&mut V, &mut ViewContext<V>) -> E + 'static,
++// E: Element,
++// F: FnOnce(&mut V, &mut WindowContext) -> E + 'static,
+ // {
+ // type Element = Self;
+
+ // fn render(self) -> Self::Element {
+ // self
+ // }
+ // }
diff --cc crates/gpui2/src/elements/div.rs
index 6587f22552d,8838e5f6edf..573ac833979
--- a/crates/gpui2/src/elements/div.rs
+++ b/crates/gpui2/src/elements/div.rs
@@@ -1,10 -1,9 +1,10 @@@
use crate::{
point, px, Action, AnyDrag, AnyElement, AnyTooltip, AnyView, AppContext, BorrowAppContext,
- BorrowWindow, Bounds, CallbackHandle, ClickEvent, Component, ConstructorHandle, DispatchPhase,
- Element, ElementId, FocusEvent, FocusHandle, KeyContext, KeyDownEvent, KeyUpEvent, LayoutId,
- MouseButton, MouseDownEvent, MouseMoveEvent, MouseUpEvent, ParentComponent, Pixels, Point,
- Render, ScrollWheelEvent, SharedString, Size, Style, StyleRefinement, Styled, Task, View,
- Visibility, WindowContext,
- 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, ViewContext, Visibility,
++ BorrowWindow, Bounds, CallbackHandle, ClickEvent, ConstructorHandle, 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,
};
use collections::HashMap;
use refineable::Refineable;
@@@ -28,8 -28,8 +28,8 @@@ pub struct GroupStyle
pub style: StyleRefinement,
}
- pub trait InteractiveComponent: Sized + Element {
-pub trait InteractiveElement<V: 'static>: Sized + Element<V> {
- fn interactivity(&mut self) -> &mut Interactivity<V>;
++pub trait InteractiveElement: Sized + Element {
+ fn interactivity(&mut self) -> &mut Interactivity;
fn group(mut self, group: impl Into<SharedString>) -> Self {
self.interactivity().group = Some(group.into());
@@@ -319,10 -314,13 +319,10 @@@
}
}
- pub trait StatefulInteractiveComponent: InteractiveComponent {
-pub trait StatefulInteractiveElement<V: 'static, E: Element<V>>: InteractiveElement<V> {
- fn focusable(mut self) -> Focusable<V, Self> {
++pub trait StatefulInteractiveElement: InteractiveElement {
+ fn focusable(mut self) -> Focusable<Self> {
self.interactivity().focusable = true;
- Focusable {
- element: self,
- view_type: PhantomData,
- }
+ Focusable { element: self }
}
fn overflow_scroll(mut self) -> Self {
@@@ -421,7 -425,7 +421,7 @@@
}
}
- pub trait FocusableComponent: InteractiveComponent {
-pub trait FocusableElement<V: 'static>: InteractiveElement<V> {
++pub trait FocusableElement: InteractiveElement {
fn focus(mut self, f: impl FnOnce(StyleRefinement) -> StyleRefinement) -> Self
where
Self: Sized,
@@@ -563,30 -587,27 +563,26 @@@ impl Styled for Div
}
}
- impl InteractiveComponent for Div {
-impl<V: 'static> InteractiveElement<V> for Div<V> {
- fn interactivity(&mut self) -> &mut Interactivity<V> {
++impl InteractiveElement for Div {
+ fn interactivity(&mut self) -> &mut Interactivity {
&mut self.interactivity
}
}
- impl ParentComponent for Div {
-impl<V: 'static> ParentElement<V> for Div<V> {
- fn children_mut(&mut self) -> &mut SmallVec<[AnyElement<V>; 2]> {
++impl ParentElement for Div {
+ fn children_mut(&mut self) -> &mut SmallVec<[AnyElement; 2]> {
&mut self.children
}
}
-impl<V: 'static> Element<V> for Div<V> {
+impl Element for Div {
- type ElementState = DivState;
-
- fn element_id(&self) -> Option<ElementId> {
- self.interactivity.element_id.clone()
- }
+ type State = DivState;
fn layout(
&mut self,
- element_state: Option<Self::ElementState>,
- view_state: &mut V,
+ element_state: Option<Self::State>,
- cx: &mut ViewContext<V>,
+ cx: &mut WindowContext,
- ) -> (LayoutId, Self::ElementState) {
+ ) -> (LayoutId, Self::State) {
let mut child_layout_ids = SmallVec::new();
let mut interactivity = mem::take(&mut self.interactivity);
let (layout_id, interactive_state) = interactivity.layout(
@@@ -614,10 -635,11 +610,10 @@@
}
fn paint(
- &mut self,
+ self,
bounds: Bounds<Pixels>,
- element_state: &mut Self::ElementState,
- view_state: &mut V,
+ element_state: &mut Self::State,
- cx: &mut ViewContext<V>,
+ cx: &mut WindowContext,
) {
let mut child_min = point(Pixels::MAX, Pixels::MAX);
let mut child_max = Point::default();
@@@ -653,8 -674,8 +648,8 @@@
cx.with_text_style(style.text_style().cloned(), |cx| {
cx.with_content_mask(style.overflow_mask(bounds), |cx| {
cx.with_element_offset(scroll_offset, |cx| {
- for child in &mut self.children {
+ for child in self.children {
- child.paint(view_state, cx);
+ child.paint(cx);
}
})
})
@@@ -666,10 -687,15 +661,15 @@@
}
}
- impl Component for Div {
- fn render(self) -> AnyElement {
- AnyElement::new(self)
-impl<V: 'static> RenderOnce<V> for Div<V> {
++impl RenderOnce for Div {
+ type Element = Self;
+
+ fn element_id(&self) -> Option<ElementId> {
+ self.interactivity.element_id.clone()
+ }
+
+ fn render_once(self) -> Self::Element {
+ self
}
}
@@@ -1050,9 -1079,11 +1050,9 @@@ impl Interactivity
}
if let Some(focus_handle) = element_state.focus_handle.as_ref() {
- for listener in self.focus_listeners.drain(..) {
+ for listener in self.focus_listeners {
let focus_handle = focus_handle.clone();
- cx.on_focus_changed(move |view, event, cx| {
- listener(view, &focus_handle, event, cx)
- });
+ cx.on_focus_changed(move |event, cx| listener(&focus_handle, event, cx));
}
}
@@@ -1228,25 -1259,31 +1228,25 @@@ impl GroupBounds
}
}
-pub struct Focusable<V, E> {
+pub struct Focusable<E> {
element: E,
- view_type: PhantomData<V>,
}
- impl<E: InteractiveComponent> FocusableComponent for Focusable<E> {}
-impl<V: 'static + Render<V>, E: InteractiveElement<V>> FocusableElement<V> for Focusable<V, E> {}
++impl<E: InteractiveElement> FocusableElement for Focusable<E> {}
- impl<E> InteractiveComponent for Focusable<E>
-impl<V, E> InteractiveElement<V> for Focusable<V, E>
++impl<E> InteractiveElement for Focusable<E>
where
- E: InteractiveComponent,
- V: 'static + Render<V>,
- E: InteractiveElement<V>,
++ E: InteractiveElement,
{
- fn interactivity(&mut self) -> &mut Interactivity<V> {
+ fn interactivity(&mut self) -> &mut Interactivity {
self.element.interactivity()
}
}
- impl<E: StatefulInteractiveComponent> StatefulInteractiveComponent for Focusable<E> {}
-impl<V: 'static + Render<V>, E: StatefulInteractiveElement<V, E>> StatefulInteractiveElement<V, E>
- for Focusable<V, E>
-{
-}
++impl<E: StatefulInteractiveElement> StatefulInteractiveElement for Focusable<E> {}
-impl<V, E> Styled for Focusable<V, E>
+impl<E> Styled for Focusable<E>
where
- V: 'static,
E: Styled,
{
fn style(&mut self) -> &mut StyleRefinement {
@@@ -1254,48 -1291,55 +1254,45 @@@
}
}
-impl<V, E> Element<V> for Focusable<V, E>
+impl<E> Element for Focusable<E>
where
- V: 'static + Render<V>,
- E: Element<V>,
+ E: Element,
{
- type ElementState = E::ElementState;
-
- fn element_id(&self) -> Option<ElementId> {
- self.element.element_id()
- }
+ type State = E::State;
fn layout(
&mut self,
- element_state: Option<Self::ElementState>,
- view_state: &mut V,
- element_state: Option<Self::State>,
- cx: &mut ViewContext<V>,
++ state: Option<Self::State>,
+ cx: &mut WindowContext,
- ) -> (LayoutId, Self::ElementState) {
- self.element.layout(element_state, cx)
+ ) -> (LayoutId, Self::State) {
- self.element.layout(view_state, element_state, cx)
++ self.element.layout(state, cx)
}
-- fn paint(
- &mut self,
- self,
-- bounds: Bounds<Pixels>,
- element_state: &mut Self::ElementState,
- cx: &mut WindowContext,
- view_state: &mut V,
- element_state: &mut Self::State,
- cx: &mut ViewContext<V>,
-- ) {
- self.element.paint(bounds, element_state, cx);
- self.element.paint(bounds, view_state, element_state, cx)
++ fn paint(self, bounds: Bounds<Pixels>, state: &mut Self::State, cx: &mut WindowContext) {
++ self.element.paint(bounds, state, cx)
}
}
- impl<E> Component for Focusable<E>
-impl<V, E> RenderOnce<V> for Focusable<V, E>
++impl<E> RenderOnce for Focusable<E>
where
- E: 'static + Element,
- V: 'static + Render<V>,
- E: Element<V>,
++ E: Element,
{
- fn render(self) -> AnyElement {
- AnyElement::new(self)
- type Element = Self;
++ type Element = E;
+
+ fn element_id(&self) -> Option<ElementId> {
+ self.element.element_id()
+ }
+
+ fn render_once(self) -> Self::Element {
- self
++ self.element
}
}
- impl<E> ParentComponent for Focusable<E>
-impl<V, E> ParentElement<V> for Focusable<V, E>
++impl<E> ParentElement for Focusable<E>
where
- E: ParentComponent,
- V: 'static,
- E: ParentElement<V>,
++ E: ParentElement,
{
- fn children_mut(&mut self) -> &mut SmallVec<[AnyElement<V>; 2]> {
+ fn children_mut(&mut self) -> &mut SmallVec<[AnyElement; 2]> {
self.element.children_mut()
}
}
@@@ -1313,66 -1359,75 +1310,63 @@@ wher
}
}
- impl<E> StatefulInteractiveComponent for Stateful<E>
-impl<V, E> StatefulInteractiveElement<V, E> for Stateful<V, E>
++impl<E> StatefulInteractiveElement for Stateful<E>
where
- V: 'static,
- E: Element<V>,
- Self: InteractiveElement<V>,
+ E: Element,
- Self: InteractiveComponent,
++ Self: InteractiveElement,
{
}
- impl<E> InteractiveComponent for Stateful<E>
-impl<V, E> InteractiveElement<V> for Stateful<V, E>
++impl<E> InteractiveElement for Stateful<E>
where
- E: InteractiveComponent,
- V: 'static,
- E: InteractiveElement<V>,
++ E: InteractiveElement,
{
- fn interactivity(&mut self) -> &mut Interactivity<V> {
+ fn interactivity(&mut self) -> &mut Interactivity {
self.element.interactivity()
}
}
- impl<E: FocusableComponent> FocusableComponent for Stateful<E> {}
-impl<V: 'static, E: FocusableElement<V>> FocusableElement<V> for Stateful<V, E> {}
++impl<E: FocusableElement> FocusableElement for Stateful<E> {}
-impl<V, E> Element<V> for Stateful<V, E>
+impl<E> Element for Stateful<E>
where
- V: 'static,
- E: Element<V>,
+ E: Element,
{
- type ElementState = E::ElementState;
-
- fn element_id(&self) -> Option<ElementId> {
- self.element.element_id()
- }
+ type State = E::State;
fn layout(
&mut self,
- element_state: Option<Self::ElementState>,
- view_state: &mut V,
- element_state: Option<Self::State>,
- cx: &mut ViewContext<V>,
++ state: Option<Self::State>,
+ cx: &mut WindowContext,
- ) -> (LayoutId, Self::ElementState) {
- self.element.layout(element_state, cx)
+ ) -> (LayoutId, Self::State) {
- self.element.layout(view_state, element_state, cx)
++ self.element.layout(state, cx)
}
-- fn paint(
- &mut self,
- self,
-- bounds: Bounds<Pixels>,
- element_state: &mut Self::ElementState,
- cx: &mut WindowContext,
- view_state: &mut V,
- element_state: &mut Self::State,
- cx: &mut ViewContext<V>,
-- ) {
- self.element.paint(bounds, element_state, cx)
- self.element.paint(bounds, view_state, element_state, cx)
++ fn paint(self, bounds: Bounds<Pixels>, state: &mut Self::State, cx: &mut WindowContext) {
++ self.element.paint(bounds, state, cx)
}
}
- impl<E> Component for Stateful<E>
-impl<V, E> RenderOnce<V> for Stateful<V, E>
++impl<E> RenderOnce for Stateful<E>
where
- E: 'static + Element,
- V: 'static,
- E: Element<V>,
++ E: Element,
{
- fn render(self) -> AnyElement {
- AnyElement::new(self)
+ type Element = Self;
+
+ fn element_id(&self) -> Option<ElementId> {
+ self.element.element_id()
+ }
+
+ fn render_once(self) -> Self::Element {
+ self
}
}
- impl<E> ParentComponent for Stateful<E>
-impl<V, E> ParentElement<V> for Stateful<V, E>
++impl<E> ParentElement for Stateful<E>
where
- E: ParentComponent,
- V: 'static,
- E: ParentElement<V>,
++ E: ParentElement,
{
- fn children_mut(&mut self) -> &mut SmallVec<[AnyElement<V>; 2]> {
+ fn children_mut(&mut self) -> &mut SmallVec<[AnyElement; 2]> {
self.element.children_mut()
}
}
diff --cc crates/gpui2/src/elements/img.rs
index 023811dcd25,a6579b022a6..3c0f4c00be8
--- a/crates/gpui2/src/elements/img.rs
+++ b/crates/gpui2/src/elements/img.rs
@@@ -1,6 -1,6 +1,6 @@@
use crate::{
- AnyElement, Bounds, Component, Element, InteractiveComponent, InteractiveElementState,
- Interactivity, LayoutId, Pixels, SharedString, StyleRefinement, Styled, WindowContext,
- BorrowWindow, Bounds, Element, InteractiveElement, InteractiveElementState, Interactivity,
- LayoutId, Pixels, RenderOnce, SharedString, StyleRefinement, Styled, ViewContext,
++ Bounds, Element, InteractiveElement, InteractiveElementState, Interactivity, LayoutId, Pixels,
++ RenderOnce, SharedString, StyleRefinement, Styled, WindowContext,
};
use futures::FutureExt;
use util::ResultExt;
@@@ -31,34 -34,26 +31,24 @@@ impl Img
}
}
- impl Component for Img {
- fn render(self) -> AnyElement {
- AnyElement::new(self)
- }
- }
-
-impl<V> Element<V> for Img<V> {
+impl Element for Img {
- type ElementState = InteractiveElementState;
-
- fn element_id(&self) -> Option<crate::ElementId> {
- self.interactivity.element_id.clone()
- }
+ type State = InteractiveElementState;
fn layout(
&mut self,
- element_state: Option<Self::ElementState>,
- _view_state: &mut V,
+ element_state: Option<Self::State>,
- cx: &mut ViewContext<V>,
+ cx: &mut WindowContext,
- ) -> (LayoutId, Self::ElementState) {
+ ) -> (LayoutId, Self::State) {
self.interactivity.layout(element_state, cx, |style, cx| {
cx.request_layout(&style, None)
})
}
fn paint(
- &mut self,
+ self,
bounds: Bounds<Pixels>,
- element_state: &mut Self::ElementState,
- _view_state: &mut V,
+ element_state: &mut Self::State,
- cx: &mut ViewContext<V>,
+ cx: &mut WindowContext,
) {
self.interactivity.paint(
bounds,
@@@ -96,14 -91,26 +86,26 @@@
}
}
-impl<V: 'static> RenderOnce<V> for Img<V> {
++impl RenderOnce for Img {
+ type Element = Self;
+
+ fn element_id(&self) -> Option<crate::ElementId> {
+ self.interactivity.element_id.clone()
+ }
+
+ fn render_once(self) -> Self::Element {
+ self
+ }
+ }
+
-impl<V> Styled for Img<V> {
+impl Styled for Img {
fn style(&mut self) -> &mut StyleRefinement {
&mut self.interactivity.base_style
}
}
- impl InteractiveComponent for Img {
-impl<V> InteractiveElement<V> for Img<V> {
- fn interactivity(&mut self) -> &mut Interactivity<V> {
++impl InteractiveElement for Img {
+ fn interactivity(&mut self) -> &mut Interactivity {
&mut self.interactivity
}
}
diff --cc crates/gpui2/src/elements/overlay.rs
index 19dab158cc0,79a3643bdc2..29ac2f00c46
--- a/crates/gpui2/src/elements/overlay.rs
+++ b/crates/gpui2/src/elements/overlay.rs
@@@ -2,8 -2,8 +2,8 @@@ use smallvec::SmallVec
use taffy::style::{Display, Position};
use crate::{
- point, AnyElement, BorrowWindow, Bounds, Component, Element, LayoutId, ParentComponent, Pixels,
- Point, Size, Style, WindowContext,
+ point, AnyElement, BorrowWindow, Bounds, Element, LayoutId, ParentElement, Pixels, Point,
- RenderOnce, Size, Style,
++ RenderOnce, Size, Style, WindowContext,
};
pub struct OverlayState {
@@@ -51,30 -51,21 +51,20 @@@ impl Overlay
}
}
- impl ParentComponent for Overlay {
-impl<V: 'static> ParentElement<V> for Overlay<V> {
- fn children_mut(&mut self) -> &mut SmallVec<[AnyElement<V>; 2]> {
++impl ParentElement for Overlay {
+ fn children_mut(&mut self) -> &mut SmallVec<[AnyElement; 2]> {
&mut self.children
}
}
- impl Component for Overlay {
- fn render(self) -> AnyElement {
- AnyElement::new(self)
- }
- }
-
-impl<V: 'static> Element<V> for Overlay<V> {
+impl Element for Overlay {
- type ElementState = OverlayState;
-
- fn element_id(&self) -> Option<crate::ElementId> {
- None
- }
+ type State = OverlayState;
fn layout(
&mut self,
- _: Option<Self::ElementState>,
- view_state: &mut V,
+ _: Option<Self::State>,
- cx: &mut crate::ViewContext<V>,
+ cx: &mut WindowContext,
- ) -> (crate::LayoutId, Self::ElementState) {
+ ) -> (crate::LayoutId, Self::State) {
let child_layout_ids = self
.children
.iter_mut()
@@@ -91,10 -82,11 +81,10 @@@
}
fn paint(
- &mut self,
+ self,
bounds: crate::Bounds<crate::Pixels>,
- element_state: &mut Self::ElementState,
- view_state: &mut V,
+ element_state: &mut Self::State,
- cx: &mut crate::ViewContext<V>,
+ cx: &mut WindowContext,
) {
if element_state.child_layout_ids.is_empty() {
return;
@@@ -154,13 -146,25 +144,25 @@@
}
cx.with_element_offset(desired.origin - bounds.origin, |cx| {
- for child in &mut self.children {
+ for child in self.children {
- child.paint(view_state, cx);
+ child.paint(cx);
}
})
}
}
-impl<V: 'static> RenderOnce<V> for Overlay<V> {
++impl RenderOnce for Overlay {
+ type Element = Self;
+
+ fn element_id(&self) -> Option<crate::ElementId> {
+ None
+ }
+
+ fn render_once(self) -> Self::Element {
+ self
+ }
+ }
+
enum Axis {
Horizontal,
Vertical,
diff --cc crates/gpui2/src/elements/svg.rs
index 2a0df36c942,0940bdd1eb2..c24e4d9b8b8
--- a/crates/gpui2/src/elements/svg.rs
+++ b/crates/gpui2/src/elements/svg.rs
@@@ -1,7 -1,6 +1,6 @@@
use crate::{
- AnyElement, Bounds, Component, Element, ElementId, InteractiveComponent,
- InteractiveElementState, Interactivity, LayoutId, Pixels, SharedString, StyleRefinement,
- Styled, WindowContext,
+ Bounds, Element, ElementId, InteractiveElement, InteractiveElementState, Interactivity,
- LayoutId, Pixels, RenderOnce, SharedString, StyleRefinement, Styled, ViewContext,
++ LayoutId, Pixels, RenderOnce, SharedString, StyleRefinement, Styled, WindowContext,
};
use util::ResultExt;
@@@ -24,35 -23,27 +23,21 @@@ impl Svg
}
}
- impl Component for Svg {
- fn render(self) -> AnyElement {
- AnyElement::new(self)
- }
- }
-
-impl<V> Element<V> for Svg<V> {
+impl Element for Svg {
- type ElementState = InteractiveElementState;
-
- fn element_id(&self) -> Option<ElementId> {
- self.interactivity.element_id.clone()
- }
+ type State = InteractiveElementState;
fn layout(
&mut self,
- element_state: Option<Self::ElementState>,
- _view_state: &mut V,
+ element_state: Option<Self::State>,
- cx: &mut ViewContext<V>,
+ cx: &mut WindowContext,
- ) -> (LayoutId, Self::ElementState) {
+ ) -> (LayoutId, Self::State) {
self.interactivity.layout(element_state, cx, |style, cx| {
cx.request_layout(&style, None)
})
}
-- fn paint(
- &mut self,
- self,
-- bounds: Bounds<Pixels>,
- element_state: &mut Self::ElementState,
- cx: &mut WindowContext,
- _view_state: &mut V,
- element_state: &mut Self::State,
- cx: &mut ViewContext<V>,
-- ) where
++ fn paint(self, bounds: Bounds<Pixels>, element_state: &mut Self::State, cx: &mut WindowContext)
++ where
Self: Sized,
{
self.interactivity
@@@ -64,14 -55,26 +49,26 @@@
}
}
-impl<V: 'static> RenderOnce<V> for Svg<V> {
++impl RenderOnce for Svg {
+ type Element = Self;
+
+ fn element_id(&self) -> Option<ElementId> {
+ self.interactivity.element_id.clone()
+ }
+
+ fn render_once(self) -> Self::Element {
+ self
+ }
+ }
+
-impl<V> Styled for Svg<V> {
+impl Styled for Svg {
fn style(&mut self) -> &mut StyleRefinement {
&mut self.interactivity.base_style
}
}
- impl InteractiveComponent for Svg {
-impl<V> InteractiveElement<V> for Svg<V> {
- fn interactivity(&mut self) -> &mut Interactivity<V> {
++impl InteractiveElement for Svg {
+ fn interactivity(&mut self) -> &mut Interactivity {
&mut self.interactivity
}
}
diff --cc crates/gpui2/src/elements/text.rs
index 99226baaa71,c68684c8d39..05ab85ca633
--- a/crates/gpui2/src/elements/text.rs
+++ b/crates/gpui2/src/elements/text.rs
@@@ -1,13 -1,89 +1,75 @@@
use crate::{
- AnyElement, Bounds, Component, Element, ElementId, LayoutId, Pixels, SharedString, Size,
- TextRun, WindowContext, WrappedLine,
- BorrowWindow, Bounds, Element, ElementId, LayoutId, Pixels, RenderOnce, SharedString, Size,
- TextRun, ViewContext, WindowContext, WrappedLine,
++ Bounds, Element, ElementId, LayoutId, Pixels, RenderOnce, SharedString, Size, TextRun,
++ WindowContext, WrappedLine,
};
+ use anyhow::anyhow;
use parking_lot::{Mutex, MutexGuard};
use smallvec::SmallVec;
use std::{cell::Cell, rc::Rc, sync::Arc};
use util::ResultExt;
- pub struct Text {
-impl<V: 'static> Element<V> for &'static str {
++impl Element for &'static str {
+ type State = TextState;
+
+ fn layout(
+ &mut self,
- _: &mut V,
+ _: Option<Self::State>,
- cx: &mut ViewContext<V>,
++ cx: &mut WindowContext,
+ ) -> (LayoutId, Self::State) {
+ let mut state = TextState::default();
+ let layout_id = state.layout(SharedString::from(*self), None, cx);
+ (layout_id, state)
+ }
+
- fn paint(
- self,
- bounds: Bounds<Pixels>,
- _: &mut V,
- state: &mut TextState,
- cx: &mut ViewContext<V>,
- ) {
++ fn paint(self, bounds: Bounds<Pixels>, state: &mut TextState, cx: &mut WindowContext) {
+ state.paint(bounds, self, cx)
+ }
+ }
+
-impl<V: 'static> RenderOnce<V> for &'static str {
++impl RenderOnce for &'static str {
+ type Element = Self;
+
+ fn element_id(&self) -> Option<ElementId> {
+ None
+ }
+
+ fn render_once(self) -> Self::Element {
+ self
+ }
+ }
+
-impl<V: 'static> Element<V> for SharedString {
++impl Element for SharedString {
+ type State = TextState;
+
+ fn layout(
+ &mut self,
- _: &mut V,
+ _: Option<Self::State>,
- cx: &mut ViewContext<V>,
++ cx: &mut WindowContext,
+ ) -> (LayoutId, Self::State) {
+ let mut state = TextState::default();
+ let layout_id = state.layout(self.clone(), None, cx);
+ (layout_id, state)
+ }
+
- fn paint(
- self,
- bounds: Bounds<Pixels>,
- _: &mut V,
- state: &mut TextState,
- cx: &mut ViewContext<V>,
- ) {
++ fn paint(self, bounds: Bounds<Pixels>, state: &mut TextState, cx: &mut WindowContext) {
+ let text_str: &str = self.as_ref();
+ state.paint(bounds, text_str, cx)
+ }
+ }
+
-impl<V: 'static> RenderOnce<V> for SharedString {
++impl RenderOnce for SharedString {
+ type Element = Self;
+
+ fn element_id(&self) -> Option<ElementId> {
- Some(self.clone().into())
++ None
+ }
+
+ fn render_once(self) -> Self::Element {
+ self
+ }
+ }
+
+ pub struct StyledText {
text: SharedString,
runs: Option<Vec<TextRun>>,
}
@@@ -26,14 -102,111 +88,26 @@@ impl StyledText
}
}
- impl Component for Text {
- fn render(self) -> AnyElement {
- AnyElement::new(self)
-impl<V: 'static> Element<V> for StyledText {
++impl Element for StyledText {
+ type State = TextState;
+
+ fn layout(
+ &mut self,
- _view: &mut V,
- element_state: Option<Self::State>,
- cx: &mut ViewContext<V>,
++ _: Option<Self::State>,
++ cx: &mut WindowContext,
+ ) -> (LayoutId, Self::State) {
- let element_state = element_state.unwrap_or_default();
- let text_system = cx.text_system().clone();
- let text_style = cx.text_style();
- let font_size = text_style.font_size.to_pixels(cx.rem_size());
- let line_height = text_style
- .line_height
- .to_pixels(font_size.into(), cx.rem_size());
- let text = self.text.clone();
-
- let rem_size = cx.rem_size();
-
- let runs = if let Some(runs) = self.runs.take() {
- runs
- } else {
- vec![text_style.to_run(text.len())]
- };
-
- let layout_id = cx.request_measured_layout(Default::default(), rem_size, {
- let element_state = element_state.clone();
- move |known_dimensions, available_space| {
- let wrap_width = known_dimensions.width.or(match available_space.width {
- crate::AvailableSpace::Definite(x) => Some(x),
- _ => None,
- });
-
- if let Some(text_state) = element_state.0.lock().as_ref() {
- if text_state.size.is_some()
- && (wrap_width.is_none() || wrap_width == text_state.wrap_width)
- {
- return text_state.size.unwrap();
- }
- }
-
- let Some(lines) = text_system
- .shape_text(
- &text,
- font_size,
- &runs[..],
- wrap_width, // Wrap if we know the width.
- )
- .log_err()
- else {
- element_state.lock().replace(TextStateInner {
- lines: Default::default(),
- line_height,
- wrap_width,
- size: Some(Size::default()),
- });
- return Size::default();
- };
-
- let mut size: Size<Pixels> = Size::default();
- for line in &lines {
- let line_size = line.size(line_height);
- size.height += line_size.height;
- size.width = size.width.max(line_size.width);
- }
-
- element_state.lock().replace(TextStateInner {
- lines,
- line_height,
- wrap_width,
- size: Some(size),
- });
-
- size
- }
- });
-
- (layout_id, element_state)
++ let mut state = TextState::default();
++ let layout_id = state.layout(self.text.clone(), self.runs.take(), cx);
++ (layout_id, state)
+ }
+
- fn paint(
- self,
- bounds: Bounds<Pixels>,
- _: &mut V,
- element_state: &mut Self::State,
- cx: &mut ViewContext<V>,
- ) {
- let element_state = element_state.lock();
- let element_state = element_state
- .as_ref()
- .ok_or_else(|| anyhow!("measurement has not been performed on {}", &self.text))
- .unwrap();
-
- let line_height = element_state.line_height;
- let mut line_origin = bounds.origin;
- for line in &element_state.lines {
- line.paint(line_origin, line_height, cx).log_err();
- line_origin.y += line.size(line_height).height;
- }
++ fn paint(self, bounds: Bounds<Pixels>, state: &mut Self::State, cx: &mut WindowContext) {
++ state.paint(bounds, &self.text, cx)
}
}
- impl Element for Text {
- type ElementState = TextState;
-impl<V: 'static> RenderOnce<V> for StyledText {
++impl RenderOnce for StyledText {
+ type Element = Self;
fn element_id(&self) -> Option<crate::ElementId> {
None
@@@ -137,25 -326,9 +227,9 @@@ impl TextState
}
}
- #[derive(Default, Clone)]
- pub struct TextState(Arc<Mutex<Option<TextStateInner>>>);
-
- impl TextState {
- fn lock(&self) -> MutexGuard<Option<TextStateInner>> {
- self.0.lock()
- }
- }
-
- struct TextStateInner {
- lines: SmallVec<[WrappedLine; 1]>,
- line_height: Pixels,
- wrap_width: Option<Pixels>,
- size: Option<Size<Pixels>>,
- }
-
struct InteractiveText {
-- id: ElementId,
- text: Text,
++ element_id: ElementId,
+ text: StyledText,
}
struct InteractiveTextState {
@@@ -163,24 -336,21 +237,20 @@@
clicked_range_ixs: Rc<Cell<SmallVec<[usize; 1]>>>,
}
-impl<V: 'static> Element<V> for InteractiveText {
+impl Element for InteractiveText {
- type ElementState = InteractiveTextState;
-
- fn element_id(&self) -> Option<ElementId> {
- Some(self.id.clone())
- }
+ type State = InteractiveTextState;
fn layout(
&mut self,
- element_state: Option<Self::ElementState>,
- view_state: &mut V,
- element_state: Option<Self::State>,
- cx: &mut ViewContext<V>,
++ state: Option<Self::State>,
+ cx: &mut WindowContext,
- ) -> (LayoutId, Self::ElementState) {
+ ) -> (LayoutId, Self::State) {
if let Some(InteractiveTextState {
text_state,
clicked_range_ixs,
-- }) = element_state
++ }) = state
{
- let (layout_id, text_state) = self.text.layout(view_state, Some(text_state), cx);
+ let (layout_id, text_state) = self.text.layout(Some(text_state), cx);
let element_state = InteractiveTextState {
text_state,
clicked_range_ixs,
@@@ -196,44 -366,26 +266,19 @@@
}
}
-- fn paint(
- &mut self,
- self,
-- bounds: Bounds<Pixels>,
- element_state: &mut Self::ElementState,
- cx: &mut WindowContext,
- view_state: &mut V,
- element_state: &mut Self::State,
- cx: &mut ViewContext<V>,
-- ) {
- self.text.paint(bounds, &mut element_state.text_state, cx)
- self.text
- .paint(bounds, view_state, &mut element_state.text_state, cx)
++ fn paint(self, bounds: Bounds<Pixels>, state: &mut Self::State, cx: &mut WindowContext) {
++ self.text.paint(bounds, &mut state.text_state, cx)
}
}
- impl Component for SharedString {
- fn render(self) -> AnyElement {
- Text {
- text: self,
- runs: None,
- }
- .render()
- }
- }
-impl<V: 'static> RenderOnce<V> for InteractiveText {
++impl RenderOnce for InteractiveText {
+ type Element = Self;
- impl Component for &'static str {
- fn render(self) -> AnyElement {
- Text {
- text: self.into(),
- runs: None,
- }
- .render()
+ fn element_id(&self) -> Option<ElementId> {
- Some(self.id.clone())
++ Some(self.element_id.clone())
}
- }
- // TODO: Figure out how to pass `String` to `child` without this.
- // This impl doesn't exist in the `gpui2` crate.
- impl Component for String {
- fn render(self) -> AnyElement {
- Text {
- text: self.into(),
- runs: None,
- }
- .render()
+ fn render_once(self) -> Self::Element {
+ self
}
}
diff --cc crates/gpui2/src/elements/uniform_list.rs
index 48d666383fc,caf18962ec0..0c7e9eb3fd8
--- a/crates/gpui2/src/elements/uniform_list.rs
+++ b/crates/gpui2/src/elements/uniform_list.rs
@@@ -1,23 -1,24 +1,23 @@@
use crate::{
- point, px, size, AnyElement, AvailableSpace, Bounds, Component, Element, ElementId,
- InteractiveComponent, InteractiveElementState, Interactivity, LayoutId, Pixels, Point, Size,
- point, px, size, AnyElement, AvailableSpace, BorrowWindow, Bounds, Element, ElementId,
- InteractiveElement, InteractiveElementState, Interactivity, LayoutId, Pixels, Point,
- RenderOnce, Size, StyleRefinement, Styled, ViewContext,
++ point, px, size, AnyElement, AvailableSpace, Bounds, Element, ElementId, InteractiveElement,
++ InteractiveElementState, Interactivity, LayoutId, Pixels, Point, RenderOnce, Size,
+ StyleRefinement, Styled, WindowContext,
};
use smallvec::SmallVec;
- use std::{cell::RefCell, cmp, mem, ops::Range, rc::Rc};
+ use std::{cell::RefCell, cmp, ops::Range, rc::Rc};
use taffy::style::Overflow;
/// uniform_list provides lazy rendering for a set of items that are of uniform height.
/// When rendered into a container with overflow-y: hidden and a fixed (or max) height,
/// uniform_list will only render the visibile subset of items.
- pub fn uniform_list<I, C>(
-pub fn uniform_list<I, V, E>(
++pub fn uniform_list<I, R>(
id: I,
item_count: usize,
- f: impl 'static + Fn(Range<usize>, &mut WindowContext) -> Vec<C>,
- f: impl 'static + Fn(&mut V, Range<usize>, &mut ViewContext<V>) -> Vec<E>,
-) -> UniformList<V>
++ f: impl 'static + Fn(Range<usize>, &mut WindowContext) -> Vec<R>,
+) -> UniformList
where
I: Into<ElementId>,
- C: Component,
- V: 'static,
- E: Element<V>,
++ R: RenderOnce,
{
let id = id.into();
let mut style = StyleRefinement::default();
@@@ -28,10 -29,10 +28,10 @@@
style,
item_count,
item_to_measure_index: 0,
- render_items: Box::new(move |view, visible_range, cx| {
- f(view, visible_range, cx)
+ render_items: Box::new(move |visible_range, cx| {
+ f(visible_range, cx)
.into_iter()
- .map(|component| component.render())
- .map(|component| component.into_any())
++ .map(|component| component.render_into_any())
.collect()
}),
interactivity: Interactivity {
@@@ -95,28 -101,25 +95,24 @@@ pub struct UniformListState
item_size: Size<Pixels>,
}
-impl<V: 'static> Element<V> for UniformList<V> {
+impl Element for UniformList {
- type ElementState = UniformListState;
-
- fn element_id(&self) -> Option<crate::ElementId> {
- Some(self.id.clone())
- }
+ type State = UniformListState;
fn layout(
&mut self,
- element_state: Option<Self::ElementState>,
- view_state: &mut V,
- element_state: Option<Self::State>,
- cx: &mut ViewContext<V>,
++ state: Option<Self::State>,
+ cx: &mut WindowContext,
- ) -> (LayoutId, Self::ElementState) {
+ ) -> (LayoutId, Self::State) {
let max_items = self.item_count;
let rem_size = cx.rem_size();
-- let item_size = element_state
++ let item_size = state
.as_ref()
.map(|s| s.item_size)
- .unwrap_or_else(|| self.measure_item(view_state, None, cx));
+ .unwrap_or_else(|| self.measure_item(None, cx));
let (layout_id, interactive) =
self.interactivity
-- .layout(element_state.map(|s| s.interactive), cx, |style, cx| {
++ .layout(state.map(|s| s.interactive), cx, |style, cx| {
cx.request_measured_layout(
style,
rem_size,
@@@ -152,10 -155,11 +148,10 @@@
}
fn paint(
- &mut self,
+ self,
bounds: Bounds<crate::Pixels>,
- element_state: &mut Self::ElementState,
- view_state: &mut V,
+ element_state: &mut Self::State,
- cx: &mut ViewContext<V>,
+ cx: &mut WindowContext,
) {
let style =
self.interactivity
@@@ -182,7 -185,11 +177,9 @@@
.get_or_insert_with(Rc::default)
.clone();
- interactivity.paint(
- let item_height = self
- .measure_item(view_state, Some(padded_bounds.size.width), cx)
- .height;
++ let item_height = self.measure_item(Some(padded_bounds.size.width), cx).height;
+
+ self.interactivity.paint(
bounds,
content_size,
&mut element_state.interactive,
@@@ -224,9 -229,9 +219,9 @@@
self.item_count,
);
- let mut items = (self.render_items)(visible_range.clone(), cx);
- let items = (self.render_items)(view_state, visible_range.clone(), cx);
++ let items = (self.render_items)(visible_range.clone(), cx);
cx.with_z_index(1, |cx| {
- for (item, ix) in items.iter_mut().zip(visible_range) {
+ for (item, ix) in items.into_iter().zip(visible_range) {
let item_origin = padded_bounds.origin
+ point(px(0.), item_height * ix + scroll_offset.y);
let available_space = size(
@@@ -240,7 -245,18 +235,18 @@@
})
},
);
- self.interactivity = interactivity;
+ }
+ }
+
-impl<V> RenderOnce<V> for UniformList<V> {
++impl RenderOnce for UniformList {
+ type Element = Self;
+
+ fn element_id(&self) -> Option<crate::ElementId> {
+ Some(self.id.clone())
+ }
+
+ fn render_once(self) -> Self::Element {
+ self
}
}
@@@ -273,8 -294,8 +279,8 @@@ impl UniformList
}
}
- impl InteractiveComponent for UniformList {
-impl<V> InteractiveElement<V> for UniformList<V> {
- fn interactivity(&mut self) -> &mut crate::Interactivity<V> {
++impl InteractiveElement for UniformList {
+ fn interactivity(&mut self) -> &mut crate::Interactivity {
&mut self.interactivity
}
}
diff --cc crates/gpui2/src/gpui2.rs
index 39b148b15bd,98e1c043cd3..2115380c7c2
--- a/crates/gpui2/src/gpui2.rs
+++ b/crates/gpui2/src/gpui2.rs
@@@ -136,7 -134,7 +134,7 @@@ pub trait VisualContext: Context
build_view: impl FnOnce(&mut ViewContext<'_, V>) -> V,
) -> Self::Result<View<V>>
where
- V: Render;
- V: 'static + Render<V>;
++ V: 'static + Render;
fn focus_view<V>(&mut self, view: &View<V>) -> Self::Result<()>
where
diff --cc crates/gpui2/src/interactive.rs
index 2604d24f89a,e988697ca70..483f7599c56
--- a/crates/gpui2/src/interactive.rs
+++ b/crates/gpui2/src/interactive.rs
@@@ -1,8 -1,8 +1,9 @@@
use crate::{
- div, point, Div, FocusHandle, Keystroke, Modifiers, Pixels, Point, Render, ViewContext,
- div, point, Div, Element, FocusHandle, Keystroke, Modifiers, Pixels, Point, Render, ViewContext,
++ div, point, Div, Element, FocusHandle, Keystroke, Modifiers, Pixels, Point, Render, RenderOnce,
++ ViewContext,
};
use smallvec::SmallVec;
- use std::{any::Any, fmt::Debug, ops::Deref, path::PathBuf};
+ use std::{any::Any, fmt::Debug, marker::PhantomData, ops::Deref, path::PathBuf};
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct KeyDownEvent {
@@@ -59,6 -59,32 +60,32 @@@ pub struct ClickEvent
pub up: MouseUpEvent,
}
+ pub struct Drag<S, R, V, E>
+ where
+ R: Fn(&mut V, &mut ViewContext<V>) -> E,
+ V: 'static,
- E: Element<()>,
++ E: RenderOnce,
+ {
+ pub state: S,
+ pub render_drag_handle: R,
- view_type: PhantomData<V>,
++ view_element_types: PhantomData<(V, E)>,
+ }
+
+ impl<S, R, V, E> Drag<S, R, V, E>
+ where
+ R: Fn(&mut V, &mut ViewContext<V>) -> E,
+ V: 'static,
- E: Element<()>,
++ E: Element,
+ {
+ pub fn new(state: S, render_drag_handle: R) -> Self {
+ Drag {
+ state,
+ render_drag_handle,
- view_type: PhantomData,
++ view_element_types: Default::default(),
+ }
+ }
+ }
+
#[derive(Hash, PartialEq, Eq, Copy, Clone, Debug)]
pub enum MouseButton {
Left,
@@@ -259,8 -285,8 +286,8 @@@ pub struct FocusEvent
#[cfg(test)]
mod test {
use crate::{
- self as gpui, div, Component, Div, FocusHandle, InteractiveComponent, KeyBinding,
- Keystroke, ParentComponent, Render, Stateful, TestAppContext, VisualContext,
+ self as gpui, div, Div, FocusHandle, InteractiveElement, KeyBinding, Keystroke,
- ParentElement, Render, Stateful, TestAppContext, VisualContext,
++ ParentElement, Render, RenderOnce, Stateful, TestAppContext, VisualContext,
};
struct TestView {
@@@ -278,18 -304,9 +305,18 @@@
div().id("testview").child(
div()
.key_context("parent")
- .on_key_down(|this: &mut TestView, _, _, _| this.saw_key_down = true)
- .on_action(|this: &mut TestView, _: &TestAction, _| this.saw_action = true)
- .child(div().key_context("nested").track_focus(&self.focus_handle)),
+ .on_key_down(cx.callback(|this, _, _| this.saw_key_down = true))
+ .on_action(
+ cx.callback(|this: &mut TestView, _: &TestAction, _| {
+ this.saw_action = true
+ }),
+ )
+ .child(
+ div()
+ .key_context("nested")
+ .track_focus(&self.focus_handle)
- .render(),
++ .render_once(),
+ ),
)
}
}
diff --cc crates/gpui2/src/view.rs
index 7bbbbce7aff,a1ed8373c5e..734150ee58b
--- a/crates/gpui2/src/view.rs
+++ b/crates/gpui2/src/view.rs
@@@ -6,7 -6,7 +6,7 @@@ use crate::
};
use anyhow::{Context, Result};
use std::{
-- any::{Any, TypeId},
++ any::TypeId,
hash::{Hash, Hasher},
};
@@@ -65,15 -59,15 +59,15 @@@ impl<V: 'static> View<V>
self.model.read(cx)
}
- pub fn render_with<C>(&self, component: C) -> RenderViewWith<C, V>
- pub fn render_with<E>(&self, component: E) -> RenderViewWith<E, V>
-- where
- C: 'static + Component,
- E: 'static + Element<V>,
-- {
-- RenderViewWith {
-- view: self.clone(),
- component: Some(component),
- element: Some(component),
-- }
-- }
++ // pub fn render_with<E>(&self, component: E) -> RenderViewWith<E, V>
++ // where
++ // E: 'static + Element,
++ // {
++ // RenderViewWith {
++ // view: self.clone(),
++ // element: Some(component),
++ // }
++ // }
pub fn focus_handle(&self, cx: &AppContext) -> FocusHandle
where
@@@ -83,6 -77,6 +77,26 @@@
}
}
++impl<V: Render> Element for View<V> {
++ type State = Option<AnyElement>;
++
++ fn layout(
++ &mut self,
++ _state: Option<Self::State>,
++ cx: &mut WindowContext,
++ ) -> (LayoutId, Self::State) {
++ self.update(cx, |view, cx| {
++ let mut element = view.render(cx).into_any();
++ let layout_id = element.layout(cx);
++ (layout_id, Some(element))
++ })
++ }
++
++ fn paint(self, _: Bounds<Pixels>, element: &mut Self::State, cx: &mut WindowContext) {
++ element.take().unwrap().paint(cx);
++ }
++}
++
impl<V> Clone for View<V> {
fn clone(&self) -> Self {
Self {
@@@ -163,8 -151,8 +171,8 @@@ impl<V> Eq for WeakView<V> {
#[derive(Clone, Debug)]
pub struct AnyView {
model: AnyModel,
-- layout: fn(&AnyView, &mut WindowContext) -> (LayoutId, Box<dyn Any>),
- paint: fn(&AnyView, &mut AnyBox, &mut WindowContext),
- paint: fn(&AnyView, Box<dyn Any>, &mut WindowContext),
++ layout: fn(&AnyView, &mut WindowContext) -> (LayoutId, AnyElement),
++ paint: fn(&AnyView, AnyElement, &mut WindowContext),
}
impl AnyView {
@@@ -211,13 -199,7 +219,7 @@@
}
}
- impl Component for AnyView {
- fn render(self) -> AnyElement {
- AnyElement::new(self)
- }
- }
-
-impl<V: 'static + Render<V>> From<View<V>> for AnyView {
+impl<V: Render> From<View<V>> for AnyView {
fn from(value: View<V>) -> Self {
AnyView {
model: value.model.into_any(),
@@@ -227,35 -209,87 +229,51 @@@
}
}
-impl<V: 'static + Render<V>, ParentV: 'static> Element<ParentV> for View<V> {
- type State = Option<AnyElement<V>>;
+impl Element for AnyView {
- type ElementState = Box<dyn Any>;
++ type State = Option<AnyElement>;
+
+ fn layout(
+ &mut self,
- _parent_view: &mut ParentV,
+ _state: Option<Self::State>,
- cx: &mut ViewContext<ParentV>,
++ cx: &mut WindowContext,
+ ) -> (LayoutId, Self::State) {
- self.update(cx, |view, cx| {
- let mut element = view.render(cx).into_any();
- let layout_id = element.layout(view, cx);
- (layout_id, Some(element))
- })
++ let (layout_id, state) = (self.layout)(self, cx);
++ (layout_id, Some(state))
+ }
+
- fn paint(
- self,
- _: Bounds<Pixels>,
- _parent: &mut ParentV,
- element: &mut Self::State,
- cx: &mut ViewContext<ParentV>,
- ) {
- self.update(cx, |view, cx| {
- element.take().unwrap().paint(view, cx);
- });
++ fn paint(self, _: Bounds<Pixels>, state: &mut Self::State, cx: &mut WindowContext) {
++ (self.paint)(&self, state.take().unwrap(), cx)
+ }
+ }
+
-impl<V: 'static + Render<V>, ParentV: 'static> RenderOnce<ParentV> for View<V> {
++impl<V: 'static + Render> RenderOnce for View<V> {
+ type Element = View<V>;
fn element_id(&self) -> Option<ElementId> {
Some(self.model.entity_id.into())
}
- fn layout(
- &mut self,
- _element_state: Option<Self::ElementState>,
- cx: &mut WindowContext,
- ) -> (LayoutId, Self::ElementState) {
- (self.layout)(self, cx)
+ fn render_once(self) -> Self::Element {
+ self
}
+ }
- fn paint(
-impl<V: 'static> Element<V> for AnyView {
- type State = Option<Box<dyn Any>>;
-
- fn layout(
-- &mut self,
- _view_state: &mut V,
- _element_state: Option<Self::State>,
- cx: &mut ViewContext<V>,
- ) -> (LayoutId, Self::State) {
- let (layout_id, rendered_element) = (self.layout)(self, cx);
- (layout_id, Some(rendered_element))
- }
-
- fn paint(
- mut self,
-- _bounds: Bounds<Pixels>,
- rendered_element: &mut Self::ElementState,
- cx: &mut WindowContext,
- _view_state: &mut V,
- rendered_element: &mut Self::State,
- cx: &mut ViewContext<V>,
-- ) {
- (self.paint)(self, rendered_element, cx)
- (self.paint)(&mut self, rendered_element.take().unwrap(), cx)
- }
-}
-
-impl<ParentV: 'static> RenderOnce<ParentV> for AnyView {
++impl RenderOnce for AnyView {
+ type Element = Self;
+
+ fn element_id(&self) -> Option<ElementId> {
+ Some(self.model.entity_id.into())
+ }
+
+ fn render_once(self) -> Self::Element {
+ self
}
}
pub struct AnyWeakView {
model: AnyWeakModel,
-- layout: fn(&AnyView, &mut WindowContext) -> (LayoutId, Box<dyn Any>),
- paint: fn(&AnyView, &mut AnyBox, &mut WindowContext),
- paint: fn(&AnyView, Box<dyn Any>, &mut WindowContext),
++ layout: fn(&AnyView, &mut WindowContext) -> (LayoutId, AnyElement),
++ paint: fn(&AnyView, AnyElement, &mut WindowContext),
}
impl AnyWeakView {
@@@ -269,7 -303,7 +287,7 @@@
}
}
- impl<V: Render> From<WeakView<V>> for AnyWeakView {
-impl<V: 'static + Render<V>> From<WeakView<V>> for AnyWeakView {
++impl<V: 'static + Render> From<WeakView<V>> for AnyWeakView {
fn from(view: WeakView<V>) -> Self {
Self {
model: view.model.into(),
@@@ -291,78 -351,50 +309,74 @@@ wher
}
}
- pub struct RenderViewWith<C, V> {
- view: View<V>,
- component: Option<C>,
- }
-
- impl<C, V> Component for RenderViewWith<C, V>
- where
- V: 'static + Render,
- C: 'static + Component,
- {
- fn render(self) -> AnyElement {
- AnyElement::new(self)
- }
- }
-
- impl<C, V> Element for RenderViewWith<C, V>
-impl<E, V, ParentV> RenderOnce<ParentV> for RenderViewWith<E, V>
--where
- V: 'static + Render,
- C: 'static + Component,
- E: 'static + Element<V>,
- V: 'static,
- ParentV: 'static,
--{
- type ElementState = AnyElement;
- type Element = Self;
--
-- fn element_id(&self) -> Option<ElementId> {
- Some(self.view.entity_id().into())
- }
-
- fn layout(
- &mut self,
- _: Option<Self::ElementState>,
- cx: &mut WindowContext,
- ) -> (LayoutId, Self::ElementState) {
- let mut element = self.component.take().unwrap().render();
- let layout_id = element.layout(cx);
- (layout_id, element)
- self.element.as_ref().unwrap().element_id()
-- }
--
- fn paint(
- &mut self,
- _: Bounds<Pixels>,
- element: &mut Self::ElementState,
- cx: &mut WindowContext,
- ) {
- element.paint(cx)
- fn render_once(self) -> Self::Element {
- self
-- }
--}
++// pub struct RenderViewWith<E, V> {
++// view: View<V>,
++// element: Option<E>,
++// }
++
++// impl<E> Element for RenderViewWith<E>
++// where
++// E: 'static + Element,
++// {
++// type State = Option<AnyElement>;
++
++// fn layout(
++// &mut self,
++// _: Option<Self::State>,
++// cx: &mut WindowContext,
++// ) -> (LayoutId, Self::State) {
++// self.view.update(cx, |view, cx| {
++// let mut element = self.element.take().unwrap().into_any();
++// let layout_id = element.layout(view, cx);
++// (layout_id, Some(element))
++// })
++// }
++
++// fn paint(self, _: Bounds<Pixels>, element: &mut Self::ElementState, cx: &mut WindowContext) {
++// element.paint(cx)
++// }
++// }
++
++// impl<E> RenderOnce for RenderViewWith<E>
++// where
++// E: 'static + Element<V>,
++// ParentV: 'static,
++// {
++// type Element = Self;
++
++// fn element_id(&self) -> Option<ElementId> {
++// self.element.as_ref().unwrap().element_id()
++// }
++
++// fn render_once(self) -> Self::Element {
++// self
++// }
++// }
mod any_view {
-- use crate::{AnyElement, AnyView, BorrowWindow, LayoutId, Render, WindowContext};
-- use std::any::Any;
++ use crate::{AnyElement, AnyView, BorrowWindow, Element, LayoutId, Render, WindowContext};
- pub(crate) fn layout<V: Render>(
- pub(crate) fn layout<V: 'static + Render<V>>(
++ pub(crate) fn layout<V: 'static + Render>(
view: &AnyView,
cx: &mut WindowContext,
-- ) -> (LayoutId, Box<dyn Any>) {
++ ) -> (LayoutId, AnyElement) {
cx.with_element_id(Some(view.model.entity_id), |cx| {
let view = view.clone().downcast::<V>().unwrap();
view.update(cx, |view, cx| {
-- let mut element = AnyElement::new(view.render(cx));
- let layout_id = element.layout(view, cx);
- (layout_id, Box::new(element) as Box<dyn Any>)
++ let mut element = view.render(cx).into_any();
+ let layout_id = element.layout(cx);
- (layout_id, Box::new(element) as Box<dyn Any>)
++ (layout_id, element)
})
})
}
- pub(crate) fn paint<V: Render>(
- pub(crate) fn paint<V: 'static + Render<V>>(
++ pub(crate) fn paint<V: 'static + Render>(
view: &AnyView,
- element: &mut Box<dyn Any>,
- element: Box<dyn Any>,
++ element: AnyElement,
cx: &mut WindowContext,
) {
cx.with_element_id(Some(view.model.entity_id), |cx| {
- let element = element.downcast_mut::<AnyElement>().unwrap();
- let view = view.clone().downcast::<V>().unwrap();
- let element = element.downcast::<AnyElement<V>>().unwrap();
- view.update(cx, |view, cx| element.paint(view, cx))
+ element.paint(cx);
})
}
}
diff --cc crates/gpui2/src/window.rs
index d3517996c80,c17e7e06f4c..fdcafc03bbe
--- a/crates/gpui2/src/window.rs
+++ b/crates/gpui2/src/window.rs
@@@ -1,15 -1,15 +1,15 @@@
use crate::{
- key_dispatch::DispatchActionListener, px, size, Action, AnyBox, AnyDrag, AnyView, AppContext,
+ key_dispatch::DispatchActionListener, px, size, Action, AnyDrag, AnyView, AppContext,
- AsyncWindowContext, AvailableSpace, Bounds, BoxShadow, Context, Corners, CursorStyle,
- DevicePixels, DispatchNodeId, DispatchTree, DisplayId, Edges, Effect, Entity, EntityId,
- EventEmitter, FileDropEvent, Flatten, FocusEvent, FontId, GlobalElementId, GlyphId, Hsla,
- ImageData, InputEvent, IsZero, KeyBinding, KeyContext, KeyDownEvent, LayoutId, Model,
- ModelContext, Modifiers, MonochromeSprite, MouseButton, MouseDownEvent, MouseMoveEvent,
- MouseUpEvent, Path, Pixels, PlatformAtlas, PlatformDisplay, PlatformInputHandler,
- PlatformWindow, Point, PolychromeSprite, PromptLevel, Quad, Render, RenderGlyphParams,
- RenderImageParams, RenderSvgParams, ScaledPixels, SceneBuilder, Shadow, SharedString, Size,
- Style, SubscriberSet, Subscription, TaffyLayoutEngine, Task, Underline, UnderlineStyle, View,
- VisualContext, WeakView, WindowBounds, WindowOptions, SUBPIXEL_VARIANTS,
+ AsyncWindowContext, AvailableSpace, Bounds, BoxShadow, CallbackHandle, ConstructorHandle,
+ Context, Corners, CursorStyle, DevicePixels, DispatchNodeId, DispatchTree, DisplayId, Edges,
+ Effect, Entity, EntityId, EventEmitter, FileDropEvent, Flatten, FocusEvent, FontId,
+ GlobalElementId, GlyphId, Hsla, ImageData, InputEvent, IsZero, KeyBinding, KeyContext,
+ KeyDownEvent, LayoutId, Model, ModelContext, Modifiers, MonochromeSprite, MouseButton,
+ MouseDownEvent, MouseMoveEvent, MouseUpEvent, Path, Pixels, PlatformAtlas, PlatformDisplay,
+ PlatformInputHandler, PlatformWindow, Point, PolychromeSprite, PromptLevel, Quad, Render,
+ RenderGlyphParams, RenderImageParams, RenderSvgParams, ScaledPixels, SceneBuilder, Shadow,
+ SharedString, Size, Style, SubscriberSet, Subscription, TaffyLayoutEngine, Task, Underline,
+ UnderlineStyle, View, VisualContext, WeakView, WindowBounds, WindowOptions, SUBPIXEL_VARIANTS,
};
use anyhow::{anyhow, Context as _, Result};
use collections::HashMap;
@@@ -187,7 -187,7 +187,7 @@@ impl Drop for FocusHandle
/// FocusableView allows users of your view to easily
/// focus it (using cx.focus_view(view))
- pub trait FocusableView: Render {
-pub trait FocusableView: 'static + Render<Self> {
++pub trait FocusableView: 'static + Render {
fn focus_handle(&self, cx: &AppContext) -> FocusHandle;
}
@@@ -1600,7 -1559,7 +1600,7 @@@ impl VisualContext for WindowContext<'_
build_view: impl FnOnce(&mut ViewContext<'_, V>) -> V,
) -> Self::Result<View<V>>
where
- V: Render,
- V: 'static + Render<V>,
++ V: 'static + Render,
{
let slot = self.app.entities.reserve();
let view = View {
@@@ -2372,7 -2355,7 +2372,7 @@@ impl<V: 'static> VisualContext for View
build_view: impl FnOnce(&mut ViewContext<'_, W>) -> W,
) -> Self::Result<View<W>>
where
- W: Render,
- W: 'static + Render<W>,
++ W: 'static + Render,
{
self.window_cx.replace_root_view(build_view)
}
diff --cc crates/ui2/src/components/button.rs
index fdfad8a435e,efbb4c9e72e..d7d526b0877
--- a/crates/ui2/src/components/button.rs
+++ b/crates/ui2/src/components/button.rs
@@@ -1,7 -1,8 +1,8 @@@
use std::sync::Arc;
use gpui::{
- CallbackHandle, DefiniteLength, Hsla, MouseButton, StatefulInteractiveComponent, WindowContext,
- DefiniteLength, Div, Hsla, MouseButton, RenderOnce, Stateful, StatefulInteractiveElement,
- WindowContext,
++ DefiniteLength, DefiniteLength, Div, Hsla, MouseButton, RenderOnce, Stateful,
++ StatefulInteractiveElement, WindowContext,
};
use crate::prelude::*;
@@@ -63,10 -64,25 +64,9 @@@ impl ButtonVariant
}
}
- // #[derive(Component)] <- todo
-pub type ClickHandler<V> = Arc<dyn Fn(&mut V, &mut ViewContext<V>)>;
-
-struct ButtonHandlers<V: 'static> {
- click: Option<ClickHandler<V>>,
-}
-
-unsafe impl<S> Send for ButtonHandlers<S> {}
-unsafe impl<S> Sync for ButtonHandlers<S> {}
-
-impl<V: 'static> Default for ButtonHandlers<V> {
- fn default() -> Self {
- Self { click: None }
- }
-}
-
-#[derive(RenderOnce)]
-pub struct Button<V: 'static> {
+pub struct Button {
disabled: bool,
- handlers: ButtonHandlers<V>,
+ click_handler: Option<CallbackHandle<()>>,
icon: Option<Icon>,
icon_position: Option<IconPosition>,
label: SharedString,
@@@ -75,7 -91,59 +75,58 @@@
color: Option<TextColor>,
}
-impl<V: 'static> Component<V> for Button<V> {
- type Rendered = Stateful<V, Div<V>>;
++impl RenderOnce for Button {
++ type Element = Stateful<Div>;
+
- fn render(self, view: &mut V, cx: &mut ViewContext<V>) -> Self::Rendered {
- let _view: &mut V = view;
++ fn render(self) -> Self::Rendered {
+ let (icon_color, label_color) = match (self.disabled, self.color) {
+ (true, _) => (TextColor::Disabled, TextColor::Disabled),
+ (_, None) => (TextColor::Default, TextColor::Default),
+ (_, Some(color)) => (TextColor::from(color), color),
+ };
+
+ let mut button = h_stack()
+ .id(SharedString::from(format!("{}", self.label)))
+ .relative()
+ .p_1()
+ .text_ui()
+ .rounded_md()
+ .bg(self.variant.bg_color(cx))
+ .cursor_pointer()
+ .hover(|style| style.bg(self.variant.bg_color_hover(cx)))
+ .active(|style| style.bg(self.variant.bg_color_active(cx)));
+
+ match (self.icon, self.icon_position) {
+ (Some(_), Some(IconPosition::Left)) => {
+ button = button
+ .gap_1()
+ .child(self.render_label(label_color))
+ .children(self.render_icon(icon_color))
+ }
+ (Some(_), Some(IconPosition::Right)) => {
+ button = button
+ .gap_1()
+ .children(self.render_icon(icon_color))
+ .child(self.render_label(label_color))
+ }
+ (_, _) => button = button.child(self.render_label(label_color)),
+ }
+
+ if let Some(width) = self.width {
+ button = button.w(width).justify_center();
+ }
+
+ if let Some(click_handler) = self.handlers.click.clone() {
+ button = button.on_mouse_down(MouseButton::Left, move |state, event, cx| {
+ click_handler(state, cx);
+ });
+ }
+
+ button
+ }
+ }
+
-impl<V: 'static> Button<V> {
+impl Button {
pub fn new(label: impl Into<SharedString>) -> Self {
Self {
disabled: false,
@@@ -150,72 -218,77 +201,30 @@@
fn render_icon(&self, icon_color: TextColor) -> Option<IconElement> {
self.icon.map(|i| IconElement::new(i).color(icon_color))
}
--
- pub fn render(self, cx: &mut WindowContext) -> impl Component {
- pub fn render(self, _view: &mut V, cx: &mut ViewContext<V>) -> impl Element<V> {
-- let (icon_color, label_color) = match (self.disabled, self.color) {
-- (true, _) => (TextColor::Disabled, TextColor::Disabled),
-- (_, None) => (TextColor::Default, TextColor::Default),
-- (_, Some(color)) => (TextColor::from(color), color),
-- };
--
-- let mut button = h_stack()
-- .id(SharedString::from(format!("{}", self.label)))
-- .relative()
-- .p_1()
-- .text_ui()
-- .rounded_md()
-- .bg(self.variant.bg_color(cx))
-- .cursor_pointer()
-- .hover(|style| style.bg(self.variant.bg_color_hover(cx)))
-- .active(|style| style.bg(self.variant.bg_color_active(cx)));
--
-- match (self.icon, self.icon_position) {
-- (Some(_), Some(IconPosition::Left)) => {
-- button = button
-- .gap_1()
-- .child(self.render_label(label_color))
-- .children(self.render_icon(icon_color))
-- }
-- (Some(_), Some(IconPosition::Right)) => {
-- button = button
-- .gap_1()
-- .children(self.render_icon(icon_color))
-- .child(self.render_label(label_color))
-- }
-- (_, _) => button = button.child(self.render_label(label_color)),
-- }
--
-- if let Some(width) = self.width {
-- button = button.w(width).justify_center();
-- }
--
-- if let Some(click_handler) = self.handlers.click.clone() {
-- button = button.on_mouse_down(MouseButton::Left, move |state, event, cx| {
-- click_handler(state, cx);
-- });
-- }
--
-- button
-- }
}
+ #[derive(RenderOnce)]
-pub struct ButtonGroup<V: 'static> {
- buttons: Vec<Button<V>>,
+pub struct ButtonGroup {
+ buttons: Vec<Button>,
}
- impl ButtonGroup {
- pub fn new(buttons: Vec<Button>) -> Self {
- Self { buttons }
- }
-impl<V: 'static> Component<V> for ButtonGroup<V> {
- type Rendered = Div<V>;
++impl Component for ButtonGroup {
++ type Rendered = Div;
- fn render(self, cx: &mut WindowContext) -> impl Component {
- let mut el = h_stack().text_ui();
- fn render(self, view: &mut V, cx: &mut ViewContext<V>) -> Self::Rendered {
++ fn render(self, cx: &mut WindowContext) -> Self::Rendered {
+ let mut group = h_stack();
- for button in self.buttons {
- el = el.child(button.render(cx));
+ for button in self.buttons.into_iter() {
+ group = group.child(button.render(view, cx));
}
- el
+ group
+ }
+ }
+
+ impl<V: 'static> ButtonGroup<V> {
+ pub fn new(buttons: Vec<Button<V>>) -> Self {
+ Self { buttons }
}
}