From 08339bd1da5581deea2cb4b4ed100ded25a8e645 Mon Sep 17 00:00:00 2001 From: Tim Fish Date: Wed, 13 Sep 2023 02:36:01 +0200 Subject: [PATCH] fix: `BrowserWindowSession` should check visibility to work in Electron v12-13 (#744) --- src/main/integrations/browser-window-session.ts | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/src/main/integrations/browser-window-session.ts b/src/main/integrations/browser-window-session.ts index 142cdf45..02e1e0ab 100644 --- a/src/main/integrations/browser-window-session.ts +++ b/src/main/integrations/browser-window-session.ts @@ -4,6 +4,19 @@ import { app, BrowserWindow } from 'electron'; import { ELECTRON_MAJOR_VERSION } from '../electron-normalize'; import { endSession, endSessionOnExit, startSession } from '../sessions'; +function focusedWindow(): boolean { + for (const window of BrowserWindow.getAllWindows()) { + if (!window.isDestroyed() && window.webContents && !window.webContents.isDestroyed()) { + // It's important to test both isVisible and isFocused, since + // Electron v12-13 do not report hidden as a loss of focus + if (window.isFocused() && window.isVisible()) { + return true; + } + } + } + return false; +} + interface Options { /** * Number of seconds to wait before ending a session after the app loses focus. @@ -61,9 +74,9 @@ export class BrowserWindowSession implements Integration { } private _windowStateChanged = (): void => { - const aWindowIsActive = !!BrowserWindow.getFocusedWindow(); + const hasFocusedWindow = focusedWindow(); - if (aWindowIsActive) { + if (hasFocusedWindow) { // We are now active if (this._state.name === 'inactive') { // If we were inactive, start a new session