Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Windows.setPosition("center") still bug? #5390

Closed
kkm opened this issue Oct 21, 2016 · 9 comments
Closed

Windows.setPosition("center") still bug? #5390

kkm opened this issue Oct 21, 2016 · 9 comments
Assignees

Comments

@kkm
Copy link

kkm commented Oct 21, 2016

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

@mscreenie
Copy link
Contributor

Can you post your own sample app?

I have the same bug, I posted it here #4766.

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

@ghostoy
Copy link
Member

ghostoy commented Oct 24, 2016

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

@kkm
Copy link
Author

kkm commented Oct 27, 2016

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

@mscreenie
Copy link
Contributor

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.

@mscreenie
Copy link
Contributor

mscreenie commented Oct 31, 2016

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

@ghostoy
Copy link
Member

ghostoy commented Nov 1, 2016

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.

@mscreenie
Copy link
Contributor

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.

@ghostoy
Copy link
Member

ghostoy commented Nov 2, 2016

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

@mscreenie
Copy link
Contributor

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants