Re: saving window size?



On Tue, 2009-01-20 at 09:26 -0500, Dr. Michael J. Chudobiak wrote:
Hi all,

Suppose I have an app, and I would like to save the window size when 
when quitting.

This is easy to do if the user selects File->Quit, but what if they 
click window-close? By the time you get the destroy signal, the true 
width/height values are gone. (You seem to get the default values.)

Is there a "resize" signal I can listen to?

if you try to save the window size on delete-event or destroy you might
already have lost.

use the ::size-allocate signal on the Window to save the size inside
private data, and the ::window-state-event signal to know whether the
window has been maximized/minimized/fullscreen, etc.:

    this.size_allocate += on_size_allocate;
    this.window_state_event += on_window_state_event;
    this.destroy += on_destroy;

  private void on_size_allocate (Allocation alloc) {
    this._width = alloc.width;
    this._height = alloc.height;
  }

  private void on_window_state_event (Gdk.EventWindowState event) {
    if (event.new_window_state & Gdk.WindowState.MAXIMIZED != 0) {
      this._is_maximized = true;
    }
    else {
      this._is_maximized = false;
    }
  }

then use the ::destroy or the ::delete-event signals to save the state
of the window:

  private void on_destroy () {
    GLib.KeyFile kf = new GLib.KeyFile();

    kf.set_integer("WindowState", "WindowWidth", this._width);
    kf.set_integer("WindowState", "WindowHeight", this._height);
    kf.set_boolean("WindowState", "IsMaximized", this._is_maximized);

    buf = kf.to_data();
    try {
      GLib.FileUtils.set_contents("window-state.cache", buf);
    }
    catch (Error e) {
      warning("Unable to save window state: %s", e.message);
    };
  }

[BEWARE: this is untested code; it can be regarded as pseudo-code
looking similar to Vala]

Here's what I have, in vala:


window.destroy += quitSave;

//quit menu item
Action quit = (Action)builder.get_object("menubar_quit");
quit.activate += (quit) => {quitSave (window);};

...
private void quitSave(Window window)
{
var gc = GConf.Client.get_default ();
int width = 0;
int height = 0;
window.get_size (out width, out height);

don't *ever* use GConf to store the window size. it's *not* a user
preference, and the GConf can very well be not writable.

use a file inside a directory like the user cache directory as
established by the XDG base dir specification.

ciao,
 Emmanuele.

-- 
Emmanuele Bassi,
W: http://www.emmanuelebassi.net
B: http://log.emmanuelebassi.net




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]