granite icon indicating copy to clipboard operation
granite copied to clipboard

Reusable logic to remember window position and size

Open peteruithoven opened this issue 5 years ago • 10 comments

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.

peteruithoven avatar Oct 19 '18 20:10 peteruithoven

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.

dumblob avatar Nov 21 '18 15:11 dumblob

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);
}

naaando avatar Jan 30 '19 09:01 naaando

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.

peteruithoven avatar Jan 30 '19 11:01 peteruithoven

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.

naaando avatar Jan 30 '19 11:01 naaando

On the way #262

naaando avatar Jan 31 '19 04:01 naaando

Upstream is considering adding this feature natively https://gitlab.gnome.org/GNOME/glib/merge_requests/683/diffs

tintou avatar Feb 20 '19 13:02 tintou

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.

hadess avatar Feb 20 '19 13:02 hadess

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.

naaando avatar Mar 02 '19 01:03 naaando

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.

hadess avatar Mar 02 '19 08:03 hadess

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

naaando avatar Mar 05 '19 21:03 naaando

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

danirabbit avatar Nov 15 '23 16:11 danirabbit