Browse Source

Cache browser visibility state & emit visibilitychange event on change

Fixes #3788
Arek Sredzki 9 years ago
parent
commit
ee61ab2d26

+ 12 - 0
lib/browser/api/browser-window.js

@@ -88,6 +88,18 @@ BrowserWindow.prototype._init = function() {
     };
   })(this));
 
+  // Evented visibilityState changes
+  this.on('minimize', (function(_this) {
+    return function() {
+      return _this.webContents.send('ATOM_RENDERER_WINDOW_VISIBILITY_CHANGE', false);
+    };
+  })(this));
+  this.on('restore', (function(_this) {
+    return function() {
+      return _this.webContents.send('ATOM_RENDERER_WINDOW_VISIBILITY_CHANGE', true);
+    };
+  })(this));
+
   // Notify the creation of the window.
   app.emit('browser-window-created', {}, this);
 

+ 1 - 1
lib/renderer/api/ipc-renderer.js

@@ -3,7 +3,7 @@ const v8Util = process.atomBinding('v8_util');
 
 var slice = [].slice;
 
-// Created by init.coffee.
+// Created by init.js.
 const ipcRenderer = v8Util.getHiddenValue(global, 'ipc');
 
 ipcRenderer.send = function() {

+ 1 - 1
lib/renderer/inspector.js

@@ -3,7 +3,7 @@ window.onload = function() {
   InspectorFrontendHost.showContextMenuAtPoint = createMenu;
 
   // Use dialog API to override file chooser dialog.
-  return WebInspector.createFileSelectorElement = createFileSelectorElement;
+  return (WebInspector.createFileSelectorElement = createFileSelectorElement);
 };
 
 var convertToMenuTemplate = function(items) {

+ 21 - 10
lib/renderer/override.js

@@ -3,6 +3,13 @@ const remote = require('electron').remote;
 
 var slice = [].slice;
 
+// Cache browser window visibility
+var _isVisible = (function() {
+  var currentWindow;
+  currentWindow = remote.getCurrentWindow();
+  return currentWindow.isMinimized() || !currentWindow.isVisible();
+})();
+
 // Helper function to resolve relative url.
 var a = window.top.document.createElement('a');
 
@@ -30,7 +37,7 @@ var BrowserWindowProxy = (function() {
     ipcRenderer.once("ATOM_SHELL_GUEST_WINDOW_MANAGER_WINDOW_CLOSED_" + this.guestId, (function(_this) {
       return function() {
         BrowserWindowProxy.remove(_this.guestId);
-        return _this.closed = true;
+        return (_this.closed = true);
       };
     })(this));
   }
@@ -87,7 +94,9 @@ window.open = function(url, frameName, features) {
   ref1 = features.split(/,\s*/);
   for (i = 0, len = ref1.length; i < len; i++) {
     feature = ref1[i];
-    ref2 = feature.split(/\s*=/), name = ref2[0], value = ref2[1];
+    ref2 = feature.split(/\s*=/);
+    name = ref2[0];
+    value = ref2[1];
     options[name] = value === 'yes' || value === '1' ? true : value === 'no' || value === '0' ? false : value;
   }
   if (options.left) {
@@ -168,6 +177,12 @@ if (process.openerId != null) {
   window.opener = BrowserWindowProxy.getOrCreate(process.openerId);
 }
 
+ipcRenderer.on('ATOM_RENDERER_WINDOW_VISIBILITY_CHANGE', function (event, isVisible) {
+  _isVisible = isVisible;
+
+  document.dispatchEvent(new Event('visibilitychange'));
+});
+
 ipcRenderer.on('ATOM_SHELL_GUEST_WINDOW_POSTMESSAGE', function(event, sourceId, message, sourceOrigin) {
   // Manually dispatch event instead of using postMessage because we also need to
   // set event.source.
@@ -212,19 +227,15 @@ Object.defineProperty(window.history, 'length', {
 
 // Make document.hidden and document.visibilityState return the correct value.
 Object.defineProperty(document, 'hidden', {
-  get: function() {
-    var currentWindow;
-    currentWindow = remote.getCurrentWindow();
-    return currentWindow.isMinimized() || !currentWindow.isVisible();
-  }
+  get: !_isVisible
 });
 
 Object.defineProperty(document, 'visibilityState', {
   get: function() {
-    if (document.hidden) {
-      return "hidden";
-    } else {
+    if (_isVisible) {
       return "visible";
+    } else {
+      return "hidden";
     }
   }
 });