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 disclosable-component
Mikayla707ca34f19ba
diff --cc crates/component_test/src/component_test.rsindex 30fd431290d,00000000000..9f6b4918b9emode 100644,000000..100644--- a/crates/component_test/src/component_test.rs+++ b/crates/component_test/src/component_test.rs@@@ -1,122 -1,0 +1,121 @@@+use gpui::{+ actions,- color::Color,+ elements::{Component, Flex, ParentElement, SafeStylable},+ AppContext, Element, Entity, ModelHandle, Task, View, ViewContext, ViewHandle, WeakViewHandle,+};+use project::Project;+use theme::components::{action_button::Button, label::Label, ComponentExt};+use workspace::{+ item::Item, register_deserializable_item, ItemId, Pane, PaneBackdrop, Workspace, WorkspaceId,+};++pub fn init(cx: &mut AppContext) {+ cx.add_action(ComponentTest::toggle_disclosure);+ cx.add_action(ComponentTest::toggle_toggle);+ cx.add_action(ComponentTest::deploy);+ register_deserializable_item::<ComponentTest>(cx);+}++actions!(+ test,+ [NoAction, ToggleDisclosure, ToggleToggle, NewComponentTest]+);++struct ComponentTest {+ disclosed: bool,+ toggled: bool,+}++impl ComponentTest {+ fn new() -> Self {+ Self {+ disclosed: false,+ toggled: false,+ }+ }++ fn deploy(workspace: &mut Workspace, _: &NewComponentTest, cx: &mut ViewContext<Workspace>) {+ workspace.add_item(Box::new(cx.add_view(|_| ComponentTest::new())), cx);+ }++ fn toggle_disclosure(&mut self, _: &ToggleDisclosure, cx: &mut ViewContext<Self>) {+ self.disclosed = !self.disclosed;+ cx.notify();+ }++ fn toggle_toggle(&mut self, _: &ToggleToggle, cx: &mut ViewContext<Self>) {+ self.toggled = !self.toggled;+ cx.notify();+ }+}++impl Entity for ComponentTest {+ type Event = ();+}++impl View for ComponentTest {+ fn ui_name() -> &'static str {+ "Component Test"+ }++ fn render(&mut self, cx: &mut gpui::ViewContext<Self>) -> gpui::AnyElement<Self> {+ let theme = theme::current(cx);++ PaneBackdrop::new(+ cx.view_id(),+ Flex::column()+ .with_spacing(10.)+ .with_child(+ Button::action(NoAction)+ .with_tooltip("Here's what a tooltip looks like", theme.tooltip.clone())+ .with_contents(Label::new("Click me!"))+ .with_style(theme.component_test.button.clone())+ .element(),+ )+ .with_child(+ Button::action(ToggleToggle)+ .with_tooltip("Here's what a tooltip looks like", theme.tooltip.clone())+ .with_contents(Label::new("Toggle me!"))+ .toggleable(self.toggled)+ .with_style(theme.component_test.toggle.clone())+ .element(),+ )+ .with_child(+ Label::new("A disclosure")+ .disclosable(Some(self.disclosed), Box::new(ToggleDisclosure))+ .with_style(theme.component_test.disclosure.clone())+ .element(),+ )+ .constrained()+ .with_width(200.)+ .aligned()+ .into_any(),+ )+ .into_any()+ }+}++impl Item for ComponentTest {- fn tab_content<V: View>(++ fn tab_content<V: 'static>(+ &self,+ _: Option<usize>,+ style: &theme::Tab,+ _: &AppContext,+ ) -> gpui::AnyElement<V> {+ gpui::elements::Label::new("Component test", style.label.clone()).into_any()+ }++ fn serialized_item_kind() -> Option<&'static str> {+ Some("ComponentTest")+ }++ fn deserialize(+ _project: ModelHandle<Project>,+ _workspace: WeakViewHandle<Workspace>,+ _workspace_id: WorkspaceId,+ _item_id: ItemId,+ cx: &mut ViewContext<Pane>,+ ) -> Task<anyhow::Result<ViewHandle<Self>>> {+ Task::ready(Ok(cx.add_view(|_| Self::new())))+ }+}diff --cc crates/gpui/src/elements.rsindex 3c80ef73fe8,bde15068ef0..ca0a0b3586e--- a/crates/gpui/src/elements.rs+++ b/crates/gpui/src/elements.rs@@@ -229,34 -234,9 +234,30 @@@ pub trait Element<V: 'static>: 'static{MouseEventHandler::for_child::<Tag>(self.into_any(), region_id)}++ fn component(self) -> StatelessElementAdapter+ where+ Self: Sized,+ {+ StatelessElementAdapter::new(self.into_any())+ }++ fn stateful_component(self) -> StatefulElementAdapter<V>+ where+ Self: Sized,+ {+ StatefulElementAdapter::new(self.into_any())+ }++ fn styleable_component(self) -> StylableAdapter<StatelessElementAdapter>+ where+ Self: Sized,+ {+ StatelessElementAdapter::new(self.into_any()).stylable()+ }}- pub trait RenderElement {- fn render<V: View>(&mut self, view: &mut V, cx: &mut ViewContext<V>) -> AnyElement<V>;- }-- trait AnyElementState<V: View> {+ trait AnyElementState<V> {fn layout(&mut self,constraint: SizeConstraint,diff --cc crates/gpui/src/elements/component.rsindex 3776abcaa0a,e2770c01485..c8800d18a16--- a/crates/gpui/src/elements/component.rs+++ b/crates/gpui/src/elements/component.rs@@@ -3,52 -3,25 +3,51 @@@ use std::{any::Any, marker::PhantomDatause pathfinder_geometry::{rect::RectF, vector::Vector2F};use crate::{-- AnyElement, Element, LayoutContext, PaintContext, SceneBuilder, SizeConstraint, View,-- ViewContext,++ AnyElement, Element, LayoutContext, PaintContext, SceneBuilder, SizeConstraint, ViewContext,};use super::Empty;-pub trait GeneralComponent {- fn render<V: View>(self, v: &mut V, cx: &mut ViewContext<V>) -> AnyElement<V>;- fn element<V: View>(self) -> ComponentAdapter<V, Self>+/// The core stateless component trait, simply rendering an element tree+pub trait Component {- fn render<V: View>(self, cx: &mut ViewContext<V>) -> AnyElement<V>;++ fn render<V: 'static>(self, cx: &mut ViewContext<V>) -> AnyElement<V>;+- fn element<V: View>(self) -> ComponentAdapter<V, Self>++ fn element<V: 'static>(self) -> ComponentAdapter<V, Self>whereSelf: Sized,{ComponentAdapter::new(self)}++ fn stylable(self) -> StylableAdapter<Self>+ where+ Self: Sized,+ {+ StylableAdapter::new(self)+ }+- fn stateful<V: View>(self) -> StatefulAdapter<Self, V>++ fn stateful<V: 'static>(self) -> StatefulAdapter<Self, V>+ where+ Self: Sized,+ {+ StatefulAdapter::new(self)+ }}-pub trait StyleableComponent {+/// Allows a a component's styles to be rebound in a simple way.+pub trait Stylable: Component {type Style: Clone;- type Output: GeneralComponent;++ fn with_style(self, style: Self::Style) -> Self;+}++/// This trait models the typestate pattern for a component's style,+/// enforcing at compile time that a component is only usable after+/// it has been styled while still allowing for late binding of the+/// styling information+pub trait SafeStylable {+ type Style: Clone;+ type Output: Component;fn with_style(self, style: Self::Style) -> Self::Output;}@@@ -86,12 -41,7 +85,12 @@@ impl<C: Component> SafeStylable for Sty}}-pub trait Component<V: View> {+/// This is a secondary trait for components that can be styled+/// which rely on their view's state. This is useful for components that, for example,+/// want to take click handler callbacks Unfortunately, the generic bound on the+/// Component trait makes it incompatible with the stateless components above.+// So let's just replicate them for now- pub trait StatefulComponent<V: View> {++pub trait StatefulComponent<V: 'static> {fn render(self, v: &mut V, cx: &mut ViewContext<V>) -> AnyElement<V>;fn element(self) -> ComponentAdapter<V, Self>@@@ -100,63 -50,21 +99,63 @@@{ComponentAdapter::new(self)}++ fn styleable(self) -> StatefulStylableAdapter<Self, V>+ where+ Self: Sized,+ {+ StatefulStylableAdapter::new(self)+ }++ fn stateless(self) -> StatelessElementAdapter+ where+ Self: Sized + 'static,+ {+ StatelessElementAdapter::new(self.element().into_any())+ }}-impl<V: View, C: GeneralComponent> Component<V> for C {- fn render(self, v: &mut V, cx: &mut ViewContext<V>) -> AnyElement<V> {- self.render(v, cx)+/// It is trivial to convert stateless components to stateful components, so lets+/// do so en masse. Note that the reverse is impossible without a helper.- impl<V: View, C: Component> StatefulComponent<V> for C {++impl<V: 'static, C: Component> StatefulComponent<V> for C {+ fn render(self, _: &mut V, cx: &mut ViewContext<V>) -> AnyElement<V> {+ self.render(cx)}}-// StylableComponent -> GeneralComponent-pub struct StylableComponentAdapter<C: Component<V>, V: View> {+/// Same as stylable, but generic over a view type- pub trait StatefulStylable<V: View>: StatefulComponent<V> {++pub trait StatefulStylable<V: 'static>: StatefulComponent<V> {+ type Style: Clone;++ fn with_style(self, style: Self::Style) -> Self;+}++/// Same as SafeStylable, but generic over a view type- pub trait StatefulSafeStylable<V: View> {++pub trait StatefulSafeStylable<V: 'static> {+ type Style: Clone;+ type Output: StatefulComponent<V>;++ fn with_style(self, style: Self::Style) -> Self::Output;+}++/// Converting from stateless to stateful- impl<V: View, C: SafeStylable> StatefulSafeStylable<V> for C {++impl<V: 'static, C: SafeStylable> StatefulSafeStylable<V> for C {+ type Style = C::Style;++ type Output = C::Output;++ fn with_style(self, style: Self::Style) -> Self::Output {+ self.with_style(style)+ }+}++// A helper for converting stateless components into stateful ones+pub struct StatefulAdapter<C, V> {component: C,phantom: std::marker::PhantomData<V>,}- impl<C: Component, V: View> StatefulAdapter<C, V> {-impl<C: Component<V>, V: View> StylableComponentAdapter<C, V> {++impl<C: Component, V: 'static> StatefulAdapter<C, V> {pub fn new(component: C) -> Self {Self {component,@@@ -165,29 -73,7 +164,31 @@@}}- impl<C: Component, V: View> StatefulComponent<V> for StatefulAdapter<C, V> {-impl<C: GeneralComponent, V: View> StyleableComponent for StylableComponentAdapter<C, V> {++impl<C: Component, V: 'static> StatefulComponent<V> for StatefulAdapter<C, V> {+ fn render(self, _: &mut V, cx: &mut ViewContext<V>) -> AnyElement<V> {+ self.component.render(cx)+ }+}++// A helper for converting stateful but style-less components into stylable ones+// by using `()` as the style type- pub struct StatefulStylableAdapter<C: StatefulComponent<V>, V: View> {++pub struct StatefulStylableAdapter<C: StatefulComponent<V>, V: 'static> {+ component: C,+ phantom: std::marker::PhantomData<V>,+}+- impl<C: StatefulComponent<V>, V: View> StatefulStylableAdapter<C, V> {++impl<C: StatefulComponent<V>, V: 'static> StatefulStylableAdapter<C, V> {+ pub fn new(component: C) -> Self {+ Self {+ component,+ phantom: std::marker::PhantomData,+ }+ }+}+- impl<C: StatefulComponent<V>, V: View> StatefulSafeStylable<V> for StatefulStylableAdapter<C, V> {++impl<C: StatefulComponent<V>, V: 'static> StatefulSafeStylable<V>++ for StatefulStylableAdapter<C, V>++{type Style = ();type Output = C;@@@ -197,37 -83,13 +198,37 @@@}}-// Element -> Component-pub struct ElementAdapter<V: View> {+/// A way of erasing the view generic from an element, useful+/// for wrapping up an explicit element tree into stateless+/// components+pub struct StatelessElementAdapter {+ element: Box<dyn Any>,+}++impl StatelessElementAdapter {- pub fn new<V: View>(element: AnyElement<V>) -> Self {++ pub fn new<V: 'static>(element: AnyElement<V>) -> Self {+ StatelessElementAdapter {+ element: Box::new(element) as Box<dyn Any>,+ }+ }+}++impl Component for StatelessElementAdapter {- fn render<V: View>(self, _: &mut ViewContext<V>) -> AnyElement<V> {++ fn render<V: 'static>(self, _: &mut ViewContext<V>) -> AnyElement<V> {+ *self+ .element+ .downcast::<AnyElement<V>>()+ .expect("Don't move elements out of their view :(")+ }+}++// For converting elements into stateful components- pub struct StatefulElementAdapter<V: View> {++pub struct StatefulElementAdapter<V: 'static> {element: AnyElement<V>,_phantom: std::marker::PhantomData<V>,}- impl<V: View> StatefulElementAdapter<V> {-impl<V: View> ElementAdapter<V> {++impl<V: 'static> StatefulElementAdapter<V> {pub fn new(element: AnyElement<V>) -> Self {Self {element,@@@ -236,35 -98,20 +237,35 @@@}}- impl<V: View> StatefulComponent<V> for StatefulElementAdapter<V> {-impl<V: View> Component<V> for ElementAdapter<V> {++impl<V: 'static> StatefulComponent<V> for StatefulElementAdapter<V> {fn render(self, _: &mut V, _: &mut ViewContext<V>) -> AnyElement<V> {self.element}}-// Component -> Element-pub struct ComponentAdapter<V: View, E> {+/// A convenient shorthand for creating an empty component.+impl Component for () {- fn render<V: View>(self, _: &mut ViewContext<V>) -> AnyElement<V> {++ fn render<V: 'static>(self, _: &mut ViewContext<V>) -> AnyElement<V> {+ Empty::new().into_any()+ }+}++impl Stylable for () {+ type Style = ();++ fn with_style(self, _: Self::Style) -> Self {+ ()+ }+}++// For converting components back into Elements- pub struct ComponentAdapter<V: View, E> {++pub struct ComponentAdapter<V: 'static, E> {component: Option<E>,element: Option<AnyElement<V>>,phantom: PhantomData<V>,}--impl<E, V: View> ComponentAdapter<V, E> {++impl<E, V: 'static> ComponentAdapter<V, E> {pub fn new(e: E) -> Self {Self {component: Some(e),@@@ -274,7 -121,7 +275,7 @@@}}- impl<V: View, C: StatefulComponent<V> + 'static> Element<V> for ComponentAdapter<V, C> {-impl<V: View, C: Component<V> + 'static> Element<V> for ComponentAdapter<V, C> {++impl<V: 'static, C: StatefulComponent<V> + 'static> Element<V> for ComponentAdapter<V, C> {type LayoutState = ();type PaintState = ();diff --cc crates/gpui/src/elements/container.rsindex 68d3e576105,03c2f1d0464..9a590f3a5a5--- a/crates/gpui/src/elements/container.rs+++ b/crates/gpui/src/elements/container.rs@@@ -45,17 -44,9 +44,17 @@@ impl ContainerStyle..Default::default()}}++ pub fn additional_length(&self) -> f32 {+ self.padding.left+ + self.padding.right+ + self.border.width * 2.+ + self.margin.left+ + self.margin.right+ }}- pub struct Container<V: View> {+ pub struct Container<V> {child: AnyElement<V>,style: ContainerStyle,}diff --cc crates/gpui/src/elements/flex.rsindex 9d175afc03a,d9c43ca4c81..d43a152a649--- a/crates/gpui/src/elements/flex.rs+++ b/crates/gpui/src/elements/flex.rs@@@ -22,10 -22,9 +22,10 @@@ pub struct Flex<V>children: Vec<AnyElement<V>>,scroll_state: Option<(ElementStateHandle<Rc<ScrollState>>, usize)>,child_alignment: f32,+ spacing: f32,}- impl<V: View> Flex<V> {+ impl<V: 'static> Flex<V> {pub fn new(axis: Axis) -> Self {Self {axis,diff --cc crates/theme/src/components.rsindex 8eaab91fe69,8a0d8527a4d..9011821b772--- a/crates/theme/src/components.rs+++ b/crates/theme/src/components.rs@@@ -16,124 -14,6 +16,124 @@@ impl<C: SafeStylable> ComponentExt<C> ffn toggleable(self, active: bool) -> Toggle<C, ()> {Toggle::new(self, active)}++ /// Some(True) => disclosed => content is visible+ /// Some(false) => closed => content is hidden+ /// None => No disclosure button, but reserve disclosure spacing+ fn disclosable(self, disclosed: Option<bool>, action: Box<dyn Action>) -> Disclosable<C, ()> {+ Disclosable::new(disclosed, self, action)+ }+}++pub mod disclosure {++ use gpui::{+ elements::{Component, ContainerStyle, Empty, Flex, ParentElement, SafeStylable},+ Action, Element,+ };+ use schemars::JsonSchema;+ use serde_derive::Deserialize;+- use super::{action_button::Button, svg::Svg, ComponentExt, IconButtonStyle};++ use super::{action_button::Button, svg::Svg, IconButtonStyle};++ #[derive(Clone, Default, Deserialize, JsonSchema)]+ pub struct DisclosureStyle<S> {+ pub button: IconButtonStyle,+ #[serde(flatten)]+ pub container: ContainerStyle,+ pub spacing: f32,+ #[serde(flatten)]+ content: S,+ }++ impl<S> DisclosureStyle<S> {+ pub fn button_space(&self) -> f32 {+ self.spacing + self.button.button_width.unwrap()+ }+ }++ pub struct Disclosable<C, S> {+ disclosed: Option<bool>,+ action: Box<dyn Action>,+ id: usize,+ content: C,+ style: S,+ }++ impl Disclosable<(), ()> {+ pub fn new<C>(+ disclosed: Option<bool>,+ content: C,+ action: Box<dyn Action>,+ ) -> Disclosable<C, ()> {+ Disclosable {+ disclosed,+ content,+ action,+ id: 0,+ style: (),+ }+ }+ }++ impl<C> Disclosable<C, ()> {+ pub fn with_id(mut self, id: usize) -> Disclosable<C, ()> {+ self.id = id;+ self+ }+ }++ impl<C: SafeStylable> SafeStylable for Disclosable<C, ()> {+ type Style = DisclosureStyle<C::Style>;++ type Output = Disclosable<C, Self::Style>;++ fn with_style(self, style: Self::Style) -> Self::Output {+ Disclosable {+ disclosed: self.disclosed,+ action: self.action,+ content: self.content,+ id: self.id,+ style,+ }+ }+ }++ impl<C: SafeStylable> Component for Disclosable<C, DisclosureStyle<C::Style>> {- fn render<V: gpui::View>(self, cx: &mut gpui::ViewContext<V>) -> gpui::AnyElement<V> {++ fn render<V: 'static>(self, cx: &mut gpui::ViewContext<V>) -> gpui::AnyElement<V> {+ Flex::row()+ .with_spacing(self.style.spacing)+ .with_child(if let Some(disclosed) = self.disclosed {+ Button::dynamic_action(self.action)+ .with_id(self.id)+ .with_contents(Svg::new(if disclosed {+ "icons/file_icons/chevron_down.svg"+ } else {+ "icons/file_icons/chevron_right.svg"+ }))+ .with_style(self.style.button)+ .element()+ .into_any()+ } else {+ Empty::new()+ .into_any()+ .constrained()+ // TODO: Why is this optional at all?+ .with_width(self.style.button.button_width.unwrap())+ .into_any()+ })+ .with_child(+ self.content+ .with_style(self.style.content)+ .render(cx)+ .flex(1., true),+ )+ .align_children_center()+ .contained()+ .with_style(self.style.container)+ .into_any()+ }+ }}pub mod toggle {@@@ -171,11 -51,15 +171,11 @@@}}- impl<C: StyleableComponent> GeneralComponent for Toggle<C, Toggleable<C::Style>> {- fn render<V: gpui::View>(- self,- v: &mut V,- cx: &mut gpui::ViewContext<V>,- ) -> gpui::AnyElement<V> {+ impl<C: SafeStylable> Component for Toggle<C, Toggleable<C::Style>> {- fn render<V: gpui::View>(self, cx: &mut gpui::ViewContext<V>) -> gpui::AnyElement<V> {++ fn render<V: 'static>(self, cx: &mut gpui::ViewContext<V>) -> gpui::AnyElement<V> {self.component.with_style(self.style.in_state(self.active).clone())- .render(v, cx)+ .render(cx)}}}@@@ -184,9 -68,11 +184,9 @@@ pub mod action_buttonuse std::borrow::Cow;use gpui::{- elements::{- ContainerStyle, GeneralComponent, MouseEventHandler, StyleableComponent, TooltipStyle,- },+ elements::{Component, ContainerStyle, MouseEventHandler, SafeStylable, TooltipStyle},platform::{CursorStyle, MouseButton},- Action, Element, EventContext, TypeTag, View,- Action, Element, TypeTag, View,++ Action, Element, TypeTag,};use schemars::JsonSchema;use serde_derive::Deserialize;@@@ -273,9 -150,9 +273,9 @@@}}- impl<C: StyleableComponent> GeneralComponent for ActionButton<C, ButtonStyle<C::Style>> {- fn render<V: View>(self, v: &mut V, cx: &mut gpui::ViewContext<V>) -> gpui::AnyElement<V> {- MouseEventHandler::new_dynamic(self.tag, 0, cx, |state, cx| {+ impl<C: SafeStylable> Component for Button<C, ButtonStyle<C::Style>> {- fn render<V: View>(self, cx: &mut gpui::ViewContext<V>) -> gpui::AnyElement<V> {++ fn render<V: 'static>(self, cx: &mut gpui::ViewContext<V>) -> gpui::AnyElement<V> {+ let mut button = MouseEventHandler::new_dynamic(self.tag, self.id, cx, |state, cx| {let style = self.style.style_for(state);let mut contents = self.contents@@@ -420,19 -281,18 +420,19 @@@ pub mod svg}}- impl GeneralComponent for Svg<SvgStyle> {- fn render<V: gpui::View>(- self,- _: &mut V,- _: &mut gpui::ViewContext<V>,- ) -> gpui::AnyElement<V> {- gpui::elements::Svg::new(self.path)- .with_color(self.style.color)- .constrained()- .with_width(self.style.icon_width)- .with_height(self.style.icon_height)- .into_any()+ impl Component for Svg<SvgStyle> {- fn render<V: gpui::View>(self, _: &mut gpui::ViewContext<V>) -> gpui::AnyElement<V> {++ fn render<V: 'static>(self, _: &mut gpui::ViewContext<V>) -> gpui::AnyElement<V> {+ if let Some(path) = self.path {+ gpui::elements::Svg::new(path)+ .with_color(self.style.color)+ .constrained()+ } else {+ Empty::new().constrained()+ }+ .constrained()+ .with_width(self.style.icon_width)+ .with_height(self.style.icon_height)+ .into_any()}}}@@@ -473,8 -332,12 +473,8 @@@ pub mod label}}- impl GeneralComponent for Label<LabelStyle> {- fn render<V: gpui::View>(- self,- _: &mut V,- _: &mut gpui::ViewContext<V>,- ) -> gpui::AnyElement<V> {+ impl Component for Label<LabelStyle> {- fn render<V: gpui::View>(self, _: &mut gpui::ViewContext<V>) -> gpui::AnyElement<V> {++ fn render<V: 'static>(self, _: &mut gpui::ViewContext<V>) -> gpui::AnyElement<V> {gpui::elements::Label::new(self.text, self.style).into_any()}}