Merge Component
and ComponentPreview
trait (#28365)
- Merge `Component` and `ComponentPreview` trait - Adds a number of component previews - Removes a number of stories Release Notes: - N/A
This commit is contained in:
parent
b15ee1b1cc
commit
c05bf096f8
52 changed files with 3276 additions and 1848 deletions
25
crates/ui_macros/src/derive_register_component.rs
Normal file
25
crates/ui_macros/src/derive_register_component.rs
Normal file
|
@ -0,0 +1,25 @@
|
|||
use proc_macro::TokenStream;
|
||||
use quote::quote;
|
||||
use syn::{DeriveInput, parse_macro_input};
|
||||
|
||||
pub fn derive_register_component(input: TokenStream) -> TokenStream {
|
||||
let input = parse_macro_input!(input as DeriveInput);
|
||||
let name = input.ident;
|
||||
let reg_fn_name = syn::Ident::new(
|
||||
&format!("__component_registry_internal_register_{}", name),
|
||||
name.span(),
|
||||
);
|
||||
let expanded = quote! {
|
||||
const _: () = {
|
||||
struct AssertComponent<T: component::Component>(::std::marker::PhantomData<T>);
|
||||
let _ = AssertComponent::<#name>(::std::marker::PhantomData);
|
||||
};
|
||||
|
||||
#[allow(non_snake_case)]
|
||||
#[linkme::distributed_slice(component::__ALL_COMPONENTS)]
|
||||
fn #reg_fn_name() {
|
||||
component::register_component::<#name>();
|
||||
}
|
||||
};
|
||||
expanded.into()
|
||||
}
|
|
@ -1,5 +1,5 @@
|
|||
mod derive_component;
|
||||
mod derive_path_str;
|
||||
mod derive_register_component;
|
||||
mod dynamic_spacing;
|
||||
|
||||
use proc_macro::TokenStream;
|
||||
|
@ -60,26 +60,28 @@ pub fn derive_dynamic_spacing(input: TokenStream) -> TokenStream {
|
|||
dynamic_spacing::derive_spacing(input)
|
||||
}
|
||||
|
||||
/// Derives the `Component` trait for a struct.
|
||||
/// Registers components that implement the `Component` trait.
|
||||
///
|
||||
/// This macro generates implementations for the `Component` trait and associated
|
||||
/// registration functions for the component system.
|
||||
/// This proc macro is used to automatically register structs that implement
|
||||
/// the `Component` trait with the [`component::ComponentRegistry`].
|
||||
///
|
||||
/// # Attributes
|
||||
///
|
||||
/// - `#[component(scope = "...")]`: Required. Specifies the scope of the component.
|
||||
/// - `#[component(description = "...")]`: Optional. Provides a description for the component.
|
||||
/// If the component trait is not implemented, it will generate a compile-time error.
|
||||
///
|
||||
/// # Example
|
||||
///
|
||||
/// ```
|
||||
/// use ui_macros::Component;
|
||||
/// use ui_macros::RegisterComponent;
|
||||
///
|
||||
/// #[derive(Component)]
|
||||
/// #[component(scope = "toggle", description = "A element that can be toggled on and off")]
|
||||
/// struct Checkbox;
|
||||
/// #[derive(RegisterComponent)]
|
||||
/// struct MyComponent;
|
||||
///
|
||||
/// impl Component for MyComponent {
|
||||
/// // Component implementation
|
||||
/// }
|
||||
/// ```
|
||||
#[proc_macro_derive(IntoComponent, attributes(component))]
|
||||
pub fn derive_component(input: TokenStream) -> TokenStream {
|
||||
derive_component::derive_into_component(input)
|
||||
///
|
||||
/// This example will add MyComponent to the ComponentRegistry.
|
||||
#[proc_macro_derive(RegisterComponent)]
|
||||
pub fn derive_register_component(input: TokenStream) -> TokenStream {
|
||||
derive_register_component::derive_register_component(input)
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue