From 69e555b24040e7cde44ca62f33a007a9ed61b5ea Mon Sep 17 00:00:00 2001 From: Albert Zhang Date: Wed, 27 Dec 2023 21:11:54 -0500 Subject: [PATCH] Revert to export let; ignore autosorted import order --- src/main/appBadge.ts | 7 +++---- src/main/arrpc.ts | 7 +++---- src/main/index.ts | 3 +-- src/main/ipc.ts | 19 ++++++++----------- src/main/mainWindow.ts | 24 ++++++++++-------------- 5 files changed, 25 insertions(+), 35 deletions(-) diff --git a/src/main/appBadge.ts b/src/main/appBadge.ts index e744eed..ac2659a 100644 --- a/src/main/appBadge.ts +++ b/src/main/appBadge.ts @@ -7,8 +7,7 @@ import { app, NativeImage, nativeImage } from "electron"; import { join } from "path"; import { BADGE_DIR, TRAY_ICON_DIR, TRAY_ICON_PATH } from "shared/paths"; - -import { globals } from "./mainWindow"; +import { tray, mainWin } from "./mainWindow"; import { Settings } from "./settings"; const imgCache = new Map(); @@ -37,7 +36,7 @@ export function setBadgeCount(count: number) { const [index, description] = getBadgeIndexAndDescription(count); if (Settings.store.trayBadge) { - globals.tray?.setImage(loadTrayIcon(index ?? 0)); + tray?.setImage(loadTrayIcon(index ?? 0)); } if (!Settings.store.appBadge) return; @@ -59,7 +58,7 @@ export function setBadgeCount(count: number) { lastIndex = index; - globals.mainWin?.setOverlayIcon(index === null ? null : loadBadge(index), description); + mainWin.setOverlayIcon(index === null ? null : loadBadge(index), description); break; } } diff --git a/src/main/arrpc.ts b/src/main/arrpc.ts index dd435bb..1899d9c 100644 --- a/src/main/arrpc.ts +++ b/src/main/arrpc.ts @@ -7,7 +7,7 @@ import Server from "arrpc"; import { IpcEvents } from "shared/IpcEvents"; -import { globals } from "./mainWindow"; +import { mainWin } from "./mainWindow"; import { Settings } from "./settings"; let server: any; @@ -19,13 +19,12 @@ export async function initArRPC() { try { server = await new Server(); - const { mainWin } = globals; - server.on("activity", (data: any) => mainWin!.webContents.send(IpcEvents.ARRPC_ACTIVITY, JSON.stringify(data))); + server.on("activity", (data: any) => mainWin.webContents.send(IpcEvents.ARRPC_ACTIVITY, JSON.stringify(data))); server.on("invite", (invite: string, callback: (valid: boolean) => void) => { invite = String(invite); if (!inviteCodeRegex.test(invite)) return callback(false); - mainWin!.webContents + mainWin.webContents // Safety: Result of JSON.stringify should always be safe to equal // Also, just to be super super safe, invite is regex validated above .executeJavaScript(`Vesktop.openInviteModal(${JSON.stringify(invite)})`) diff --git a/src/main/index.ts b/src/main/index.ts index 4998361..832afc8 100644 --- a/src/main/index.ts +++ b/src/main/index.ts @@ -11,7 +11,7 @@ import { checkUpdates } from "updater/main"; import { DATA_DIR } from "./constants"; import { createFirstLaunchTour } from "./firstLaunch"; -import { createWindows, globals } from "./mainWindow"; +import { createWindows, mainWin } from "./mainWindow"; import { registerMediaPermissionsHandler } from "./mediaPermissions"; import { registerScreenShareHandler } from "./screenShare"; import { Settings } from "./settings"; @@ -43,7 +43,6 @@ function init() { ); app.on("second-instance", (_event, _cmdLine, _cwd, data: any) => { - const { mainWin } = globals; if (data.IS_DEV) app.quit(); else if (mainWin) { if (mainWin.isMinimized()) mainWin.restore(); diff --git a/src/main/ipc.ts b/src/main/ipc.ts index 6cade98..318b1e3 100644 --- a/src/main/ipc.ts +++ b/src/main/ipc.ts @@ -6,6 +6,7 @@ if (process.platform === "linux") import("./virtmic"); +// eslint-disable-next-line simple-import-sort/imports import { execFile } from "child_process"; import { app, BrowserWindow, dialog, RelaunchOptions, session, shell } from "electron"; import { mkdirSync, readFileSync, watch } from "fs"; @@ -17,8 +18,9 @@ import { debounce } from "shared/utils/debounce"; import { IpcEvents } from "../shared/IpcEvents"; import { autoStart } from "./autoStart"; import { VENCORD_FILES_DIR, VENCORD_QUICKCSS_FILE, VENCORD_THEMES_DIR } from "./constants"; -import { globals } from "./mainWindow"; +import { mainWin } from "./mainWindow"; import { Settings } from "./settings"; +import { setBadgeCount } from "./appBadge"; import { handle, handleSync } from "./utils/ipcWrappers"; import { isDeckGameMode, showGamePage } from "./utils/steamOS"; import { isValidVencordInstall } from "./utils/vencordLoader"; @@ -67,8 +69,6 @@ handle(IpcEvents.SHOW_ITEM_IN_FOLDER, (_, path) => { }); handle(IpcEvents.FOCUS, () => { - const mainWin = globals.mainWin!; - if (process.platform === "win32") mainWin.minimize(); // Windows is weird mainWin.restore(); @@ -80,11 +80,10 @@ handle(IpcEvents.CLOSE, e => { }); handle(IpcEvents.MINIMIZE, e => { - globals.mainWin!.minimize(); + mainWin.minimize(); }); handle(IpcEvents.MAXIMIZE, e => { - const mainWin = globals.mainWin!; if (mainWin.isMaximized()) { mainWin.unmaximize(); } else { @@ -109,7 +108,7 @@ handle(IpcEvents.SPELLCHECK_ADD_TO_DICTIONARY, (e, word: string) => { }); handle(IpcEvents.SELECT_VENCORD_DIR, async () => { - const res = await dialog.showOpenDialog(globals.mainWin!, { + const res = await dialog.showOpenDialog(mainWin!, { properties: ["openDirectory"] }); if (!res.filePaths.length) return "cancelled"; @@ -120,9 +119,7 @@ handle(IpcEvents.SELECT_VENCORD_DIR, async () => { return dir; }); -handle(IpcEvents.SET_BADGE_COUNT, (_, count: number) => { - (require("./appBadge") as typeof import("./appBadge")).setBadgeCount(count); -}); +handle(IpcEvents.SET_BADGE_COUNT, (_, count: number) => setBadgeCount(count)); function readCss() { return readFile(VENCORD_QUICKCSS_FILE, "utf-8").catch(() => ""); @@ -134,7 +131,7 @@ open(VENCORD_QUICKCSS_FILE, "a+").then(fd => { VENCORD_QUICKCSS_FILE, { persistent: false }, debounce(async () => { - globals.mainWin?.webContents.postMessage("VencordQuickCssUpdate", await readCss()); + mainWin?.webContents.postMessage("VencordQuickCssUpdate", await readCss()); }, 50) ); }); @@ -144,6 +141,6 @@ watch( VENCORD_THEMES_DIR, { persistent: false }, debounce(() => { - globals.mainWin?.webContents.postMessage("VencordThemeUpdate", void 0); + mainWin?.webContents.postMessage("VencordThemeUpdate", void 0); }) ); diff --git a/src/main/mainWindow.ts b/src/main/mainWindow.ts index eeef003..3651ae8 100644 --- a/src/main/mainWindow.ts +++ b/src/main/mainWindow.ts @@ -48,12 +48,8 @@ app.on("before-quit", () => { isQuitting = true; }); -// Export a container object of objects that are used by other modules -// but won't be initialized at import time. -export const globals = { - tray: null, - mainWin: null -}; +export let mainWin: BrowserWindow; +export let tray: Tray | null = null; function makeSettingsListenerHelpers(o: SettingsStore) { const listeners = new Map<(data: any) => void, PropertyKey>(); @@ -122,7 +118,7 @@ function initTray(win: BrowserWindow) { } ]); - const tray = (globals.tray = new Tray(TRAY_ICON_PATH)); + tray = new Tray(TRAY_ICON_PATH); tray.setToolTip("Vesktop"); tray.setContextMenu(trayMenu); tray.on("click", () => win.show()); @@ -205,7 +201,7 @@ function initMenuBar(win: BrowserWindow) { label: "Settings", accelerator: "CmdOrCtrl+,", async click() { - globals.mainWin!.webContents.executeJavaScript( + mainWin.webContents.executeJavaScript( "Vencord.Webpack.Common.SettingsRouter.open('My Account')" ); } @@ -337,9 +333,9 @@ function initSettingsListeners(win: BrowserWindow) { addSettingsListener("tray", enable => { if (enable) { initTray(win); - } else if (globals.tray) { - globals.tray.destroy(); - globals.tray = null; + } else if (tray) { + tray.destroy(); + tray = null; } }); addSettingsListener("disableMinSize", disable => { @@ -378,7 +374,7 @@ function initSpellCheck(win: BrowserWindow) { }); } -function createMainWindow(): BrowserWindow { +function createMainWindow() { // Clear up previous settings listeners removeSettingsListeners(); removeVencordSettingsListeners(); @@ -389,7 +385,7 @@ function createMainWindow(): BrowserWindow { const noFrame = frameless === true || (process.platform === "win32" && discordWindowsTitleBar === true); - const win = (globals.mainWin = new BrowserWindow({ + const win = (mainWin = new BrowserWindow({ show: false, webPreferences: { nodeIntegration: false, @@ -457,7 +453,7 @@ export async function createWindows() { await ensureVencordFiles(); runVencordMain(); - const mainWin = (globals.mainWin = createMainWindow()); + mainWin = createMainWindow(); mainWin.webContents.on("did-finish-load", () => { splash.destroy();