[][src]Macro gtk_fnonce_on_eventloop::gtk_refs

macro_rules! gtk_refs {
    ( $modpub:vis mod $modname:ident ; struct $structname:ident ; $( $i:ident : $t:ty ),* ) => { ... };
}

Use this. Generates a new module with thread local storage for references to widgets, a struct to hold these references and a function to execute closures on the gtk event loop that can access these references.

Example usage

gtk_refs!(
    pub mod widgets;                // The macro emits a new module with this name
    struct WidgetRefs;              // The macro emits a struct with this name containing:
    main_window : gtk::Window ,     // widget_name : Widgettype
    button1 : gtk::Button           // ..
);

Generated items

pub mod widgets {
    pub struct WidgetRefs {
        pub main_window : gtk::Window,
        ...
    }
    impl From<&gtk::Builder> for WidgetRefs { ... };
    impl WidgetRefs {
        fn main_window() -> gtk::Window { } // returns a .clone() of the widget
        ...
    }

    pub fn init_storage(WidgetRefs);
    pub fn init_storage_from_builder(&gtk::Builder);
    pub fn do_in_gtk_eventloop( FnOnce(Rc<WidgetRefs>) );
}

Initialization

Do this in the gtk thread before starting the event loop gtk::main().

    let widget_references = widgets::WidgetRefs {
        main_window: window.clone(),
        button1:     button.clone(),
    };

    widgets::init_storage(widget_references);

Usage in threads

    widgets::do_in_gtk_eventloop(|refs| {
        refs.button1().set_label(&text);
    });

Don't call do_in_gtk_eventloop() in the main thread since this will deadlock.