From 98cb301df53305f397ac6e1b4e603c930820f228 Mon Sep 17 00:00:00 2001 From: Vendicated Date: Wed, 31 Aug 2022 04:07:16 +0200 Subject: Make Settings & Settings Page --- src/ipcMain.ts | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) (limited to 'src/ipcMain.ts') diff --git a/src/ipcMain.ts b/src/ipcMain.ts index c8fba37..38a16ab 100644 --- a/src/ipcMain.ts +++ b/src/ipcMain.ts @@ -1,25 +1,39 @@ -import { app, BrowserWindow, ipcMain } from "electron"; -import { fstat, watch } from "fs"; -import { open, readFile } from "fs/promises"; +import { app, BrowserWindow, ipcMain, shell } from "electron"; +import { readFileSync, watch } from "fs"; +import { open, readFile, writeFile } from "fs/promises"; import { join } from 'path'; -import { IPC_GET_SETTINGS_DIR, IPC_GET_QUICK_CSS, IPC_QUICK_CSS_UPDATE } from './utils/ipcEvents'; +import IpcEvents from './utils/IpcEvents'; const DATA_DIR = join(app.getPath("userData"), "..", "Vencord"); const SETTINGS_DIR = join(DATA_DIR, "settings"); const QUICKCSS_PATH = join(SETTINGS_DIR, "quickCss.css"); +const SETTINGS_FILE = join(SETTINGS_DIR, "settings.json"); function readCss() { return readFile(QUICKCSS_PATH, "utf-8").catch(() => ""); } -ipcMain.handle(IPC_GET_SETTINGS_DIR, () => SETTINGS_DIR); -ipcMain.handle(IPC_GET_QUICK_CSS, () => readCss()); +function readSettings() { + try { + return readFileSync(SETTINGS_FILE, "utf-8"); + } catch { + return "{}"; + } +} + +ipcMain.handle(IpcEvents.GET_SETTINGS_DIR, () => SETTINGS_DIR); +ipcMain.handle(IpcEvents.GET_QUICK_CSS, () => readCss()); +// .on because we need Settings synchronously (ipcRenderer.sendSync) +ipcMain.on(IpcEvents.GET_SETTINGS, (e) => e.returnValue = readSettings()); +ipcMain.handle(IpcEvents.SET_SETTINGS, (_, s) => void writeFile(SETTINGS_FILE, s)); +ipcMain.handle(IpcEvents.OPEN_PATH, (_, path) => shell.openPath(path)); +ipcMain.handle(IpcEvents.OPEN_EXTERNAL, (_, url) => shell.openExternal(url)); export function initIpc(mainWindow: BrowserWindow) { open(QUICKCSS_PATH, "a+").then(fd => { fd.close(); watch(QUICKCSS_PATH, async () => { - mainWindow.webContents.postMessage(IPC_QUICK_CSS_UPDATE, await readCss()); + mainWindow.webContents.postMessage(IpcEvents.QUICK_CSS_UPDATE, await readCss()); }); }); } -- cgit