Skip to content

Preserve window state between sessions

zcbenz edited this page Oct 27, 2012 · 15 revisions

First you need to lazily show the window until your app loads previous state:

{
  "window": {
    "show": false
  }
}

Then you can load everything in the onload or $(document).ready, and save them by listening to close event:

<html>
<body>
<script>
  var gui = require('nw.gui');
  var win = gui.Window.get();

  // Save size on close.
  win.on('close', function() {
    localStorage.x      = win.x;
    localStorage.y      = win.y;
    localStorage.width  = win.width;
    localStorage.height = win.height;
    this.close(true);
  });

  // Restore on startup.
  onload = function() {
    if (localStorage.width && localStorage.height) {
      win.resizeTo(localStorage.width, localStorage.height);
      win.moveTo(localStorage.x, localStorage.y);
    }

    win.show();
  };
</script>
</body>
</html>
Clone this wiki locally