granite
granite copied to clipboard
Reusable logic to remember window position and size
A lot of applications are writing custom logic to store and retrieve the last position and sometimes size of the window.
It would be great if there was something build into Granite that could handle this.
Maybe load_state()
and save_state()
functions that apps could optional call.
Want to back this issue? Post a bounty on it! We accept bounties via Bountysource.
Mind there might be more than one state - so load_state()
and save_state()
would need to work with a list of states rather then with exactly 2 states.
I was with the same trouble so I made a mixin for that on my app, pushing it to granite was my first thought, see if it suits your needs.
https://github.com/naaando/lyrics/blob/0.5.2/src/View/Helper/SaveWindowStateMixin.vala
The class user just have to append the SaveWindowStateMixin
interface and use enable_restore_state (GLib.Settings settings)
and it saves window_x
, window_y
, window_width
and window_height
. And of course it could and should be a bit more complex to support more use cases.
Usage:
public class Window : Gtk.ApplicationWindow, SaveWindowStateMixin {
GLib.Settings settings;
// inside the constructor
public Window () {
enable_restore_state (settings);
}
Great! That enables reuse with minimal effort while still keeping the settings extendable. I'm super curious how the team things about how that works, with the mixin concept.
The Reference was recently updated with information on Saving Window State, The references does things somewhat differently:
- It saves position and size in 2 settings instead of 4, I'm not sure what's better.
- It supports window-maximized
- It throttles writing to GSettings from the configure_event which seems important.
- It uses a different way to get & set the window size, no clue what's better, I feel you're approach is easier to read.
Cool, I didn't know about that reference, I think I can implement this tomorrow. About the method to obtain the window size I don't think this makes any difference so if there's no objection I'll stick with what I'm already using, I agree it looks easier to read.
On the way #262
Upstream is considering adding this feature natively https://gitlab.gnome.org/GNOME/glib/merge_requests/683/diffs
Note that, just like for GConf, GSettings' predecessor, you're not supposed to store state in the GSettings:
Do not store anything but preferences in GConf. Documents, session state, random data blobs do not belong in GConf. Stuff breaks if you do this. Moreover, THERE IS NO GUARANTEE THAT IT'S EVEN POSSIBLE TO WRITE TO THE GCONF DATABASE. Which means you may not use GConf as an IPC mechanism or when it's required to be able to store a piece of data.
Any lockdown done by the admin and your state is never saved.
I believe session state they meant is the running time of a clock, results of a function (grossly speaking), usually the logic to save windows are implemented with throttling, so I think it's an appropriated place to persist this kind of state.
I believe session state they meant is the running time of a clock, results of a function (grossly speaking), usually the logic to save windows are implemented with throttling, so I think it's an appropriated place to persist this kind of state.
No, any state.
I'll add that some state information can't be saved or restored by applications, like window placement when using Wayland.
I was reading the reference of gsettings and it suggests that it can be used to store window geometry, have a look at the second paragraph of https://developer.gnome.org/gio/stable/GSettings.html#gsettings-relocatable
As of Gtk 4 and Wayland apps can no longer position themselves, so there is no recommended way to save/restore position. This will be handled by Gala in OS 8 automatically.
In Gtk 4 you can do a simple gsettings bind to default_height
and default_width
so it's really trivial to save state here with gsettings.
I'm going to close this since we now don't need to save window position and saving size has become trivial