diff --git a/package.json b/package.json index 9b66338..56bd6ee 100644 --- a/package.json +++ b/package.json @@ -14,6 +14,7 @@ "package:dir": "pnpm build && electron-builder --dir", "start": "pnpm build && electron .", "start:dev": "pnpm build --dev && electron .", + "start:watch": "tsx scripts/startWatch.mts", "test": "echo \"Error: no test specified\" && exit 1", "watch": "pnpm build --watch" }, diff --git a/scripts/startWatch.mts b/scripts/startWatch.mts index 8dad898..d30a938 100644 --- a/scripts/startWatch.mts +++ b/scripts/startWatch.mts @@ -11,5 +11,5 @@ function spawn(bin: string, args: string[]) { cpSpawn(join("node_modules", ".bin", bin + EXT), args, OPTS); } -spawn("tsx", ["scripts/build/build.mts", "--", "--watch"]); +spawn("tsx", ["scripts/build/build.mts", "--", "--watch", "--dev"]); spawn("electron", ["."]); diff --git a/src/main/ipc.ts b/src/main/ipc.ts index 52fb9d3..3f2c35b 100644 --- a/src/main/ipc.ts +++ b/src/main/ipc.ts @@ -20,12 +20,18 @@ ipcMain.on(IpcEvents.GET_RENDERER_SCRIPT, e => { e.returnValue = readFileSync(join(__dirname, "renderer.js"), "utf-8"); }); -ipcMain.handle(IpcEvents.GET_RENDERER_STYLES, () => readFile(join(__dirname, "renderer.css"), "utf-8")); +ipcMain.on(IpcEvents.GET_RENDERER_CSS_FILE, e => { + e.returnValue = join(__dirname, "renderer.css"); +}); ipcMain.on(IpcEvents.GET_SETTINGS, e => { e.returnValue = PlainSettings; }); +ipcMain.on(IpcEvents.GET_VERSION, e => { + e.returnValue = app.getVersion(); +}); + ipcMain.handle(IpcEvents.SET_SETTINGS, (_, settings) => { setSettings(settings); }); diff --git a/src/preload/VencordDesktopNative.ts b/src/preload/VencordDesktopNative.ts index dbd015b..88f1a22 100644 --- a/src/preload/VencordDesktopNative.ts +++ b/src/preload/VencordDesktopNative.ts @@ -1,4 +1,4 @@ -import { app, ipcRenderer } from "electron"; +import { ipcRenderer } from "electron"; import type { Settings } from "shared/settings"; import type { LiteralUnion } from "type-fest"; import { IpcEvents } from "../shared/IpcEvents"; @@ -14,7 +14,7 @@ function sendSync(event: IpcEvents, ...args: any[]) { export const VencordDesktopNative = { app: { relaunch: () => invoke(IpcEvents.RELAUNCH), - getVersion: () => app.getVersion() + getVersion: () => sendSync(IpcEvents.GET_VERSION) }, fileManager: { showItemInFolder: (path: string) => invoke(IpcEvents.SHOW_ITEM_IN_FOLDER, path), diff --git a/src/preload/index.ts b/src/preload/index.ts index 4d66d5f..8f93b21 100644 --- a/src/preload/index.ts +++ b/src/preload/index.ts @@ -1,4 +1,5 @@ import { contextBridge, ipcRenderer, webFrame } from "electron"; +import { readFileSync, watch } from "fs"; import { IpcEvents } from "../shared/IpcEvents"; import { VencordDesktopNative } from "./VencordDesktopNative"; @@ -8,4 +9,28 @@ require(ipcRenderer.sendSync(IpcEvents.GET_VENCORD_PRELOAD_FILE)); webFrame.executeJavaScript(ipcRenderer.sendSync(IpcEvents.GET_VENCORD_RENDERER_SCRIPT)); webFrame.executeJavaScript(ipcRenderer.sendSync(IpcEvents.GET_RENDERER_SCRIPT)); -ipcRenderer.invoke(IpcEvents.GET_RENDERER_STYLES).then(s => webFrame.insertCSS(s)); + +// #region css +const rendererCss = ipcRenderer.sendSync(IpcEvents.GET_RENDERER_CSS_FILE); + +const style = document.createElement("style"); +style.id = "vcd-css-core"; +style.textContent = readFileSync(rendererCss, "utf-8"); + +if (document.readyState === "complete") { + document.documentElement.appendChild(style); +} else { + document.addEventListener("DOMContentLoaded", () => document.documentElement.appendChild(style), { + once: true + }); +} + +if (IS_DEV) { + // persistent means keep process running if watcher is the only thing still running + // which we obviously don't want + watch(rendererCss, { persistent: false }, () => { + document.getElementById("vcd-css-core")!.textContent = readFileSync(rendererCss, "utf-8"); + }); +} + +// #endregion diff --git a/src/shared/IpcEvents.ts b/src/shared/IpcEvents.ts index 8bb6518..605033e 100644 --- a/src/shared/IpcEvents.ts +++ b/src/shared/IpcEvents.ts @@ -2,11 +2,13 @@ export const enum IpcEvents { GET_VENCORD_PRELOAD_FILE = "VCD_GET_VC_PRELOAD_FILE", GET_VENCORD_RENDERER_SCRIPT = "VCD_GET_VC_RENDERER_SCRIPT", GET_RENDERER_SCRIPT = "VCD_GET_RENDERER_SCRIPT", - GET_RENDERER_STYLES = "VCD_GET_RENDERER_STYLES", + GET_RENDERER_CSS_FILE = "VCD_GET_RENDERER_CSS_FILE", RELAUNCH = "VCD_RELAUNCH", FOCUS = "VCD_FOCUS", + GET_VERSION = "VCD_GET_VERSION", + SHOW_ITEM_IN_FOLDER = "VCD_SHOW_ITEM_IN_FOLDER", GET_SETTINGS = "VCD_GET_SETTINGS", SET_SETTINGS = "VCD_SET_SETTINGS",