GtkPicture fixed aspect ratio and size

This original post was so poorly written, I’m rewriting it in the hopes of clarifying my question.

I have a GtkBox subclass that defines its UI in a GtkBuilder template. It’s widget hierarchy is roughly:

GtkBox
├── AdwClamp
│   └── GtkOverlay
│       ├── GtkStack
│       │   ├── GtkStackPage
│       │   │   └── GtkPicture
│       │   └── GtkStackPage
│       │       └── GtkPicture
│       └── [overlay] GtkButton
├── GtkLabel
└── GtkLabel

It looks like this:

Screenshot from 2023-09-13 09-51-49

The contents of the GtkPicture changes when a new image is downloaded. I’d like to fix the aspect ratio of the GtkPicture to a constant value, so that whenever a new picture is set as the GtkPicture’s paintable, it crops the image. I essentially want a fixed aspect ratio preview window, but I can’t figure out how to fix the size of the GtkPicture (or a parent widget, if that’s the right way to go about doing this).

Currently, if the GtkPicture is loaded with an image that has larger vertical size than horizontal size, the GtkPicture scales up when I resize my window, looking something like this:

What is the right way to go about fixing the dimensions of a GtkPicture so that it is a constant size?

As far as I know you probably want to use Gtk.Image. It is different to Gtk.Picture in that the size is defined by the application, and not the picture.

Gtk.Image is super fixed on size. That is even if the user resizes the window manually, it won’t scale with it. Maybe setting Gtk.Picture’s can-shrink property to true and content-fit property to COVER can do the job.

This is what I’m working on. I think some combination of using a GtkPicture and setting its paintable to a snapshot of a texture. I’ll be sure to share what I settle on when I figure it out.

OK, So, I tried a simple example with a Gtk.Picture being the only child of a Gtk.ApplicationWindow. Keeping everything the default and changing just the file property of the picture doesn’t resize the window. The Gdk.Paintable that was autogenerated by changing the file property had its STATIC_SIZE flag set. So, if whatever implementation of Gtk.Paintable you are using has a way to set that flag, doing so might help. Otherwise, just use Gio.File instead of Gdk.Paintable.

So I figured out that if I set the GtkPicture’s width-request and height-request properties to:

            int width = int.min (300, texture.get_intrinsic_width ());
            int height = int.min (200, texture.get_intrinsic_height ());

Then I can realistically expect a smallish size preview image that sets the size of the GtkPicture to exactly the size of the texture used as its paintable.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.