This repository has been archived on 2025-03-16. You can view files and clone it, but cannot push or open issues or pull requests.
project-client/src/main/ipc.ts

132 lines
3.9 KiB
TypeScript
Raw Normal View History

2023-04-10 05:49:50 +09:00
/*
* SPDX-License-Identifier: GPL-3.0
2023-07-14 02:03:13 +09:00
* Vesktop, a desktop app aiming to give you a snappier Discord Experience
2023-04-10 05:49:50 +09:00
* Copyright (c) 2023 Vendicated and Vencord contributors
*/
import { app, dialog, session, shell } from "electron";
2023-08-12 10:13:03 +09:00
import { mkdirSync, readFileSync, watch } from "fs";
2023-04-06 03:01:31 +09:00
import { open, readFile } from "fs/promises";
import { release } from "os";
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-10 05:49:50 +09:00
2023-04-09 08:22:31 +09:00
import { IpcEvents } from "../shared/IpcEvents";
2023-06-24 00:20:54 +09:00
import { setBadgeCount } from "./appBadge";
2023-06-21 23:13:20 +09:00
import { autoStart } from "./autoStart";
2023-08-05 02:39:33 +09:00
import { VENCORD_FILES_DIR, VENCORD_QUICKCSS_FILE, VENCORD_THEMES_DIR } from "./constants";
import { mainWin } from "./mainWindow";
2023-04-10 08:04:41 +09:00
import { Settings } from "./settings";
import { handle, handleSync } from "./utils/ipcWrappers";
2023-08-12 10:13:03 +09:00
import { isValidVencordInstall } from "./utils/vencordLoader";
2023-04-04 07:41:52 +09:00
handleSync(IpcEvents.GET_VENCORD_PRELOAD_FILE, () => join(VENCORD_FILES_DIR, "vencordDesktopPreload.js"));
handleSync(IpcEvents.GET_VENCORD_RENDERER_SCRIPT, () =>
readFileSync(join(VENCORD_FILES_DIR, "vencordDesktopRenderer.js"), "utf-8")
);
2023-04-05 12:31:44 +09:00
handleSync(IpcEvents.GET_RENDERER_SCRIPT, () => readFileSync(join(__dirname, "renderer.js"), "utf-8"));
handleSync(IpcEvents.GET_RENDERER_CSS_FILE, () => join(__dirname, "renderer.css"));
handleSync(IpcEvents.GET_SETTINGS, () => Settings.plain);
handleSync(IpcEvents.GET_VERSION, () => app.getVersion());
2023-04-09 10:06:19 +09:00
handleSync(
IpcEvents.SUPPORTS_WINDOWS_TRANSPARENCY,
() => process.platform === "win32" && Number(release().split(".").pop()) >= 22621
);
handleSync(IpcEvents.AUTOSTART_ENABLED, () => autoStart.isEnabled());
handle(IpcEvents.ENABLE_AUTOSTART, autoStart.enable);
handle(IpcEvents.DISABLE_AUTOSTART, autoStart.disable);
2023-06-21 23:13:20 +09:00
handle(IpcEvents.SET_SETTINGS, (_, settings: typeof Settings.store, path?: string) => {
Settings.setData(settings, path);
});
handle(IpcEvents.RELAUNCH, () => {
2023-04-04 08:35:37 +09:00
app.relaunch();
app.exit();
});
handle(IpcEvents.SHOW_ITEM_IN_FOLDER, (_, path) => {
shell.showItemInFolder(path);
});
handle(IpcEvents.FOCUS, () => {
2023-07-01 01:57:28 +09:00
if (process.platform === "win32") mainWin.minimize(); // Windows is weird
mainWin.restore();
mainWin.show();
2023-04-11 05:53:44 +09:00
});
handle(IpcEvents.CLOSE, e => {
2023-08-07 07:23:27 +09:00
mainWin.close();
});
handle(IpcEvents.MINIMIZE, e => {
2023-08-07 07:23:27 +09:00
mainWin.minimize();
});
handle(IpcEvents.MAXIMIZE, e => {
2023-08-07 07:23:27 +09:00
if (mainWin.isMaximized()) {
mainWin.unmaximize();
} else {
mainWin.maximize();
}
});
2023-04-06 03:01:31 +09:00
handle(IpcEvents.SPELLCHECK_SET_LANGUAGES, (_, languages: string[]) => {
2023-04-14 11:05:56 +09:00
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);
});
handle(IpcEvents.SPELLCHECK_REPLACE_MISSPELLING, (e, word: string) => {
2023-06-25 10:44:19 +09:00
e.sender.replaceMisspelling(word);
});
handle(IpcEvents.SPELLCHECK_ADD_TO_DICTIONARY, (e, word: string) => {
2023-06-25 10:44:19 +09:00
e.sender.session.addWordToSpellCheckerDictionary(word);
});
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];
2023-08-12 10:13:03 +09:00
if (!isValidVencordInstall(dir)) return "invalid";
return dir;
});
handle(IpcEvents.SET_BADGE_COUNT, (_, count: number) => setBadgeCount(count));
2023-06-24 00:20:54 +09:00
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();
2023-04-10 05:49:50 +09:00
watch(
VENCORD_QUICKCSS_FILE,
{ persistent: false },
debounce(async () => {
mainWin?.webContents.postMessage("VencordQuickCssUpdate", await readCss());
}, 50)
);
2023-04-06 03:01:31 +09:00
});
2023-08-05 02:39:33 +09:00
mkdirSync(VENCORD_THEMES_DIR, { recursive: true });
watch(
VENCORD_THEMES_DIR,
{ persistent: false },
debounce(() => {
mainWin?.webContents.postMessage("VencordThemeUpdate", void 0);
})
);