gtk-rs / gtk3-rs

Rust bindings for GTK 3
https://gtk-rs.org
MIT License
507 stars 90 forks source link

FileChooser interface is not implementable #756

Open kelnos opened 2 years ago

kelnos commented 2 years ago

I'm trying to build my own widget and have it implement the GtkFileChooser interface. To my ObjectSubclass impl, I've added:

type Interfaces = (gtk::FileChooser,);

However, I get the following error:

error[E0277]: the trait bound `gtk::FileChooser: IsImplementable<file_chooser_widget::imp::BetterFileChooserWidget>` is not satisfied
   --> widget/src/file_chooser_widget.rs:110:27
    |
110 |         type Interfaces = (gtk::FileChooser,);
    |                           ^^^^^^^^^^^^^^^^^^^ the trait `IsImplementable<file_chooser_widget::imp::BetterFileChooserWidget>` is not implemented for `gtk::FileChooser`
    |
    = note: required because of the requirements on the impl of `InterfaceList<file_chooser_widget::imp::BetterFileChooserWidget>` for `(gtk::FileChooser,)`
note: required by a bound in `gtk::subclass::prelude::ObjectSubclass::Interfaces`
   --> /home/brian/.cargo/registry/src/github.com-1ecc6299db9ec823/glib-0.15.11/src/subclass/types.rs:549:22
    |
549 |     type Interfaces: InterfaceList<Self>;
    |                      ^^^^^^^^^^^^^^^^^^^ required by this bound in `gtk::subclass::prelude::ObjectSubclass::Interfaces`

This seems to be correct; I don't see an IsImplementable implementation for FileChooser. There also isn't a FileChooserImpl trait to implement like I'd expect (though there is FileChooserExt, but my -- perhaps incorrect -- understanding is that is for consumers, not subclassers). Am I missing something here, or is this feature not implemented?

sdroege commented 2 years ago

Support for implementing FileChooser is not implemented in the bindings yet. You'd have to implement IsImplementable for that in the bindings and also add a FileChooserImpl, etc., similar to the other interfaces.

kelnos commented 2 years ago

Ah, I see! Ok, I'll take a crack at that. Thanks for the quick reply!

bilelmoussaoui commented 2 years ago

There is an implementation in gtk4-rs, you can look at it for inspiration.

kelnos commented 2 years ago

Hmm, gtk4-rs doesn't seem to have FileChooserImpl either, but it does have impls for interfaces, unlike gtk3-rs, so I'll take a look.

One thing I'm noticing that I don't quite understand is that some of the *Impl and *ImplExt traits use ObjectImpl as a trait bound, and others use ObjectSubclass. What is the reason for using one or the other?

bilelmoussaoui commented 2 years ago

Hmm, gtk4-rs doesn't seem to have FileChooserImpl either, but it does have impls for interfaces, unlike gtk3-rs, so I'll take a look.

Ah yeah never mind, I was confused for a bit and thought of FontChooser.

One thing I'm noticing that I don't quite understand is that some of the Impl and ImplExt traits use ObjectImpl as a trait bound, and others use ObjectSubclass. What is the reason for using one or the other?

Impl is the trait you implement in your application, ImplExt provides a way for you to chain up to the parent implementation

The Impl trait is something the end user will be implementing and it does enforce the requirements while the ImplExt is implemented on the bindings side for each type T that implements the Impl trait, hence it is already indirectly enforcing the requirement that is defined for Impl trait.

kelnos commented 2 years ago

Ok, I think I understand.

But I am now not sure this is doable. It looks like GtkFileChooserIface is defined in gtk/gtkfilechooserprivate.h. So the autogenerated bindings in the gtk-sys crate don't pick it up. Digging through their commit history, it looks like the interface refers to GtkFileSystem, which isn't public, so that's why they moved it into a private header. Bummer.