Merge BuiltinItem and GeneratePropertyAccessors derive macros

... into the new SixtyFPSElement macro. This generates getters for all properties.
This commit is contained in:
Simon Hausmann 2021-01-07 10:58:15 +01:00
parent a48f4253a2
commit 65dcb2cb33
6 changed files with 60 additions and 97 deletions

View file

@ -325,7 +325,7 @@ pub trait GraphicsBackend: Sized {
} }
#[repr(C)] #[repr(C)]
#[derive(FieldOffsets, Default, BuiltinItem, Clone, Debug, PartialEq)] #[derive(FieldOffsets, Default, SixtyFPSElement, Clone, Debug, PartialEq)]
#[pin] #[pin]
/// PathLineTo describes the event of moving the cursor on the path to the specified location /// PathLineTo describes the event of moving the cursor on the path to the specified location
/// along a straight line. /// along a straight line.
@ -339,7 +339,7 @@ pub struct PathLineTo {
} }
#[repr(C)] #[repr(C)]
#[derive(FieldOffsets, Default, BuiltinItem, Clone, Debug, PartialEq)] #[derive(FieldOffsets, Default, SixtyFPSElement, Clone, Debug, PartialEq)]
#[pin] #[pin]
/// PathArcTo describes the event of moving the cursor on the path across an arc to the specified /// PathArcTo describes the event of moving the cursor on the path across an arc to the specified
/// x/y coordinates, with the specified x/y radius and additional properties. /// x/y coordinates, with the specified x/y radius and additional properties.

View file

@ -136,7 +136,7 @@ impl ItemWeak {
} }
#[repr(C)] #[repr(C)]
#[derive(FieldOffsets, Default, BuiltinItem, GeneratePropertyAccessors)] #[derive(FieldOffsets, Default, SixtyFPSElement)]
#[pin] #[pin]
/// The implementation of the `Rectangle` element /// The implementation of the `Rectangle` element
pub struct Rectangle { pub struct Rectangle {
@ -198,7 +198,7 @@ ItemVTable_static! {
} }
#[repr(C)] #[repr(C)]
#[derive(FieldOffsets, Default, BuiltinItem, GeneratePropertyAccessors)] #[derive(FieldOffsets, Default, SixtyFPSElement)]
#[pin] #[pin]
/// The implementation of the `BorderRectangle` element /// The implementation of the `BorderRectangle` element
pub struct BorderRectangle { pub struct BorderRectangle {
@ -276,7 +276,7 @@ ItemVTable_static! {
/// The implementation of the `TouchArea` element /// The implementation of the `TouchArea` element
#[repr(C)] #[repr(C)]
#[derive(FieldOffsets, Default, BuiltinItem)] #[derive(FieldOffsets, Default, SixtyFPSElement)]
#[pin] #[pin]
pub struct TouchArea { pub struct TouchArea {
pub x: Property<f32>, pub x: Property<f32>,
@ -373,7 +373,7 @@ ItemVTable_static! {
} }
#[repr(C)] #[repr(C)]
#[derive(FieldOffsets, Default, BuiltinItem)] #[derive(FieldOffsets, Default, SixtyFPSElement)]
#[pin] #[pin]
/// The implementation of the `Clip` element /// The implementation of the `Clip` element
pub struct Clip { pub struct Clip {
@ -433,7 +433,7 @@ ItemVTable_static! {
/// The implementation of the `Path` element /// The implementation of the `Path` element
#[repr(C)] #[repr(C)]
#[derive(FieldOffsets, Default, BuiltinItem)] #[derive(FieldOffsets, Default, SixtyFPSElement)]
#[pin] #[pin]
pub struct Path { pub struct Path {
pub x: Property<f32>, pub x: Property<f32>,
@ -496,7 +496,7 @@ ItemVTable_static! {
/// The implementation of the `Flickable` element /// The implementation of the `Flickable` element
#[repr(C)] #[repr(C)]
#[derive(FieldOffsets, Default, BuiltinItem)] #[derive(FieldOffsets, Default, SixtyFPSElement)]
#[pin] #[pin]
pub struct Flickable { pub struct Flickable {
pub x: Property<f32>, pub x: Property<f32>,
@ -604,7 +604,7 @@ pub unsafe extern "C" fn sixtyfps_flickable_data_free(data: *mut FlickableDataBo
/// The implementation of the `PropertyAnimation` element /// The implementation of the `PropertyAnimation` element
#[repr(C)] #[repr(C)]
#[derive(FieldOffsets, Default, BuiltinItem, Clone, Debug)] #[derive(FieldOffsets, Default, SixtyFPSElement, Clone, Debug)]
#[pin] #[pin]
pub struct PropertyAnimation { pub struct PropertyAnimation {
#[rtti_field] #[rtti_field]
@ -617,7 +617,7 @@ pub struct PropertyAnimation {
/// The implementation of the `Window` element /// The implementation of the `Window` element
#[repr(C)] #[repr(C)]
#[derive(FieldOffsets, Default, BuiltinItem)] #[derive(FieldOffsets, Default, SixtyFPSElement)]
#[pin] #[pin]
pub struct Window { pub struct Window {
pub width: Property<f32>, pub width: Property<f32>,

View file

@ -50,7 +50,7 @@ impl Default for ImageFit {
} }
#[repr(C)] #[repr(C)]
#[derive(FieldOffsets, Default, BuiltinItem)] #[derive(FieldOffsets, Default, SixtyFPSElement)]
#[pin] #[pin]
/// The implementation of the `Image` element /// The implementation of the `Image` element
pub struct Image { pub struct Image {
@ -108,7 +108,7 @@ impl ItemConsts for Image {
} }
#[repr(C)] #[repr(C)]
#[derive(FieldOffsets, Default, BuiltinItem)] #[derive(FieldOffsets, Default, SixtyFPSElement)]
#[pin] #[pin]
/// The implementation of the `ClippedImage` element /// The implementation of the `ClippedImage` element
pub struct ClippedImage { pub struct ClippedImage {

View file

@ -71,7 +71,7 @@ const DEFAULT_FONT_WEIGHT: i32 = 400;
/// The implementation of the `Text` element /// The implementation of the `Text` element
#[repr(C)] #[repr(C)]
#[derive(FieldOffsets, Default, BuiltinItem)] #[derive(FieldOffsets, Default, SixtyFPSElement)]
#[pin] #[pin]
pub struct Text { pub struct Text {
pub text: Property<SharedString>, pub text: Property<SharedString>,
@ -139,14 +139,6 @@ impl ItemConsts for Text {
} }
impl Text { impl Text {
fn font_family(self: Pin<&Self>) -> SharedString {
Self::FIELD_OFFSETS.font_family.apply_pin(self).get()
}
fn font_weight(self: Pin<&Self>) -> i32 {
Self::FIELD_OFFSETS.font_weight.apply_pin(self).get()
}
pub fn font_pixel_size(self: Pin<&Self>, scale_factor: f32) -> f32 { pub fn font_pixel_size(self: Pin<&Self>, scale_factor: f32) -> f32 {
let font_size = Self::FIELD_OFFSETS.font_size.apply_pin(self).get(); let font_size = Self::FIELD_OFFSETS.font_size.apply_pin(self).get();
if font_size == 0.0 { if font_size == 0.0 {
@ -180,7 +172,7 @@ impl Text {
/// The implementation of the `TextInput` element /// The implementation of the `TextInput` element
#[repr(C)] #[repr(C)]
#[derive(FieldOffsets, Default, BuiltinItem)] #[derive(FieldOffsets, Default, SixtyFPSElement)]
#[pin] #[pin]
pub struct TextInput { pub struct TextInput {
pub text: Property<SharedString>, pub text: Property<SharedString>,
@ -562,14 +554,6 @@ impl TextInput {
} }
} }
fn font_family(self: Pin<&Self>) -> SharedString {
Self::FIELD_OFFSETS.font_family.apply_pin(self).get()
}
fn font_weight(self: Pin<&Self>) -> i32 {
Self::FIELD_OFFSETS.font_weight.apply_pin(self).get()
}
fn font_pixel_size(self: Pin<&Self>, scale_factor: f32) -> f32 { fn font_pixel_size(self: Pin<&Self>, scale_factor: f32) -> f32 {
let font_size = Self::FIELD_OFFSETS.font_size.apply_pin(self).get(); let font_size = Self::FIELD_OFFSETS.font_size.apply_pin(self).get();
if font_size == 0.0 { if font_size == 0.0 {

View file

@ -16,8 +16,14 @@ extern crate proc_macro;
use proc_macro::TokenStream; use proc_macro::TokenStream;
use quote::quote; use quote::quote;
#[proc_macro_derive(BuiltinItem, attributes(rtti_field))] /// This derive macro is used with structures in the run-time library that are meant
pub fn builtin_item(input: TokenStream) -> TokenStream { /// to be exposed to the language. The structure is introspected for properties and fields
/// marked with the `rtti_field` attribute and generates run-time type information for use
/// with the interpeter.
/// In addition all `Property<T> foo` fields get a convenient getter function generated
/// that works on a `Pin<&Self>` receiver.
#[proc_macro_derive(SixtyFPSElement, attributes(rtti_field))]
pub fn sixtyfps_element(input: TokenStream) -> TokenStream {
let input = syn::parse_macro_input!(input as syn::DeriveInput); let input = syn::parse_macro_input!(input as syn::DeriveInput);
let fields = match &input.data { let fields = match &input.data {
@ -32,11 +38,23 @@ pub fn builtin_item(input: TokenStream) -> TokenStream {
} }
}; };
let (prop_field_names, prop_field_types): (Vec<_>, Vec<_>) = fields let mut pub_prop_field_names = Vec::new();
.iter() let mut pub_prop_field_types = Vec::new();
.filter(|f| is_property(&f.ty) && matches!(f.vis, syn::Visibility::Public(_))) let mut property_names = Vec::new();
.map(|f| (f.ident.as_ref().unwrap(), &f.ty)) let mut property_visibility = Vec::new();
.unzip(); let mut property_types = Vec::new();
for field in fields {
if let Some(property_type) = property_type(&field.ty) {
let name = field.ident.as_ref().unwrap();
if matches!(field.vis, syn::Visibility::Public(_)) {
pub_prop_field_names.push(name);
pub_prop_field_types.push(&field.ty);
}
property_names.push(name);
property_visibility.push(field.vis.clone());
property_types.push(property_type);
}
}
let (plain_field_names, plain_field_types): (Vec<_>, Vec<_>) = fields let (plain_field_names, plain_field_types): (Vec<_>, Vec<_>) = fields
.iter() .iter()
@ -73,9 +91,9 @@ pub fn builtin_item(input: TokenStream) -> TokenStream {
} }
fn properties<Value: ValueType>() -> Vec<(&'static str, &'static dyn PropertyInfo<Self, Value>)> { fn properties<Value: ValueType>() -> Vec<(&'static str, &'static dyn PropertyInfo<Self, Value>)> {
vec![#( { vec![#( {
const O : MaybeAnimatedPropertyInfoWrapper<#item_name, #prop_field_types> = const O : MaybeAnimatedPropertyInfoWrapper<#item_name, #pub_prop_field_types> =
MaybeAnimatedPropertyInfoWrapper(#item_name::FIELD_OFFSETS.#prop_field_names); MaybeAnimatedPropertyInfoWrapper(#item_name::FIELD_OFFSETS.#pub_prop_field_names);
(stringify!(#prop_field_names), (&O).as_property_info()) (stringify!(#pub_prop_field_names), (&O).as_property_info())
} ),*] } ),*]
} }
fn fields<Value: ValueType>() -> Vec<(&'static str, &'static dyn FieldInfo<Self, Value>)> { fn fields<Value: ValueType>() -> Vec<(&'static str, &'static dyn FieldInfo<Self, Value>)> {
@ -91,6 +109,14 @@ pub fn builtin_item(input: TokenStream) -> TokenStream {
),*] ),*]
} }
} }
impl #item_name {
#(
#property_visibility fn #property_names(self: core::pin::Pin<&Self>) -> #property_types {
Self::FIELD_OFFSETS.#property_names.apply_pin(self).get()
}
)*
}
) )
.into() .into()
} }
@ -99,10 +125,6 @@ fn type_name(ty: &syn::Type) -> String {
quote!(#ty).to_string() quote!(#ty).to_string()
} }
fn is_property(ty: &syn::Type) -> bool {
type_name(ty).starts_with("Property <")
}
// Try to match `Property<Foo>` on the syn tree and return Foo if found // Try to match `Property<Foo>` on the syn tree and return Foo if found
fn property_type(ty: &syn::Type) -> Option<&syn::Type> { fn property_type(ty: &syn::Type) -> Option<&syn::Type> {
if let syn::Type::Path(syn::TypePath { path: syn::Path { segments, .. }, .. }) = ty { if let syn::Type::Path(syn::TypePath { path: syn::Path { segments, .. }, .. }) = ty {
@ -155,46 +177,3 @@ pub fn keycode_mapping(input: TokenStream) -> TokenStream {
) )
.into() .into()
} }
#[proc_macro_derive(GeneratePropertyAccessors)]
pub fn generate_property_accessors(input: TokenStream) -> TokenStream {
let input = syn::parse_macro_input!(input as syn::DeriveInput);
let fields = match &input.data {
syn::Data::Struct(syn::DataStruct { fields: f @ syn::Fields::Named(..), .. }) => f,
_ => {
return syn::Error::new(
input.ident.span(),
"Only `struct` with named field are supported",
)
.to_compile_error()
.into()
}
};
let (prop_field_names, prop_field_types): (Vec<_>, Vec<_>) = fields
.iter()
.filter_map(|field| {
property_type(&field.ty).and_then(|prop_type| {
if matches!(field.vis, syn::Visibility::Public(_)) {
Some((field.ident.as_ref().unwrap(), prop_type))
} else {
None
}
})
})
.unzip();
let item_name = &input.ident;
quote!(
impl #item_name {
#(
pub fn #prop_field_names(self: Pin<&Self>) -> #prop_field_types {
Self::FIELD_OFFSETS.#prop_field_names.apply_pin(self).get()
}
)*
}
)
.into()
}

View file

@ -127,7 +127,7 @@ cpp! {{
}} }}
#[repr(C)] #[repr(C)]
#[derive(FieldOffsets, Default, BuiltinItem)] #[derive(FieldOffsets, Default, SixtyFPSElement)]
#[pin] #[pin]
pub struct NativeButton { pub struct NativeButton {
pub x: Property<f32>, pub x: Property<f32>,
@ -248,7 +248,7 @@ impl ItemConsts for NativeButton {
ItemVTable_static! { #[no_mangle] pub static NativeButtonVTable for NativeButton } ItemVTable_static! { #[no_mangle] pub static NativeButtonVTable for NativeButton }
#[repr(C)] #[repr(C)]
#[derive(FieldOffsets, Default, BuiltinItem)] #[derive(FieldOffsets, Default, SixtyFPSElement)]
#[pin] #[pin]
pub struct NativeCheckBox { pub struct NativeCheckBox {
pub x: Property<f32>, pub x: Property<f32>,
@ -364,7 +364,7 @@ struct NativeSpinBoxData {
} }
#[repr(C)] #[repr(C)]
#[derive(FieldOffsets, Default, BuiltinItem)] #[derive(FieldOffsets, Default, SixtyFPSElement)]
#[pin] #[pin]
pub struct NativeSpinBox { pub struct NativeSpinBox {
pub x: Property<f32>, pub x: Property<f32>,
@ -574,7 +574,7 @@ struct NativeSliderData {
} }
#[repr(C)] #[repr(C)]
#[derive(FieldOffsets, Default, BuiltinItem)] #[derive(FieldOffsets, Default, SixtyFPSElement)]
#[pin] #[pin]
pub struct NativeSlider { pub struct NativeSlider {
pub x: Property<f32>, pub x: Property<f32>,
@ -768,7 +768,7 @@ impl ItemConsts for NativeSlider {
ItemVTable_static! { #[no_mangle] pub static NativeSliderVTable for NativeSlider } ItemVTable_static! { #[no_mangle] pub static NativeSliderVTable for NativeSlider }
#[repr(C)] #[repr(C)]
#[derive(FieldOffsets, Default, BuiltinItem)] #[derive(FieldOffsets, Default, SixtyFPSElement)]
#[pin] #[pin]
pub struct NativeGroupBox { pub struct NativeGroupBox {
pub x: Property<f32>, pub x: Property<f32>,
@ -961,7 +961,7 @@ impl ItemConsts for NativeGroupBox {
ItemVTable_static! { #[no_mangle] pub static NativeGroupBoxVTable for NativeGroupBox } ItemVTable_static! { #[no_mangle] pub static NativeGroupBoxVTable for NativeGroupBox }
#[repr(C)] #[repr(C)]
#[derive(FieldOffsets, Default, BuiltinItem)] #[derive(FieldOffsets, Default, SixtyFPSElement)]
#[pin] #[pin]
pub struct NativeLineEdit { pub struct NativeLineEdit {
pub x: Property<f32>, pub x: Property<f32>,
@ -1101,7 +1101,7 @@ impl ItemConsts for NativeLineEdit {
ItemVTable_static! { #[no_mangle] pub static NativeLineEditVTable for NativeLineEdit } ItemVTable_static! { #[no_mangle] pub static NativeLineEditVTable for NativeLineEdit }
#[repr(C)] #[repr(C)]
#[derive(FieldOffsets, Default, BuiltinItem)] #[derive(FieldOffsets, Default, SixtyFPSElement)]
#[pin] #[pin]
pub struct NativeScrollView { pub struct NativeScrollView {
pub x: Property<f32>, pub x: Property<f32>,
@ -1470,7 +1470,7 @@ impl ItemConsts for NativeScrollView {
ItemVTable_static! { #[no_mangle] pub static NativeScrollViewVTable for NativeScrollView } ItemVTable_static! { #[no_mangle] pub static NativeScrollViewVTable for NativeScrollView }
#[repr(C)] #[repr(C)]
#[derive(FieldOffsets, Default, BuiltinItem)] #[derive(FieldOffsets, Default, SixtyFPSElement)]
#[pin] #[pin]
pub struct NativeStandardListViewItem { pub struct NativeStandardListViewItem {
pub x: Property<f32>, pub x: Property<f32>,
@ -1585,7 +1585,7 @@ impl ItemConsts for NativeStandardListViewItem {
ItemVTable_static! { #[no_mangle] pub static NativeStandardListViewItemVTable for NativeStandardListViewItem } ItemVTable_static! { #[no_mangle] pub static NativeStandardListViewItemVTable for NativeStandardListViewItem }
#[repr(C)] #[repr(C)]
#[derive(FieldOffsets, Default, BuiltinItem)] #[derive(FieldOffsets, Default, SixtyFPSElement)]
#[pin] #[pin]
pub struct NativeComboBox { pub struct NativeComboBox {
pub x: Property<f32>, pub x: Property<f32>,
@ -1717,7 +1717,7 @@ impl ItemConsts for NativeComboBox {
ItemVTable_static! { #[no_mangle] pub static NativeComboBoxVTable for NativeComboBox } ItemVTable_static! { #[no_mangle] pub static NativeComboBoxVTable for NativeComboBox }
#[repr(C)] #[repr(C)]
#[derive(FieldOffsets, BuiltinItem)] #[derive(FieldOffsets, SixtyFPSElement)]
#[pin] #[pin]
pub struct NativeStyleMetrics { pub struct NativeStyleMetrics {
pub layout_spacing: Property<f32>, pub layout_spacing: Property<f32>,