Init renderer, expose settings via ipc, init canary/ptb support
This commit is contained in:
@@ -1,13 +1,31 @@
|
||||
import { app, ipcMain } from "electron";
|
||||
import { app, ipcMain, shell } from "electron";
|
||||
import { readFileSync } from "fs";
|
||||
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 { 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");
|
||||
});
|
||||
|
||||
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, () => {
|
||||
app.relaunch();
|
||||
app.exit();
|
||||
});
|
||||
|
||||
ipcMain.handle(SHOW_IN_FOLDER, (_, path) => {
|
||||
shell.showItemInFolder(path);
|
||||
});
|
||||
|
||||
@@ -142,7 +142,11 @@ export function createMainWindow() {
|
||||
initTray(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;
|
||||
}
|
||||
|
||||
@@ -13,24 +13,36 @@ interface Settings {
|
||||
width: number;
|
||||
height: number;
|
||||
};
|
||||
discordBranch: "stable" | "canary" | "ptb";
|
||||
}
|
||||
|
||||
let settings = {} as Settings;
|
||||
export let PlainSettings = {} as Settings;
|
||||
try {
|
||||
const content = readFileSync(SETTINGS_FILE, "utf8");
|
||||
try {
|
||||
settings = JSON.parse(content);
|
||||
PlainSettings = JSON.parse(content);
|
||||
} catch (err) {
|
||||
console.error("Failed to parse settings.json:", err);
|
||||
}
|
||||
} catch { }
|
||||
|
||||
export const Settings = new Proxy(settings, {
|
||||
set(target, prop, value) {
|
||||
Reflect.set(target, prop, value);
|
||||
function makeSettingsProxy(settings: Settings) {
|
||||
return new Proxy(settings, {
|
||||
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;
|
||||
}
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user