Vesktop/src/main/ipc.ts

99 lines
2.9 KiB
TypeScript
Raw Normal View History

2023-04-09 22:49:50 +02:00
/*
* SPDX-License-Identifier: GPL-3.0
* Vencord Desktop, a desktop app aiming to give you a snappier Discord Experience
* Copyright (c) 2023 Vendicated and Vencord contributors
*/
2023-04-14 04:05:56 +02:00
import { app, dialog, ipcMain, session, shell } from "electron";
import { existsSync, readFileSync, watch } from "fs";
2023-04-05 20:01:31 +02:00
import { open, readFile } from "fs/promises";
2023-04-04 00:41:52 +02:00
import { join } from "path";
2023-04-05 20:01:31 +02:00
import { debounce } from "shared/utils/debounce";
2023-04-09 22:49:50 +02:00
2023-04-09 01:22:31 +02:00
import { IpcEvents } from "../shared/IpcEvents";
2023-04-05 20:01:31 +02:00
import { VENCORD_FILES_DIR, VENCORD_QUICKCSS_FILE } from "./constants";
import { mainWin } from "./mainWindow";
2023-04-10 01:04:41 +02:00
import { Settings } from "./settings";
2023-04-04 00:41:52 +02:00
2023-04-09 01:22:31 +02:00
ipcMain.on(IpcEvents.GET_VENCORD_PRELOAD_FILE, e => {
2023-04-04 00:41:52 +02:00
e.returnValue = join(VENCORD_FILES_DIR, "preload.js");
});
2023-04-04 01:35:37 +02:00
ipcMain.on(IpcEvents.GET_VENCORD_RENDERER_SCRIPT, e => {
e.returnValue = readFileSync(join(VENCORD_FILES_DIR, "vencordDesktopRenderer.js"), "utf-8");
});
2023-04-09 01:22:31 +02:00
ipcMain.on(IpcEvents.GET_RENDERER_SCRIPT, e => {
e.returnValue = readFileSync(join(__dirname, "renderer.js"), "utf-8");
});
2023-04-09 03:06:19 +02:00
ipcMain.on(IpcEvents.GET_RENDERER_CSS_FILE, e => {
e.returnValue = join(__dirname, "renderer.css");
});
2023-04-05 05:31:44 +02:00
2023-04-09 01:22:31 +02:00
ipcMain.on(IpcEvents.GET_SETTINGS, e => {
2023-04-10 01:04:41 +02:00
e.returnValue = Settings.plain;
});
2023-04-09 03:06:19 +02:00
ipcMain.on(IpcEvents.GET_VERSION, e => {
e.returnValue = app.getVersion();
});
ipcMain.handle(IpcEvents.SET_SETTINGS, (_, settings: typeof Settings.store, path?: string) => {
Settings.setData(settings, path);
});
2023-04-09 01:22:31 +02:00
ipcMain.handle(IpcEvents.RELAUNCH, () => {
2023-04-04 01:35:37 +02:00
app.relaunch();
app.exit();
});
2023-04-09 01:22:31 +02:00
ipcMain.handle(IpcEvents.SHOW_ITEM_IN_FOLDER, (_, path) => {
shell.showItemInFolder(path);
});
2023-04-10 22:53:44 +02:00
ipcMain.handle(IpcEvents.FOCUS, e => {
e.sender.focus();
});
ipcMain.handle(IpcEvents.CLOSE, e => {
e.sender.close();
});
2023-04-05 20:01:31 +02:00
2023-04-14 04:05:56 +02:00
ipcMain.handle(IpcEvents.SPELLCHECK_SET_LANGUAGES, (_, languages: string[]) => {
const ses = session.defaultSession;
const available = ses.availableSpellCheckerLanguages;
const applicable = languages.filter(l => available.includes(l)).slice(0, 3);
if (applicable.length) ses.setSpellCheckerLanguages(applicable);
});
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-05 20:01:31 +02:00
function readCss() {
return readFile(VENCORD_QUICKCSS_FILE, "utf-8").catch(() => "");
}
open(VENCORD_QUICKCSS_FILE, "a+").then(fd => {
fd.close();
2023-04-09 22:49:50 +02:00
watch(
VENCORD_QUICKCSS_FILE,
{ persistent: false },
debounce(async () => {
mainWin?.webContents.postMessage("VencordQuickCssUpdate", await readCss());
}, 50)
);
2023-04-05 20:01:31 +02:00
});