PopoverMenu too small when created programmatically with a dynamic model

I’m creating a GtkPopoverMenu programmatically in a GTK4/Rust application, and the popover is rendering too small, both too narrow and too short, with scrollbars appearing instead of it simply sizing itself to fit its contents (see the below image).

I’m creating it like this on right-click on the relevant widget:

let popover = gtk::PopoverMenu::from_model(Some(&menu_model));
popover.set_has_arrow(false);
popover.set_parent(&*imp.preview_scroller);
popover.set_halign(gtk::Align::Start);
popover.set_pointing_to(Some(&gtk::gdk::Rectangle::new(x as i32, y as i32, 0, 0)));
popover.popup();

The menu model is built dynamically in Rust using gtk::gio::Menu with two sections, one of which has a label. The popover is parented to a GtkScrolledWindow.

Is there something specific I need to do to allow a PopoverMenu to size itself naturally to its content?

Hi,

GtkScrolledWindow doesn’t use a LayoutManager, so the popover won’t be able to compute its size automatically.

You need to parent the popover either to a widget that uses a LayoutManager, or to a custom widget in which you have to manually call gtk_popover_present() in its size_allocate() vfunc.