How do save/restore window size/pos on gtk3 on linux

I am using the Go/Gtk3 bindings to create a simple GUI application on Linux (Debian 11.5 with the Xfce desktop).

I want to be able to save and restore the size and position of the application’s main window, but can’t figure it out. The Gtk docs essentially say (paraphrasing) “this isn’t supported by Gtk; use something platform-specific”; but don’t actually point to how this could be done on Linux.

I don’t know Go, but the Perl bindings have

( $width, $height ) = $window->get_size;
( $x, $y ) = $window->get_position;

and then:

$window->set_default_size( $width,  $height );
$window->move( $x, $y );

I’m guessing you can use something similar.

This strictly applies to GTK4, but it’s a good advice for any version of GTK. Even if GTK3 exposes API to retrieve the position of a top-level window, that functionality is not implemented on all backends, because not all windowing systems give you access to the global screen coordinates.

Saving the window state is perfectly possible, by following the tutorial on this very topic.

You should not save and restore the window position: positioning windows is the job of the window manager, and you cannot know what kind of geometry or stacking order the screen has; that information is window manager state, and it’s not available to toolkits.

1 Like

The Go API has the same functions as the Perl one you show. But when I tried them, the setters worked but the getters always returned the original size and position, not the last size and position the application had before closing.

As for the tutorial, it shows how to use the GSettings API: but I don’t want to use that, I want to use my own configuration file stored where I prefer.

I’m now wondering if the problem is actually a bug in the Go bindings. I’ve created a tiny eg and submitted a bug report:
gotk3 bug.

It is very likely not a bug. There is just no portable way for a client to change its own window position on Linux. On X11, the window manager can ignore the configure request and refuse to move the window. On Wayland, there is no way to do it at all.

The only reliable way to do this on Linux is to use the window manager’s API. On GNOME, the window management API is private and can only be accessed with shell extensions. On KDE Plasma, there is KWin scripts to do similar things, etc, you will have to look up the API for the specific window manager you are targeting.

We’re slightly at cross-purposes: moving & resizing the window work fine.
The problem is that GetPosition() and GetSize() always return the original pos & size that were set, and not the current pos & size (assuming the window has been moved & resized & therefore differs from the originals). That’s why it seems like a bug to me.

Solution I now connect to the configure-event signal and update my vars for x,y,w,h accordingly; then I just save & restore these and it works.

Thanks for your help!

Hmm, that is odd. Still, I think it is important to note: moving the window in that way will probably not work at all on some X11 window managers, or on Wayland. It may work fine on your current window manager, but it still is not something you probably want to ship in an app.

I’ll just have to hope that the gtk devs decide that this functionality – save/restore win size/pos and basic to desktop apps for decades – ought to be provided.
(Just like I hope that one day they’ll make scrollbars work like they did for decades.) In fact save/restore win size/pos – and scrollbar behavior – works correctly in Qt, Tk, FLTK, so it is pretty sad they don’t work (or work as expected in the case of scrollbars), in Gtk. (Every day when I start my computer I have to move Gtk application Claws Mail’s main window the same few pixels to the right to position it at the right of my screen. While a Qt 5 application that starts at the same time, also at the right of my screen, is always in the correct position.)

I also discovered that the solution is imperfect: it doesn’t account for window decorations & I don’t know an API for finding the titlebar height or the frame width: so I’m fudging these.

That is pretty unrelated to gtk. It is absolutely not basic functionality and it never has been. On Linux it is the window manager that has full control over window positioning, not the apps. Toolkit developers have no control over that. So you will need to configure the window manager to set the positions, or get another window manager that is able to do it. I have no idea why it works in those other toolkits, it is unreliable to depend on that in any toolkit.

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