Paned resizes the wrong child

If my understanding of the documentation is correct, then if I have a GtkPaned with resize-end-child=False and resize-start-child=True, then when that GtkPaned resizes, the extra space should be consumed by the start child first.

I have a layout with a GtkPaned as the child of my main window. When I resize the window, despite having resize-end-child=False and resize-start-child=True, it is the end child that resizes to take up the new space.

Are there some conditions that I have missed for making this work?
I tried changing various halign/hexpand properties on the children, and had no luck.

testui.xml

<?xml version='1.0' encoding='UTF-8'?>
<interface>
  <requires lib="gtk" version="4.6"/>
  <object class="GtkApplicationWindow" id="mainwindow">
    <property name="default-height">600</property>
    <property name="default-width">800</property>
    <child>
      <object class="GtkBox">
        <property name="orientation">vertical</property>
        <child>
          <object class="GtkPaned" id="mainpane">
            <property name="wide-handle">True</property>
            <property name="orientation">vertical</property>
            <property name="resize-end-child">False</property>
            <property name="resize-start-child">True</property>
            <property name="end-child">scrolltext</property>
            <property name="start-child">images</property>
            <child>
              <object class="GtkPaned" id="images">
                <property name="hexpand">True</property>
                <property name="vexpand">True</property>
                <property name="halign">fill</property>
                <property name="wide-handle">True</property>
                <child>
                  <object class="GtkDrawingArea">
                    <property name="content-height">230</property>
                    <property name="content-width">230</property>
                  </object>
                </child>
                <child>
                  <object class="GtkDrawingArea">
                    <property name="content-height">400</property>
                    <property name="content-width">400</property>
                  </object>
                </child>
              </object>
            </child>
            <child>
              <object class="GtkScrolledWindow" id="scrolltext">
                <property name="child">log</property>
                <child>
                  <object class="GtkTextView" id="log">
                    <property name="buffer">
                      <object class="GtkTextBuffer"/>
                    </property>
                    <property name="editable">False</property>
                    <property name="height-request">300</property>
                  </object>
                </child>
              </object>
            </child>
          </object>
        </child>
      </object>
    </child>
  </object>
</interface>

test.py

#!/usr/bin/env python3
import sys 
import gi
gi.require_version("Gtk", "4.0")
from gi.repository import GLib, Gtk 

class MyApplication(Gtk.Application):
    def __init__(self):
        super().__init__(application_id="com.example.MyGtkApplication")
        GLib.set_application_name('My Gtk Application')

    def do_activate(self):
        builder = Gtk.Builder()
        builder.add_from_file("testui.xml")
        self.win = builder.get_object("mainwindow")
        self.win.set_application(self)
        self.win.present()

app = MyApplication()
exit_status = app.run(sys.argv)
sys.exit(exit_status)

Looking in the inspector, resize-start-child and resize-end-child are set opposite of what you have in your UI file. As it turns out, this is because of GtkPaned’s GtkBuildable implementation:

I’m not entirely sure why, but adding a start child sets resize-start-child to False, and adding an end child sets resize-end-child to True.

You can solve this by putting the property-setting lines after the <child> elements.

Also, setting start-child and end-child doesn’t work like that. You can rely on the fact that the first child added will be the start child, or you can use <child type="start">.

Aha! yep, moving the property lines below the child tags did resolve the issue. Thanks :slight_smile:

I had previously been using the Cambalache editor to generate the xml for me, but I ran into so many issues that I abandoned it and manually fixed up the xml. I found that sometimes Cambalache added the start-child and end-child properties with invalid values, so I had to manually fix them. They remained in my file until now. I have removed them and it appears to work fine.

As someone who has only a small amount of experience with GTK, it is surprising to me that the order of things in the XML file changes the behaviour of the UI. And indeed, not even Cambalache generated the file in the working order. I wonder if there is a good way to make this problem easier to spot for a beginner.

anyway, thanks for the help :slight_smile:

I was surprised as well. A little digging turned up:

Apparently the right way is to use start-child and end-child, but like this:

<property name="start-child">
  <object class="GtkPaned" id="images">
    [...]
  </object>
</property>

i.e. don’t use <child> at all with GtkPaned.

1 Like

ah, I wish they had implemented the warning that was suggested there, would have saved me some bother for sure :laughing:

Adding a warning is not really trivial, because UI description files just call the public API; from within GtkPaned you cannot know that a property or a child were added from a UI definition file, by design.

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