fix formatting

This commit is contained in:
pupbrained 2023-10-27 19:56:16 -04:00
parent ebd68c635b
commit 5e69e05b8b

View file

@ -5,14 +5,14 @@
*/ */
import { import {
app, app,
BrowserWindow, BrowserWindow,
BrowserWindowConstructorOptions, BrowserWindowConstructorOptions,
dialog, dialog,
Menu, Menu,
MenuItemConstructorOptions, MenuItemConstructorOptions,
nativeTheme, nativeTheme,
Tray Tray
} from "electron"; } from "electron";
import { rm } from "fs/promises"; import { rm } from "fs/promises";
import { join } from "path"; import { join } from "path";
@ -25,13 +25,13 @@ import { ICON_PATH } from "../shared/paths";
import { createAboutWindow } from "./about"; import { createAboutWindow } from "./about";
import { initArRPC } from "./arrpc"; import { initArRPC } from "./arrpc";
import { import {
DATA_DIR, DATA_DIR,
DEFAULT_HEIGHT, DEFAULT_HEIGHT,
DEFAULT_WIDTH, DEFAULT_WIDTH,
MIN_HEIGHT, MIN_HEIGHT,
MIN_WIDTH, MIN_WIDTH,
UserAgent, UserAgent,
VENCORD_FILES_DIR VENCORD_FILES_DIR
} from "./constants"; } from "./constants";
import { Settings, VencordSettings } from "./settings"; import { Settings, VencordSettings } from "./settings";
import { createSplashWindow } from "./splash"; import { createSplashWindow } from "./splash";
@ -42,419 +42,419 @@ let isQuitting = false;
let tray: Tray; let tray: Tray;
app.on("before-quit", () => { app.on("before-quit", () => {
isQuitting = true; isQuitting = true;
}); });
export let mainWin: BrowserWindow; export let mainWin: BrowserWindow;
function makeSettingsListenerHelpers<O extends object>(o: SettingsStore<O>) { function makeSettingsListenerHelpers<O extends object>(o: SettingsStore<O>) {
const listeners = new Map<(data: any) => void, PropertyKey>(); const listeners = new Map<(data: any) => void, PropertyKey>();
const addListener: typeof o.addChangeListener = (path, cb) => { const addListener: typeof o.addChangeListener = (path, cb) => {
listeners.set(cb, path); listeners.set(cb, path);
o.addChangeListener(path, cb); o.addChangeListener(path, cb);
}; };
const removeAllListeners = () => { const removeAllListeners = () => {
for (const [listener, path] of listeners) { for (const [listener, path] of listeners) {
o.removeChangeListener(path as any, listener); o.removeChangeListener(path as any, listener);
} }
listeners.clear(); listeners.clear();
}; };
return [addListener, removeAllListeners] as const; return [addListener, removeAllListeners] as const;
} }
const [addSettingsListener, removeSettingsListeners] = makeSettingsListenerHelpers(Settings); const [addSettingsListener, removeSettingsListeners] = makeSettingsListenerHelpers(Settings);
const [addVencordSettingsListener, removeVencordSettingsListeners] = makeSettingsListenerHelpers(VencordSettings); const [addVencordSettingsListener, removeVencordSettingsListeners] = makeSettingsListenerHelpers(VencordSettings);
function initTray(win: BrowserWindow) { function initTray(win: BrowserWindow) {
const trayMenu = Menu.buildFromTemplate([ const trayMenu = Menu.buildFromTemplate([
{ {
label: "Open", label: "Open",
click() { click() {
win.show(); win.show();
}, },
enabled: false enabled: false
}, },
{ {
label: "About", label: "About",
click: createAboutWindow click: createAboutWindow
}, },
{ {
label: "Update Vencord", label: "Update Vencord",
async click() { async click() {
await downloadVencordFiles(); await downloadVencordFiles();
app.relaunch(); app.relaunch();
app.quit(); app.quit();
} }
}, },
{ {
label: "Reset Vesktop", label: "Reset Vesktop",
async click() { async click() {
await clearData(win); await clearData(win);
} }
}, },
{ {
type: "separator" type: "separator"
}, },
{ {
label: "Relaunch", label: "Relaunch",
click() { click() {
app.relaunch(); app.relaunch();
app.quit(); app.quit();
} }
}, },
{ {
label: "Quit Vesktop", label: "Quit Vesktop",
click() { click() {
isQuitting = true; isQuitting = true;
app.quit(); app.quit();
} }
} }
]); ]);
tray = new Tray(ICON_PATH); tray = new Tray(ICON_PATH);
tray.setToolTip("Vesktop"); tray.setToolTip("Vesktop");
tray.setContextMenu(trayMenu); tray.setContextMenu(trayMenu);
tray.on("click", () => win.show()); tray.on("click", () => win.show());
win.on("show", () => { win.on("show", () => {
trayMenu.items[0].enabled = false; trayMenu.items[0].enabled = false;
}); });
win.on("hide", () => { win.on("hide", () => {
trayMenu.items[0].enabled = true; trayMenu.items[0].enabled = true;
}); });
} }
const enum MessageBoxChoice { const enum MessageBoxChoice {
Default, Default,
Cancel Cancel
} }
async function clearData(win: BrowserWindow) { async function clearData(win: BrowserWindow) {
const { response } = await dialog.showMessageBox(win, { const { response } = await dialog.showMessageBox(win, {
message: "Are you sure you want to reset Vesktop?", message: "Are you sure you want to reset Vesktop?",
detail: "This will log you out, clear caches and reset all your settings!\n\nVesktop will automatically restart after this operation.", detail: "This will log you out, clear caches and reset all your settings!\n\nVesktop will automatically restart after this operation.",
buttons: ["Yes", "No"], buttons: ["Yes", "No"],
cancelId: MessageBoxChoice.Cancel, cancelId: MessageBoxChoice.Cancel,
defaultId: MessageBoxChoice.Default, defaultId: MessageBoxChoice.Default,
type: "warning" type: "warning"
}); });
if (response === MessageBoxChoice.Cancel) return; if (response === MessageBoxChoice.Cancel) return;
win.close(); win.close();
await win.webContents.session.clearStorageData(); await win.webContents.session.clearStorageData();
await win.webContents.session.clearCache(); await win.webContents.session.clearCache();
await win.webContents.session.clearCodeCaches({}); await win.webContents.session.clearCodeCaches({});
await rm(DATA_DIR, { force: true, recursive: true }); await rm(DATA_DIR, { force: true, recursive: true });
app.relaunch(); app.relaunch();
app.quit(); app.quit();
} }
type MenuItemList = Array<MenuItemConstructorOptions | false>; type MenuItemList = Array<MenuItemConstructorOptions | false>;
function initMenuBar(win: BrowserWindow) { function initMenuBar(win: BrowserWindow) {
const isWindows = process.platform === "win32"; const isWindows = process.platform === "win32";
const isDarwin = process.platform === "darwin"; const isDarwin = process.platform === "darwin";
const wantCtrlQ = !isWindows || VencordSettings.store.winCtrlQ; const wantCtrlQ = !isWindows || VencordSettings.store.winCtrlQ;
const subMenu = [ const subMenu = [
{
label: "About Vesktop",
click: createAboutWindow
},
{
label: "Force Update Vencord",
async click() {
await downloadVencordFiles();
app.relaunch();
app.quit();
},
toolTip: "Vesktop will automatically restart after this operation"
},
{
label: "Reset Vesktop",
async click() {
await clearData(win);
},
toolTip: "Vesktop will automatically restart after this operation"
},
{
label: "Relaunch",
accelerator: "CmdOrCtrl+Shift+R",
click() {
app.relaunch();
app.quit();
}
},
...(!isDarwin
? []
: ([
{ {
type: "separator" label: "About Vesktop",
click: createAboutWindow
}, },
{ {
label: "Settings", label: "Force Update Vencord",
accelerator: "CmdOrCtrl+,", async click() {
async click() { await downloadVencordFiles();
mainWin.webContents.executeJavaScript( app.relaunch();
"Vencord.Webpack.Common.SettingsRouter.open('My Account')" app.quit();
); },
} toolTip: "Vesktop will automatically restart after this operation"
}, },
{ {
type: "separator" label: "Reset Vesktop",
async click() {
await clearData(win);
},
toolTip: "Vesktop will automatically restart after this operation"
}, },
{ {
label: "Hide Vesktop", // Should probably remove the label, but it says "Hide VencordDesktop" instead of "Hide Vesktop" label: "Relaunch",
role: "hide" accelerator: "CmdOrCtrl+Shift+R",
click() {
app.relaunch();
app.quit();
}
}, },
...(!isDarwin
? []
: ([
{
type: "separator"
},
{
label: "Settings",
accelerator: "CmdOrCtrl+,",
async click() {
mainWin.webContents.executeJavaScript(
"Vencord.Webpack.Common.SettingsRouter.open('My Account')"
);
}
},
{
type: "separator"
},
{
label: "Hide Vesktop", // Should probably remove the label, but it says "Hide VencordDesktop" instead of "Hide Vesktop"
role: "hide"
},
{
role: "hideOthers"
},
{
role: "unhide"
},
{
type: "separator"
}
] satisfies MenuItemList)),
{ {
role: "hideOthers" label: "Quit",
accelerator: wantCtrlQ ? "CmdOrCtrl+Q" : void 0,
visible: !isWindows,
role: "quit",
click() {
app.quit();
}
}, },
{ isWindows && {
role: "unhide" label: "Quit",
accelerator: "Alt+F4",
role: "quit",
click() {
app.quit();
}
}, },
// See https://github.com/electron/electron/issues/14742 and https://github.com/electron/electron/issues/5256
{ {
type: "separator" label: "Zoom in (hidden, hack for Qwertz and others)",
accelerator: "CmdOrCtrl+=",
role: "zoomIn",
visible: false
} }
] satisfies MenuItemList)), ] satisfies MenuItemList;
{
label: "Quit",
accelerator: wantCtrlQ ? "CmdOrCtrl+Q" : void 0,
visible: !isWindows,
role: "quit",
click() {
app.quit();
}
},
isWindows && {
label: "Quit",
accelerator: "Alt+F4",
role: "quit",
click() {
app.quit();
}
},
// 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
}
] satisfies MenuItemList;
const menu = Menu.buildFromTemplate([ const menu = Menu.buildFromTemplate([
{ {
label: "Vesktop", label: "Vesktop",
role: "appMenu", role: "appMenu",
submenu: subMenu.filter(isTruthy) submenu: subMenu.filter(isTruthy)
}, },
{ role: "fileMenu" }, { role: "fileMenu" },
{ role: "editMenu" }, { role: "editMenu" },
{ role: "viewMenu" }, { role: "viewMenu" },
{ role: "windowMenu" } { role: "windowMenu" }
]); ]);
Menu.setApplicationMenu(menu); Menu.setApplicationMenu(menu);
} }
function getWindowBoundsOptions(): BrowserWindowConstructorOptions { function getWindowBoundsOptions(): BrowserWindowConstructorOptions {
const { x, y, width, height } = Settings.store.windowBounds ?? {}; const { x, y, width, height } = Settings.store.windowBounds ?? {};
const options = { const options = {
width: width ?? DEFAULT_WIDTH, width: width ?? DEFAULT_WIDTH,
height: height ?? DEFAULT_HEIGHT height: height ?? DEFAULT_HEIGHT
} as BrowserWindowConstructorOptions; } as BrowserWindowConstructorOptions;
if (x != null && y != null) { if (x != null && y != null) {
options.x = x; options.x = x;
options.y = y; options.y = y;
} }
if (!Settings.store.disableMinSize) { if (!Settings.store.disableMinSize) {
options.minWidth = MIN_WIDTH; options.minWidth = MIN_WIDTH;
options.minHeight = MIN_HEIGHT; options.minHeight = MIN_HEIGHT;
} }
return options; return options;
} }
function getDarwinOptions(): BrowserWindowConstructorOptions { function getDarwinOptions(): BrowserWindowConstructorOptions {
const options = { const options = {
titleBarStyle: "hidden", titleBarStyle: "hidden",
trafficLightPosition: { x: 10, y: 10 } trafficLightPosition: { x: 10, y: 10 }
} as BrowserWindowConstructorOptions; } as BrowserWindowConstructorOptions;
const { splashTheming, splashBackground } = Settings.store; const { splashTheming, splashBackground } = Settings.store;
const { macosTranslucency } = VencordSettings.store; const { macosTranslucency } = VencordSettings.store;
if (macosTranslucency) { if (macosTranslucency) {
options.vibrancy = "sidebar"; options.vibrancy = "sidebar";
options.backgroundColor = "#ffffff00"; options.backgroundColor = "#ffffff00";
} else {
if (splashTheming) {
options.backgroundColor = splashBackground;
} else { } else {
options.backgroundColor = nativeTheme.shouldUseDarkColors ? "#313338" : "#ffffff"; if (splashTheming) {
options.backgroundColor = splashBackground;
} else {
options.backgroundColor = nativeTheme.shouldUseDarkColors ? "#313338" : "#ffffff";
}
} }
}
return options; return options;
} }
function initWindowBoundsListeners(win: BrowserWindow) { function initWindowBoundsListeners(win: BrowserWindow) {
const saveState = () => { const saveState = () => {
Settings.store.maximized = win.isMaximized(); Settings.store.maximized = win.isMaximized();
Settings.store.minimized = win.isMinimized(); Settings.store.minimized = win.isMinimized();
}; };
win.on("maximize", saveState); win.on("maximize", saveState);
win.on("minimize", saveState); win.on("minimize", saveState);
win.on("unmaximize", saveState); win.on("unmaximize", saveState);
const saveBounds = () => { const saveBounds = () => {
Settings.store.windowBounds = win.getBounds(); Settings.store.windowBounds = win.getBounds();
}; };
win.on("resize", saveBounds); win.on("resize", saveBounds);
win.on("move", saveBounds); win.on("move", saveBounds);
} }
function initSettingsListeners(win: BrowserWindow) { function initSettingsListeners(win: BrowserWindow) {
addSettingsListener("tray", enable => { addSettingsListener("tray", enable => {
if (enable) initTray(win); if (enable) initTray(win);
else tray?.destroy(); else tray?.destroy();
}); });
addSettingsListener("disableMinSize", disable => { addSettingsListener("disableMinSize", disable => {
if (disable) { if (disable) {
// 0 no work // 0 no work
win.setMinimumSize(1, 1); win.setMinimumSize(1, 1);
} else { } else {
win.setMinimumSize(MIN_WIDTH, MIN_HEIGHT); win.setMinimumSize(MIN_WIDTH, MIN_HEIGHT);
const { width, height } = win.getBounds(); const { width, height } = win.getBounds();
win.setBounds({ win.setBounds({
width: Math.max(width, MIN_WIDTH), width: Math.max(width, MIN_WIDTH),
height: Math.max(height, MIN_HEIGHT) height: Math.max(height, MIN_HEIGHT)
}); });
} }
}); });
addVencordSettingsListener("macosTranslucency", enabled => { addVencordSettingsListener("macosTranslucency", enabled => {
if (enabled) { if (enabled) {
win.setVibrancy("sidebar"); win.setVibrancy("sidebar");
win.setBackgroundColor("#ffffff00"); win.setBackgroundColor("#ffffff00");
} else { } else {
win.setVibrancy(null); win.setVibrancy(null);
win.setBackgroundColor("#ffffff"); win.setBackgroundColor("#ffffff");
} }
}); });
addSettingsListener("enableMenu", enabled => { addSettingsListener("enableMenu", enabled => {
win.setAutoHideMenuBar(enabled ?? false); win.setAutoHideMenuBar(enabled ?? false);
}); });
} }
function initSpellCheck(win: BrowserWindow) { function initSpellCheck(win: BrowserWindow) {
win.webContents.on("context-menu", (_, data) => { win.webContents.on("context-menu", (_, data) => {
win.webContents.send(IpcEvents.SPELLCHECK_RESULT, data.misspelledWord, data.dictionarySuggestions); win.webContents.send(IpcEvents.SPELLCHECK_RESULT, data.misspelledWord, data.dictionarySuggestions);
}); });
} }
function createMainWindow() { function createMainWindow() {
// Clear up previous settings listeners // Clear up previous settings listeners
removeSettingsListeners(); removeSettingsListeners();
removeVencordSettingsListeners(); removeVencordSettingsListeners();
const { staticTitle, transparencyOption, enableMenu, discordWindowsTitleBar } = Settings.store; const { staticTitle, transparencyOption, enableMenu, discordWindowsTitleBar } = Settings.store;
const { frameless } = VencordSettings.store; const { frameless } = VencordSettings.store;
const noFrame = frameless === true || (process.platform === "win32" && discordWindowsTitleBar === true); const noFrame = frameless === true || (process.platform === "win32" && discordWindowsTitleBar === true);
const win = (mainWin = new BrowserWindow({ const win = (mainWin = new BrowserWindow({
show: false, show: false,
webPreferences: { webPreferences: {
nodeIntegration: false, nodeIntegration: false,
sandbox: false, sandbox: false,
contextIsolation: true, contextIsolation: true,
devTools: true, devTools: true,
preload: join(__dirname, "preload.js"), preload: join(__dirname, "preload.js"),
spellcheck: true spellcheck: true
}, },
icon: ICON_PATH, icon: ICON_PATH,
frame: !noFrame, frame: !noFrame,
...(transparencyOption && ...(transparencyOption &&
transparencyOption !== "none" && { transparencyOption !== "none" && {
backgroundColor: "#00000000", backgroundColor: "#00000000",
backgroundMaterial: transparencyOption, backgroundMaterial: transparencyOption,
transparent: true transparent: true
}), }),
...(staticTitle && { title: "Vesktop" }), ...(staticTitle && { title: "Vesktop" }),
...(process.platform === "darwin" && getDarwinOptions()), ...(process.platform === "darwin" && getDarwinOptions()),
...getWindowBoundsOptions(), ...getWindowBoundsOptions(),
autoHideMenuBar: enableMenu autoHideMenuBar: enableMenu
})); }));
win.setMenuBarVisibility(false); win.setMenuBarVisibility(false);
win.on("close", e => { win.on("close", e => {
const useTray = Settings.store.minimizeToTray !== false && Settings.store.tray !== false; const useTray = Settings.store.minimizeToTray !== false && Settings.store.tray !== false;
if (isQuitting || (process.platform !== "darwin" && !useTray)) return; if (isQuitting || (process.platform !== "darwin" && !useTray)) return;
e.preventDefault(); e.preventDefault();
if (process.platform === "darwin") app.hide(); if (process.platform === "darwin") app.hide();
else win.hide(); else win.hide();
return false; return false;
}); });
if (Settings.store.staticTitle) win.on("page-title-updated", e => e.preventDefault()); if (Settings.store.staticTitle) win.on("page-title-updated", e => e.preventDefault());
initWindowBoundsListeners(win); initWindowBoundsListeners(win);
if ((Settings.store.tray ?? true) && process.platform !== "darwin") initTray(win); if ((Settings.store.tray ?? true) && process.platform !== "darwin") initTray(win);
initMenuBar(win); initMenuBar(win);
makeLinksOpenExternally(win); makeLinksOpenExternally(win);
initSettingsListeners(win); initSettingsListeners(win);
initSpellCheck(win); initSpellCheck(win);
win.webContents.setUserAgent(UserAgent); win.webContents.setUserAgent(UserAgent);
const subdomain = const subdomain =
Settings.store.discordBranch === "canary" || Settings.store.discordBranch === "ptb" Settings.store.discordBranch === "canary" || Settings.store.discordBranch === "ptb"
? `${Settings.store.discordBranch}.` ? `${Settings.store.discordBranch}.`
: ""; : "";
win.loadURL(`https://${subdomain}discord.com/app`); win.loadURL(`https://${subdomain}discord.com/app`);
return win; return win;
} }
const runVencordMain = once(() => require(join(VENCORD_FILES_DIR, "vencordDesktopMain.js"))); const runVencordMain = once(() => require(join(VENCORD_FILES_DIR, "vencordDesktopMain.js")));
export async function createWindows() { export async function createWindows() {
const splash = createSplashWindow(); const splash = createSplashWindow();
await ensureVencordFiles(); await ensureVencordFiles();
runVencordMain(); runVencordMain();
mainWin = createMainWindow(); mainWin = createMainWindow();
mainWin.once("ready-to-show", () => { mainWin.once("ready-to-show", () => {
splash.destroy(); splash.destroy();
mainWin!.show(); mainWin!.show();
if (Settings.store.maximized) { if (Settings.store.maximized) {
mainWin!.maximize(); mainWin!.maximize();
} }
}); });
initArRPC(); initArRPC();
} }