diff --git a/Cargo.lock b/Cargo.lock index a0be9756bf..1ff9981a6a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3172,6 +3172,7 @@ dependencies = [ name = "gpui_macros" version = "0.1.0" dependencies = [ + "gpui", "proc-macro2", "quote", "syn 1.0.109", diff --git a/crates/gpui/src/elements.rs b/crates/gpui/src/elements.rs index bc903fe74c..56a712802b 100644 --- a/crates/gpui/src/elements.rs +++ b/crates/gpui/src/elements.rs @@ -201,6 +201,10 @@ pub trait Element: 'static { } } +pub trait RenderElement { + fn render(&mut self, view: &mut V, cx: &mut ViewContext) -> AnyElement; +} + trait AnyElementState { fn layout( &mut self, diff --git a/crates/gpui_macros/Cargo.toml b/crates/gpui_macros/Cargo.toml index 76daeae2a8..9ff340299b 100644 --- a/crates/gpui_macros/Cargo.toml +++ b/crates/gpui_macros/Cargo.toml @@ -14,3 +14,5 @@ syn = "1.0" quote = "1.0" proc-macro2 = "1.0" +[dev-dependencies] +gpui = { path = "../gpui" } diff --git a/crates/gpui_macros/src/gpui_macros.rs b/crates/gpui_macros/src/gpui_macros.rs index dbf57b83e5..2e6c725872 100644 --- a/crates/gpui_macros/src/gpui_macros.rs +++ b/crates/gpui_macros/src/gpui_macros.rs @@ -283,8 +283,12 @@ pub fn element_derive(input: TokenStream) -> TokenStream { // The name of the struct/enum let name = input.ident; + let must_implement = format_ident!("{}MustImplementRenderElement", name); let expanded = quote! { + trait #must_implement : gpui::elements::RenderElement {} + impl #must_implement for #name {} + impl gpui::elements::Element for #name { type LayoutState = gpui::elements::AnyElement; type PaintState = (); @@ -307,7 +311,7 @@ pub fn element_derive(input: TokenStream) -> TokenStream { visible_bounds: gpui::geometry::rect::RectF, element: &mut gpui::elements::AnyElement, view: &mut V, - cx: &mut gpui::ViewContext, + cx: &mut gpui::PaintContext, ) { element.paint(scene, bounds.origin(), visible_bounds, view, cx); } @@ -332,7 +336,7 @@ pub fn element_derive(input: TokenStream) -> TokenStream { _: &(), view: &V, cx: &gpui::ViewContext, - ) -> serde_json::Value { + ) -> gpui::serde_json::Value { element.debug(view, cx) } } diff --git a/crates/gpui_macros/tests/test.rs b/crates/gpui_macros/tests/test.rs new file mode 100644 index 0000000000..b5a91a7e98 --- /dev/null +++ b/crates/gpui_macros/tests/test.rs @@ -0,0 +1,14 @@ +use gpui::{elements::RenderElement, View, ViewContext}; +use gpui_macros::Element; + +#[test] +fn test_derive_render_element() { + #[derive(Element)] + struct TestElement {} + + impl RenderElement for TestElement { + fn render(&mut self, _: &mut V, _: &mut ViewContext) -> gpui::AnyElement { + unimplemented!() + } + } +}