You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Regression: when hidden (show = false), outerHeight and outerWidth are set to 0 #2935
In v0.33.3, if you create a BrowserWindow with options.show = false, window.outerHeight and window.outerWidth will be equal to 0. If you reload the page after it becomes visible, the values will change to the correct values.
This is a regression. It worked in all the previous versions we used, including v0.33.0.
To reconstruct, please use the following code:
main.js
varapp=require('app');varBrowserWindow=require('browser-window');varmainWindow=null;app.on('window-all-closed',function(){if(process.platform!='darwin'){app.quit();}});app.on('ready',function(){mainWindow=newBrowserWindow({width: 800,height: 600,show: false// The window starts hidden});mainWindow.loadUrl('file://'+__dirname+'/index.html');// We delay showing the window for 2 secondssetTimeout(function(){mainWindow.show();},2000);mainWindow.on('closed',function(){mainWindow=null;});});
index.html
<!DOCTYPE html><html><head><metacharset="UTF-8"><title>Hello World!</title></head><script>console.log('window.innerHeight',window.innerHeight,'window.outerHeight',window.outerHeight,'window.innerWidth',window.innerWidth,'window.outerWidth',window.outerWidth);</script><body><h1>Hello World!</h1>
We are using io.js <script>document.write(process.version)</script>
and Electron <script>document.write(process.versions['electron'])</script>.
</body></html>
Run this with v0.33.3. The window is shown roughly 2 seconds after creation. Once the window is visible, open the devtools console and you'll see something like this (notice the zeros):
@etiktin i wouldnt call this regression, the behaviour is now consistent with what you get when explicitly calling mainWindow.hide() . And you have mainWindow.getSize to use in those scenario.
Is there anything else that might be effected by this change, other than outerHeight, outerWidth and document.hidden?
There are scenarios when you want to run Electron without showing it and you still want it to render everything as if it was visible. Here's 2 examples that come to mind:
When a new BrowserWindow is created, you first see a white background until the page is loaded with your custom css. This is especially annoying if your app uses dark colors. To mitigate this issue, we start our apps hidden and show it only after it's rendered. This solution was suggested in a couple of issues. For example here. For us, when outerHeight and outerWidth are set to zero, the rendering is incorrect.
Electron, doesn't have a proper off screen support, so instead some projects choose to run it with show = false. One such project is Nightmare, which is a browser automation library (similar to PhantomJS). It's supposed to test your web apps as if it was running in a visible browser, but now some rendering will break.
/ cc @matthewmueller
And you have mainWindow.getSize to use in those scenario
It doesn't always return the same values as outerHeight and outerWidth. For example when running using the reconstruction code with show = true the outer dimensions are: 784x561
When running mainWindow.getContentSize() I get: 784x541.
When running mainWindow.getSize() I get: 800x600.
So as you can see non of the values is exactly the same.
In any case, even if it worked, we couldn't have changed it in all the sources, since some of them are from the web. We could have tried to fake it using preload script, but that only works for window === window.top (preload scripts don't currently run in iframes etc., see #2605). Edit: It also seems that outerHeight, outerWidth are set to read only.
I also tried to use web-preferences.page-visibility = true, thinking that it might cause the page to think it's visible, but the result was the same (the dimensions were 0). Even if it had worked, I would still need some way to reset it after the initial display because of it's adverse effect on battery life.
I respect wanting to confirm to the spec, but I also think we should provide a way to run an app hidden and make it think it's visible so it can render everything as expected. Some possible solutions:
Add a new option (or a new possible value to show) so we can skip the initial set as hidden which will make it act like before. This way the default behavior will confirm with the spec, but the use cases I mentioned will be able to work with minimal changes.
Remove the change, and instead fake document.hidden like the workaround mentioned here (maybe add it to override.coffee).
@etiktin Excellent explanation, it looks quite reasonable to keep the old behavior, I'm more lean to the second solution, e.g. hacking the document.hidden, so we can still fix the original issue, while keeping everything else unchanged.
I'm working on a project doing similar work in a hidden background window that I need to be hidden, but needs to think it is visible for rendering purposes.
I am using 0.33.1 for the time being, as it still seems to work at that version.
Activity
deepak1556 commentedon Sep 30, 2015
@etiktin i wouldnt call this regression, the behaviour is now consistent with what you get when explicitly calling
mainWindow.hide(). And you havemainWindow.getSizeto use in those scenario.zcbenz commentedon Sep 30, 2015
From the W3C draft:
It says nothing about how it should behave when window is hidden, and it seems that Chrome treats hidden window as "no client window".
I'm 👍 on keeping current behavior because it is how Chrome is expected to behave, and what we did was wrong.
etiktin commentedon Sep 30, 2015
Is there anything else that might be effected by this change, other than
outerHeight,outerWidthanddocument.hidden?There are scenarios when you want to run Electron without showing it and you still want it to render everything as if it was visible. Here's 2 examples that come to mind:
outerHeightandouterWidthare set to zero, the rendering is incorrect.show = false. One such project isNightmare, which is a browser automation library (similar to PhantomJS). It's supposed to test your web apps as if it was running in a visible browser, but now some rendering will break./ cc @matthewmueller
It doesn't always return the same values as
outerHeightandouterWidth. For example when running using the reconstruction code withshow = truethe outer dimensions are: 784x561When running
mainWindow.getContentSize()I get: 784x541.When running
mainWindow.getSize()I get: 800x600.So as you can see non of the values is exactly the same.
In any case, even if it worked, we couldn't have changed it in all the sources, since some of them are from the web. We could have tried to fake it using
preloadscript, but that only works forwindow === window.top(preload scripts don't currently run in iframes etc., see #2605). Edit: It also seems thatouterHeight,outerWidthare set to read only.I also tried to use
web-preferences.page-visibility = true, thinking that it might cause the page to think it's visible, but the result was the same (the dimensions were 0). Even if it had worked, I would still need some way to reset it after the initial display because of it's adverse effect on battery life.I respect wanting to confirm to the spec, but I also think we should provide a way to run an app hidden and make it think it's visible so it can render everything as expected. Some possible solutions:
show) so we can skip the initial set as hidden which will make it act like before. This way the default behavior will confirm with the spec, but the use cases I mentioned will be able to work with minimal changes.document.hiddenlike the workaround mentioned here (maybe add it to override.coffee).What do you think?
zcbenz commentedon Sep 30, 2015
@etiktin Excellent explanation, it looks quite reasonable to keep the old behavior, I'm more lean to the second solution, e.g. hacking the
document.hidden, so we can still fix the original issue, while keeping everything else unchanged.etiktin commentedon Sep 30, 2015
Thanks :)
benmkramer commentedon Sep 30, 2015
I'm working on a project doing similar work in a hidden background window that I need to be hidden, but needs to think it is visible for rendering purposes.
I am using 0.33.1 for the time being, as it still seems to work at that version.
deepak1556 commentedon Oct 1, 2015
@etiktin thanks for explaining :)
👍