diff --git a/crates/gpui/src/elements.rs b/crates/gpui/src/elements.rs index 779f4b6ec3..78403444ff 100644 --- a/crates/gpui/src/elements.rs +++ b/crates/gpui/src/elements.rs @@ -41,13 +41,7 @@ use collections::HashMap; use core::panic; use json::ToJson; use smallvec::SmallVec; -use std::{ - any::Any, - borrow::Cow, - marker::PhantomData, - mem, - ops::{Deref, DerefMut, Range}, -}; +use std::{any::Any, borrow::Cow, mem, ops::Range}; pub trait Element: 'static { type LayoutState; @@ -567,90 +561,6 @@ impl RootElement { } } -pub trait Component: 'static { - fn render(&self, view: &mut V, cx: &mut ViewContext) -> AnyElement; -} - -pub struct ComponentHost> { - component: C, - view_type: PhantomData, -} - -impl> ComponentHost { - pub fn new(c: C) -> Self { - Self { - component: c, - view_type: PhantomData, - } - } -} - -impl> Deref for ComponentHost { - type Target = C; - - fn deref(&self) -> &Self::Target { - &self.component - } -} - -impl> DerefMut for ComponentHost { - fn deref_mut(&mut self) -> &mut Self::Target { - &mut self.component - } -} - -impl> Element for ComponentHost { - type LayoutState = AnyElement; - type PaintState = (); - - fn layout( - &mut self, - constraint: SizeConstraint, - view: &mut V, - cx: &mut LayoutContext, - ) -> (Vector2F, AnyElement) { - let mut element = self.component.render(view, cx); - let size = element.layout(constraint, view, cx); - (size, element) - } - - fn paint( - &mut self, - scene: &mut SceneBuilder, - bounds: RectF, - visible_bounds: RectF, - element: &mut AnyElement, - view: &mut V, - cx: &mut ViewContext, - ) { - element.paint(scene, bounds.origin(), visible_bounds, view, cx); - } - - fn rect_for_text_range( - &self, - range_utf16: Range, - _: RectF, - _: RectF, - element: &AnyElement, - _: &(), - view: &V, - cx: &ViewContext, - ) -> Option { - element.rect_for_text_range(range_utf16, view, cx) - } - - fn debug( - &self, - _: RectF, - element: &AnyElement, - _: &(), - view: &V, - cx: &ViewContext, - ) -> serde_json::Value { - element.debug(view, cx) - } -} - pub trait AnyRootElement { fn layout( &mut self, diff --git a/crates/gpui/src/gpui.rs b/crates/gpui/src/gpui.rs index a172667fb9..25d022d8ed 100644 --- a/crates/gpui/src/gpui.rs +++ b/crates/gpui/src/gpui.rs @@ -26,7 +26,7 @@ pub mod color; pub mod json; pub mod keymap_matcher; pub mod platform; -pub use gpui_macros::test; +pub use gpui_macros::{test, Element}; pub use window::{Axis, SizeConstraint, Vector2FExt, WindowContext}; pub use anyhow; diff --git a/crates/gpui_macros/src/gpui_macros.rs b/crates/gpui_macros/src/gpui_macros.rs index e976245e06..dbf57b83e5 100644 --- a/crates/gpui_macros/src/gpui_macros.rs +++ b/crates/gpui_macros/src/gpui_macros.rs @@ -3,8 +3,8 @@ use proc_macro2::Ident; use quote::{format_ident, quote}; use std::mem; use syn::{ - parse_macro_input, parse_quote, spanned::Spanned as _, AttributeArgs, FnArg, ItemFn, Lit, Meta, - NestedMeta, Type, + parse_macro_input, parse_quote, spanned::Spanned as _, AttributeArgs, DeriveInput, FnArg, + ItemFn, Lit, Meta, NestedMeta, Type, }; #[proc_macro_attribute] @@ -275,3 +275,68 @@ fn parse_bool(literal: &Lit) -> Result { result.map_err(|err| TokenStream::from(err.into_compile_error())) } + +#[proc_macro_derive(Element)] +pub fn element_derive(input: TokenStream) -> TokenStream { + // Parse the input tokens into a syntax tree + let input = parse_macro_input!(input as DeriveInput); + + // The name of the struct/enum + let name = input.ident; + + let expanded = quote! { + impl gpui::elements::Element for #name { + type LayoutState = gpui::elements::AnyElement; + type PaintState = (); + + fn layout( + &mut self, + constraint: gpui::SizeConstraint, + view: &mut V, + cx: &mut gpui::LayoutContext, + ) -> (gpui::geometry::vector::Vector2F, gpui::elements::AnyElement) { + let mut element = self.render(view, cx); + let size = element.layout(constraint, view, cx); + (size, element) + } + + fn paint( + &mut self, + scene: &mut gpui::SceneBuilder, + bounds: gpui::geometry::rect::RectF, + visible_bounds: gpui::geometry::rect::RectF, + element: &mut gpui::elements::AnyElement, + view: &mut V, + cx: &mut gpui::ViewContext, + ) { + element.paint(scene, bounds.origin(), visible_bounds, view, cx); + } + + fn rect_for_text_range( + &self, + range_utf16: std::ops::Range, + _: gpui::geometry::rect::RectF, + _: gpui::geometry::rect::RectF, + element: &gpui::elements::AnyElement, + _: &(), + view: &V, + cx: &gpui::ViewContext, + ) -> Option { + element.rect_for_text_range(range_utf16, view, cx) + } + + fn debug( + &self, + _: gpui::geometry::rect::RectF, + element: &gpui::elements::AnyElement, + _: &(), + view: &V, + cx: &gpui::ViewContext, + ) -> serde_json::Value { + element.debug(view, cx) + } + } + }; + // Return generated code + TokenStream::from(expanded) +}