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 108 109 110 111 112 113
//! Easily import Glade-generated UI files into Rust code. //! //! ``` //! use gtk::prelude::*; //! use gladis::Gladis; //! use gladis_proc_macro::Gladis; //! //! const GLADE_SRC: &str = r#" //! <?xml version="1.0" encoding="UTF-8"?> //! <!-- Generated with glade 3.22.2 --> //! <interface> //! <requires lib="gtk+" version="3.20"/> //! <object class="GtkApplicationWindow" id="window"> //! <property name="can_focus">False</property> //! <child type="titlebar"> //! <placeholder/> //! </child> //! <child> //! <object class="GtkLabel" id="label"> //! <property name="visible">True</property> //! <property name="can_focus">False</property> //! <property name="label" translatable="yes">label</property> //! </object> //! </child> //! </object> //! </interface>"#; //! //! #[derive(Gladis, Clone)] //! pub struct Window { //! pub window: gtk::ApplicationWindow, //! pub label: gtk::Label, //! } //! //! fn main() { //! gtk::init().unwrap(); //! let _ui = Window::from_string(GLADE_SRC); //! } //! ``` use gtk; pub trait Gladis { //! A trait to load a struct from a builder. //! //! # Automatic implementation //! //! This trait wakes little sense alone, but truly show its power when used //! with the [gladis_proc_macro](https://docs.rs/gladis_proc_macro) crate //! and its `#[derive(Gladis)]` macro. //! //! ``` //! use gtk::prelude::*; //! use gladis::Gladis; //! use gladis_proc_macro::Gladis; //! //! #[derive(Gladis, Clone)] //! pub struct Window { //! pub window: gtk::ApplicationWindow, //! pub label: gtk::Label, //! } //! ``` //! //! # Manual implementation //! //! Below is an example of manual implementation of the trait. //! //! ``` //! use gtk::prelude::*; //! use gladis::Gladis; //! //! pub struct Window { //! pub window: gtk::ApplicationWindow, //! pub label: gtk::Label, //! } //! //! impl Gladis for Window { //! fn from_builder(builder: gtk::Builder) -> Self { //! let window: gtk::ApplicationWindow = builder //! .get_object("window") //! .expect("Failed to find the window object"); //! //! let label: gtk::Label = builder //! .get_object("label") //! .expect("Failed to find the label object"); //! //! Self { window, label } //! } //! //! fn from_string(src: &str) -> Self { //! let builder = gtk::Builder::from_string(src); //! Gladis::from_builder(builder) //! } //! //! fn from_resource(resource_path: &str) -> Self { //! let builder = gtk::Builder::from_resource(resource_path); //! Gladis::from_builder(builder) //! } //! } //! ``` /// Populate struct from a builder. /// /// This method should not be called directly but is used as a common /// function for the `from_string` and `from_resource` functions to /// share the same code. fn from_builder(builder: gtk::Builder) -> Self; /// Populate struct from a Glade document. fn from_string(src: &str) -> Self; /// Populate struct from a Glade document as a resource. fn from_resource(resource_path: &str) -> Self; }