Nw.js: Windows.setPosition("center") still bug?

Created on 21 Oct 2016  路  9Comments  路  Source: nwjs/nw.js

I want to know, is that bug or still development?

Most helpful comment

The root cause is that resizeTo is an async API and setPosition is implemented based on current bounds of app window. After resizeTo is called, the bounds is not updated immediately until the window is actually resized. So that setPosition is still using the old size and turned out to get a wrong position.

If you want your window always pinning to the center of the screen, hook up resize and move events of win and call setPosition('center') in the event handler.

If you want to move it immediately to center just once, you can manually calculate and set window's x and y coordinations after calling resizeTo. See calculation in NW.js source code.

All 9 comments

Can you post your own sample app?

I have the same bug, I posted it here https://github.com/nwjs/nw.js/issues/4766.

But ignored because it cannot be reproduced, so please post your own.

@kkm @mscreenie I cannot reproduce this issue either. Could you post your OS, NW.js version and full reproducible app?

I believe I have two monitors. I will post my sample soon.

Sure. I'm using Windows 7 and the problem affects 13,14 and 17 last I checked. I'll post an app and try it on other machines just in case its my problem.

Sample app: https://ufile.io/58c16

        var nw = require('nw.gui');
        var win = nw.Window.get();

        win.resizeTo(500, 500);
        win.setPosition("center"); // does not work
        document.write("Pos center");
        win.setResizable(true);

        setTimeout(function () {

            win.setPosition("center"); // works after timeout

            document.write("Center Timeout");


        }, 1000);

The root cause is that resizeTo is an async API and setPosition is implemented based on current bounds of app window. After resizeTo is called, the bounds is not updated immediately until the window is actually resized. So that setPosition is still using the old size and turned out to get a wrong position.

If you want your window always pinning to the center of the screen, hook up resize and move events of win and call setPosition('center') in the event handler.

If you want to move it immediately to center just once, you can manually calculate and set window's x and y coordinations after calling resizeTo. See calculation in NW.js source code.

Thank you the explanation. I didn't realize it was async. My users are still using 12.3 and this issue has only been present in above. I'm guessing after setPosition has been called I should remove the eventHandler?

Thanks.

You can use win.once instead of win.on, which does remove the handler after its first run.

@kkm If your bug is the same as mine - Could you close this? Seems like the method was async.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

loretoparisi picture loretoparisi  路  3Comments

chino23 picture chino23  路  3Comments

rumax picture rumax  路  4Comments

bambooCZ picture bambooCZ  路  3Comments

daverave1212 picture daverave1212  路  3Comments