Cannot set_default_size() to a maximize window
I’m used to save both the size and the maximized state of the main windows of my applications, and to restore them at application startup.
In Gtk4, I’m having a failure to get things working in a specific case: when the window is maximized, I call the gtk_window_maximize()
method during the construct of the window, and then the default size of the window is set to (1, 1)
, even if set_default_size()
has been called with the correctly saved size; so when I unmaximize the window, it is taking a buggy minimalist size.
Some selected lines from the code:
private class GameWindow : ApplicationWindow
{
private GLib.Settings settings = new GLib.Settings ("org.gnome.five-or-more");
private bool window_is_maximized;
private int window_width;
private int window_height;
construct
{
map.connect (init_state_watcher);
unmap.connect (on_unmap);
window_width = settings.get_int ("window-width");
window_height = settings.get_int ("window-height");
window_is_maximized = settings.get_boolean ("window-is-maximized");
if (window_is_maximized)
maximize ();
set_default_size (window_width, window_height);
}
private void init_state_watcher ()
{
Gdk.Surface? nullable_surface = get_surface (); // TODO report bug, get_surface() returns a nullable Surface
if (nullable_surface == null || !((!) nullable_surface is Gdk.Toplevel))
assert_not_reached ();
surface = (Gdk.Toplevel) (!) nullable_surface;
surface.notify ["state"].connect (on_window_state_event);
surface.size_changed.connect (on_size_changed);
}
private Gdk.Toplevel surface;
private inline void on_window_state_event ()
{
Gdk.ToplevelState state = surface.get_state ();
window_is_maximized = (state & Gdk.ToplevelState.MAXIMIZED) != 0;
}
private inline void on_size_changed (int width, int height)
{
if (window_is_maximized)
return;
get_size (out window_width, out window_height);
}
private inline void on_unmap ()
{
settings.delay ();
settings.set_int ("window-width", window_width);
settings.set_int ("window-height", window_height);
settings.set_boolean ("window-is-maximized", window_is_maximized);
settings.apply ();
application.quit ();
}
}
There’s certainly a better way to do things, but in any way, that should work I think. Restoring a non-maximized window works as expected, saving the state works as expected in all the cases, etc.