Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add the ability to add child properties to relm container's children
  • Loading branch information
antoyo committed Apr 24, 2017
1 parent 5c79eea commit 911c14b
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 7 deletions.
4 changes: 4 additions & 0 deletions examples/container-attribute.rs
Expand Up @@ -36,6 +36,7 @@ use gtk::{
};
use gtk::Orientation::Vertical;
use relm::Widget;
use relm::gtk_ext::BoxExtManual;
use relm_attributes::widget;

use self::Msg::*;
Expand Down Expand Up @@ -111,6 +112,9 @@ impl Widget for Win {
gtk::Window {
VBox {
gtk::Button {
packing: {
padding: 20,
},
clicked => Increment,
label: "+",
},
Expand Down
17 changes: 14 additions & 3 deletions relm-gen-widget/src/gen.rs
Expand Up @@ -65,7 +65,7 @@ macro_rules! set_container {
};
}

#[derive(PartialEq)]
#[derive(Clone, Copy, PartialEq)]
enum WidgetType {
IsGtk,
IsRelm,
Expand Down Expand Up @@ -249,7 +249,7 @@ impl<'a> Generator<'a> {
let add_child_or_show_all = self.add_child_or_show_all(widget, parent, parent_widget_type);
let ident = quote! { #widget_name };
let (properties, visible_properties) = gen_set_prop_calls!(widget, ident);
let child_properties = gen_set_child_prop_calls(widget, parent);
let child_properties = gen_set_child_prop_calls(widget, parent, parent_widget_type);

quote! {
let #widget_name: #struct_name = #construct_widget;
Expand Down Expand Up @@ -349,12 +349,23 @@ fn gen_relm_component_type(name: &Ident) -> Ident {
Ident::new(format!("::relm::Component<{0}>", name).as_ref())
}

fn gen_set_child_prop_calls(widget: &GtkWidget, parent: Option<&Ident>) -> Vec<Tokens> {
fn gen_set_child_prop_calls(widget: &GtkWidget, parent: Option<&Ident>, parent_widget_type: WidgetType) -> Vec<Tokens> {
let widget_name = &widget.name;
let mut child_properties = vec![];
if let Some(parent) = parent {
for (key, value) in &widget.child_properties {
let property_func = Ident::new(format!("set_child_{}", key));
let parent =
if parent_widget_type == IsGtk {
quote! {
#parent
}
}
else {
quote! {
::relm::Container::container(#parent.widget())
}
};
child_properties.push(quote! {
#parent.#property_func(&#widget_name, #value);
});
Expand Down
4 changes: 0 additions & 4 deletions src/lib.rs
Expand Up @@ -491,10 +491,6 @@ fn init_gtk() {
/// let component = relm::init_test::<Win>().unwrap();
/// # }
/// ```
///
/// ## Warning
/// You **should** use `_component` instead of `_` to avoid dropping it too early, which will cause
/// events to not be sent.
pub fn init_test<WIDGET>() -> Result<Component<WIDGET>, ()>
where WIDGET: Widget + Clone + 'static,
WIDGET::Model: Clone + Send,
Expand Down

0 comments on commit 911c14b

Please sign in to comment.