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(result_cloned)]
#![feature(iterator_fold_self)]
#![feature(try_blocks)]
extern crate gio;

View file

@ -14,18 +14,17 @@ use std::path::Path;
macro_rules! resolve_block {
($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('_', "-"));
)*
// TODO reimplement unused warnings
let attr_map: Result<_> = try {
::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,
::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
Ok(())
@ -45,14 +44,13 @@ macro_rules! resolve_block {
})+
};
// required
//($args:ident, $gtk_widget:ident, $func:ident => $attr:literal req => |$arg:ident| $body:expr) => {
//$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();
//move |$arg| { $body; }
//});
//};
(@get_value $args:ident, $name:expr, = $default:expr) => {
$args.widget.get_attr($name).cloned().unwrap_or(AttrValue::Concrete(PrimitiveValue::from($default)))
};
(@get_value $args:ident, $name:expr,) => {
$args.widget.get_attr($name)?.clone()
}
}
/// attributes that apply to all widgets
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(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(active: as_bool ) { gtk_widget.set_sensitive(active) },
prop(active: as_bool = true) { gtk_widget.set_sensitive(active) },
prop(visible: as_bool ) {
// 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(); }
@ -73,8 +71,8 @@ pub(super) fn resolve_widget_attrs(bargs: &mut BuilderArgs, gtk_widget: &gtk::Wi
/// attributes that apply to all container widgets
pub(super) fn resolve_container_attrs(bargs: &mut BuilderArgs, gtk_widget: &gtk::Container) {
resolve_block!(bargs, gtk_widget, {
prop(vexpand: as_bool) { gtk_widget.set_vexpand(vexpand) },
prop(hexpand: as_bool) { gtk_widget.set_hexpand(hexpand) },
prop(vexpand: as_bool = false) { gtk_widget.set_vexpand(vexpand) },
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, {
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)
}
@ -147,9 +145,10 @@ fn build_gtk_image(bargs: &mut BuilderArgs) -> Result<gtk::Image> {
fn build_gtk_layout(bargs: &mut BuilderArgs) -> Result<gtk::Box> {
let gtk_widget = gtk::Box::new(gtk::Orientation::Horizontal, 0);
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(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)
}