Skip to content
This repository has been archived by the owner on Oct 26, 2019. It is now read-only.

Commit

Permalink
Merge pull request #278 from jlaffaye/window
Browse files Browse the repository at this point in the history
Only setup interval when window is visible
  • Loading branch information
Blaine Schmeisser authored Mar 20, 2017
2 parents 5f4ea67 + ec5862f commit 8f0e950
Showing 1 changed file with 35 additions and 9 deletions.
44 changes: 35 additions & 9 deletions app/helpers/window.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,43 @@ const autoResize = (dynamicWindow) => {
}
}

dynamicWindow.webContents.on('did-finish-load', () => {
const updateHeight = () => {
if (!dynamicWindow) { return }
dynamicWindow.webContents.executeJavaScript('document.body.children[0].offsetHeight', (mainContentHeight) => {
resize(mainContentHeight)
})
const updateHeight = () => {
if (!dynamicWindow) { return }
dynamicWindow.webContents.executeJavaScript('document.body.children[0].offsetHeight', (mainContentHeight) => {
resize(mainContentHeight)
})
}

let updateHeightIntervalId = null
const clearUpdateHeightInterval = () => {
if (updateHeightIntervalId) {
clearInterval(updateHeightIntervalId)
updateHeightIntervalId = null
}
}

// register updateHeight interval only when dynamicWindow is visible
const registerUpdateHeightInterval = () => {
clearUpdateHeightInterval()
if (dynamicWindow.isVisible()) {
// avoid 125ms delay and redraw the window right away
updateHeight()
updateHeightIntervalId = setInterval(updateHeight, 125)
}
const id = setInterval(updateHeight, 125)
dynamicWindow.on('closed', () => {
clearInterval(id)
}

dynamicWindow.webContents.on('did-finish-load', () => {
// remove the interval as soon as the dynamicWindow is not visible or destroyed
dynamicWindow.on('closed', clearUpdateHeightInterval)
dynamicWindow.on('hide', clearUpdateHeightInterval)

// reregister the interval as soon as the window is visible
dynamicWindow.on('show', () => {
registerUpdateHeightInterval()
})

// if the window is already visible, the interval should be set
registerUpdateHeightInterval()
})
}

Expand Down

0 comments on commit 8f0e950

Please sign in to comment.