Finished when -> if workaround, but it does not work for multiple whens.

This commit is contained in:
Samuel Guerra 2020-04-02 00:16:16 -03:00
parent b04b780d5c
commit 6793240130
4 changed files with 62 additions and 26 deletions

View File

@ -7,3 +7,4 @@ req
fnv
Fnv
Deinit
syn

View File

@ -883,7 +883,7 @@ impl<T: VarValue, V: Var<T> + Clone> ObjVar<T> for ReadOnlyVar<T, V> {
}
}
impl<T: VarValue, V: Var<T> + Clone> Clone for ReadOnlyVar<T, V> {
impl<T: VarValue, V: Var<T>> Clone for ReadOnlyVar<T, V> {
fn clone(&self) -> Self {
ReadOnlyVar {
_t: PhantomData,
@ -892,7 +892,7 @@ impl<T: VarValue, V: Var<T> + Clone> Clone for ReadOnlyVar<T, V> {
}
}
impl<T: VarValue, V: Var<T> + Clone> Var<T> for ReadOnlyVar<T, V> {
impl<T: VarValue, V: Var<T>> Var<T> for ReadOnlyVar<T, V> {
type AsReadOnly = Self;
type AsLocal = CloningLocalVar<T, Self>;
@ -931,6 +931,14 @@ impl<T: VarValue, V: Var<T> + Clone> Var<T> for ReadOnlyVar<T, V> {
}
}
impl<T: VarValue, V: Var<T>> IntoVar<T> for ReadOnlyVar<T, V> {
type Var = Self;
#[inline]
fn into_var(self) -> Self::Var {
self
}
}
// #endregion ReadOnlyVar<T>
// #region MapSharedVar<T> and MapBiDiSharedVar<T>
@ -1857,6 +1865,22 @@ where
}
}
impl<T, S, O, M, N> IntoVar<O> for MapVarBiDi<T, S, O, M, N>
where
T: VarValue,
S: ObjVar<T>,
O: VarValue,
M: FnMut(&T) -> O + 'static,
N: FnMut(&O) -> T + 'static,
{
type Var = Self;
#[inline]
fn into_var(self) -> Self::Var {
self
}
}
// #endregion MapVar<T> and MapVarBidi<T>
// #region SwitchVar2<T>..SwitchVar8<T>

View File

@ -4,7 +4,6 @@ pub use crate::properties::{align, background_color, is_hovered, is_pressed, on_
use crate::widget;
use crate::widgets::{container, focusable_mixin};
context_var! {
/// Default background of [`button!`](crate::widgets::button) widgets.
pub struct ButtonBackground: ColorF = rgb(0.2, 0.2, 0.2);
@ -25,16 +24,16 @@ widget! {
background_color: ButtonBackground;
}
///// When the pointer device is over this button.
//when self.is_hovered {
// background_color: ButtonBackgroundHovered;
/// When the pointer device is over this button.
when self.is_hovered {
background_color: ButtonBackgroundHovered;
}
///// When the mouse or touch pressed on this button and has not yet released.
//when self.is_pressed {
// background_color: ButtonBackgroundPressed;
//}
/// When the mouse or touch pressed on this button and has not yet released.
when self.content_align == crate::properties::Alignment::CENTER {
background_color: ButtonBackgroundPressed;
}
///// When the button is not enabled.
//when {
// for i in 0..1000 {
@ -44,12 +43,10 @@ widget! {
// return self.is_hovered.0
// }
// }
// self.is_pressed && test(self.content_align)
// self.is_pressed
//} {
// background_color: ButtonBackgroundDisabled;
//}
}
fn test(a: crate::properties::Alignment) -> bool {
a == crate::properties::Alignment::CENTER
}
//TODO support properties with IntoVar parameters.

View File

@ -368,7 +368,8 @@ fn declare_widget(mixin: bool, mut input: WidgetInput) -> proc_macro::TokenStrea
let pss = properties
.clone()
.map(|p| if defined_props.contains(p) { quote! (ps::) } else { quote!() });
let params = quote!(#(#param_names: &impl #pss#properties::Args),*); {}
let params = quote!(#(#param_names: &impl #pss#properties::Args),*);
{}
let local_names = property_members.keys();
let param_names = property_members.values().map(|p| &property_params[&p.property]);
@ -944,22 +945,35 @@ pub struct WhenBlock {
}
impl Parse for WhenBlock {
fn parse(input: ParseStream) -> Result<Self> {
// we use `ExprIf` parse because we need the private
// function `syn::expr::parsing::expr_no_struct` to
// parse the condition expression.
// require `when`.
input.parse::<keyword::when>()?;
let mut if_ = quote! ( if );
// recreate a TokenStream where `when` is `if`.
let mut if_ = quote!(if);
if_.extend(input.parse::<TokenStream>()?);
panic!{"hello {}", quote!{#if_}}
let condition = input.parse()?;
let if_: ExprIf = syn::parse2(if_)?;
let inner;
braced!(inner in input);
let condition = *if_.cond;
let attrs = if_.attrs;
let attrs = Attribute::parse_inner(input)?;
let ps = if_.then_branch;
let ps = quote!(#ps);
let mut properties = vec![];
while !inner.is_empty() {
properties.push(inner.parse()?);
}
let parse_properties = |input: ParseStream| {
let inner;
braced!(inner in input);
let mut properties = vec![];
while !inner.is_empty() {
properties.push(inner.parse()?);
}
Ok(properties)
};
let properties = parse_properties.parse2(ps)?;
Ok(WhenBlock {
attrs,