implement default values in resolve macro

This commit is contained in:
elkowar 2020-10-07 20:17:49 +02:00
parent 2df0984ac3
commit 7187b32ae5
2 changed files with 20 additions and 20 deletions

View file

@ -1,4 +1,5 @@
#![feature(trace_macros)] #![feature(trace_macros)]
#![feature(result_cloned)]
#![feature(iterator_fold_self)] #![feature(iterator_fold_self)]
#![feature(try_blocks)] #![feature(try_blocks)]
extern crate gio; extern crate gio;

View file

@ -14,18 +14,17 @@ use std::path::Path;
macro_rules! resolve_block { macro_rules! resolve_block {
($args:ident, $gtk_widget:ident, { ($args:ident, $gtk_widget:ident, {
$( $(
prop( $( $attr_name:ident : $typecast_func:ident ),*) $code:block prop( $( $attr_name:ident : $typecast_func:ident $(= $default:expr)?),*) $code:block
),+ $(,)? ),+ $(,)?
}) => { }) => {
$({ $({
$( $(
$args.unhandled_attrs.retain(|a| a != &::std::stringify!($attr_name).replace('_', "-")); $args.unhandled_attrs.retain(|a| a != &::std::stringify!($attr_name).replace('_', "-"));
)* )*
// TODO reimplement unused warnings
let attr_map: Result<_> = try { let attr_map: Result<_> = try {
::maplit::hashmap! { ::maplit::hashmap! {
$( $(
::std::stringify!($attr_name).to_owned() => $args.widget.get_attr(&::std::stringify!($attr_name).replace('_', "-"))?.clone() ::std::stringify!($attr_name).to_owned() => resolve_block!(@get_value $args, &::std::stringify!($attr_name).replace('_', "-"), $(= $default)?)
),* ),*
} }
}; };
@ -35,7 +34,7 @@ macro_rules! resolve_block {
attr_map, attr_map,
::glib::clone!(@strong $gtk_widget => move |attrs| { ::glib::clone!(@strong $gtk_widget => move |attrs| {
$( $(
let $attr_name = attrs.get( ::std::stringify!($attr_name) ).context("REEE")?.$typecast_func()?; let $attr_name = attrs.get( ::std::stringify!($attr_name) ).context("something went terribly wrong....")?.$typecast_func()?;
)* )*
$code $code
Ok(()) Ok(())
@ -45,14 +44,13 @@ macro_rules! resolve_block {
})+ })+
}; };
// required (@get_value $args:ident, $name:expr, = $default:expr) => {
//($args:ident, $gtk_widget:ident, $func:ident => $attr:literal req => |$arg:ident| $body:expr) => { $args.widget.get_attr($name).cloned().unwrap_or(AttrValue::Concrete(PrimitiveValue::from($default)))
//$args.unhandled_attrs.retain(|a| a != &$attr); };
//$args.eww_state.$func($args.local_env, $args.widget.get_attr($attr)?, {
//let $gtk_widget = $gtk_widget.clone(); (@get_value $args:ident, $name:expr,) => {
//move |$arg| { $body; } $args.widget.get_attr($name)?.clone()
//}); }
//};
} }
/// attributes that apply to all widgets /// attributes that apply to all widgets
pub(super) fn resolve_widget_attrs(bargs: &mut BuilderArgs, gtk_widget: &gtk::Widget) { pub(super) fn resolve_widget_attrs(bargs: &mut BuilderArgs, gtk_widget: &gtk::Widget) {
@ -62,7 +60,7 @@ pub(super) fn resolve_widget_attrs(bargs: &mut BuilderArgs, gtk_widget: &gtk::Wi
prop(halign: as_string) { gtk_widget.set_halign(parse_align(&halign)) }, prop(halign: as_string) { gtk_widget.set_halign(parse_align(&halign)) },
prop(width: as_f64 ) { gtk_widget.set_size_request(width as i32, gtk_widget.get_allocated_height()) }, prop(width: as_f64 ) { gtk_widget.set_size_request(width as i32, gtk_widget.get_allocated_height()) },
prop(height: as_f64 ) { gtk_widget.set_size_request(gtk_widget.get_allocated_width(), height as i32) }, prop(height: as_f64 ) { gtk_widget.set_size_request(gtk_widget.get_allocated_width(), height as i32) },
prop(active: as_bool ) { gtk_widget.set_sensitive(active) }, prop(active: as_bool = true) { gtk_widget.set_sensitive(active) },
prop(visible: as_bool ) { prop(visible: as_bool ) {
// TODO how do i call this only after the widget has been mapped? this is actually an issue,.... // TODO how do i call this only after the widget has been mapped? this is actually an issue,....
if visible { gtk_widget.show(); } else { gtk_widget.hide(); } if visible { gtk_widget.show(); } else { gtk_widget.hide(); }
@ -73,8 +71,8 @@ pub(super) fn resolve_widget_attrs(bargs: &mut BuilderArgs, gtk_widget: &gtk::Wi
/// attributes that apply to all container widgets /// attributes that apply to all container widgets
pub(super) fn resolve_container_attrs(bargs: &mut BuilderArgs, gtk_widget: &gtk::Container) { pub(super) fn resolve_container_attrs(bargs: &mut BuilderArgs, gtk_widget: &gtk::Container) {
resolve_block!(bargs, gtk_widget, { resolve_block!(bargs, gtk_widget, {
prop(vexpand: as_bool) { gtk_widget.set_vexpand(vexpand) }, prop(vexpand: as_bool = false) { gtk_widget.set_vexpand(vexpand) },
prop(hexpand: as_bool) { gtk_widget.set_hexpand(hexpand) }, prop(hexpand: as_bool = false) { gtk_widget.set_hexpand(hexpand) },
}); });
} }
@ -123,7 +121,7 @@ fn build_gtk_scale(bargs: &mut BuilderArgs) -> Result<gtk::Scale> {
); );
resolve_block!(bargs, gtk_widget, { resolve_block!(bargs, gtk_widget, {
prop(flipped: as_bool) { gtk_widget.set_inverted(flipped) }, prop(flipped: as_bool) { gtk_widget.set_inverted(flipped) },
prop(draw_value: as_bool) { gtk_widget.set_draw_value(draw_value) }, prop(draw_value: as_bool = false) { gtk_widget.set_draw_value(draw_value) },
}); });
Ok(gtk_widget) Ok(gtk_widget)
} }
@ -147,9 +145,10 @@ fn build_gtk_image(bargs: &mut BuilderArgs) -> Result<gtk::Image> {
fn build_gtk_layout(bargs: &mut BuilderArgs) -> Result<gtk::Box> { fn build_gtk_layout(bargs: &mut BuilderArgs) -> Result<gtk::Box> {
let gtk_widget = gtk::Box::new(gtk::Orientation::Horizontal, 0); let gtk_widget = gtk::Box::new(gtk::Orientation::Horizontal, 0);
resolve_block!(bargs, gtk_widget, { resolve_block!(bargs, gtk_widget, {
prop(spacing: as_f64 ) { gtk_widget.set_spacing(spacing as i32) },
prop(spacing: as_f64 = 0.0) { gtk_widget.set_spacing(spacing as i32) },
prop(orientation: as_string) { gtk_widget.set_orientation(parse_orientation(&orientation)) }, prop(orientation: as_string) { gtk_widget.set_orientation(parse_orientation(&orientation)) },
prop(space_evenly: as_bool) { gtk_widget.set_homogeneous(space_evenly) }, prop(space_evenly: as_bool = true) { gtk_widget.set_homogeneous(space_evenly) },
}); });
Ok(gtk_widget) Ok(gtk_widget)
} }