Skip to content

Commit

Permalink
fix: BrowserWindowSession should check visibility to work in Electr…
Browse files Browse the repository at this point in the history
…on v12-13 (#744)
  • Loading branch information
timfish committed Sep 13, 2023
1 parent 91d4380 commit 08339bd
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions src/main/integrations/browser-window-session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 08339bd

Please sign in to comment.