2023-04-09 22:49:50 +02:00
|
|
|
/*
|
|
|
|
* SPDX-License-Identifier: GPL-3.0
|
2023-07-13 19:03:13 +02:00
|
|
|
* Vesktop, a desktop app aiming to give you a snappier Discord Experience
|
2023-04-09 22:49:50 +02:00
|
|
|
* Copyright (c) 2023 Vendicated and Vencord contributors
|
|
|
|
*/
|
|
|
|
|
2023-10-21 22:15:55 +02:00
|
|
|
if (process.platform === "linux") import("./virtmic");
|
|
|
|
|
2023-09-27 08:22:00 +05:30
|
|
|
import { execFile } from "child_process";
|
2024-01-13 19:04:32 +01:00
|
|
|
import { app, BrowserWindow, clipboard, dialog, nativeImage, RelaunchOptions, session, shell } from "electron";
|
2023-08-12 03:13:03 +02:00
|
|
|
import { mkdirSync, readFileSync, watch } from "fs";
|
2023-04-05 20:01:31 +02:00
|
|
|
import { open, readFile } from "fs/promises";
|
2023-07-11 19:45:30 +01:00
|
|
|
import { release } from "os";
|
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-06-23 17:20:54 +02:00
|
|
|
import { setBadgeCount } from "./appBadge";
|
2023-06-21 16:13:20 +02:00
|
|
|
import { autoStart } from "./autoStart";
|
2023-08-04 19:39:33 +02:00
|
|
|
import { VENCORD_FILES_DIR, VENCORD_QUICKCSS_FILE, VENCORD_THEMES_DIR } from "./constants";
|
2023-04-05 05:19:48 +02:00
|
|
|
import { mainWin } from "./mainWindow";
|
2023-04-10 01:04:41 +02:00
|
|
|
import { Settings } from "./settings";
|
2023-08-25 15:32:06 +02:00
|
|
|
import { handle, handleSync } from "./utils/ipcWrappers";
|
2023-12-22 08:56:11 -05:00
|
|
|
import { isDeckGameMode, showGamePage } from "./utils/steamOS";
|
2023-08-12 03:13:03 +02:00
|
|
|
import { isValidVencordInstall } from "./utils/vencordLoader";
|
2023-04-04 00:41:52 +02:00
|
|
|
|
2023-08-25 15:32:06 +02: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 05:31:44 +02:00
|
|
|
|
2023-08-25 15:32:06 +02:00
|
|
|
handleSync(IpcEvents.GET_RENDERER_SCRIPT, () => readFileSync(join(__dirname, "renderer.js"), "utf-8"));
|
|
|
|
handleSync(IpcEvents.GET_RENDERER_CSS_FILE, () => join(__dirname, "renderer.css"));
|
2023-04-04 04:40:03 +02:00
|
|
|
|
2023-08-25 15:32:06 +02:00
|
|
|
handleSync(IpcEvents.GET_SETTINGS, () => Settings.plain);
|
|
|
|
handleSync(IpcEvents.GET_VERSION, () => app.getVersion());
|
2023-04-09 03:06:19 +02:00
|
|
|
|
2023-08-25 15:32:06 +02:00
|
|
|
handleSync(
|
|
|
|
IpcEvents.SUPPORTS_WINDOWS_TRANSPARENCY,
|
|
|
|
() => process.platform === "win32" && Number(release().split(".").pop()) >= 22621
|
|
|
|
);
|
2023-07-11 19:45:30 +01:00
|
|
|
|
2023-08-25 15:32:06 +02:00
|
|
|
handleSync(IpcEvents.AUTOSTART_ENABLED, () => autoStart.isEnabled());
|
|
|
|
handle(IpcEvents.ENABLE_AUTOSTART, autoStart.enable);
|
|
|
|
handle(IpcEvents.DISABLE_AUTOSTART, autoStart.disable);
|
2023-06-21 16:13:20 +02:00
|
|
|
|
2023-08-25 15:32:06 +02:00
|
|
|
handle(IpcEvents.SET_SETTINGS, (_, settings: typeof Settings.store, path?: string) => {
|
2023-04-10 01:43:47 +02:00
|
|
|
Settings.setData(settings, path);
|
2023-04-04 04:40:03 +02:00
|
|
|
});
|
|
|
|
|
2023-12-22 08:56:11 -05:00
|
|
|
handle(IpcEvents.RELAUNCH, async () => {
|
2023-09-27 08:22:00 +05:30
|
|
|
const options: RelaunchOptions = {
|
|
|
|
args: process.argv.slice(1).concat(["--relaunch"])
|
|
|
|
};
|
2023-12-22 08:56:11 -05:00
|
|
|
if (isDeckGameMode) {
|
|
|
|
// We can't properly relaunch when running under gamescope, but we can at least navigate to our page in Steam.
|
|
|
|
await showGamePage();
|
|
|
|
} else if (app.isPackaged && process.env.APPIMAGE) {
|
2023-09-27 08:22:00 +05:30
|
|
|
execFile(process.env.APPIMAGE, options.args);
|
|
|
|
} else {
|
|
|
|
app.relaunch(options);
|
|
|
|
}
|
2023-04-04 01:35:37 +02:00
|
|
|
app.exit();
|
|
|
|
});
|
2023-04-04 04:40:03 +02:00
|
|
|
|
2023-08-25 15:32:06 +02:00
|
|
|
handle(IpcEvents.SHOW_ITEM_IN_FOLDER, (_, path) => {
|
2023-04-04 04:40:03 +02:00
|
|
|
shell.showItemInFolder(path);
|
|
|
|
});
|
2023-04-05 05:19:48 +02:00
|
|
|
|
2023-08-25 15:32:06 +02:00
|
|
|
handle(IpcEvents.FOCUS, () => {
|
2023-06-30 18:57:28 +02:00
|
|
|
if (process.platform === "win32") mainWin.minimize(); // Windows is weird
|
|
|
|
|
|
|
|
mainWin.restore();
|
|
|
|
mainWin.show();
|
2023-04-10 22:53:44 +02:00
|
|
|
});
|
|
|
|
|
2023-08-25 15:32:06 +02:00
|
|
|
handle(IpcEvents.CLOSE, e => {
|
2023-10-25 00:30:35 +02:00
|
|
|
(BrowserWindow.fromWebContents(e.sender) ?? e.sender).close();
|
2023-08-07 00:23:27 +02:00
|
|
|
});
|
|
|
|
|
2023-08-25 15:32:06 +02:00
|
|
|
handle(IpcEvents.MINIMIZE, e => {
|
2023-08-07 00:23:27 +02:00
|
|
|
mainWin.minimize();
|
|
|
|
});
|
|
|
|
|
2023-08-25 15:32:06 +02:00
|
|
|
handle(IpcEvents.MAXIMIZE, e => {
|
2023-08-07 00:23:27 +02:00
|
|
|
if (mainWin.isMaximized()) {
|
|
|
|
mainWin.unmaximize();
|
|
|
|
} else {
|
|
|
|
mainWin.maximize();
|
|
|
|
}
|
2023-04-05 05:19:48 +02:00
|
|
|
});
|
2023-04-05 20:01:31 +02:00
|
|
|
|
2023-08-25 15:32:06 +02:00
|
|
|
handle(IpcEvents.SPELLCHECK_SET_LANGUAGES, (_, languages: string[]) => {
|
2023-04-14 04:05:56 +02: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);
|
|
|
|
});
|
|
|
|
|
2023-08-25 15:32:06 +02:00
|
|
|
handle(IpcEvents.SPELLCHECK_REPLACE_MISSPELLING, (e, word: string) => {
|
2023-06-25 03:44:19 +02:00
|
|
|
e.sender.replaceMisspelling(word);
|
|
|
|
});
|
|
|
|
|
2023-08-25 15:32:06 +02:00
|
|
|
handle(IpcEvents.SPELLCHECK_ADD_TO_DICTIONARY, (e, word: string) => {
|
2023-06-25 03:44:19 +02:00
|
|
|
e.sender.session.addWordToSpellCheckerDictionary(word);
|
|
|
|
});
|
|
|
|
|
2023-08-25 15:32:06 +02:00
|
|
|
handle(IpcEvents.SELECT_VENCORD_DIR, async () => {
|
2023-04-09 02:26:31 +02:00
|
|
|
const res = await dialog.showOpenDialog(mainWin!, {
|
|
|
|
properties: ["openDirectory"]
|
|
|
|
});
|
|
|
|
if (!res.filePaths.length) return "cancelled";
|
|
|
|
|
|
|
|
const dir = res.filePaths[0];
|
2023-08-12 03:13:03 +02:00
|
|
|
if (!isValidVencordInstall(dir)) return "invalid";
|
2023-04-09 02:26:31 +02:00
|
|
|
|
|
|
|
return dir;
|
|
|
|
});
|
|
|
|
|
2023-08-25 15:32:06 +02:00
|
|
|
handle(IpcEvents.SET_BADGE_COUNT, (_, count: number) => setBadgeCount(count));
|
2023-06-23 17:20:54 +02:00
|
|
|
|
2024-01-13 19:04:32 +01:00
|
|
|
handle(IpcEvents.CLIPBOARD_COPY_IMAGE, async (_, buf: ArrayBuffer, src: string) => {
|
|
|
|
clipboard.write({
|
|
|
|
html: `<img src="${src.replaceAll('"', '\\"')}">`,
|
|
|
|
image: nativeImage.createFromBuffer(Buffer.from(buf))
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
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
|
|
|
});
|
2023-08-04 19:39:33 +02:00
|
|
|
|
|
|
|
mkdirSync(VENCORD_THEMES_DIR, { recursive: true });
|
|
|
|
watch(
|
|
|
|
VENCORD_THEMES_DIR,
|
|
|
|
{ persistent: false },
|
|
|
|
debounce(() => {
|
|
|
|
mainWin?.webContents.postMessage("VencordThemeUpdate", void 0);
|
|
|
|
})
|
|
|
|
);
|