Skip to content

Commit

Permalink
Fix on_add for relm containers
Browse files Browse the repository at this point in the history
  • Loading branch information
antoyo committed Apr 30, 2017
1 parent 50ac9c8 commit 6257c50
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 15 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ documentation = "https://docs.rs/relm/"
license = "MIT"
name = "relm"
repository = "https://github.com/antoyo/relm"
version = "0.9.1"
version = "0.9.2"

[badges]
travis-ci = { repository = "antoyo/relm" }
Expand Down
7 changes: 5 additions & 2 deletions examples/multi-container.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ use gtk::{
};
use gtk::Orientation::{Horizontal, Vertical};
use gtk::WindowType::Toplevel;
use relm::{Component, Container, ContainerWidget, RelmContainer, RemoteRelm, Widget};
use relm::{Cast, Component, Container, ContainerWidget, RelmContainer, RemoteRelm, Widget};

use self::Msg::*;

Expand Down Expand Up @@ -160,15 +160,18 @@ impl Container for SplitBox {
&self.hbox1
}

fn add_widget<WIDGET: Widget>(&self, widget: &WIDGET) {
fn add_widget<WIDGET: Widget>(&self, widget: &WIDGET) -> gtk::Container {
if WIDGET::data() == Some("right") {
self.hbox3.add(widget.root());
self.hbox3.widget().root().clone().upcast()
}
else if WIDGET::data() == Some("center") {
self.hbox2.add(widget.root());
self.hbox2.clone().upcast()
}
else {
self.hbox1.add(widget.root());
self.hbox1.clone().upcast()
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion relm-gen-widget/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ documentation = "https://docs.rs/relm-gen-widget/"
license = "MIT"
name = "relm-gen-widget"
repository = "https://github.com/antoyo/relm"
version = "0.9.7"
version = "0.9.8"

[dependencies]
lazy_static = "^0.2.4"
Expand Down
19 changes: 13 additions & 6 deletions relm-gen-widget/src/gen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -386,27 +386,33 @@ fn gen_add_widget_method(container_names: &HashMap<Option<String>, (Ident, Path)
let mut other_containers = Tokens::new();
for (data, &(ref name, ref typ)) in container_names {
let first_type_part = &typ.segments.first().expect("first segment").ident;
let container_trait =
let (container_trait, upcast_container) =
if first_type_part == "gtk" {
quote! {
(quote! {
::gtk::ContainerExt
}
}, quote! {
::relm::Cast::upcast(self.#name.clone())
})
}
else {
quote! {
(quote! {
::relm::RelmContainer
}
}, quote! {
::relm::Cast::upcast(self.#name.widget().root().clone())
})
};
if data.is_none() {
default_container = quote! {
#container_trait::add(&self.#name, widget.root());
#upcast_container
};
}
else {
if other_containers.as_str().is_empty() {
other_containers = quote! {
if WIDGET::data() == Some(#data) {
#container_trait::add(&self.#name, widget.root());
#upcast_container
}
};
}
Expand All @@ -415,6 +421,7 @@ fn gen_add_widget_method(container_names: &HashMap<Option<String>, (Ident, Path)
#other_containers
else if WIDGET::data() == Some(#data) {
#container_trait::add(&self.#name, widget.root());
#upcast_container
}
};
}
Expand All @@ -428,7 +435,7 @@ fn gen_add_widget_method(container_names: &HashMap<Option<String>, (Ident, Path)
};
}
quote! {
fn add_widget<WIDGET: Widget>(&self, widget: &WIDGET) {
fn add_widget<WIDGET: Widget>(&self, widget: &WIDGET) -> ::gtk::Container {
#other_containers
#default_container
}
Expand Down
12 changes: 7 additions & 5 deletions src/container.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

use glib::Cast;
use gtk;
use gtk::{ContainerExt, IsA, Object, WidgetExt};

Expand All @@ -29,7 +30,7 @@ use widget::Widget;
/// Trait to implement relm container widget.
pub trait Container: Widget {
/// The type of the containing widget, i.e. where the child widgets will be added.
type Container: IsA<gtk::Container> + IsA<Object>;
type Container: Clone + IsA<gtk::Container> + IsA<Object>;

/// Get the containing widget, i.e. the widget where the children will be added.
fn container(&self) -> &Self::Container;
Expand All @@ -40,8 +41,10 @@ pub trait Container: Widget {
}

/// Add a relm widget to this container.
fn add_widget<WIDGET: Widget>(&self, widget: &WIDGET) {
self.container().add(widget.root());
fn add_widget<WIDGET: Widget>(&self, widget: &WIDGET) -> gtk::Container {
let container = self.container();
container.add(widget.root());
container.clone().upcast()
}
}

Expand Down Expand Up @@ -124,8 +127,7 @@ impl<WIDGET> RelmContainer for Component<WIDGET>
PARENTWIDGET: Widget
{
let component = create_widget::<CHILDWIDGET>(&relm.remote, model_param);
let container = self.widget().container();
self.widget().add_widget(&component.widget);
let container = self.widget().add_widget(&component.widget);
component.widget.on_add(container.clone());
init_component::<CHILDWIDGET>(&component, &relm.remote);
Component::new(component)
Expand Down
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ use std::time::SystemTime;
use futures::{Future, Stream};
use glib::Continue;
#[doc(hidden)]
pub use glib::Cast;
#[doc(hidden)]
pub use glib::object::Downcast;
#[doc(hidden)]
pub use glib::translate::{FromGlibPtrNone, ToGlib};
Expand Down

0 comments on commit 6257c50

Please sign in to comment.