Attribute Macro widget

Source
#[widget]
Expand description

Define a new GTK widget based on a struct

§Example

use gdk4::subclass::prelude::ObjectSubclassIsExt;
use glib::closure_local;
use gtk::prelude::*;
use std::cell::Cell;

#[widget(extends gtk::Box)]
#[template(file = "card.ui")]
pub struct Card {
    #[property_string]
    pub text: Cell<String>,
    #[signal]
    pub card_changed: (),
    #[signal]
    pub card_clicked: (),

    #[template_child]
    pub card_button: TemplateChild<gtk::Button>,

    #[template_child]
    pub card_entry: TemplateChild<gtk::Entry>,
}

impl Card {
    pub fn new(&self) {
        let s = self;
        self.imp()
            .card_button
            .connect_clicked(glib::clone!(@weak s => move |_| {
                s.emit_card_clicked()
            }));
        self.imp()
            .card_entry
            .connect_changed(glib::clone!(@weak s => move |entry| {
                let text = entry.text().to_string();
                s.imp().text.replace(text);
                s.emit_card_changed()
            }));
    }

    // If you want to have a public connector you can define one like this.
    // The method _connect_<signal-name> is generated for this purpose
    pub fn conenct_card_clicked(&self, f: impl Fn(&Self) + 'static) {
        self._connect_card_clicked(f);
    }

    pub fn connect_card_changed(&self, f: impl Fn(&Self) + 'static) {
        self._connect_card_changed(f);
    }

    pub fn text(&self) -> String {
        self.property("text")
    }
}