Skip to content

Commit 911c14b

Browse files
committed
Add the ability to add child properties to relm container's children
1 parent 5c79eea commit 911c14b

3 files changed

Lines changed: 18 additions & 7 deletions

File tree

‎examples/container-attribute.rs‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ use gtk::{
3636
};
3737
use gtk::Orientation::Vertical;
3838
use relm::Widget;
39+
use relm::gtk_ext::BoxExtManual;
3940
use relm_attributes::widget;
4041

4142
use self::Msg::*;
@@ -111,6 +112,9 @@ impl Widget for Win {
111112
gtk::Window {
112113
VBox {
113114
gtk::Button {
115+
packing: {
116+
padding: 20,
117+
},
114118
clicked => Increment,
115119
label: "+",
116120
},

‎relm-gen-widget/src/gen.rs‎

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ macro_rules! set_container {
6565
};
6666
}
6767

68-
#[derive(PartialEq)]
68+
#[derive(Clone, Copy, PartialEq)]
6969
enum WidgetType {
7070
IsGtk,
7171
IsRelm,
@@ -249,7 +249,7 @@ impl<'a> Generator<'a> {
249249
let add_child_or_show_all = self.add_child_or_show_all(widget, parent, parent_widget_type);
250250
let ident = quote! { #widget_name };
251251
let (properties, visible_properties) = gen_set_prop_calls!(widget, ident);
252-
let child_properties = gen_set_child_prop_calls(widget, parent);
252+
let child_properties = gen_set_child_prop_calls(widget, parent, parent_widget_type);
253253

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

352-
fn gen_set_child_prop_calls(widget: &GtkWidget, parent: Option<&Ident>) -> Vec<Tokens> {
352+
fn gen_set_child_prop_calls(widget: &GtkWidget, parent: Option<&Ident>, parent_widget_type: WidgetType) -> Vec<Tokens> {
353353
let widget_name = &widget.name;
354354
let mut child_properties = vec![];
355355
if let Some(parent) = parent {
356356
for (key, value) in &widget.child_properties {
357357
let property_func = Ident::new(format!("set_child_{}", key));
358+
let parent =
359+
if parent_widget_type == IsGtk {
360+
quote! {
361+
#parent
362+
}
363+
}
364+
else {
365+
quote! {
366+
::relm::Container::container(#parent.widget())
367+
}
368+
};
358369
child_properties.push(quote! {
359370
#parent.#property_func(&#widget_name, #value);
360371
});

‎src/lib.rs‎

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -491,10 +491,6 @@ fn init_gtk() {
491491
/// let component = relm::init_test::<Win>().unwrap();
492492
/// # }
493493
/// ```
494-
///
495-
/// ## Warning
496-
/// You **should** use `_component` instead of `_` to avoid dropping it too early, which will cause
497-
/// events to not be sent.
498494
pub fn init_test<WIDGET>() -> Result<Component<WIDGET>, ()>
499495
where WIDGET: Widget + Clone + 'static,
500496
WIDGET::Model: Clone + Send,

0 commit comments

Comments
 (0)