#gtk #derive #gio #gtk-rs

gtk_resources_derive

Procedural derive macro for easily loading gtk gresources

5 releases

0.1.6 Aug 16, 2020
0.1.5 Mar 31, 2020
0.1.4 Jan 22, 2020
0.1.2 Nov 21, 2019
0.1.0 Nov 20, 2019

#87 in GUI

Download history 9/week @ 2021-04-11 18/week @ 2021-04-18 4/week @ 2021-04-25 12/week @ 2021-05-02 3/week @ 2021-05-09 8/week @ 2021-05-16 5/week @ 2021-05-23 5/week @ 2021-05-30 11/week @ 2021-06-06 5/week @ 2021-06-13 1/week @ 2021-06-20 5/week @ 2021-06-27 8/week @ 2021-07-04 2/week @ 2021-07-11 6/week @ 2021-07-18 9/week @ 2021-07-25

52 downloads per month
Used in gtk_resources

MIT license

4KB
58 lines

GTK Resources

Procedural derive macro for easily loading gtk gresources.

This package depends on the gtk-rs project.

Documentation

Find it on Docs.rs.

Example

Add gtk-resources to your dependencies of your Cargo.toml:

[dependencies]
gio = "0.9"
gtk = "0.9"
glib = "0.10"
gtk_resources = "0.1.6"

And then in your gtk-rs project.

use gtk_resources::UIResource;

#[derive(UIResource, Debug)]
#[resource = "/com/name/project/window.ui"]
struct WindowResource {
    window: gtk::ApplicationWindow,
    display_label: gtk::Label,
    hello_btn: gtk::Button,
}

fn main() {
    gtk::init().unwrap();

    // Register resource bundles
    let res_bytes = include_bytes!("../test/test.gresource");
    let data = glib::Bytes::from(&res_bytes[..]);
    let resource = gio::Resource::from_data(&data).unwrap();
    gio::resources_register(&resource);

    let res = WindowResource::load().unwrap();
    println!("res: {:?}", res);

    // ...
}

The file test/test.gresource.xml defines the resource bundle which is used by this example.

See Gio::Resource and glib-compile-resources for more info about resource bundles.

The #[resource = "/com/name/project/window.ui"] attribute at the WindowResource struct specifies the path to the corresponding resource.

The field names and types of the UIResource struct have to match the ids and types from the exported resource objects. Otherwise the gtk::Builder is unable to load them during runtime.

The code gerated for the previous example looks as follows:

impl UIResource for WindowResource {
    fn load() -> Result<WindowResource, ()> {
        use gtk::BuilderExtManual;
        let b = gtk::Builder::from_resource("/com/name/project/window.ui");
        Ok (WindowResource {
            window: b.get_object::<gtk::ApplicationWindow>("window").ok_or(())?,
            display_label: b.get_object::<gtk::Label>("display_label").ok_or(())?,
            hello_btn: b.get_object::<gtk::Button>("hello_btn").ok_or(())?,
        })
    }
}

Dependencies

~325–750KB
~19K SLoC