gtk-rs / gtk-rs-core

Rust bindings for GNOME libraries
https://gtk-rs.org/gtk-rs-core
MIT License
272 stars 103 forks source link

[FEATURE REQUEST] Automatic `ObjectSubclass` #1412

Open DaKnig opened 1 month ago

DaKnig commented 1 month ago

every class needs this tiny bit of boilerplate (from the book):

// The central trait for subclassing a GObject
#[glib::object_subclass]
impl ObjectSubclass for Window {
    // `NAME` needs to match `class` attribute of template
    const NAME: &'static str = "MyGtkAppWindow";
    type Type = super::Window;
    type ParentType = gtk::ApplicationWindow;

    fn class_init(klass: &mut Self::Class) {
        klass.bind_template();
        klass.bind_template_callbacks();
    }

    fn instance_init(obj: &InitializingObject<Self>) {
        obj.init_template();
    }
}

from what I understand we need that because in some cases we may or may not want to call klass.bind_template_callbacks(). every single widget I have contains this much boilerplate... and most of them do nothing interesting besides that.

suggestions:

DaKnig commented 1 month ago

this is by far the most annoying bit of the boilerplate. I always get it wrong. I always forget something. with this we could just have impl ObjectSubclass for DewYtItemList {} or something just like we have for everything else.

bilelmoussaoui commented 4 weeks ago

The issue itself is not really actionable. Instead, it should mention having a way to inject something into class_init / instance_init automatically.