Permalink
Browse files

Add the ability to add child properties to relm container's children

  • Loading branch information...
antoyo committed Apr 24, 2017
1 parent 5c79eea commit 911c14b39018a8f751c6ac0c3d71cceef7cf0df0
Showing with 18 additions and 7 deletions.
  1. +4 −0 examples/container-attribute.rs
  2. +14 −3 relm-gen-widget/src/gen.rs
  3. +0 −4 src/lib.rs
@@ -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::*;
@@ -111,6 +112,9 @@ impl Widget for Win {
gtk::Window {
VBox {
gtk::Button {
packing: {
padding: 20,
},
clicked => Increment,
label: "+",
},
View
@@ -65,7 +65,7 @@ macro_rules! set_container {
};
}
#[derive(PartialEq)]
#[derive(Clone, Copy, PartialEq)]
enum WidgetType {
IsGtk,
IsRelm,
@@ -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;
@@ -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);
});
View
@@ -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,

0 comments on commit 911c14b

Please sign in to comment.