How to center GtkWindows in gtk4?

As I’ve said above, not all windowing systems have a concept of global coordinates or allow direct positioning of the windowing from an application. Wayland, for instance, does not. All windows have their own coordinate space, with the origin placed in the top-left corner. There is no way, down to the protocol level, to allow GTK to position a window using screen coordinates. This means that GTK cannot possibly implement this functionality.

This functionality needs to be negotiated between toolkits and desktop environments; GNOME definitely wants to be able to deal with session management, but it’s a very complicated task, and it most definitely does not belong to the application to restore its own position across sessions. What happens when the previously occupied region is already occupied? What happens when you launch two instances of an application? What happens when the geometry of the screens changes across sessions? What happens when you update your desktop and the UI layout has changed?

Of course, I’m only considering the case of well-behaved, benign applications. Hostile applications could do even worse, like positioning on top of another application, and preventing you from accessing it; or overlapping a system dialog, and doing things like intercepting your passwords. Or position themselves right outside of the visible screen and stealing all focus.

In other words: an application has a (by design) limited view of the environment in which it’s been executed. The only component that has a complete view is the window manager. As such, it’s the role of the window manager to position windows and to restore their location.

Because it’s not just a matter of window managers: the preferred windowing system API on Linux does not have this functionality, and as such, a toolkit using it cannot maintain it.

Of course, you can delay moving to GTK4 as long as you like. A word of advice: it’s extremely unlikely (though not impossible) that this functionality will ever be added again, considering that we’re moving in the direction of self-contained application with minimal privileges all across the board.

If you are using a platform and a windowing system that allows you to position windows directly, then you’re also free to use platform-specific API to do that; all that GTK4 removed was the generic, platform-agnostic API, given that it wasn’t portable or sustainable any more.

1 Like