[GTK4/3] ScrolledWindow propagate_natural_height not calculating multi-line label height correctly
Steps to reproduce
Here's a small GTK4 Vala example:
const string LOREM = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magnam aliquam quaerat voluptatem. Ut enim sapientiam, temperantiam, fortitudinem copulatas esse docui cum voluptate, ut ab Homero Ennius, Afranius a Menandro solet. Nec vero, ut noster Lucilius, recusabo, quo minus id, quod sentiant non esse faciendum, ii voluptatem maximam adipiscuntur praetermittenda voluptate. Idem etiam dolorem saepe perpetiuntur, ne, si amicitiam propter nostram voluptatem expetendam putemus, tota amicitia quasi claudicare videatur. Itaque primos congressus copulationesque et consuetudinum instituendarum voluntates fieri propter voluptatem; cum autem usus progrediens familiaritatem effecerit, tum amorem efflorescere tantum, ut, etiamsi nulla sit utilitas ex amicitia, tamen ipsi amici propter se esse fugiendum. Itaque aiunt hanc quasi naturalem atque insitam in animis inclusae inter se reprehensiones non sunt vituperandae, maledicta, contumeliae, tum iracundiae, contentiones concertationesque in disputando pertinaces indignae philosophia mihi videri solent.";
class Window : Gtk.ApplicationWindow {
Gtk.Box box = new Gtk.Box (Gtk.Orientation.VERTICAL, 12);
Gtk.Button add_button = new Gtk.Button.with_label ("Append");
Gtk.ScrolledWindow scrolled_window = new Gtk.ScrolledWindow () {
has_frame = true,
min_content_height = -1,
max_content_height = 500,
propagate_natural_height = true,
};
Gtk.Viewport viewport = new Gtk.Viewport (null, null);
Gtk.Box view_box = new Gtk.Box (Gtk.Orientation.VERTICAL, 12);
public Window (Gtk.Application app) {
Object(application: app);
set_child (box);
box.append (add_button);
// Adding a multi-line label doesn't expand the ScrolledWindow past the first line
add_button.clicked.connect (() => {
view_box.append (new Gtk.Label (LOREM) {
wrap = true,
lines = 5,
ellipsize = Pango.EllipsizeMode.END,
});
});
box.append (scrolled_window);
scrolled_window.set_child (viewport);
viewport.set_child (view_box);
// Append a large image. The ScrolledWindow adapts its size correctly as expected
view_box.append (new Gtk.Image.from_icon_name ("audio-volume-high") {
pixel_size = 256,
});
}
}
int main () {
var app = new Gtk.Application ("com.example.app",
ApplicationFlags.DEFAULT_FLAGS);
app.activate.connect (() => {
new Window (app).present();
});
return app.run (null);
}
Click on the button on at the top of the widow which appends a Label to the Viewport.
Current behavior
The ScrolledWindow only expands for the height of the first Label line (when the window is larger than the ScrolledWindow).
Expected outcome
The ScrolledWindow should expand for the full height of the Label.
Version information
GTK Version: 4.10.3 Distribution: Fedora 38
Additional information
I also experienced this issue in my GTK3 application but we were able to "fix" the bug by doing this:
- Connecting to the ::size-allocate signal of the labels parent, in our case, a ListBoxRow
- Setting the Labels size request height to its allocated height:
label.set_size_request (-1, label.get_allocated_height ());
in said signal callback - Call of a
queue_resize
for the ListBoxRow directly after it's added to the ListBox
That workaround doesn't work anymore (at least with my limited GTK4 knowledge) in GTK4 now when the ::size-allocate signal has been removed.
Here's a screenshot of the GTK4 example application that I provided the code for above.
Edited by Erik Reider