I needed some fit-to-width widget so GtkPicture inside a AdwPreferenceRow doesn’t get crushed into a dot when I resize the window vertically.
So I wrote this code (gtk-rs, Rust), and it works well.
use glib::Object;
use gtk::glib;
glib::wrapper! {
pub struct FitToWidthWidget(ObjectSubclass<imp::FitToWidthWidget>)
@extends gtk::Widget,
@implements gtk::Accessible, gtk::Buildable, gtk::ConstraintTarget;
}
impl FitToWidthWidget {
pub fn new() -> Self {
Object::builder().build()
}
}
mod imp {
use adw::subclass::prelude::*;
use gtk::prelude::*;
use gtk::{SizeRequestMode, glib};
#[derive(Default)]
pub struct FitToWidthWidget;
#[glib::object_subclass]
impl ObjectSubclass for FitToWidthWidget {
const NAME: &'static str = "SceFitToWidthWidget";
type Type = super::FitToWidthWidget;
type ParentType = gtk::Widget;
}
impl WidgetImpl for FitToWidthWidget {
fn request_mode(&self) -> SizeRequestMode {
SizeRequestMode::WidthForHeight
}
fn measure(&self, orientation: gtk::Orientation, for_size: i32) -> (i32, i32, i32, i32) {
let child = self.obj().first_child().unwrap();
let (min, nat, min_baseline, nat_baseline) = child.measure(orientation, for_size);
if orientation == gtk::Orientation::Vertical {
// return natural size as minimum size
return (nat, nat, min_baseline, nat_baseline);
}
(min, nat, min_baseline, nat_baseline)
}
fn size_allocate(&self, width: i32, height: i32, baseline: i32) {
let child = self.obj().first_child().unwrap();
child.allocate(width, height, baseline, None);
}
}
impl ObjectImpl for FitToWidthWidget {
fn dispose(&self) {
let child = self.obj().first_child().unwrap();
child.unparent();
}
}
}
But I got these warnings on Windows (G_ENABLE_DEBUG is turned off on my Linux distro)
(program.exe:73640): Gtk-WARNING **: 01:11:28.263: Widget reports min height of 195 for width of 467, but min width of 0 for height of 191
(program.exe:73640): Gtk-WARNING **: 01:11:28.263: Widget reports min height of 219 for width of 491, but min width of 24 for height of 215
(program.exe:73640): Gtk-WARNING **: 01:11:28.264: Widget reports min height of 224 for width of 495, but min width of 28 for height of 220
(program.exe:73640): Gtk-WARNING **: 01:11:28.226: Widget reports min width of 0 for height of 195, but min height of 204 for width of 488
(program.exe:73640): Gtk-WARNING **: 01:11:28.226: Widget reports min width of 24 for height of 219, but min height of 228 for width of 512
(program.exe:73640): Gtk-WARNING **: 01:11:28.226: Widget reports min width of 28 for height of 224, but min height of 233 for width of 516
It appears that they are from this:
and the code was the introduced at this change with the following commit message:
sizerequestcache: Warn on contradictory measurements (3e7ec4f9) · Commits · GNOME / gtk · GitLab
When committing the measurement for a widget to the cache, check if the
measurement contradicts a previous measurement in the opposite
orientation, and print a warning. Unfortunately, we don’t have a
reference to the widget itself at this point in the code, so we can’t
name the specific widget; nevertheless it should be easy to break on the
wanring and inspect the stack trace to see which measurement has
triggered the warning.
I get the idea. When measuring the minimum size (say, height) for width, comparing with the minimum width for height makes sense.
But I don’t get how the log message’s dimensions are contradictory.
(program.exe:73640): Gtk-WARNING **: 01:11:28.263: Widget reports min height of 195 for width of 467, but min width of 0 for height of 191
(program.exe:73640): Gtk-WARNING **: 01:11:28.263: Widget reports min height of 219 for width of 491, but min width of 24 for height of 215
(program.exe:73640): Gtk-WARNING **: 01:11:28.264: Widget reports min height of 224 for width of 495, but min width of 28 for height of 220
Why is it contradictory to return a larger min height (than for height) for a larger for width (than min width)?
Would it be more contradictory to return a smaller min height for a larger for width and vice versa?
(program.exe:73640): Gtk-WARNING **: 01:11:28.226: Widget reports min width of 0 for height of 195, but min height of 204 for width of 488
(program.exe:73640): Gtk-WARNING **: 01:11:28.226: Widget reports min width of 24 for height of 219, but min height of 228 for width of 512
(program.exe:73640): Gtk-WARNING **: 01:11:28.226: Widget reports min width of 28 for height of 224, but min height of 233 for width of 516
Similarly, why is it contradictory to return a smaller min width for a smaller for height?


