From 7187b32ae54457f784e1fd0e36c61ad4394ac0c4 Mon Sep 17 00:00:00 2001 From: elkowar <5300871+elkowar@users.noreply.github.com> Date: Wed, 7 Oct 2020 20:17:49 +0200 Subject: [PATCH] implement default values in resolve macro --- src/main.rs | 1 + src/widgets/widget_definitions.rs | 39 +++++++++++++++---------------- 2 files changed, 20 insertions(+), 20 deletions(-) diff --git a/src/main.rs b/src/main.rs index 88ab277..e68ffba 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,5 @@ #![feature(trace_macros)] +#![feature(result_cloned)] #![feature(iterator_fold_self)] #![feature(try_blocks)] extern crate gio; diff --git a/src/widgets/widget_definitions.rs b/src/widgets/widget_definitions.rs index 4453d07..ddf9fb2 100644 --- a/src/widgets/widget_definitions.rs +++ b/src/widgets/widget_definitions.rs @@ -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: >k::Widget) { @@ -62,7 +60,7 @@ pub(super) fn resolve_widget_attrs(bargs: &mut BuilderArgs, gtk_widget: >k::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: >k::Wi /// attributes that apply to all container widgets pub(super) fn resolve_container_attrs(bargs: &mut BuilderArgs, gtk_widget: >k::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) }, }); } @@ -122,8 +120,8 @@ fn build_gtk_scale(bargs: &mut BuilderArgs) -> Result { Some(>k::Adjustment::new(0.0, 0.0, 100.0, 1.0, 1.0, 1.0)), ); 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(flipped: as_bool) { gtk_widget.set_inverted(flipped) }, + 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 { fn build_gtk_layout(bargs: &mut BuilderArgs) -> Result { 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(orientation: as_string ) { gtk_widget.set_orientation(parse_orientation(&orientation)) }, - prop(space_evenly: as_bool) { gtk_widget.set_homogeneous(space_evenly) }, + + 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 = true) { gtk_widget.set_homogeneous(space_evenly) }, }); Ok(gtk_widget) }