Reduces the IPC messages used for visibilityState
This commit is contained in:
parent
c3ac92b500
commit
07a4c52919
2 changed files with 29 additions and 57 deletions
|
@ -3,17 +3,6 @@
|
|||
const ipcRenderer = require('electron').ipcRenderer
|
||||
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.
|
||||
var a = window.top.document.createElement('a')
|
||||
|
||||
|
@ -200,17 +189,6 @@ if (process.openerId != null) {
|
|||
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) {
|
||||
// Manually dispatch event instead of using postMessage because we also need to
|
||||
// set event.source.
|
||||
|
@ -249,19 +227,24 @@ Object.defineProperty(window.history, 'length', {
|
|||
}
|
||||
})
|
||||
|
||||
// Subscribe to visibilityState changes.
|
||||
let cachedVisibilityState = 'hidden'
|
||||
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.
|
||||
Object.defineProperty(document, 'hidden', {
|
||||
get: function () {
|
||||
return _isMinimized || !_isVisible
|
||||
return cachedVisibilityState != 'visible'
|
||||
}
|
||||
})
|
||||
|
||||
Object.defineProperty(document, 'visibilityState', {
|
||||
get: function () {
|
||||
if (_isVisible && !_isMinimized) {
|
||||
return 'visible'
|
||||
} else {
|
||||
return 'hidden'
|
||||
}
|
||||
return cachedVisibilityState
|
||||
}
|
||||
})
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue