diff options
author | Vendicated <vendicated@riseup.net> | 2022-08-31 04:07:16 +0200 |
---|---|---|
committer | Vendicated <vendicated@riseup.net> | 2022-08-31 04:07:16 +0200 |
commit | 98cb301df53305f397ac6e1b4e603c930820f228 (patch) | |
tree | 8c3bc642d0871a38f99aa2c2e8586bd7310cb361 /src/ipcMain.ts | |
parent | cb288e204dd531b31f957f82150398d22930fdeb (diff) | |
download | Vencord-98cb301df53305f397ac6e1b4e603c930820f228.tar.gz Vencord-98cb301df53305f397ac6e1b4e603c930820f228.tar.bz2 Vencord-98cb301df53305f397ac6e1b4e603c930820f228.zip |
Make Settings & Settings Page
Diffstat (limited to 'src/ipcMain.ts')
-rw-r--r-- | src/ipcMain.ts | 28 |
1 files changed, 21 insertions, 7 deletions
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()); }); }); } |