2023-04-10 05:49:50 +09: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
|
|
|
|
*/
|
|
|
|
|
|
|
|
import { app, BrowserWindow, BrowserWindowConstructorOptions, Menu, Tray } from "electron";
|
2023-03-30 08:02:30 +09:00
|
|
|
import { join } from "path";
|
2023-06-25 10:44:19 +09:00
|
|
|
import { IpcEvents } from "shared/IpcEvents";
|
2023-06-21 21:39:41 +09:00
|
|
|
import { once } from "shared/utils/once";
|
2023-06-25 23:48:26 +09:00
|
|
|
import type { SettingsStore } from "shared/utils/SettingsStore";
|
2023-04-10 05:49:50 +09:00
|
|
|
|
2023-04-04 08:35:37 +09:00
|
|
|
import { ICON_PATH } from "../shared/paths";
|
2023-04-06 00:55:49 +09:00
|
|
|
import { createAboutWindow } from "./about";
|
2023-06-21 21:39:41 +09:00
|
|
|
import { initArRPC } from "./arrpc";
|
|
|
|
import { DEFAULT_HEIGHT, DEFAULT_WIDTH, MIN_HEIGHT, MIN_WIDTH, VENCORD_FILES_DIR } from "./constants";
|
2023-04-09 12:25:45 +09:00
|
|
|
import { Settings, VencordSettings } from "./settings";
|
2023-06-21 21:39:41 +09:00
|
|
|
import { createSplashWindow } from "./splash";
|
2023-04-06 00:55:49 +09:00
|
|
|
import { makeLinksOpenExternally } from "./utils/makeLinksOpenExternally";
|
2023-06-21 21:39:41 +09:00
|
|
|
import { downloadVencordFiles, ensureVencordFiles } from "./utils/vencordLoader";
|
2023-03-30 08:02:30 +09:00
|
|
|
|
2023-04-04 08:47:33 +09:00
|
|
|
let isQuitting = false;
|
2023-04-11 02:12:58 +09:00
|
|
|
let tray: Tray;
|
2023-04-04 07:41:52 +09:00
|
|
|
|
2023-04-04 08:47:33 +09:00
|
|
|
app.on("before-quit", () => {
|
|
|
|
isQuitting = true;
|
|
|
|
});
|
|
|
|
|
2023-04-05 12:19:48 +09:00
|
|
|
export let mainWin: BrowserWindow;
|
|
|
|
|
2023-06-25 23:48:26 +09:00
|
|
|
function makeSettingsListenerHelpers<O extends object>(o: SettingsStore<O>) {
|
|
|
|
const listeners = new Map<(data: any) => void, PropertyKey>();
|
|
|
|
|
|
|
|
const addListener: typeof o.addChangeListener = (path, cb) => {
|
|
|
|
listeners.set(cb, path);
|
|
|
|
o.addChangeListener(path, cb);
|
|
|
|
};
|
2023-06-26 07:05:08 +09:00
|
|
|
const removeAllListeners = () => {
|
2023-06-25 23:48:26 +09:00
|
|
|
for (const [listener, path] of listeners) {
|
|
|
|
o.removeChangeListener(path as any, listener);
|
|
|
|
}
|
|
|
|
|
|
|
|
listeners.clear();
|
|
|
|
};
|
|
|
|
|
2023-06-26 07:05:08 +09:00
|
|
|
return [addListener, removeAllListeners] as const;
|
2023-06-25 23:48:26 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
const [addSettingsListener, removeSettingsListeners] = makeSettingsListenerHelpers(Settings);
|
|
|
|
const [addVencordSettingsListener, removeVencordSettingsListeners] = makeSettingsListenerHelpers(VencordSettings);
|
|
|
|
|
2023-04-04 08:47:33 +09:00
|
|
|
function initTray(win: BrowserWindow) {
|
2023-04-04 08:35:37 +09:00
|
|
|
const trayMenu = Menu.buildFromTemplate([
|
|
|
|
{
|
|
|
|
label: "Open",
|
|
|
|
click() {
|
|
|
|
win.show();
|
|
|
|
},
|
|
|
|
enabled: false
|
|
|
|
},
|
2023-04-06 07:08:28 +09:00
|
|
|
{
|
|
|
|
label: "About",
|
|
|
|
click: createAboutWindow
|
|
|
|
},
|
|
|
|
{
|
|
|
|
label: "Update Vencord",
|
|
|
|
async click() {
|
|
|
|
await downloadVencordFiles();
|
|
|
|
app.relaunch();
|
|
|
|
app.quit();
|
|
|
|
}
|
|
|
|
},
|
2023-04-06 10:52:06 +09:00
|
|
|
{
|
|
|
|
type: "separator"
|
|
|
|
},
|
2023-04-06 07:08:28 +09:00
|
|
|
{
|
|
|
|
label: "Relaunch",
|
|
|
|
click() {
|
|
|
|
app.relaunch();
|
|
|
|
app.quit();
|
|
|
|
}
|
|
|
|
},
|
2023-04-04 08:35:37 +09:00
|
|
|
{
|
|
|
|
label: "Quit Vencord Desktop",
|
|
|
|
click() {
|
|
|
|
isQuitting = true;
|
|
|
|
app.quit();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]);
|
|
|
|
|
2023-04-11 02:12:58 +09:00
|
|
|
tray = new Tray(ICON_PATH);
|
2023-04-04 08:35:37 +09:00
|
|
|
tray.setToolTip("Vencord Desktop");
|
|
|
|
tray.setContextMenu(trayMenu);
|
|
|
|
tray.on("click", () => win.show());
|
|
|
|
|
2023-04-04 08:47:33 +09:00
|
|
|
win.on("show", () => {
|
|
|
|
trayMenu.items[0].enabled = false;
|
|
|
|
});
|
|
|
|
|
|
|
|
win.on("hide", () => {
|
|
|
|
trayMenu.items[0].enabled = true;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-04-06 00:25:29 +09:00
|
|
|
function initMenuBar(win: BrowserWindow) {
|
2023-04-09 12:25:45 +09:00
|
|
|
const isWindows = process.platform === "win32";
|
2023-04-10 08:04:41 +09:00
|
|
|
const wantCtrlQ = !isWindows || VencordSettings.store.winCtrlQ;
|
2023-04-09 12:25:45 +09:00
|
|
|
|
2023-04-06 00:25:29 +09:00
|
|
|
const menu = Menu.buildFromTemplate([
|
|
|
|
{
|
|
|
|
label: "Vencord Desktop",
|
2023-04-15 19:37:52 +09:00
|
|
|
role: "appMenu",
|
2023-04-06 00:25:29 +09:00
|
|
|
submenu: [
|
|
|
|
{
|
|
|
|
label: "About Vencord Desktop",
|
2023-04-06 00:55:49 +09:00
|
|
|
click: createAboutWindow
|
2023-04-06 00:25:29 +09:00
|
|
|
},
|
|
|
|
{
|
|
|
|
label: "Force Update Vencord",
|
|
|
|
async click() {
|
|
|
|
await downloadVencordFiles();
|
|
|
|
app.relaunch();
|
|
|
|
app.quit();
|
|
|
|
},
|
|
|
|
toolTip: "Vencord Desktop will automatically restart after this operation"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
label: "Relaunch",
|
|
|
|
accelerator: "CmdOrCtrl+Shift+R",
|
|
|
|
click() {
|
|
|
|
app.relaunch();
|
|
|
|
app.quit();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
label: "Quit",
|
2023-04-09 12:25:45 +09:00
|
|
|
accelerator: wantCtrlQ ? "CmdOrCtrl+Q" : void 0,
|
|
|
|
visible: !isWindows,
|
2023-04-15 19:37:52 +09:00
|
|
|
role: "quit",
|
2023-04-06 00:25:29 +09:00
|
|
|
click() {
|
|
|
|
app.quit();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
label: "Quit",
|
2023-04-09 12:25:45 +09:00
|
|
|
accelerator: isWindows ? "Alt+F4" : void 0,
|
|
|
|
visible: isWindows,
|
2023-04-15 19:37:52 +09:00
|
|
|
role: "quit",
|
2023-04-06 00:25:29 +09:00
|
|
|
click() {
|
|
|
|
app.quit();
|
|
|
|
}
|
2023-04-27 09:23:17 +09:00
|
|
|
},
|
|
|
|
// See https://github.com/electron/electron/issues/14742 and https://github.com/electron/electron/issues/5256
|
|
|
|
{
|
|
|
|
label: "Zoom in (hidden, hack for Qwertz and others)",
|
|
|
|
accelerator: "CmdOrCtrl+=",
|
|
|
|
role: "zoomIn",
|
|
|
|
visible: false
|
2023-04-06 00:25:29 +09:00
|
|
|
}
|
|
|
|
]
|
|
|
|
},
|
2023-04-15 19:37:52 +09:00
|
|
|
{ role: "fileMenu" },
|
|
|
|
{ role: "editMenu" },
|
|
|
|
{ role: "viewMenu" },
|
2023-04-27 09:23:17 +09:00
|
|
|
{ role: "windowMenu" }
|
2023-04-06 00:25:29 +09:00
|
|
|
]);
|
|
|
|
|
|
|
|
Menu.setApplicationMenu(menu);
|
|
|
|
}
|
|
|
|
|
2023-04-09 12:04:49 +09:00
|
|
|
function getWindowBoundsOptions(): BrowserWindowConstructorOptions {
|
2023-04-10 08:04:41 +09:00
|
|
|
const { x, y, width, height } = Settings.store.windowBounds ?? {};
|
2023-04-09 12:04:49 +09:00
|
|
|
|
|
|
|
const options = {
|
|
|
|
width: width ?? DEFAULT_WIDTH,
|
|
|
|
height: height ?? DEFAULT_HEIGHT
|
|
|
|
} as BrowserWindowConstructorOptions;
|
|
|
|
|
2023-04-04 11:17:38 +09:00
|
|
|
if (x != null && y != null) {
|
|
|
|
options.x = x;
|
|
|
|
options.y = y;
|
|
|
|
}
|
|
|
|
|
2023-04-10 08:04:41 +09:00
|
|
|
if (!Settings.store.disableMinSize) {
|
2023-04-09 12:04:49 +09:00
|
|
|
options.minWidth = MIN_WIDTH;
|
|
|
|
options.minHeight = MIN_HEIGHT;
|
|
|
|
}
|
2023-04-04 11:17:38 +09:00
|
|
|
|
|
|
|
return options;
|
|
|
|
}
|
|
|
|
|
|
|
|
function initWindowBoundsListeners(win: BrowserWindow) {
|
2023-04-09 12:04:49 +09:00
|
|
|
const saveState = () => {
|
2023-04-10 08:04:41 +09:00
|
|
|
Settings.store.maximized = win.isMaximized();
|
|
|
|
Settings.store.minimized = win.isMinimized();
|
2023-04-09 12:04:49 +09:00
|
|
|
};
|
2023-04-04 11:17:38 +09:00
|
|
|
|
2023-04-09 12:04:49 +09:00
|
|
|
win.on("maximize", saveState);
|
|
|
|
win.on("minimize", saveState);
|
|
|
|
win.on("unmaximize", saveState);
|
2023-04-04 11:17:38 +09:00
|
|
|
|
|
|
|
const saveBounds = () => {
|
2023-04-10 08:04:41 +09:00
|
|
|
Settings.store.windowBounds = win.getBounds();
|
2023-04-04 11:17:38 +09:00
|
|
|
};
|
|
|
|
|
|
|
|
win.on("resize", saveBounds);
|
|
|
|
win.on("move", saveBounds);
|
|
|
|
}
|
|
|
|
|
2023-04-10 08:43:47 +09:00
|
|
|
function initSettingsListeners(win: BrowserWindow) {
|
2023-06-25 23:48:26 +09:00
|
|
|
addSettingsListener("tray", enable => {
|
2023-04-11 02:18:00 +09:00
|
|
|
if (enable) initTray(win);
|
|
|
|
else tray?.destroy();
|
2023-04-11 02:12:58 +09:00
|
|
|
});
|
2023-06-25 23:48:26 +09:00
|
|
|
addSettingsListener("disableMinSize", disable => {
|
2023-04-10 08:55:48 +09:00
|
|
|
if (disable) {
|
|
|
|
// 0 no work
|
|
|
|
win.setMinimumSize(1, 1);
|
|
|
|
} else {
|
|
|
|
win.setMinimumSize(MIN_WIDTH, MIN_HEIGHT);
|
|
|
|
|
|
|
|
const { width, height } = win.getBounds();
|
|
|
|
win.setBounds({
|
|
|
|
width: Math.max(width, MIN_WIDTH),
|
|
|
|
height: Math.max(height, MIN_HEIGHT)
|
|
|
|
});
|
|
|
|
}
|
2023-04-10 08:43:47 +09:00
|
|
|
});
|
2023-04-11 02:21:45 +09:00
|
|
|
|
2023-06-25 23:48:26 +09:00
|
|
|
addVencordSettingsListener("macosTranslucency", enabled => {
|
2023-04-11 02:21:45 +09:00
|
|
|
if (enabled) {
|
|
|
|
win.setVibrancy("sidebar");
|
|
|
|
win.setBackgroundColor("#ffffff00");
|
|
|
|
} else {
|
|
|
|
win.setVibrancy(null);
|
|
|
|
win.setBackgroundColor("#ffffff");
|
|
|
|
}
|
|
|
|
});
|
2023-04-10 08:43:47 +09:00
|
|
|
}
|
|
|
|
|
2023-06-25 10:44:19 +09:00
|
|
|
function initSpellCheck(win: BrowserWindow) {
|
|
|
|
win.webContents.on("context-menu", (_, data) => {
|
|
|
|
win.webContents.send(IpcEvents.SPELLCHECK_RESULT, data.misspelledWord, data.dictionarySuggestions);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-06-21 21:39:41 +09:00
|
|
|
function createMainWindow() {
|
2023-06-25 23:48:26 +09:00
|
|
|
// Clear up previous settings listeners
|
|
|
|
removeSettingsListeners();
|
|
|
|
removeVencordSettingsListeners();
|
|
|
|
|
2023-04-10 05:49:50 +09:00
|
|
|
const win = (mainWin = new BrowserWindow({
|
2023-04-04 08:47:33 +09:00
|
|
|
show: false,
|
|
|
|
webPreferences: {
|
|
|
|
nodeIntegration: false,
|
|
|
|
sandbox: false,
|
|
|
|
contextIsolation: true,
|
|
|
|
devTools: true,
|
2023-06-25 10:44:19 +09:00
|
|
|
preload: join(__dirname, "preload.js"),
|
|
|
|
spellcheck: true
|
2023-04-04 08:47:33 +09:00
|
|
|
},
|
2023-04-04 11:17:38 +09:00
|
|
|
icon: ICON_PATH,
|
2023-04-10 08:04:41 +09:00
|
|
|
frame: VencordSettings.store.frameless !== true,
|
2023-07-12 03:45:30 +09:00
|
|
|
...(Settings.store.transparencyOption !== "none"
|
|
|
|
? {
|
|
|
|
backgroundColor: "#00000000",
|
|
|
|
backgroundMaterial: Settings.store.transparencyOption,
|
|
|
|
transparent: true
|
|
|
|
}
|
|
|
|
: {
|
|
|
|
transparent: false
|
|
|
|
}),
|
2023-04-20 11:26:56 +09:00
|
|
|
...(Settings.store.staticTitle ? { title: "Vencord" } : {}),
|
2023-04-11 02:21:45 +09:00
|
|
|
...(VencordSettings.store.macosTranslucency
|
|
|
|
? {
|
|
|
|
vibrancy: "sidebar",
|
|
|
|
backgroundColor: "#ffffff00"
|
|
|
|
}
|
|
|
|
: {}),
|
2023-04-04 11:17:38 +09:00
|
|
|
...getWindowBoundsOptions()
|
2023-04-10 05:49:50 +09:00
|
|
|
}));
|
2023-07-04 05:25:11 +09:00
|
|
|
win.setMenuBarVisibility(false);
|
2023-04-04 07:41:52 +09:00
|
|
|
|
|
|
|
win.on("close", e => {
|
2023-04-11 02:12:58 +09:00
|
|
|
if (isQuitting || Settings.store.minimizeToTray === false || Settings.store.tray === false) return;
|
2023-04-04 07:41:52 +09:00
|
|
|
|
|
|
|
e.preventDefault();
|
|
|
|
win.hide();
|
|
|
|
|
|
|
|
return false;
|
2023-03-30 08:02:30 +09:00
|
|
|
});
|
|
|
|
|
2023-04-20 11:26:56 +09:00
|
|
|
if (Settings.store.staticTitle) win.on("page-title-updated", e => e.preventDefault());
|
|
|
|
|
2023-04-04 11:17:38 +09:00
|
|
|
initWindowBoundsListeners(win);
|
2023-04-11 02:12:58 +09:00
|
|
|
if (Settings.store.tray ?? true) initTray(win);
|
2023-04-06 00:25:29 +09:00
|
|
|
initMenuBar(win);
|
2023-04-06 00:55:49 +09:00
|
|
|
makeLinksOpenExternally(win);
|
2023-04-10 08:43:47 +09:00
|
|
|
initSettingsListeners(win);
|
2023-06-25 10:44:19 +09:00
|
|
|
initSpellCheck(win);
|
2023-04-04 07:41:52 +09:00
|
|
|
|
2023-04-16 03:13:18 +09:00
|
|
|
win.webContents.setUserAgent(
|
2023-06-23 23:03:31 +09:00
|
|
|
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36"
|
2023-04-16 03:13:18 +09:00
|
|
|
);
|
|
|
|
|
2023-04-10 05:49:50 +09:00
|
|
|
const subdomain =
|
2023-04-10 08:04:41 +09:00
|
|
|
Settings.store.discordBranch === "canary" || Settings.store.discordBranch === "ptb"
|
|
|
|
? `${Settings.store.discordBranch}.`
|
|
|
|
: "";
|
2023-04-04 11:40:03 +09:00
|
|
|
|
|
|
|
win.loadURL(`https://${subdomain}discord.com/app`);
|
2023-03-30 08:02:30 +09:00
|
|
|
|
|
|
|
return win;
|
|
|
|
}
|
2023-06-21 21:39:41 +09:00
|
|
|
|
|
|
|
const runVencordMain = once(() => require(join(VENCORD_FILES_DIR, "vencordDesktopMain.js")));
|
|
|
|
|
|
|
|
export async function createWindows() {
|
|
|
|
const splash = createSplashWindow();
|
|
|
|
|
|
|
|
await ensureVencordFiles();
|
|
|
|
runVencordMain();
|
|
|
|
|
|
|
|
mainWin = createMainWindow();
|
|
|
|
|
|
|
|
mainWin.once("ready-to-show", () => {
|
|
|
|
splash.destroy();
|
|
|
|
mainWin!.show();
|
|
|
|
|
|
|
|
if (Settings.store.maximized) {
|
|
|
|
mainWin!.maximize();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
initArRPC();
|
|
|
|
}
|