diff --git a/src/main/ipcMain.ts b/src/main/ipcMain.ts index 8c6489680..652859c31 100644 --- a/src/main/ipcMain.ts +++ b/src/main/ipcMain.ts @@ -22,7 +22,7 @@ import "./settings"; import { debounce } from "@shared/debounce"; import { IpcEvents } from "@shared/IpcEvents"; -import { BrowserWindow, ipcMain, nativeTheme, shell, systemPreferences } from "electron"; +import { BrowserWindow, ipcMain, nativeTheme, shell, systemPreferences, app, dialog } from "electron"; import monacoHtml from "file://monacoWin.html?minify&base64"; import { FSWatcher, mkdirSync, readFileSync, watch, writeFileSync } from "fs"; import { open, readdir, readFile, unlink } from "fs/promises"; @@ -139,16 +139,17 @@ ipcMain.on(IpcEvents.GET_MONACO_THEME, e => { e.returnValue = nativeTheme.shouldUseDarkColors ? "vs-dark" : "vs-light"; }); +let monacoWin: BrowserWindow | null = null; + ipcMain.handle(IpcEvents.OPEN_MONACO_EDITOR, async () => { - const title = "Equicord QuickCSS Editor"; - const existingWindow = BrowserWindow.getAllWindows().find(w => w.title === title); - if (existingWindow && !existingWindow.isDestroyed()) { - existingWindow.focus(); + if (monacoWin && !monacoWin.isDestroyed()) { + monacoWin.show(); + monacoWin.focus(); return; } - const win = new BrowserWindow({ - title, + monacoWin = new BrowserWindow({ + title: "Equicord QuickCSS Editor", autoHideMenuBar: true, darkTheme: true, webPreferences: { @@ -159,9 +160,28 @@ ipcMain.handle(IpcEvents.OPEN_MONACO_EDITOR, async () => { } }); - makeLinksOpenExternally(win); + monacoWin.once("closed", () => { monacoWin = null; }); - await win.loadURL(`data:text/html;base64,${monacoHtml}`); + makeLinksOpenExternally(monacoWin); + + await monacoWin.loadURL(`data:text/html;base64,${monacoHtml}`); +}); + +app.on("before-quit", async (event) => { + if (monacoWin && !monacoWin.isDestroyed() && !monacoWin.isVisible()) { + const result = await dialog.showMessageBox({ + type: "question", + buttons: ["Cancel", "Close Anyway"], + defaultId: 0, + title: "QuickCSS Editor Open", + message: "QuickCSS editor is still open in the background.", + detail: "Do you want to close Discord anyway? This will also close the QuickCSS editor." + }); + + if (result.response === 1) { + app.exit(); + } + } }); ipcMain.handle(IpcEvents.GET_RENDERER_CSS, () => readFile(RENDERER_CSS_PATH, "utf-8"));