Derive Macro glib_macros::Downgrade

source ·
#[derive(Downgrade)]
Expand description

Macro for deriving implementations of glib::clone::Downgrade and glib::clone::Upgrade traits and a weak type.

§Examples

§New Type Idiom

#[derive(glib::Downgrade)]
pub struct FancyLabel(gtk::Label);

impl FancyLabel {
    pub fn new(label: &str) -> Self {
        Self(gtk::LabelBuilder::new().label(label).build())
    }

    pub fn flip(&self) {
        self.0.set_angle(180.0 - self.0.angle());
    }
}

let fancy_label = FancyLabel::new("Look at me!");
let button = gtk::ButtonBuilder::new().label("Click me!").build();
button.connect_clicked(clone!(@weak fancy_label => move || fancy_label.flip()));

§Generic New Type

#[derive(glib::Downgrade)]
pub struct TypedEntry<T>(gtk::Entry, std::marker::PhantomData<T>);

impl<T: ToString + FromStr> for TypedEntry<T> {
    // ...
}

§Structures and Enums

#[derive(Clone, glib::Downgrade)]
pub struct ControlButtons {
    pub up: gtk::Button,
    pub down: gtk::Button,
    pub left: gtk::Button,
    pub right: gtk::Button,
}

#[derive(Clone, glib::Downgrade)]
pub enum DirectionButton {
    Left(gtk::Button),
    Right(gtk::Button),
    Up(gtk::Button),
    Down(gtk::Button),
}