pid
pid

Reputation: 11597

Get and set correct window position with node.js

In the close event I persist the window position as follows:

localStorage.lastlayout = JSON.stringify({
  "left": win.x,
  "top": win.y,
  "width": win.width,
  "height": win.height
});

On start, I restore it with:

    data = JSON.parse(localStorage.lastlayout);
    win.moveTo(data.left, data.top);
    win.resizeTo(data.width, data.height);

The problem is the position is off. As far as I can tell, I get the inner position. When setting I set the outer position. The windowing title and borders only accounted for when setting.

As this is cross-platform code I cannot test it on all platforms (yet) and I don't know if it is possible to get the metrics of the windowing engine so that I can correct it programmatically (think about the user changing the DPI or theme on Windows).

Is there a way to compensate for this?

Upvotes: 2

Views: 2089

Answers (1)

pid
pid

Reputation: 11597

Ok, I've nailed it. I detect the offsets in respect to a known position (0, 0) and then detract it from the desired position. It's not perfect but it works:

      win.resizeTo(128, 128);
      win.moveTo(0, 0);
      log("dbg", "win", "detected metrics: " + win.x + ", " + win.y);
      win.moveTo(data.left - win.x, data.top - win.y);
      win.resizeTo(data.width, data.height);

The initial resize is necessary for some reason. It looks like the title and borders are not drawn at all on some platforms if the window is not large enough. Next I move it to the origin. The win.x and win.y contain the offsets at this point. The second moveTo() uses the corrected coordinates and finally I resize the window to the actually desired size.

Upvotes: 4

Related Questions