From 591d380160ab3ee9b786849abb80b93bc894ed62 Mon Sep 17 00:00:00 2001 From: Vendicated Date: Sun, 9 Apr 2023 01:22:31 +0200 Subject: [PATCH] Turn IpcEvents into a const enum --- src/main/ipc.ts | 18 +++++++++--------- src/preload/VencordDesktopNative.ts | 12 ++++++------ src/preload/index.ts | 8 ++++---- src/shared/IpcEvents.ts | 19 +++++++++++-------- 4 files changed, 30 insertions(+), 27 deletions(-) diff --git a/src/main/ipc.ts b/src/main/ipc.ts index 1ee90b7..48d08b4 100644 --- a/src/main/ipc.ts +++ b/src/main/ipc.ts @@ -3,39 +3,39 @@ import { readFileSync, watch } from "fs"; import { open, readFile } from "fs/promises"; import { join } from "path"; import { debounce } from "shared/utils/debounce"; -import { FOCUS, GET_RENDERER_SCRIPT, GET_RENDERER_STYLES, GET_SETTINGS, GET_VENCORD_PRELOAD_FILE, RELAUNCH, SET_SETTINGS, SHOW_ITEM_IN_FOLDER } from "../shared/IpcEvents"; +import { IpcEvents } from "../shared/IpcEvents"; import { VENCORD_FILES_DIR, VENCORD_QUICKCSS_FILE } from "./constants"; import { mainWin } from "./mainWindow"; import { PlainSettings, setSettings } from "./settings"; -ipcMain.on(GET_VENCORD_PRELOAD_FILE, e => { +ipcMain.on(IpcEvents.GET_VENCORD_PRELOAD_FILE, e => { e.returnValue = join(VENCORD_FILES_DIR, "preload.js"); }); -ipcMain.on(GET_RENDERER_SCRIPT, e => { +ipcMain.on(IpcEvents.GET_RENDERER_SCRIPT, e => { e.returnValue = readFileSync(join(__dirname, "renderer.js"), "utf-8"); }); -ipcMain.handle(GET_RENDERER_STYLES, () => readFile(join(__dirname, "renderer.css"), "utf-8")); +ipcMain.handle(IpcEvents.GET_RENDERER_STYLES, () => readFile(join(__dirname, "renderer.css"), "utf-8")); -ipcMain.on(GET_SETTINGS, e => { +ipcMain.on(IpcEvents.GET_SETTINGS, e => { e.returnValue = PlainSettings; }); -ipcMain.handle(SET_SETTINGS, (_, settings) => { +ipcMain.handle(IpcEvents.SET_SETTINGS, (_, settings) => { setSettings(settings); }); -ipcMain.handle(RELAUNCH, () => { +ipcMain.handle(IpcEvents.RELAUNCH, () => { app.relaunch(); app.exit(); }); -ipcMain.handle(SHOW_ITEM_IN_FOLDER, (_, path) => { +ipcMain.handle(IpcEvents.SHOW_ITEM_IN_FOLDER, (_, path) => { shell.showItemInFolder(path); }); -ipcMain.handle(FOCUS, () => { +ipcMain.handle(IpcEvents.FOCUS, () => { mainWin?.focus(); }); diff --git a/src/preload/VencordDesktopNative.ts b/src/preload/VencordDesktopNative.ts index 4d12b93..0d55ed1 100644 --- a/src/preload/VencordDesktopNative.ts +++ b/src/preload/VencordDesktopNative.ts @@ -1,21 +1,21 @@ import { app, ipcRenderer } from "electron"; import type { Settings } from "../main/settings"; -import { FOCUS, GET_SETTINGS, RELAUNCH, SET_SETTINGS, SHOW_ITEM_IN_FOLDER } from "../shared/IpcEvents"; +import { IpcEvents } from "../shared/IpcEvents"; export const VencordDesktopNative = { app: { - relaunch: () => ipcRenderer.invoke(RELAUNCH), + relaunch: () => ipcRenderer.invoke(IpcEvents.RELAUNCH), getVersion: () => app.getVersion() }, fileManager: { - showItemInFolder: (path: string) => ipcRenderer.invoke(SHOW_ITEM_IN_FOLDER, path) + showItemInFolder: (path: string) => ipcRenderer.invoke(IpcEvents.SHOW_ITEM_IN_FOLDER, path) }, settings: { - get: () => ipcRenderer.sendSync(GET_SETTINGS), - set: (settings: typeof Settings) => ipcRenderer.invoke(SET_SETTINGS, settings) + get: () => ipcRenderer.sendSync(IpcEvents.GET_SETTINGS), + set: (settings: typeof Settings) => ipcRenderer.invoke(IpcEvents.SET_SETTINGS, settings) }, win: { - focus: () => ipcRenderer.invoke(FOCUS) + focus: () => ipcRenderer.invoke(IpcEvents.FOCUS) } } diff --git a/src/preload/index.ts b/src/preload/index.ts index 64d0b66..28225f6 100644 --- a/src/preload/index.ts +++ b/src/preload/index.ts @@ -1,10 +1,10 @@ import { contextBridge, ipcRenderer, webFrame } from "electron"; -import { GET_RENDERER_SCRIPT, GET_RENDERER_STYLES, GET_VENCORD_PRELOAD_FILE } from "../shared/IpcEvents"; +import { IpcEvents } from "../shared/IpcEvents"; import { VencordDesktopNative } from "./VencordDesktopNative"; contextBridge.exposeInMainWorld("VencordDesktopNative", VencordDesktopNative); -require(ipcRenderer.sendSync(GET_VENCORD_PRELOAD_FILE)); +require(ipcRenderer.sendSync(IpcEvents.GET_VENCORD_PRELOAD_FILE)); -webFrame.executeJavaScript(ipcRenderer.sendSync(GET_RENDERER_SCRIPT)); -ipcRenderer.invoke(GET_RENDERER_STYLES).then(s => webFrame.insertCSS(s)); +webFrame.executeJavaScript(ipcRenderer.sendSync(IpcEvents.GET_RENDERER_SCRIPT)); +ipcRenderer.invoke(IpcEvents.GET_RENDERER_STYLES).then(s => webFrame.insertCSS(s)); diff --git a/src/shared/IpcEvents.ts b/src/shared/IpcEvents.ts index 6fb7f98..1041394 100644 --- a/src/shared/IpcEvents.ts +++ b/src/shared/IpcEvents.ts @@ -1,10 +1,13 @@ -export const GET_VENCORD_PRELOAD_FILE = "VCD_GET_VC_PRELOAD_FILE"; -export const GET_RENDERER_SCRIPT = "VCD_GET_RENDERER_SCRIPT"; -export const GET_RENDERER_STYLES = "VCD_GET_RENDERER_STYLES"; +export const enum IpcEvents { + GET_VENCORD_PRELOAD_FILE = "VCD_GET_VC_PRELOAD_FILE", + GET_RENDERER_SCRIPT = "VCD_GET_RENDERER_SCRIPT", + GET_RENDERER_STYLES = "VCD_GET_RENDERER_STYLES", -export const RELAUNCH = "VCD_RELAUNCH"; -export const FOCUS = "VCD_FOCUS"; + RELAUNCH = "VCD_RELAUNCH", + FOCUS = "VCD_FOCUS", + + SHOW_ITEM_IN_FOLDER = "VCD_SHOW_ITEM_IN_FOLDER", + GET_SETTINGS = "VCD_GET_SETTINGS", + SET_SETTINGS = "VCD_SET_SETTINGS", +} -export const SHOW_ITEM_IN_FOLDER = "VCD_SHOW_ITEM_IN_FOLDER"; -export const GET_SETTINGS = "VCD_GET_SETTINGS"; -export const SET_SETTINGS = "VCD_SET_SETTINGS";