I am trying to create an image which fills its parent widget in both dimensions while keeping the aspect ratio. In gtk4-rs I did
let picture_widget = gtk::Picture::builder()
.hexpand(true)
.vexpand(true)
.halign(gtk::Align::Fill)
.valign(gtk::Align::Fill)
.keep_aspect_ratio(true)
.can_shrink(true)
.build();
But this fills only one dimension of the parent. However, I would like to overflow the parent in that dimension, such that both dimensions are filled.
Generally, I think my goal could be achieved by child.set_size_request(parent_width, parent_height)
. That needs to be done every time the parent size changes.
Idea 1: Listen to size_allocate
signal of parent. Does not work, because the size_allocate
signal has been removed: https://docs.gtk.org/gtk4/migrating-3to4.html#adapt-to-gtkwidgets-size-allocation-changes
Idea 2: Subclass Picture
and set size in allocate()
. But Picture
is apparently not subclassable. The compiler tells me that
type ParentType = gtk::Picture; => the trait
IsSubclassable<FillingPicture>
is not implemented forPicture
Idea 3: Subclass Widget
and use Picture
inside (from https://github.com/gtk-rs/gtk4-rs/issues/393). I do not know how to then link all the function calls together such that the Picture
is actually shown. (It also feels wrong to do this. If this was easily possible, Picture
would probably be subclassable?)
Idea 4: Reimplement the whole Picture
class from scratch. Seems overkill and too much maintenance overhead just for a simple resize.
Idea 5: Use CSS properties to fill the parent. I tried setting width
and height
to 100%, but apparently relative sizes are not allowed. Also the cover
CSS attribute is not supported.
Idea 6: Drop GTK
Any other ideas or fixes for the ideas above?