Init renderer, expose settings via ipc, init canary/ptb support

This commit is contained in:
Vendicated 2023-04-04 04:40:03 +02:00
parent 18a77d45b8
commit 79d73e23c0
No known key found for this signature in database
GPG key ID: A1DC0CFB5615D905
8 changed files with 82 additions and 22 deletions

View file

@ -1,15 +1,20 @@
import { BuildContext, BuildOptions, context } from "esbuild"; import { BuildContext, BuildOptions, context } from "esbuild";
const NodeCommonOpts: BuildOptions = { const CommonOpts: BuildOptions = {
format: "cjs",
platform: "node",
external: ["electron"],
minify: true, minify: true,
bundle: true, bundle: true,
sourcemap: "linked", sourcemap: "linked",
logLevel: "info" logLevel: "info"
}; };
const NodeCommonOpts: BuildOptions = {
...CommonOpts,
format: "cjs",
platform: "node",
external: ["electron"],
target: ["esnext"],
};
const contexts = [] as BuildContext[]; const contexts = [] as BuildContext[];
async function createContext(options: BuildOptions) { async function createContext(options: BuildOptions) {
contexts.push(await context(options)); contexts.push(await context(options));
@ -25,6 +30,12 @@ await Promise.all([
...NodeCommonOpts, ...NodeCommonOpts,
entryPoints: ["src/preload/index.ts"], entryPoints: ["src/preload/index.ts"],
outfile: "dist/js/preload.js" outfile: "dist/js/preload.js"
}),
createContext({
...CommonOpts,
entryPoints: ["src/renderer/index.ts"],
outfile: "dist/js/renderer.js",
format: "iife",
}) })
]); ]);

View file

@ -1,13 +1,31 @@
import { app, ipcMain } from "electron"; import { app, ipcMain, shell } from "electron";
import { readFileSync } from "fs";
import { join } from "path"; import { join } from "path";
import { GET_PRELOAD_FILE, RELAUNCH } from "../shared/IpcEvents"; import { GET_RENDERER_SCRIPT, GET_SETTINGS, GET_VENCORD_PRELOAD_FILE, RELAUNCH, SET_SETTINGS, SHOW_IN_FOLDER } from "../shared/IpcEvents";
import { VENCORD_FILES_DIR } from "./constants"; import { VENCORD_FILES_DIR } from "./constants";
import { PlainSettings, setSettings } from "./settings";
ipcMain.on(GET_PRELOAD_FILE, e => { ipcMain.on(GET_VENCORD_PRELOAD_FILE, e => {
e.returnValue = join(VENCORD_FILES_DIR, "preload.js"); e.returnValue = join(VENCORD_FILES_DIR, "preload.js");
}); });
ipcMain.on(GET_RENDERER_SCRIPT, e => {
e.returnValue = readFileSync(join(__dirname, "renderer.js"), "utf-8");
});
ipcMain.on(GET_SETTINGS, e => {
e.returnValue = PlainSettings;
});
ipcMain.handle(SET_SETTINGS, (_, settings) => {
setSettings(settings);
});
ipcMain.handle(RELAUNCH, () => { ipcMain.handle(RELAUNCH, () => {
app.relaunch(); app.relaunch();
app.exit(); app.exit();
}); });
ipcMain.handle(SHOW_IN_FOLDER, (_, path) => {
shell.showItemInFolder(path);
});

View file

@ -142,7 +142,11 @@ export function createMainWindow() {
initTray(win); initTray(win);
initWindowOpenHandler(win); initWindowOpenHandler(win);
win.loadURL("https://discord.com/app"); const subdomain = Settings.discordBranch === "canary" || Settings.discordBranch === "ptb"
? `${Settings.discordBranch}.`
: "";
win.loadURL(`https://${subdomain}discord.com/app`);
return win; return win;
} }

View file

@ -13,24 +13,36 @@ interface Settings {
width: number; width: number;
height: number; height: number;
}; };
discordBranch: "stable" | "canary" | "ptb";
} }
let settings = {} as Settings; export let PlainSettings = {} as Settings;
try { try {
const content = readFileSync(SETTINGS_FILE, "utf8"); const content = readFileSync(SETTINGS_FILE, "utf8");
try { try {
settings = JSON.parse(content); PlainSettings = JSON.parse(content);
} catch (err) { } catch (err) {
console.error("Failed to parse settings.json:", err); console.error("Failed to parse settings.json:", err);
} }
} catch { } } catch { }
export const Settings = new Proxy(settings, { function makeSettingsProxy(settings: Settings) {
set(target, prop, value) { return new Proxy(settings, {
Reflect.set(target, prop, value); set(target, prop, value) {
Reflect.set(target, prop, value);
writeFileSync(SETTINGS_FILE, JSON.stringify(target, null, 4)); writeFileSync(SETTINGS_FILE, JSON.stringify(target, null, 4));
return true;
}
});
}
export let Settings = makeSettingsProxy(PlainSettings);
export function setSettings(settings: Settings) {
writeFileSync(SETTINGS_FILE, JSON.stringify(settings, null, 4));
PlainSettings = settings;
Settings = makeSettingsProxy(settings);
}
return true;
}
});

View file

@ -1,9 +1,17 @@
import { ipcRenderer } from "electron"; import { ipcRenderer } from "electron";
import { RELAUNCH } from "../shared/IpcEvents"; import type { Settings } from "../main/settings";
import { GET_SETTINGS, RELAUNCH, SET_SETTINGS, SHOW_IN_FOLDER } from "../shared/IpcEvents";
export const VencordDesktop = { export const VencordDesktop = {
app: { app: {
relaunch: () => ipcRenderer.invoke(RELAUNCH) relaunch: () => ipcRenderer.invoke(RELAUNCH)
},
files: {
showInFolder: (path: string) => ipcRenderer.invoke(SHOW_IN_FOLDER, path)
},
settings: {
get: () => ipcRenderer.sendSync(GET_SETTINGS),
set: (settings: typeof Settings) => ipcRenderer.invoke(SET_SETTINGS, settings)
} }
} }

View file

@ -1,7 +1,9 @@
import { contextBridge, ipcRenderer } from "electron"; import { contextBridge, ipcRenderer, webFrame } from "electron";
import { GET_PRELOAD_FILE } from "../shared/IpcEvents"; import { GET_RENDERER_SCRIPT, GET_VENCORD_PRELOAD_FILE } from "../shared/IpcEvents";
import { VencordDesktop } from "./VencordDesktop"; import { VencordDesktop } from "./VencordDesktop";
contextBridge.exposeInMainWorld("VencordDesktop", VencordDesktop); contextBridge.exposeInMainWorld("VencordDesktop", VencordDesktop);
require(ipcRenderer.sendSync(GET_PRELOAD_FILE)); require(ipcRenderer.sendSync(GET_VENCORD_PRELOAD_FILE));
webFrame.executeJavaScript(ipcRenderer.sendSync(GET_RENDERER_SCRIPT));

1
src/renderer/index.ts Normal file
View file

@ -0,0 +1 @@
console.log("read if cute :3");

View file

@ -1,2 +1,6 @@
export const GET_PRELOAD_FILE = "VCD_GET_PRELOAD_FILE"; export const GET_VENCORD_PRELOAD_FILE = "VCD_GET_VC_PRELOAD_FILE";
export const GET_RENDERER_SCRIPT = "VCD_GET_RENDERER_SCRIPT";
export const RELAUNCH = "VCD_RELAUNCH"; export const RELAUNCH = "VCD_RELAUNCH";
export const SHOW_IN_FOLDER = "VCD_SHOW_IN_FOLDER";
export const GET_SETTINGS = "VCD_GET_SETTINGS";
export const SET_SETTINGS = "VCD_SET_SETTINGS";