Add derive macro now new elements
This commit is contained in:
parent
77bc682a69
commit
4efc46c763
4 changed files with 129 additions and 68 deletions
|
@ -1,9 +1,10 @@
|
||||||
use crate::{
|
use crate::{
|
||||||
element::{AnyElement, Element, ElementMetadata},
|
element::{Element, ElementMetadata},
|
||||||
frame,
|
frame,
|
||||||
themes::rose_pine,
|
themes::rose_pine,
|
||||||
};
|
};
|
||||||
use gpui::ViewContext;
|
use gpui::ViewContext;
|
||||||
|
use playground_macros::Element;
|
||||||
use std::{borrow::Cow, marker::PhantomData, rc::Rc};
|
use std::{borrow::Cow, marker::PhantomData, rc::Rc};
|
||||||
|
|
||||||
struct ButtonHandlers<V, D> {
|
struct ButtonHandlers<V, D> {
|
||||||
|
@ -16,6 +17,8 @@ impl<V, D> Default for ButtonHandlers<V, D> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Element)]
|
||||||
|
#[element_crate = "crate"]
|
||||||
pub struct Button<V: 'static, D: 'static> {
|
pub struct Button<V: 'static, D: 'static> {
|
||||||
metadata: ElementMetadata<V>,
|
metadata: ElementMetadata<V>,
|
||||||
handlers: ButtonHandlers<V, D>,
|
handlers: ButtonHandlers<V, D>,
|
||||||
|
@ -89,34 +92,34 @@ impl<V: 'static, D: 'static> Button<V, D> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<V: 'static, D> Element<V> for Button<V, D> {
|
// impl<V: 'static, D> Element<V> for Button<V, D> {
|
||||||
type Layout = AnyElement<V>;
|
// type Layout = AnyElement<V>;
|
||||||
|
|
||||||
fn style_mut(&mut self) -> &mut crate::style::ElementStyle {
|
// fn style_mut(&mut self) -> &mut crate::style::ElementStyle {
|
||||||
&mut self.metadata.style
|
// &mut self.metadata.style
|
||||||
}
|
// }
|
||||||
|
|
||||||
fn handlers_mut(&mut self) -> &mut crate::element::ElementHandlers<V> {
|
// fn handlers_mut(&mut self) -> &mut crate::element::ElementHandlers<V> {
|
||||||
&mut self.metadata.handlers
|
// &mut self.metadata.handlers
|
||||||
}
|
// }
|
||||||
|
|
||||||
fn layout(
|
// fn layout(
|
||||||
&mut self,
|
// &mut self,
|
||||||
view: &mut V,
|
// view: &mut V,
|
||||||
cx: &mut crate::element::LayoutContext<V>,
|
// cx: &mut crate::element::LayoutContext<V>,
|
||||||
) -> anyhow::Result<(taffy::tree::NodeId, Self::Layout)> {
|
// ) -> anyhow::Result<(taffy::tree::NodeId, Self::Layout)> {
|
||||||
let mut element = self.render(view, cx).into_any();
|
// let mut element = self.render(view, cx).into_any();
|
||||||
let node_id = element.layout(view, cx)?;
|
// let node_id = element.layout(view, cx)?;
|
||||||
Ok((node_id, element))
|
// Ok((node_id, element))
|
||||||
}
|
// }
|
||||||
|
|
||||||
fn paint<'a>(
|
// fn paint<'a>(
|
||||||
&mut self,
|
// &mut self,
|
||||||
layout: crate::element::Layout<'a, Self::Layout>,
|
// layout: crate::element::Layout<'a, Self::Layout>,
|
||||||
view: &mut V,
|
// view: &mut V,
|
||||||
cx: &mut crate::element::PaintContext<V>,
|
// cx: &mut crate::element::PaintContext<V>,
|
||||||
) -> anyhow::Result<()> {
|
// ) -> anyhow::Result<()> {
|
||||||
layout.from_element.paint(view, cx)?;
|
// layout.from_element.paint(view, cx)?;
|
||||||
Ok(())
|
// Ok(())
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
|
|
|
@ -42,7 +42,6 @@ fn main() {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn workspace<V: 'static>(theme: &ThemeColors) -> impl Element<V> {
|
fn workspace<V: 'static>(theme: &ThemeColors) -> impl Element<V> {
|
||||||
// frame().w_full().h_half().fill(theme.success(0.5))
|
|
||||||
frame()
|
frame()
|
||||||
.h_full()
|
.h_full()
|
||||||
.w(percent(50.))
|
.w(percent(50.))
|
||||||
|
|
|
@ -1,48 +1,102 @@
|
||||||
// type Result = ();
|
use proc_macro::TokenStream;
|
||||||
// type LayoutContext<> = ();
|
use quote::{format_ident, quote};
|
||||||
|
use syn::{
|
||||||
|
parse_macro_input, parse_quote, DeriveInput, GenericParam, Generics, Ident, Lit, Meta,
|
||||||
|
WhereClause,
|
||||||
|
};
|
||||||
|
|
||||||
// trait Element<V: 'static>: 'static + Clone {
|
pub fn derive_element(input: TokenStream) -> TokenStream {
|
||||||
// type Layout: 'static;
|
let ast = parse_macro_input!(input as DeriveInput);
|
||||||
|
let type_name = ast.ident;
|
||||||
|
|
||||||
// fn style_mut(&mut self) -> &mut Style;
|
let crate_name: String = ast
|
||||||
// fn layout(&mut self, view: &mut V, cx: &mut LayoutContext<V>)
|
.attrs
|
||||||
// -> Result<(NodeId, Self::Layout)>;
|
.iter()
|
||||||
// fn paint<'a>(
|
.find_map(|attr| {
|
||||||
// &mut self,
|
if attr.path.is_ident("element_crate") {
|
||||||
// layout: Layout<'a, Self::Layout>,
|
match attr.parse_meta() {
|
||||||
// view: &mut V,
|
Ok(Meta::NameValue(nv)) => {
|
||||||
// cx: &mut PaintContext<V>,
|
if let Lit::Str(s) = nv.lit {
|
||||||
// ) -> Result<()>;
|
Some(s.value())
|
||||||
// }
|
} else {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_ => None,
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.unwrap_or_else(|| String::from("playground"));
|
||||||
|
|
||||||
// struct Button {
|
let crate_name = format_ident!("{}", crate_name);
|
||||||
// style: Style,
|
|
||||||
// }
|
|
||||||
|
|
||||||
// type Style = ();
|
let placeholder_view_generics: Generics = parse_quote! { <V: 'static> };
|
||||||
|
let placeholder_view_type_name: Ident = parse_quote! { V };
|
||||||
|
let view_type_name: Ident;
|
||||||
|
let impl_generics: syn::ImplGenerics<'_>;
|
||||||
|
let type_generics: Option<syn::TypeGenerics<'_>>;
|
||||||
|
let where_clause: Option<&'_ WhereClause>;
|
||||||
|
|
||||||
// impl Button {
|
match ast.generics.params.iter().find_map(|param| {
|
||||||
// fn render<V>() -> impl Element<V> {
|
if let GenericParam::Type(type_param) = param {
|
||||||
// todo!()
|
Some(type_param.ident.clone())
|
||||||
// }
|
} else {
|
||||||
// }
|
None
|
||||||
|
}
|
||||||
|
}) {
|
||||||
|
Some(type_name) => {
|
||||||
|
view_type_name = type_name;
|
||||||
|
let generics = ast.generics.split_for_impl();
|
||||||
|
impl_generics = generics.0;
|
||||||
|
type_generics = Some(generics.1);
|
||||||
|
where_clause = generics.2;
|
||||||
|
}
|
||||||
|
_ => {
|
||||||
|
view_type_name = placeholder_view_type_name;
|
||||||
|
let generics = placeholder_view_generics.split_for_impl();
|
||||||
|
impl_generics = generics.0;
|
||||||
|
type_generics = None;
|
||||||
|
where_clause = generics.2;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// impl<V: 'static> Element<V> for Foo {
|
let gen = quote! {
|
||||||
// type Layout = ();
|
impl #impl_generics #crate_name::element::Element<#view_type_name> for #type_name #type_generics
|
||||||
|
#where_clause
|
||||||
|
{
|
||||||
|
type Layout = #crate_name::element::AnyElement<V>;
|
||||||
|
|
||||||
// fn style_mut(&mut self) -> &mut Style {
|
fn style_mut(&mut self) -> &mut #crate_name::style::ElementStyle {
|
||||||
// unimplemented!()
|
&mut self.metadata.style
|
||||||
// }
|
}
|
||||||
|
|
||||||
// fn layout(
|
fn handlers_mut(&mut self) -> &mut #crate_name::element::ElementHandlers<V> {
|
||||||
// &mut self,
|
&mut self.metadata.handlers
|
||||||
// view: &mut V,
|
}
|
||||||
// cx: &mut LayoutContext<V>,
|
|
||||||
// ) -> Result<(NodeId, Self::Layout)> {
|
|
||||||
// unimplemented!()
|
|
||||||
// }
|
|
||||||
|
|
||||||
// fn paint(&mut self, layout: Layout<()>, view: &mut V, cx: &mut PaintContext<V>) -> Result<()> {
|
fn layout(
|
||||||
// unimplemented!()
|
&mut self,
|
||||||
// }
|
view: &mut V,
|
||||||
// }
|
cx: &mut #crate_name::element::LayoutContext<V>,
|
||||||
|
) -> anyhow::Result<(taffy::tree::NodeId, Self::Layout)> {
|
||||||
|
let mut element = self.render(view, cx).into_any();
|
||||||
|
let node_id = element.layout(view, cx)?;
|
||||||
|
Ok((node_id, element))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn paint<'a>(
|
||||||
|
&mut self,
|
||||||
|
layout: #crate_name::element::Layout<'a, Self::Layout>,
|
||||||
|
view: &mut V,
|
||||||
|
cx: &mut #crate_name::element::PaintContext<V>,
|
||||||
|
) -> anyhow::Result<()> {
|
||||||
|
layout.from_element.paint(view, cx)?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
gen.into()
|
||||||
|
}
|
||||||
|
|
|
@ -7,3 +7,8 @@ mod tailwind_lengths;
|
||||||
pub fn tailwind_lengths(attr: TokenStream, item: TokenStream) -> TokenStream {
|
pub fn tailwind_lengths(attr: TokenStream, item: TokenStream) -> TokenStream {
|
||||||
tailwind_lengths::tailwind_lengths(attr, item)
|
tailwind_lengths::tailwind_lengths(attr, item)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[proc_macro_derive(Element, attributes(element_crate))]
|
||||||
|
pub fn derive_element(input: TokenStream) -> TokenStream {
|
||||||
|
derive_element::derive_element(input)
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue