Skip to content

Commit

Permalink
Merge pull request #68 from marvin-wtt/single-instance
Browse files Browse the repository at this point in the history
Prevent multiple app instances
  • Loading branch information
marvin-wtt committed Apr 10, 2024
2 parents 18ba195 + 4647fb9 commit a362f90
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions src-electron/electron-main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,25 @@ import electronUpdater, { type AppUpdater } from 'electron-updater';

// needed in case process is undefined under Linux
const platform = process.platform || os.platform();

const currentDir = fileURLToPath(new URL('.', import.meta.url));
const singleInstanceLock = app.requestSingleInstanceLock();
let mainWindow: BrowserWindow | undefined;

log.initialize();

if (!singleInstanceLock) {
log.error(
'Failed to start application: Another instance seems to be already running.',
);
app.quit();
} else {
app.on('second-instance', () => {
mainWindow?.restore();
mainWindow?.center();
mainWindow?.focus();
});
}

function getPreloadPath(name: string): string {
return path.resolve(
currentDir,
Expand All @@ -29,7 +43,7 @@ function createWindow() {
/**
* Initial window options
*/
const mainWindow = new BrowserWindow({
mainWindow = new BrowserWindow({
icon: path.resolve(currentDir, 'icons/icon.png'), // tray icon
width: 500,
height: 800,
Expand Down

0 comments on commit a362f90

Please sign in to comment.