project-client/src/main/ipc.ts

70 lines
2.1 KiB
TypeScript
Raw Normal View History

import { app, dialog, ipcMain, shell } from "electron";
import { existsSync, readFileSync, watch } from "fs";
2023-04-06 03:01:31 +09:00
import { open, readFile } from "fs/promises";
2023-04-04 07:41:52 +09:00
import { join } from "path";
2023-04-06 03:01:31 +09:00
import { debounce } from "shared/utils/debounce";
2023-04-09 08:22:31 +09:00
import { IpcEvents } from "../shared/IpcEvents";
2023-04-06 03:01:31 +09:00
import { VENCORD_FILES_DIR, VENCORD_QUICKCSS_FILE } from "./constants";
import { mainWin } from "./mainWindow";
import { PlainSettings, setSettings } from "./settings";
2023-04-04 07:41:52 +09:00
2023-04-09 08:22:31 +09:00
ipcMain.on(IpcEvents.GET_VENCORD_PRELOAD_FILE, e => {
2023-04-04 07:41:52 +09:00
e.returnValue = join(VENCORD_FILES_DIR, "preload.js");
});
2023-04-04 08:35:37 +09:00
ipcMain.on(IpcEvents.GET_VENCORD_RENDERER_SCRIPT, e => {
e.returnValue = readFileSync(join(VENCORD_FILES_DIR, "vencordDesktopRenderer.js"), "utf-8");
});
2023-04-09 08:22:31 +09:00
ipcMain.on(IpcEvents.GET_RENDERER_SCRIPT, e => {
e.returnValue = readFileSync(join(__dirname, "renderer.js"), "utf-8");
});
2023-04-09 08:22:31 +09:00
ipcMain.handle(IpcEvents.GET_RENDERER_STYLES, () => readFile(join(__dirname, "renderer.css"), "utf-8"));
2023-04-05 12:31:44 +09:00
2023-04-09 08:22:31 +09:00
ipcMain.on(IpcEvents.GET_SETTINGS, e => {
e.returnValue = PlainSettings;
});
2023-04-09 08:22:31 +09:00
ipcMain.handle(IpcEvents.SET_SETTINGS, (_, settings) => {
setSettings(settings);
});
2023-04-09 08:22:31 +09:00
ipcMain.handle(IpcEvents.RELAUNCH, () => {
2023-04-04 08:35:37 +09:00
app.relaunch();
app.exit();
});
2023-04-09 08:22:31 +09:00
ipcMain.handle(IpcEvents.SHOW_ITEM_IN_FOLDER, (_, path) => {
shell.showItemInFolder(path);
});
2023-04-09 08:22:31 +09:00
ipcMain.handle(IpcEvents.FOCUS, () => {
mainWin?.focus();
});
2023-04-06 03:01:31 +09:00
ipcMain.handle(IpcEvents.SELECT_VENCORD_DIR, async () => {
const res = await dialog.showOpenDialog(mainWin!, {
properties: ["openDirectory"]
});
if (!res.filePaths.length) return "cancelled";
const dir = res.filePaths[0];
for (const file of ["vencordDesktopMain.js", "preload.js", "vencordDesktopRenderer.js", "renderer.css"]) {
if (!existsSync(join(dir, file))) return "invalid";
}
return dir;
});
2023-04-06 03:01:31 +09:00
function readCss() {
return readFile(VENCORD_QUICKCSS_FILE, "utf-8").catch(() => "");
}
open(VENCORD_QUICKCSS_FILE, "a+").then(fd => {
fd.close();
watch(VENCORD_QUICKCSS_FILE, { persistent: false }, debounce(async () => {
mainWin?.webContents.postMessage("VencordQuickCssUpdate", await readCss());
}, 50));
});