Skip to content

Regression: when hidden (show = false), outerHeight and outerWidth are set to 0 #2935

Closed

Description

@etiktin

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
var app = require('app');
var BrowserWindow = require('browser-window');

var mainWindow = null;

app.on('window-all-closed', function() {
  if (process.platform != 'darwin') {
    app.quit();
  }
});

app.on('ready', function() {
  mainWindow = new BrowserWindow({
    width: 800,
    height: 600,
    show: false // The window starts hidden
  });

  mainWindow.loadUrl('file://' + __dirname + '/index.html');

  // We delay showing the window for 2 seconds
  setTimeout(function () {
    mainWindow.show();
  }, 2000);

  mainWindow.on('closed', function() {
    mainWindow = null;
  });
});
index.html
<!DOCTYPE html>
<html>
  <head>
    <meta charset="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):

window.innerHeight 541 window.outerHeight 0 window.innerWidth 784 window.outerWidth 0

It's important to not open devtools before the window is shown, or it won't reconstruct.

The expected output, that you will get if you run this code with v0.33.0 or change show to true, should be something like this:

window.innerHeight 541 window.outerHeight 561 window.innerWidth 784 window.outerWidth 784

This was tested on Windows 8.1 64 bit.

Edit: It seems the culprit is this line. When I comment it out, I get the correct values.
@zcbenz, @deepak1556, what do you think?

Activity

  1. deepak1556 commented on Sep 30, 2015

    @deepak1556
    Member

    @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.

  2. zcbenz commented on Sep 30, 2015

    @zcbenz
    Contributor

    From the W3C draft:

    The outerHeight attribute must return the height of the client window. If there is no client window this attribute must return zero.

    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.

  3. etiktin commented on Sep 30, 2015

    @etiktin
    ContributorAuthor

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

    What do you think?

  4. zcbenz commented on Sep 30, 2015

    @zcbenz
    Contributor

    @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.

  5. etiktin commented on Sep 30, 2015

    @etiktin
    ContributorAuthor

    Thanks :)

  6. benmkramer commented on Sep 30, 2015

    @benmkramer

    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.

  7. deepak1556 commented on Oct 1, 2015

    @deepak1556
    Member

    @etiktin thanks for explaining :)

    Remove the change, and instead fake document.hidden like the workaround mentioned here (maybe add it to override.coffee).

    👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions