More refactor.

This commit is contained in:
Samuel Guerra 2023-04-13 01:22:27 -03:00
parent b41e5be8d6
commit 30c5209fc0
13 changed files with 177 additions and 134 deletions

View File

@ -114,14 +114,14 @@ mod widgets {
#[property(BORDER, default(0, BorderStyle::Hidden), impl(MrBorders))]
pub fn border0(child: impl UiNode, widths: impl IntoVar<SideOffsets>, sides: impl IntoVar<BorderSides>) -> impl UiNode {
border(child, widths,sides)
border(child, widths, sides)
}
#[property(BORDER, default(0, BorderStyle::Hidden), impl(MrBorders))]
pub fn border1(child: impl UiNode, widths: impl IntoVar<SideOffsets>, sides: impl IntoVar<BorderSides>) -> impl UiNode {
border(child, widths,sides)
border(child, widths, sides)
}
#[property(BORDER, default(0, BorderStyle::Hidden), impl(MrBorders))]
pub fn border2(child: impl UiNode, widths: impl IntoVar<SideOffsets>, sides: impl IntoVar<BorderSides>) -> impl UiNode {
border(child, widths,sides)
border(child, widths, sides)
}
}

View File

@ -388,23 +388,23 @@ impl ImgWindow {
// use zero_ui::core::render::webrender_api::DebugFlags;
// DebugFlags::TEXTURE_CACHE_DBG | DebugFlags::TEXTURE_CACHE_DBG_CLEAR_EVICTED
// };
// render_mode = RenderMode::Software;
child_align = Align::CENTER;
state = WindowState::Maximized;
size = (1140, 770);// restore size
icon = "examples/res/image/RGB8.png";
background = Checkerboard!();
color_scheme = ColorScheme::Dark;
// content shown by all images when loading.
img_loading_fn = wgt_fn!(|_| loading());
// content shown by all images that failed to load.
img_error_fn = wgt_fn!(|args: ImageErrorArgs| {
center_viewport(Text! {
@ -419,7 +419,7 @@ impl ImgWindow {
};
})
});
// button color
button::base_colors = (rgb(0, 0, 40), rgb(0, 0, 255 - 40));
}

View File

@ -457,7 +457,7 @@ impl CfgWhenWgt {
// suppress warning in all assigns.
#[allow(non_snake_case)]
when *#is_state {
live_trace = {
util::live_trace = {
#[allow(clippy::needless_late_init)]
let weird___name;
weird___name = "is_state";
@ -992,9 +992,9 @@ pub fn allowed_in_when_without_wgt_assign2() {
let _app = App::minimal().run_headless(false);
WINDOW.with_test_context(|| {
let mut wgt = EmptyWgt! {
// live_trace_default = "default-trace";
// util::live_trace_default = "default-trace";
when *#util::is_state {
live_trace_default = "when-trace";
util::live_trace_default = "when-trace";
}
};

View File

@ -93,6 +93,26 @@ impl WidgetBase {
self.importance = Importance::INSTANCE;
}
/// Start building a `when` block, all properties set after this call go on the when block.
pub fn start_when_block(&mut self) {
todo!("!!: ")
}
/// End the current `when` block, all properties set after this call go on the widget.
pub fn end_when_block(&mut self) {
self.builder.get_mut().as_mut().expect("cannot set `when` after build").push_when(
self.importance,
WhenInfo {
inputs: todo!(),
state: todo!(),
assigns: todo!(),
build_action_data: todo!(),
expr: todo!(),
location: todo!(),
},
);
}
#[doc(hidden)]
pub fn on_start__(&mut self) {
if !self.started {

View File

@ -120,7 +120,7 @@ pub use crate::property_id;
///
/// * `path::property`: Gets the info for the property function.
/// * `Self::property`: Gets the info for the property on the `#[widget(on_start)]`.
///
///
/// If the property is generic a `::<T>` is also required.
///
/// # Examples

View File

@ -116,11 +116,7 @@ pub fn expand(args: proc_macro::TokenStream, input: proc_macro::TokenStream) ->
let generics = &item.sig.generics;
let has_generics = !generics.params.empty_or_trailing();
let (impl_gens, ty_gens, where_gens) = generics.split_for_impl();
let path_gens = if has_generics {
quote!(::#ty_gens)
} else {
quote!()
};
let path_gens = if has_generics { quote!(::#ty_gens) } else { quote!() };
let ident_unset = ident!("unset_{}", ident);
let ident_args = ident!("{}_args__", ident);

View File

@ -1,6 +1,6 @@
use proc_macro2::{Span, TokenStream, TokenTree};
use quote::{quote, ToTokens};
use syn::{parse::Parse, spanned::Spanned, *};
use syn::{ext::IdentExt, parse::Parse, spanned::Spanned, *};
use crate::{
util::{self, parse_outer_attrs, ErrorRecoverable, Errors},
@ -382,7 +382,7 @@ impl Parse for Properties {
whens.push(when);
}
} else if input.peek(Token![pub])
|| input.peek(Ident)
|| input.peek(Ident::peek_any)
|| input.peek(Token![crate])
|| input.peek(Token![super])
|| input.peek(Token![self])
@ -394,7 +394,7 @@ impl Parse for Properties {
if !input.is_empty() && p.semi.is_none() {
errors.push("expected `;`", input.span());
while !(input.is_empty()
|| input.peek(Ident)
|| input.peek(Ident::peek_any)
|| input.peek(Token![crate])
|| input.peek(Token![super])
|| input.peek(Token![self])
@ -450,107 +450,33 @@ pub fn expand_new(args: proc_macro::TokenStream) -> proc_macro::TokenStream {
let mut set_props = quote!();
for prop in &p.properties {
let custom_expand = if prop.has_custom_attrs() {
prop.custom_attrs_expand(ident!("wgt__"), None)
} else {
quote!()
};
let attrs = prop.attrs.cfg_and_lints();
set_props.extend(prop_assign(prop, &mut p.errors));
}
let ident = prop.ident();
let path = &prop.path;
let generics = &prop.generics;
macro_rules! quote_call {
(#$mtd:ident ( $($args:tt)* )) => {
if path.get_ident().is_some() {
quote! {
wgt__.#$mtd #generics($($args)*);
}
} else {
quote! {
#path::#$mtd #generics(#core::widget_base::WidgetImpl::base(wgt__), $($args)*);
}
}
}
let mut set_whens = quote!();
for when in &p.whens {
let mut assigns = quote!();
for prop in &when.assigns {
assigns.extend(prop_assign(prop, &mut p.errors));
}
let prop_init;
match &prop.value {
Some((_, val)) => match val {
widget_util::PropertyValue::Special(special, _) => {
if prop.is_unset() {
let unset_ident = ident_spanned!(ident.span()=> "unset_{}", ident);
prop_init = quote_call! {
#unset_ident()
};
} else {
p.errors.push("unknown value, expected `unset!`", special.span());
continue;
}
}
widget_util::PropertyValue::Unnamed(val) => {
prop_init = quote_call! {
#ident(#val)
};
}
widget_util::PropertyValue::Named(_, fields) => {
let mut idents_sorted: Vec<_> = fields.iter().map(|f| &f.ident).collect();
idents_sorted.sort();
let idents = fields.iter().map(|f| &f.ident);
let values = fields.iter().map(|f| &f.expr);
let ident_sorted = ident_spanned!(ident.span()=> "{}_sorted__", ident);
let ident_meta = ident_spanned!(ident.span()=> "{}_meta__", ident);
let call = quote_call! {
#ident_sorted(#(#idents_sorted),*)
};
let meta = if path.get_ident().is_some() {
quote! {
wgt__.#ident_meta()
}
} else {
quote! {
<#core::widget_builder::WgtInfo as #path>::#ident_meta(&#core::widget_builder::WgtInfo)
}
};
prop_init = quote! {
{
let meta__ = #meta;
#(
let #idents = meta__.inputs().#idents(#values);
)*
#call
}
};
}
},
None => match prop.path.get_ident() {
Some(_) => {
prop_init = quote_call! {
#ident(#ident)
};
}
None => {
p.errors.push("missing value", util::path_span(&prop.path));
continue;
}
},
}
set_props.extend(quote! {
#attrs {
#custom_expand
#prop_init
set_whens.extend(quote! {
{
#core::widget_base::WidgetImpl::base(wgt__).start_when_block();
#assigns
#core::widget_base::WidgetImpl::base(wgt__).end_when_block();
}
});
}
let errors = p.errors;
let r = quote! {
#errors
{
#start
#set_props
#set_whens
#end
}
};
@ -558,6 +484,107 @@ pub fn expand_new(args: proc_macro::TokenStream) -> proc_macro::TokenStream {
r.into()
}
fn prop_assign(prop: &WgtProperty, errors: &mut Errors) -> TokenStream {
let core = util::crate_core();
let custom_expand = if prop.has_custom_attrs() {
prop.custom_attrs_expand(ident!("wgt__"), None)
} else {
quote!()
};
let attrs = prop.attrs.cfg_and_lints();
let ident = prop.ident();
let path = &prop.path;
let generics = &prop.generics;
macro_rules! quote_call {
(#$mtd:ident ( $($args:tt)* )) => {
if path.get_ident().is_some() {
quote! {
wgt__.#$mtd #generics($($args)*);
}
} else {
quote! {
#path::#$mtd #generics(#core::widget_base::WidgetImpl::base(wgt__), $($args)*);
}
}
}
}
let prop_init;
match &prop.value {
Some((_, val)) => match val {
widget_util::PropertyValue::Special(special, _) => {
if prop.is_unset() {
let unset_ident = ident_spanned!(ident.span()=> "unset_{}", ident);
prop_init = quote_call! {
#unset_ident()
};
} else {
errors.push("unknown value, expected `unset!`", special.span());
return quote!();
}
}
widget_util::PropertyValue::Unnamed(val) => {
prop_init = quote_call! {
#ident(#val)
};
}
widget_util::PropertyValue::Named(_, fields) => {
let mut idents_sorted: Vec<_> = fields.iter().map(|f| &f.ident).collect();
idents_sorted.sort();
let idents = fields.iter().map(|f| &f.ident);
let values = fields.iter().map(|f| &f.expr);
let ident_sorted = ident_spanned!(ident.span()=> "{}_sorted__", ident);
let ident_meta = ident_spanned!(ident.span()=> "{}_meta__", ident);
let call = quote_call! {
#ident_sorted(#(#idents_sorted),*)
};
let meta = if path.get_ident().is_some() {
quote! {
wgt__.#ident_meta()
}
} else {
quote! {
<#core::widget_builder::WgtInfo as #path>::#ident_meta(&#core::widget_builder::WgtInfo)
}
};
prop_init = quote! {
{
let meta__ = #meta;
#(
let #idents = meta__.inputs().#idents(#values);
)*
#call
}
};
}
},
None => match prop.path.get_ident() {
Some(_) => {
prop_init = quote_call! {
#ident(#ident)
};
}
None => {
errors.push("missing value", util::path_span(&prop.path));
return quote!();
}
},
}
quote! {
#attrs {
#custom_expand
#prop_init
}
}
}
struct NewArgs {
start: TokenStream,
end: TokenStream,

View File

@ -390,13 +390,13 @@ impl WgtWhen {
while !inner.is_empty() {
let attrs = parse_outer_attrs(&inner, errors);
if !(inner.peek(Ident) || inner.peek(Token![super]) || inner.peek(Token![self])) {
if !(inner.peek(Ident::peek_any) || inner.peek(Token![super]) || inner.peek(Token![self])) {
errors.push(
"expected property path",
if inner.is_empty() { brace.span.join() } else { inner.span() },
);
while !(inner.is_empty()
|| inner.peek(Ident)
|| inner.peek(Ident::peek_any)
|| inner.peek(Token![super])
|| inner.peek(Token![self])
|| inner.peek(Token![#]) && inner.peek(token::Bracket))
@ -415,7 +415,7 @@ impl WgtWhen {
if !inner.is_empty() && p.semi.is_none() {
errors.push("expected `,`", inner.span());
while !(inner.is_empty()
|| input.peek(Ident)
|| input.peek(Ident::peek_any)
|| input.peek(Token![crate])
|| input.peek(Token![super])
|| input.peek(Token![self])

View File

@ -104,7 +104,7 @@ impl DefaultStyle {
border = {
widths: 1,
sides: color_scheme_pair(BASE_COLORS_VAR).map_into()
}
};
when *#is_cap_hovered {
#[easing(0.ms())]

View File

@ -14,10 +14,10 @@ impl<P: WidgetImpl> FocusableMix<P> {
self;
focusable = true;
when *#focus::is_focused_hgl {
foreground_highlight = {
offsets: vis::FOCUS_HIGHLIGHT_OFFSETS_VAR,
widths: vis::FOCUS_HIGHLIGHT_WIDTHS_VAR,
sides: vis::FOCUS_HIGHLIGHT_SIDES_VAR,
crate::properties::foreground_highlight = {
offsets: FOCUS_HIGHLIGHT_OFFSETS_VAR,
widths: FOCUS_HIGHLIGHT_WIDTHS_VAR,
sides: FOCUS_HIGHLIGHT_SIDES_VAR,
};
}
}

View File

@ -12,7 +12,11 @@ use std::fmt;
/// each icon in the fonts.
///
/// [`GlyphIcon`]: icon::GlyphIcon
#[widget($crate::widgets::Icon)]
#[widget($crate::widgets::Icon {
($ico:expr) => {
ico = $ico;
}
})]
pub struct Icon(WidgetBase);
impl Icon {
#[widget(on_start)]

View File

@ -552,7 +552,7 @@ pub fn default_list_fn(args: ListFnArgs) -> impl UiNode {
if args.items.is_empty() {
NilUiNode.boxed()
} else {
use crate::widgets::layouts::{Grid, grid};
use crate::widgets::layouts::{grid, Grid};
Grid! {
margin = (0, 0, 0, 1.em());
cells = args.items;

View File

@ -1,12 +1,6 @@
//! Toggle widget and properties.
use std::{
any::Any,
error::Error,
fmt,
marker::PhantomData,
sync::{Arc}, borrow::Cow,
};
use std::{any::Any, borrow::Cow, error::Error, fmt, marker::PhantomData, sync::Arc};
use task::parking_lot::Mutex;
@ -820,14 +814,16 @@ pub struct DefaultStyle(Style);
impl DefaultStyle {
#[widget(on_start)]
fn on_start(&mut self) {
use crate::widgets::button;
defaults! {
self;
when *#is_checked {
background_color = crate::widgets::button::vis::color_scheme_pressed(btn_vis::BASE_COLORS_VAR);
background_color = button::color_scheme_pressed(button::BASE_COLORS_VAR);
border = {
widths: 1,
sides: crate::widgets::button::vis::color_scheme_pressed(btn_vis::BASE_COLORS_VAR).map_into(),
sides: button::color_scheme_pressed(button::BASE_COLORS_VAR).map_into(),
};
}
}