From ec5862fdc82bc8f6acf479a7c4281f440a8a21d8 Mon Sep 17 00:00:00 2001 From: Julien Laffaye Date: Sun, 19 Mar 2017 23:40:45 +0100 Subject: [PATCH] Only setup interval when window is visible This avoid waking up the process when zazu is idle in the background (see issue #271) --- app/helpers/window.js | 44 ++++++++++++++++++++++++++++++++++--------- 1 file changed, 35 insertions(+), 9 deletions(-) diff --git a/app/helpers/window.js b/app/helpers/window.js index d74bca56..171ae079 100644 --- a/app/helpers/window.js +++ b/app/helpers/window.js @@ -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() }) }