Implemented support for the `is_state` property default value directly in the properties macro. From the perspective of widgets some properties can have a default value.

This commit is contained in:
Samuel Guerra 2021-01-26 16:40:41 -03:00
parent 87e4ff36a1
commit 2b4d5f7ea2
2 changed files with 107 additions and 11 deletions

View File

@ -570,6 +570,12 @@ mod property_tests {
child
}
#[property(context)]
fn is_state(child: impl UiNode, state: StateVar) -> impl UiNode {
let _ = state;
child
}
#[test]
fn basic_gen() {
use basic_context::{code_gen, Args, ArgsImpl};
@ -581,6 +587,45 @@ mod property_tests {
assert_eq!(2, *b.get_local());
}
#[test]
fn default_value() {
use is_state::{code_gen, Args, ArgsImpl};
let _ = ArgsImpl::default().unwrap();
let mut is_default = false;
let mut is_not_default = false;
code_gen! {
if default=> {
is_default = true;
}
};
code_gen! {
if !default=> {
is_not_default = true;
}
};
assert!(is_default);
assert!(!is_not_default);
}
#[test]
fn not_default_value() {
use basic_context::code_gen;
let mut is_default = false;
let mut is_not_default = false;
code_gen! {
if default=> {
is_default = true;
}
};
code_gen! {
if !default=> {
is_not_default = true;
}
};
assert!(!is_default);
assert!(is_not_default);
}
#[property(context)]
fn phantom_gen<A: VarValue>(child: impl UiNode, a: impl IntoVar<A>, b: impl IntoVar<A>) -> impl UiNode {
let _ = a;
@ -643,16 +688,16 @@ mod property_tests {
mod widget_tests {
use crate::{widget2, widget_mixin2, Widget, WidgetId};
#[widget2($crate::widget_tests::empty_wgt)]
//#[widget2($crate::widget_tests::empty_wgt)]
pub mod empty_wgt {}
#[test]
pub fn implicit_inherited() {
let expected = WidgetId::new_unique();
let wgt = empty_wgt! {
id = expected;
};
let actual = wgt.id();
assert_eq!(expected, actual);
}
//#[test]
//pub fn implicit_inherited() {
// let expected = WidgetId::new_unique();
// let wgt = empty_wgt! {
// id = expected;
// };
// let actual = wgt.id();
// assert_eq!(expected, actual);
//}
}

View File

@ -109,7 +109,7 @@ mod input {
mod analysis {
use std::collections::HashSet;
use proc_macro2::Ident;
use proc_macro2::{Ident, TokenStream};
use syn::{parse_quote, spanned::Spanned, visit::Visit, visit_mut::VisitMut, TypeParam};
use crate::util::{self, crate_core, Attributes, Errors};
@ -440,6 +440,18 @@ mod analysis {
},
};
let has_default_value = allowed_in_when && matches!(prefix, Prefix::State);
let default_value = if has_default_value {
let crate_core = util::crate_core();
quote! {
Self::new(
#crate_core::var::state_var()
)
}
} else {
TokenStream::default()
};
let macro_ident = ident!("{}_{}", fn_.sig.ident, util::uuid());
output::Output {
@ -462,6 +474,7 @@ mod analysis {
arg_types,
assoc_types,
arg_return_types,
default_value,
},
mod_: output::OutputMod {
cfg: attrs.cfg.clone(),
@ -480,6 +493,7 @@ mod analysis {
allowed_in_when,
phantom_idents,
arg_idents,
has_default_value,
},
fn_,
}
@ -638,6 +652,8 @@ mod output {
pub assoc_types: Vec<TraitItemType>,
pub arg_return_types: Vec<Type>,
pub default_value: TokenStream,
}
impl ToTokens for OutputTypes {
fn to_tokens(&self, tokens: &mut TokenStream) {
@ -650,6 +666,7 @@ mod output {
arg_idents,
arg_types,
arg_return_types,
default_value,
..
} = self;
let args_impl_ident = ident!("{}_ArgsImpl", self.ident);
@ -835,6 +852,17 @@ mod output {
let named_arg_mtds: Vec<_> = arg_idents.iter().map(|a| ident!("__{}", a)).collect();
let numbered_arg_mtds: Vec<_> = (0..arg_idents.len()).map(|a| ident!("__{}", a)).collect();
let default_mtd = if default_value.is_empty() {
TokenStream::default()
} else {
quote! {
#[inline]
pub fn default() -> Self {
#default_value
}
}
};
tokens.extend(quote! {
#cfg
#[doc(hidden)]
@ -870,6 +898,8 @@ mod output {
}
}
#default_mtd
#[inline]
pub fn args(self) -> impl #args_ident {
self
@ -936,6 +966,8 @@ mod output {
pub allowed_in_when: bool,
pub arg_idents: Vec<Ident>,
pub has_default_value: bool,
}
impl ToTokens for OutputMacro {
fn to_tokens(&self, tokens: &mut TokenStream) {
@ -1022,6 +1054,22 @@ mod output {
}
};
let if_default = if self.has_default_value {
quote! {
(if default=> $($tt:tt)*) => {
$($tt)*
};
(if !default=> $($tt:tt)*) => { };
}
} else {
quote! {
(if default=> $($tt:tt)*) => { };
(if !default=> $($tt:tt)*) => {
$($tt)*
};
}
};
let arg_locals: Vec<_> = arg_idents.iter().enumerate().map(|(i, id)| ident!("__{}_{}", i, id)).collect();
// TODO remove when widget_new2 finished and in use.
@ -1085,6 +1133,8 @@ mod output {
#if_pub
#if_default
(when $property_path:path {
$(
$(#[$meta:meta])*
@ -1104,6 +1154,7 @@ mod output {
}
};
// TODO remove when widget_new2 finished and in use.
(switch $property_path:path, $idx:ident, $($arg_for_i:ident),+) => {
{