1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
//! This crate defines the UIResource trait and its custom derive. //! //! ## Usage //! //! First, let's look at the example: //! //! ``` //! 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::new_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. //! //! ```xml //! <gresources> //! <gresource prefix="/com/name/project"> //! <file preprocess="xml-stripblanks">window.ui</file> //! </gresource> //! </gresources> //! ``` //! //! See [Gio::Resource and glib-compile-resources](https://developer.gnome.org/gtkmm-tutorial/stable/sec-gio-resource.html.en) 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::new_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(())?, //! }) //! } //! } //! ``` #[doc(hidden)] pub use gtk_resources_derive::*; /// Trait for `gtk::Builder` resources. pub trait UIResource { /// Loads the resource struct with the `gtk::Builder` /// from the corresponding resource bundle. fn load() -> Result<Self, String> where Self: Sized; } #[cfg(test)] mod test { use super::*; #[test] fn test_resource() { #[derive(UIResource, Debug)] #[resource = "/com/name/project/window.ui"] struct TestResource { window: gtk::ApplicationWindow, display_label: gtk::Label, hello_btn: gtk::Button, } 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::new_from_data(&data).unwrap(); gio::resources_register(&resource); let res = TestResource::load().unwrap(); println!("res: {:?}", res); } }