Merge pull request #5141 from electron/optimize-visibility-state
Optimize the implementation of document.visibilityState
This commit is contained in:
commit
7787dee638
4 changed files with 44 additions and 71 deletions
|
@ -8,6 +8,7 @@
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
|
#include "atom/browser/native_window.h"
|
||||||
#include "atom/common/native_mate_converters/value_converter.h"
|
#include "atom/common/native_mate_converters/value_converter.h"
|
||||||
#include "atom/common/options_switches.h"
|
#include "atom/common/options_switches.h"
|
||||||
#include "base/command_line.h"
|
#include "base/command_line.h"
|
||||||
|
@ -131,20 +132,28 @@ void WebContentsPreferences::AppendExtraCommandLineSwitches(
|
||||||
// --guest-instance-id, which is used to identify guest WebContents.
|
// --guest-instance-id, which is used to identify guest WebContents.
|
||||||
int guest_instance_id;
|
int guest_instance_id;
|
||||||
if (web_preferences.GetInteger(options::kGuestInstanceID, &guest_instance_id))
|
if (web_preferences.GetInteger(options::kGuestInstanceID, &guest_instance_id))
|
||||||
command_line->AppendSwitchASCII(switches::kGuestInstanceID,
|
command_line->AppendSwitchASCII(switches::kGuestInstanceID,
|
||||||
base::IntToString(guest_instance_id));
|
base::IntToString(guest_instance_id));
|
||||||
|
|
||||||
// Pass the opener's window id.
|
// Pass the opener's window id.
|
||||||
int opener_id;
|
int opener_id;
|
||||||
if (web_preferences.GetInteger(options::kOpenerID, &opener_id))
|
if (web_preferences.GetInteger(options::kOpenerID, &opener_id))
|
||||||
command_line->AppendSwitchASCII(switches::kOpenerID,
|
command_line->AppendSwitchASCII(switches::kOpenerID,
|
||||||
base::IntToString(opener_id));
|
base::IntToString(opener_id));
|
||||||
|
|
||||||
// Enable blink features.
|
// Enable blink features.
|
||||||
std::string blink_features;
|
std::string blink_features;
|
||||||
if (web_preferences.GetString(options::kBlinkFeatures, &blink_features))
|
if (web_preferences.GetString(options::kBlinkFeatures, &blink_features))
|
||||||
command_line->AppendSwitchASCII(::switches::kEnableBlinkFeatures,
|
command_line->AppendSwitchASCII(::switches::kEnableBlinkFeatures,
|
||||||
blink_features);
|
blink_features);
|
||||||
|
|
||||||
|
// The initial visibility state.
|
||||||
|
NativeWindow* window = NativeWindow::FromWebContents(web_contents);
|
||||||
|
if (window) {
|
||||||
|
bool visible = window->IsVisible() && !window->IsMinimized();
|
||||||
|
if (!visible) // Default state is visible.
|
||||||
|
command_line->AppendSwitch("hidden-page");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// static
|
// static
|
||||||
|
|
|
@ -78,19 +78,20 @@ BrowserWindow.prototype._init = function () {
|
||||||
app.emit('browser-window-focus', event, this)
|
app.emit('browser-window-focus', event, this)
|
||||||
})
|
})
|
||||||
|
|
||||||
// Evented visibilityState changes
|
// Subscribe to visibilityState changes and pass to renderer process.
|
||||||
this.on('show', () => {
|
let isVisible = this.isVisible() && !this.isMinimized()
|
||||||
this.webContents.send('ELECTRON_RENDERER_WINDOW_VISIBILITY_CHANGE', this.isVisible(), this.isMinimized())
|
let visibilityChanged = () => {
|
||||||
})
|
let newState = this.isVisible() && !this.isMinimized()
|
||||||
this.on('hide', () => {
|
if (isVisible !== newState) {
|
||||||
this.webContents.send('ELECTRON_RENDERER_WINDOW_VISIBILITY_CHANGE', this.isVisible(), this.isMinimized())
|
isVisible = newState
|
||||||
})
|
this.webContents.send('ELECTRON_RENDERER_WINDOW_VISIBILITY_CHANGE', isVisible ? 'visible' : 'hidden')
|
||||||
this.on('minimize', () => {
|
}
|
||||||
this.webContents.send('ELECTRON_RENDERER_WINDOW_VISIBILITY_CHANGE', this.isVisible(), this.isMinimized())
|
}
|
||||||
})
|
this.on('show', visibilityChanged)
|
||||||
this.on('restore', () => {
|
this.on('hide', visibilityChanged)
|
||||||
this.webContents.send('ELECTRON_RENDERER_WINDOW_VISIBILITY_CHANGE', this.isVisible(), this.isMinimized())
|
this.on('minimize', visibilityChanged)
|
||||||
})
|
this.on('restore', visibilityChanged)
|
||||||
|
this.on('maximize', visibilityChanged)
|
||||||
|
|
||||||
// Notify the creation of the window.
|
// Notify the creation of the window.
|
||||||
app.emit('browser-window-created', {}, this)
|
app.emit('browser-window-created', {}, this)
|
||||||
|
@ -105,6 +106,7 @@ BrowserWindow.prototype._init = function () {
|
||||||
this.webContents.on('devtools-closed', () => {
|
this.webContents.on('devtools-closed', () => {
|
||||||
this.emit('devtools-closed')
|
this.emit('devtools-closed')
|
||||||
})
|
})
|
||||||
|
|
||||||
Object.defineProperty(this, 'devToolsWebContents', {
|
Object.defineProperty(this, 'devToolsWebContents', {
|
||||||
enumerable: true,
|
enumerable: true,
|
||||||
configurable: false,
|
configurable: false,
|
||||||
|
@ -195,37 +197,21 @@ BrowserWindow.prototype.inspectServiceWorker = function () {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Deprecated.
|
// Deprecated.
|
||||||
|
|
||||||
deprecate.member(BrowserWindow, 'undo', 'webContents')
|
deprecate.member(BrowserWindow, 'undo', 'webContents')
|
||||||
|
|
||||||
deprecate.member(BrowserWindow, 'redo', 'webContents')
|
deprecate.member(BrowserWindow, 'redo', 'webContents')
|
||||||
|
|
||||||
deprecate.member(BrowserWindow, 'cut', 'webContents')
|
deprecate.member(BrowserWindow, 'cut', 'webContents')
|
||||||
|
|
||||||
deprecate.member(BrowserWindow, 'copy', 'webContents')
|
deprecate.member(BrowserWindow, 'copy', 'webContents')
|
||||||
|
|
||||||
deprecate.member(BrowserWindow, 'paste', 'webContents')
|
deprecate.member(BrowserWindow, 'paste', 'webContents')
|
||||||
|
|
||||||
deprecate.member(BrowserWindow, 'selectAll', 'webContents')
|
deprecate.member(BrowserWindow, 'selectAll', 'webContents')
|
||||||
|
|
||||||
deprecate.member(BrowserWindow, 'reloadIgnoringCache', 'webContents')
|
deprecate.member(BrowserWindow, 'reloadIgnoringCache', 'webContents')
|
||||||
|
|
||||||
deprecate.member(BrowserWindow, 'isLoading', 'webContents')
|
deprecate.member(BrowserWindow, 'isLoading', 'webContents')
|
||||||
|
|
||||||
deprecate.member(BrowserWindow, 'isWaitingForResponse', 'webContents')
|
deprecate.member(BrowserWindow, 'isWaitingForResponse', 'webContents')
|
||||||
|
|
||||||
deprecate.member(BrowserWindow, 'stop', 'webContents')
|
deprecate.member(BrowserWindow, 'stop', 'webContents')
|
||||||
|
|
||||||
deprecate.member(BrowserWindow, 'isCrashed', 'webContents')
|
deprecate.member(BrowserWindow, 'isCrashed', 'webContents')
|
||||||
|
|
||||||
deprecate.member(BrowserWindow, 'print', 'webContents')
|
deprecate.member(BrowserWindow, 'print', 'webContents')
|
||||||
|
|
||||||
deprecate.member(BrowserWindow, 'printToPDF', 'webContents')
|
deprecate.member(BrowserWindow, 'printToPDF', 'webContents')
|
||||||
|
|
||||||
deprecate.rename(BrowserWindow, 'restart', 'reload')
|
deprecate.rename(BrowserWindow, 'restart', 'reload')
|
||||||
|
|
||||||
deprecate.rename(BrowserWindow, 'loadUrl', 'loadURL')
|
deprecate.rename(BrowserWindow, 'loadUrl', 'loadURL')
|
||||||
|
|
||||||
deprecate.rename(BrowserWindow, 'getUrl', 'getURL')
|
deprecate.rename(BrowserWindow, 'getUrl', 'getURL')
|
||||||
|
|
||||||
BrowserWindow.prototype.executeJavaScriptInDevTools = deprecate('executeJavaScriptInDevTools', 'devToolsWebContents.executeJavaScript', function (code) {
|
BrowserWindow.prototype.executeJavaScriptInDevTools = deprecate('executeJavaScriptInDevTools', 'devToolsWebContents.executeJavaScript', function (code) {
|
||||||
|
|
|
@ -47,11 +47,7 @@ electron.ipcRenderer.on('ELECTRON_INTERNAL_RENDERER_ASYNC_WEB_FRAME_METHOD', (ev
|
||||||
// Process command line arguments.
|
// Process command line arguments.
|
||||||
var nodeIntegration = 'false'
|
var nodeIntegration = 'false'
|
||||||
var preloadScript = null
|
var preloadScript = null
|
||||||
|
for (let arg of process.argv) {
|
||||||
var ref = process.argv
|
|
||||||
var i, len, arg
|
|
||||||
for (i = 0, len = ref.length; i < len; i++) {
|
|
||||||
arg = ref[i]
|
|
||||||
if (arg.indexOf('--guest-instance-id=') === 0) {
|
if (arg.indexOf('--guest-instance-id=') === 0) {
|
||||||
// This is a guest web view.
|
// This is a guest web view.
|
||||||
process.guestInstanceId = parseInt(arg.substr(arg.indexOf('=') + 1))
|
process.guestInstanceId = parseInt(arg.substr(arg.indexOf('=') + 1))
|
||||||
|
|
|
@ -3,20 +3,8 @@
|
||||||
const ipcRenderer = require('electron').ipcRenderer
|
const ipcRenderer = require('electron').ipcRenderer
|
||||||
const remote = require('electron').remote
|
const remote = require('electron').remote
|
||||||
|
|
||||||
// Cache browser window visibility
|
|
||||||
var _isVisible = true
|
|
||||||
var _isMinimized = false
|
|
||||||
var initWindow = function initWindow () {
|
|
||||||
var currentWindow
|
|
||||||
currentWindow = remote.getCurrentWindow()
|
|
||||||
_isVisible = currentWindow.isVisible()
|
|
||||||
_isMinimized = currentWindow.isMinimized()
|
|
||||||
}
|
|
||||||
initWindow()
|
|
||||||
|
|
||||||
// Helper function to resolve relative url.
|
// Helper function to resolve relative url.
|
||||||
var a = window.top.document.createElement('a')
|
var a = window.top.document.createElement('a')
|
||||||
|
|
||||||
var resolveURL = function (url) {
|
var resolveURL = function (url) {
|
||||||
a.href = url
|
a.href = url
|
||||||
return a.href
|
return a.href
|
||||||
|
@ -171,8 +159,6 @@ window.alert = function (message, title) {
|
||||||
title: title,
|
title: title,
|
||||||
buttons: buttons
|
buttons: buttons
|
||||||
})
|
})
|
||||||
|
|
||||||
// Alert should always return undefined.
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// And the confirm().
|
// And the confirm().
|
||||||
|
@ -200,17 +186,6 @@ if (process.openerId != null) {
|
||||||
window.opener = BrowserWindowProxy.getOrCreate(process.openerId)
|
window.opener = BrowserWindowProxy.getOrCreate(process.openerId)
|
||||||
}
|
}
|
||||||
|
|
||||||
ipcRenderer.on('ELECTRON_RENDERER_WINDOW_VISIBILITY_CHANGE', function (event, isVisible, isMinimized) {
|
|
||||||
var hasChanged = _isVisible !== isVisible || _isMinimized !== isMinimized
|
|
||||||
|
|
||||||
if (hasChanged) {
|
|
||||||
_isVisible = isVisible
|
|
||||||
_isMinimized = isMinimized
|
|
||||||
|
|
||||||
document.dispatchEvent(new Event('visibilitychange'))
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
ipcRenderer.on('ELECTRON_GUEST_WINDOW_POSTMESSAGE', function (event, sourceId, message, sourceOrigin) {
|
ipcRenderer.on('ELECTRON_GUEST_WINDOW_POSTMESSAGE', function (event, sourceId, message, sourceOrigin) {
|
||||||
// Manually dispatch event instead of using postMessage because we also need to
|
// Manually dispatch event instead of using postMessage because we also need to
|
||||||
// set event.source.
|
// set event.source.
|
||||||
|
@ -249,19 +224,26 @@ Object.defineProperty(window.history, 'length', {
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
// The initial visibilityState.
|
||||||
|
let cachedVisibilityState = process.argv.includes('--hidden-page') ? 'hidden' : 'visible'
|
||||||
|
|
||||||
|
// Subscribe to visibilityState changes.
|
||||||
|
ipcRenderer.on('ELECTRON_RENDERER_WINDOW_VISIBILITY_CHANGE', function (event, visibilityState) {
|
||||||
|
if (cachedVisibilityState !== visibilityState) {
|
||||||
|
cachedVisibilityState = visibilityState
|
||||||
|
document.dispatchEvent(new Event('visibilitychange'))
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
// Make document.hidden and document.visibilityState return the correct value.
|
// Make document.hidden and document.visibilityState return the correct value.
|
||||||
Object.defineProperty(document, 'hidden', {
|
Object.defineProperty(document, 'hidden', {
|
||||||
get: function () {
|
get: function () {
|
||||||
return _isMinimized || !_isVisible
|
return cachedVisibilityState !== 'visible'
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
Object.defineProperty(document, 'visibilityState', {
|
Object.defineProperty(document, 'visibilityState', {
|
||||||
get: function () {
|
get: function () {
|
||||||
if (_isVisible && !_isMinimized) {
|
return cachedVisibilityState
|
||||||
return 'visible'
|
|
||||||
} else {
|
|
||||||
return 'hidden'
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue