pub trait Gladis {
    fn from_builder(builder: Builder) -> Result<Self>
    where
        Self: Sized
; fn from_string(src: &str) -> Result<Self>
    where
        Self: Sized
, { ... } fn from_resource(resource_path: &str) -> Result<Self>
    where
        Self: Sized
, { ... } }
Expand description

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 crate and its #[derive(Gladis)] macro.

use gtk::prelude::*;
use gladis::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, Result, GladisError};

pub struct Window {
    pub window: gtk::ApplicationWindow,
    pub label: gtk::Label,
}

impl Gladis for Window {
    fn from_builder(builder: gtk::Builder) -> Result<Self> {
        let window: gtk::ApplicationWindow = builder
            .object("window")
            .ok_or(GladisError::not_found("window", "gtk::ApplicationWindow"))?;

        let label: gtk::Label = builder
            .object("label")
            .ok_or(GladisError::not_found("label", "gtk::Label"))?;

        Ok(Self { window, label })
    }
}

Required Methods

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.

Provided Methods

Populate struct from a Glade document.

Populate struct from a Glade document as a resource.

Implementors