From 6885564241c3b498684e8f52dde15431fc620d94 Mon Sep 17 00:00:00 2001 From: Vendicated Date: Tue, 4 Apr 2023 01:54:03 +0200 Subject: [PATCH] Add single instance lock --- src/main/index.ts | 39 +++++++++++++++++++++++++++------------ 1 file changed, 27 insertions(+), 12 deletions(-) diff --git a/src/main/index.ts b/src/main/index.ts index 57e5535..6fdc989 100644 --- a/src/main/index.ts +++ b/src/main/index.ts @@ -7,36 +7,51 @@ import { join } from "path"; import { DATA_DIR, VENCORD_FILES_DIR } from "./constants"; import { once } from "../shared/utils/once"; -import "./ipc"; import { ensureVencordFiles } from "./utils/vencordLoader"; +import "./ipc"; + // Make the Vencord files use our DATA_DIR process.env.VENCORD_USER_DATA_DIR = DATA_DIR; const runVencordMain = once(() => require(join(VENCORD_FILES_DIR, "main.js"))); +let mainWin: BrowserWindow | null = null; + +if (!app.requestSingleInstanceLock()) { + console.log("Vencord Desktop is already running. Quitting..."); + app.quit(); +} else { + app.on("second-instance", () => { + if (mainWin) { + if (mainWin.isMinimized()) mainWin.restore(); + mainWin.focus(); + } + }); + + app.whenReady().then(async () => { + createWindows(); + + app.on('activate', () => { + if (BrowserWindow.getAllWindows().length === 0) createWindows(); + }); + }); +} + async function createWindows() { const splash = createSplashWindow(); await ensureVencordFiles(); runVencordMain(); - const mainWindow = createMainWindow(); + mainWin = createMainWindow(); - mainWindow.once("ready-to-show", () => { + mainWin.once("ready-to-show", () => { splash.destroy(); - mainWindow.show(); + mainWin!.show(); }); } -app.whenReady().then(async () => { - createWindows(); - - app.on('activate', () => { - if (BrowserWindow.getAllWindows().length === 0) createWindows(); - }); -}); - app.on("window-all-closed", () => { if (process.platform !== "darwin") app.quit();