Checkpoint
This commit is contained in:
parent
1d491fcd78
commit
746f77bf7c
14 changed files with 226 additions and 250 deletions
92
crates/gpui2_macros/src/derive_element.rs
Normal file
92
crates/gpui2_macros/src/derive_element.rs
Normal file
|
@ -0,0 +1,92 @@
|
|||
use proc_macro::TokenStream;
|
||||
use proc_macro2::Ident;
|
||||
use quote::quote;
|
||||
use syn::{parse_macro_input, parse_quote, DeriveInput, GenericParam, Generics};
|
||||
|
||||
use crate::derive_into_element::impl_into_element;
|
||||
|
||||
pub fn derive_element(input: TokenStream) -> TokenStream {
|
||||
let ast = parse_macro_input!(input as DeriveInput);
|
||||
let type_name = ast.ident;
|
||||
let placeholder_view_generics: Generics = parse_quote! { <V: 'static> };
|
||||
|
||||
let (impl_generics, type_generics, where_clause, view_type_name, lifetimes) =
|
||||
if let Some(first_type_param) = ast.generics.params.iter().find_map(|param| {
|
||||
if let GenericParam::Type(type_param) = param {
|
||||
Some(type_param.ident.clone())
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}) {
|
||||
let mut lifetimes = vec![];
|
||||
for param in ast.generics.params.iter() {
|
||||
if let GenericParam::Lifetime(lifetime_def) = param {
|
||||
lifetimes.push(lifetime_def.lifetime.clone());
|
||||
}
|
||||
}
|
||||
let generics = ast.generics.split_for_impl();
|
||||
(
|
||||
generics.0,
|
||||
Some(generics.1),
|
||||
generics.2,
|
||||
first_type_param,
|
||||
lifetimes,
|
||||
)
|
||||
} else {
|
||||
let generics = placeholder_view_generics.split_for_impl();
|
||||
let placeholder_view_type_name: Ident = parse_quote! { V };
|
||||
(
|
||||
generics.0,
|
||||
None,
|
||||
generics.2,
|
||||
placeholder_view_type_name,
|
||||
vec![],
|
||||
)
|
||||
};
|
||||
|
||||
let lifetimes = if !lifetimes.is_empty() {
|
||||
quote! { <#(#lifetimes),*> }
|
||||
} else {
|
||||
quote! {}
|
||||
};
|
||||
|
||||
let impl_into_element = impl_into_element(
|
||||
&impl_generics,
|
||||
&view_type_name,
|
||||
&type_name,
|
||||
&type_generics,
|
||||
&where_clause,
|
||||
);
|
||||
|
||||
let gen = quote! {
|
||||
impl #impl_generics playground::element::Element<#view_type_name> for #type_name #type_generics
|
||||
#where_clause
|
||||
{
|
||||
type PaintState = playground::element::AnyElement<#view_type_name #lifetimes>;
|
||||
|
||||
fn layout(
|
||||
&mut self,
|
||||
view: &mut V,
|
||||
cx: &mut playground::element::LayoutContext<V>,
|
||||
) -> anyhow::Result<(playground::element::LayoutId, Self::PaintState)> {
|
||||
let mut rendered_element = self.render(view, cx).into_element().into_any();
|
||||
let layout_id = rendered_element.layout(view, cx)?;
|
||||
Ok((layout_id, rendered_element))
|
||||
}
|
||||
|
||||
fn paint(
|
||||
&mut self,
|
||||
view: &mut V,
|
||||
layout: &playground::element::Layout,
|
||||
rendered_element: &mut Self::PaintState,
|
||||
cx: &mut playground::element::PaintContext<V>,
|
||||
) {
|
||||
rendered_element.paint(view, layout.bounds.origin(), cx);
|
||||
}
|
||||
}
|
||||
|
||||
#impl_into_element
|
||||
};
|
||||
|
||||
gen.into()
|
||||
}
|
69
crates/gpui2_macros/src/derive_into_element.rs
Normal file
69
crates/gpui2_macros/src/derive_into_element.rs
Normal file
|
@ -0,0 +1,69 @@
|
|||
use proc_macro::TokenStream;
|
||||
use quote::quote;
|
||||
use syn::{
|
||||
parse_macro_input, parse_quote, DeriveInput, GenericParam, Generics, Ident, WhereClause,
|
||||
};
|
||||
|
||||
pub fn derive_into_element(input: TokenStream) -> TokenStream {
|
||||
let ast = parse_macro_input!(input as DeriveInput);
|
||||
let type_name = ast.ident;
|
||||
|
||||
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>;
|
||||
|
||||
match ast.generics.params.iter().find_map(|param| {
|
||||
if let GenericParam::Type(type_param) = param {
|
||||
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_into_element(
|
||||
&impl_generics,
|
||||
&view_type_name,
|
||||
&type_name,
|
||||
&type_generics,
|
||||
&where_clause,
|
||||
)
|
||||
.into()
|
||||
}
|
||||
|
||||
pub fn impl_into_element(
|
||||
impl_generics: &syn::ImplGenerics<'_>,
|
||||
view_type_name: &Ident,
|
||||
type_name: &Ident,
|
||||
type_generics: &Option<syn::TypeGenerics<'_>>,
|
||||
where_clause: &Option<&WhereClause>,
|
||||
) -> proc_macro2::TokenStream {
|
||||
quote! {
|
||||
impl #impl_generics playground::element::IntoElement<#view_type_name> for #type_name #type_generics
|
||||
#where_clause
|
||||
{
|
||||
type Element = Self;
|
||||
|
||||
fn into_element(self) -> Self {
|
||||
self
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
20
crates/gpui2_macros/src/gpui2_macros.rs
Normal file
20
crates/gpui2_macros/src/gpui2_macros.rs
Normal file
|
@ -0,0 +1,20 @@
|
|||
use proc_macro::TokenStream;
|
||||
|
||||
mod derive_element;
|
||||
mod derive_into_element;
|
||||
mod styleable_helpers;
|
||||
|
||||
#[proc_macro]
|
||||
pub fn styleable_helpers(args: TokenStream) -> TokenStream {
|
||||
styleable_helpers::styleable_helpers(args)
|
||||
}
|
||||
|
||||
#[proc_macro_derive(Element, attributes(element_crate))]
|
||||
pub fn derive_element(input: TokenStream) -> TokenStream {
|
||||
derive_element::derive_element(input)
|
||||
}
|
||||
|
||||
#[proc_macro_derive(IntoElement, attributes(element_crate))]
|
||||
pub fn derive_into_element(input: TokenStream) -> TokenStream {
|
||||
derive_into_element::derive_into_element(input)
|
||||
}
|
148
crates/gpui2_macros/src/styleable_helpers.rs
Normal file
148
crates/gpui2_macros/src/styleable_helpers.rs
Normal file
|
@ -0,0 +1,148 @@
|
|||
use proc_macro::TokenStream;
|
||||
use proc_macro2::TokenStream as TokenStream2;
|
||||
use quote::{format_ident, quote};
|
||||
use syn::{
|
||||
parse::{Parse, ParseStream, Result},
|
||||
parse_macro_input,
|
||||
};
|
||||
|
||||
struct StyleableMacroInput;
|
||||
|
||||
impl Parse for StyleableMacroInput {
|
||||
fn parse(_input: ParseStream) -> Result<Self> {
|
||||
Ok(StyleableMacroInput)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn styleable_helpers(input: TokenStream) -> TokenStream {
|
||||
let _ = parse_macro_input!(input as StyleableMacroInput);
|
||||
let methods = generate_methods();
|
||||
let output = quote! {
|
||||
#(#methods)*
|
||||
};
|
||||
|
||||
output.into()
|
||||
}
|
||||
|
||||
fn generate_methods() -> Vec<TokenStream2> {
|
||||
let mut methods = Vec::new();
|
||||
|
||||
for (prefix, auto_allowed, fields) in tailwind_prefixes() {
|
||||
for (suffix, length_tokens) in tailwind_lengths() {
|
||||
if !auto_allowed && suffix == "auto" {
|
||||
// Conditional to skip "auto"
|
||||
continue;
|
||||
}
|
||||
|
||||
let method_name = format_ident!("{}_{}", prefix, suffix);
|
||||
let field_assignments = fields
|
||||
.iter()
|
||||
.map(|field_tokens| {
|
||||
quote! {
|
||||
style.#field_tokens = Some(gpui::geometry::#length_tokens);
|
||||
}
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
let method = quote! {
|
||||
fn #method_name(mut self) -> Self where Self: std::marker::Sized {
|
||||
let mut style = self.declared_style();
|
||||
#(#field_assignments)*
|
||||
self
|
||||
}
|
||||
};
|
||||
|
||||
methods.push(method);
|
||||
}
|
||||
}
|
||||
|
||||
methods
|
||||
}
|
||||
|
||||
fn tailwind_lengths() -> Vec<(&'static str, TokenStream2)> {
|
||||
vec![
|
||||
("0", quote! { pixels(0.) }),
|
||||
("1", quote! { rems(0.25) }),
|
||||
("2", quote! { rems(0.5) }),
|
||||
("3", quote! { rems(0.75) }),
|
||||
("4", quote! { rems(1.) }),
|
||||
("5", quote! { rems(1.25) }),
|
||||
("6", quote! { rems(1.5) }),
|
||||
("8", quote! { rems(2.0) }),
|
||||
("10", quote! { rems(2.5) }),
|
||||
("12", quote! { rems(3.) }),
|
||||
("16", quote! { rems(4.) }),
|
||||
("20", quote! { rems(5.) }),
|
||||
("24", quote! { rems(6.) }),
|
||||
("32", quote! { rems(8.) }),
|
||||
("40", quote! { rems(10.) }),
|
||||
("48", quote! { rems(12.) }),
|
||||
("56", quote! { rems(14.) }),
|
||||
("64", quote! { rems(16.) }),
|
||||
("72", quote! { rems(18.) }),
|
||||
("80", quote! { rems(20.) }),
|
||||
("96", quote! { rems(24.) }),
|
||||
("auto", quote! { auto() }),
|
||||
("px", quote! { pixels(1.) }),
|
||||
("full", quote! { relative(1.) }),
|
||||
("1_2", quote! { relative(0.5) }),
|
||||
("1_3", quote! { relative(1./3.) }),
|
||||
("2_3", quote! { relative(2./3.) }),
|
||||
("1_4", quote! { relative(0.25) }),
|
||||
("2_4", quote! { relative(0.5) }),
|
||||
("3_4", quote! { relative(0.75) }),
|
||||
("1_5", quote! { relative(0.2) }),
|
||||
("2_5", quote! { relative(0.4) }),
|
||||
("3_5", quote! { relative(0.6) }),
|
||||
("4_5", quote! { relative(0.8) }),
|
||||
("1_6", quote! { relative(1./6.) }),
|
||||
("5_6", quote! { relative(5./6.) }),
|
||||
("1_12", quote! { relative(1./12.) }),
|
||||
// ("screen_50", quote! { DefiniteLength::Vh(50.0) }),
|
||||
// ("screen_75", quote! { DefiniteLength::Vh(75.0) }),
|
||||
// ("screen", quote! { DefiniteLength::Vh(100.0) }),
|
||||
]
|
||||
}
|
||||
|
||||
fn tailwind_prefixes() -> Vec<(&'static str, bool, Vec<TokenStream2>)> {
|
||||
vec![
|
||||
("w", true, vec![quote! { size.width }]),
|
||||
("h", true, vec![quote! { size.height }]),
|
||||
("min_w", false, vec![quote! { min_size.width }]),
|
||||
("min_h", false, vec![quote! { min_size.height }]),
|
||||
("max_w", false, vec![quote! { max_size.width }]),
|
||||
("max_h", false, vec![quote! { max_size.height }]),
|
||||
(
|
||||
"m",
|
||||
true,
|
||||
vec![quote! { margin.top }, quote! { margin.bottom }],
|
||||
),
|
||||
("mt", true, vec![quote! { margin.top }]),
|
||||
("mb", true, vec![quote! { margin.bottom }]),
|
||||
(
|
||||
"mx",
|
||||
true,
|
||||
vec![quote! { margin.left }, quote! { margin.right }],
|
||||
),
|
||||
("ml", true, vec![quote! { margin.left }]),
|
||||
("mr", true, vec![quote! { margin.right }]),
|
||||
(
|
||||
"p",
|
||||
false,
|
||||
vec![quote! { padding.top }, quote! { padding.bottom }],
|
||||
),
|
||||
("pt", false, vec![quote! { padding.top }]),
|
||||
("pb", false, vec![quote! { padding.bottom }]),
|
||||
(
|
||||
"px",
|
||||
false,
|
||||
vec![quote! { padding.left }, quote! { padding.right }],
|
||||
),
|
||||
("pl", false, vec![quote! { padding.left }]),
|
||||
("pr", false, vec![quote! { padding.right }]),
|
||||
("top", true, vec![quote! { inset.top }]),
|
||||
("bottom", true, vec![quote! { inset.bottom }]),
|
||||
("left", true, vec![quote! { inset.left }]),
|
||||
("right", true, vec![quote! { inset.right }]),
|
||||
]
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue