antoyo / relm

Idiomatic, GTK+-based, GUI library, inspired by Elm, written in Rust
MIT License
2.43k stars 78 forks source link

Question: connect CellRendererText callback in the init_view? #181

Closed lesurp closed 5 years ago

lesurp commented 5 years ago

Context

When implementing the fn view functino manually, I have access to the relm::Relm<Self> parameters that allows me to connect event i.e:

connect!(relm, tree_view, connect_cursor_changed(_), Msg::ItemSelect);

as found in https://github.com/antoyo/relm/blob/master/relm-examples/examples/tree-view.rs

Now, I am trying to use the #[widget] attribute with the view! macro to make my life easier. This means that I have to initialize my gtk::TreeView in my Widget::init_view, macro as neither column nor cellrenderer implement Widget, i.e:

    fn init_view(&mut self) {
        let col = gtk::TreeViewColumn::new();
        col.set_title("Foo");
        let cell = gtk::CellRendererText::new();
        cell.set_property_editable(true);
        col.pack_start(&cell, true);
        col.add_attribute(&cell, "text", 2);
        self.tree_view.append_column(&col);
}

Problem

Now I try to connect a callback to the event changed of the CellRendererText, with something like:

        /* This does not work..! */
        connect!(
            self.spendings_tree_view,
            cell,
            connect_edited(_, _, _),
            Msg::Changed,
        );

However this does not work..! I don't have access to the Relm object, I don't seem to have access to the event stream in general. I thought something such as self.root().emit() could be possible (thence calling connect_edited manually) but nothing I tried actually worked. I also couldn't find anything in the various examples. Is it possible even possible in the current state of the crate?

Thanks!

lesurp commented 5 years ago

Ok according to this I can actually just copy the Relm<T> object:

pub struct Model {
    relm: Relm<Win>,
}

#[widget]
impl Widget for Win {
    fn model(relm: &Relm<Self>, (): ()) -> Model {
        let topic = "cats";
        Model {
            relm: relm.clone(),
        }
    }