From b2ecb02335fa51a7c7eadab0acb3beb91c289802 Mon Sep 17 00:00:00 2001 From: Ven Date: Tue, 24 Jan 2023 01:42:57 +0100 Subject: [PATCH] Make Windows Ctrl+Q feature optional; add opt-in auto update (#451) --- src/Vencord.ts | 26 ++++++++++-- src/api/settings.ts | 4 ++ src/components/VencordSettings/Updater.tsx | 22 +++++++++- src/components/VencordSettings/VencordTab.tsx | 21 ++++++---- src/patcher.ts | 42 ++++++++++--------- src/utils/updater.ts | 2 +- 6 files changed, 84 insertions(+), 33 deletions(-) diff --git a/src/Vencord.ts b/src/Vencord.ts index 48e628fde..82d5af0ad 100644 --- a/src/Vencord.ts +++ b/src/Vencord.ts @@ -30,7 +30,7 @@ import "./webpack/patchWebpack"; import { popNotice, showNotice } from "./api/Notices"; import { PlainSettings, Settings } from "./api/settings"; import { patches, PMLogger, startAllPlugins } from "./plugins"; -import { checkForUpdates, UpdateLogger } from "./utils/updater"; +import { checkForUpdates, rebuild, update, UpdateLogger } from "./utils/updater"; import { onceReady } from "./webpack"; import { Router } from "./webpack/common"; @@ -44,7 +44,27 @@ async function init() { if (!IS_WEB) { try { const isOutdated = await checkForUpdates(); - if (isOutdated && Settings.notifyAboutUpdates) + if (!isOutdated) return; + + if (Settings.autoUpdate) { + await update(); + const needsFullRestart = await rebuild(); + setTimeout(() => { + showNotice( + "Vencord has been updated!", + "Restart", + () => { + if (needsFullRestart) + window.DiscordNative.app.relaunch(); + else + location.reload(); + } + ); + }, 10_000); + return; + } + + if (Settings.notifyAboutUpdates) setTimeout(() => { showNotice( "A Vencord update is available!", @@ -54,7 +74,7 @@ async function init() { Router.open("VencordUpdater"); } ); - }, 10000); + }, 10_000); } catch (err) { UpdateLogger.error("Failed to check for updates", err); } diff --git a/src/api/settings.ts b/src/api/settings.ts index 9bae8b73b..4cdb24b67 100644 --- a/src/api/settings.ts +++ b/src/api/settings.ts @@ -27,10 +27,12 @@ import plugins from "~plugins"; const logger = new Logger("Settings"); export interface Settings { notifyAboutUpdates: boolean; + autoUpdate: boolean; useQuickCss: boolean; enableReactDevtools: boolean; themeLinks: string[]; frameless: boolean; + winCtrlQ: boolean; plugins: { [plugin: string]: { enabled: boolean; @@ -41,10 +43,12 @@ export interface Settings { const DefaultSettings: Settings = { notifyAboutUpdates: true, + autoUpdate: false, useQuickCss: true, themeLinks: [], enableReactDevtools: false, frameless: false, + winCtrlQ: false, plugins: {} }; diff --git a/src/components/VencordSettings/Updater.tsx b/src/components/VencordSettings/Updater.tsx index dea876676..8a126a8de 100644 --- a/src/components/VencordSettings/Updater.tsx +++ b/src/components/VencordSettings/Updater.tsx @@ -16,6 +16,7 @@ * along with this program. If not, see . */ +import { useSettings } from "@api/settings"; import ErrorBoundary from "@components/ErrorBoundary"; import { ErrorCard } from "@components/ErrorCard"; import { Flex } from "@components/Flex"; @@ -23,7 +24,7 @@ import { handleComponentFailed } from "@components/handleComponentFailed"; import { Link } from "@components/Link"; import { classes, useAwaiter } from "@utils/misc"; import { changes, checkForUpdates, getRepo, isNewer, rebuild, update, updateError, UpdateLogger } from "@utils/updater"; -import { Alerts, Button, Card, Forms, Margins, Parser, React, Toasts } from "@webpack/common"; +import { Alerts, Button, Card, Forms, Margins, Parser, React, Switch, Toasts } from "@webpack/common"; import gitHash from "~git-hash"; @@ -183,6 +184,8 @@ function Newer(props: CommonProps) { } function Updater() { + const settings = useSettings(["notifyAboutUpdates", "autoUpdate"]); + const [repo, err, repoPending] = useAwaiter(getRepo, { fallbackValue: "Loading..." }); React.useEffect(() => { @@ -197,6 +200,23 @@ function Updater() { return ( + Updater Settings + settings.notifyAboutUpdates = v} + note="Shows a toast on startup" + disabled={settings.autoUpdate} + > + Get notified about new updates + + settings.autoUpdate = v} + note="Automatically update Vencord without confirmation prompt" + > + Automatically update + + Repo {repoPending ? repo : err ? "Failed to retrieve - check console" : ( diff --git a/src/components/VencordSettings/VencordTab.tsx b/src/components/VencordSettings/VencordTab.tsx index 9429cddc5..5da444205 100644 --- a/src/components/VencordSettings/VencordTab.tsx +++ b/src/components/VencordSettings/VencordTab.tsx @@ -97,21 +97,26 @@ function VencordSettings() { settings.enableReactDevtools = v} - note="Requires a full restart"> + note="Requires a full restart" + > Enable React Developer Tools - settings.notifyAboutUpdates = v} - note="Shows a toast on startup"> - Get notified about new updates - settings.frameless = v} - note="Requires a full restart"> + note="Requires a full restart" + > Disable the window frame + {navigator.platform.toLowerCase().startsWith("win") && ( + settings.winCtrlQ = v} + note="Requires a full restart" + > + Register Ctrl+Q as shortcut to close Discord (Alternative to Alt+F4) + + )} )} diff --git a/src/patcher.ts b/src/patcher.ts index 82fc23336..2da404faa 100644 --- a/src/patcher.ts +++ b/src/patcher.ts @@ -43,33 +43,35 @@ require.main!.filename = join(asarPath, discordPkg.main); app.setAppPath(asarPath); if (!process.argv.includes("--vanilla")) { + let settings: typeof import("@api/settings").Settings = {} as any; + try { + settings = JSON.parse(readSettings()); + } catch { } + // Repatch after host updates on Windows if (process.platform === "win32") { require("./patchWin32Updater"); - const originalBuild = Menu.buildFromTemplate; - Menu.buildFromTemplate = function (template) { - if (template[0]?.label === "&File") { - const { submenu } = template[0]; - if (Array.isArray(submenu)) { - submenu.push({ - label: "Quit (Hidden)", - visible: false, - acceleratorWorksWhenHidden: true, - accelerator: "Control+Q", - click: () => app.quit() - }); + if (settings.winCtrlQ) { + const originalBuild = Menu.buildFromTemplate; + Menu.buildFromTemplate = function (template) { + if (template[0]?.label === "&File") { + const { submenu } = template[0]; + if (Array.isArray(submenu)) { + submenu.push({ + label: "Quit (Hidden)", + visible: false, + acceleratorWorksWhenHidden: true, + accelerator: "Control+Q", + click: () => app.quit() + }); + } } - } - return originalBuild.call(this, template); - }; + return originalBuild.call(this, template); + }; + } } - let settings = {} as any; - try { - settings = JSON.parse(readSettings()); - } catch { } - class BrowserWindow extends electron.BrowserWindow { constructor(options: BrowserWindowConstructorOptions) { if (options?.webPreferences?.preload && options.title) { diff --git a/src/utils/updater.ts b/src/utils/updater.ts index 04205a556..9fdec471e 100644 --- a/src/utils/updater.ts +++ b/src/utils/updater.ts @@ -22,7 +22,7 @@ import IpcEvents from "./IpcEvents"; import Logger from "./Logger"; import { IpcRes } from "./types"; -export const UpdateLogger = new Logger("Updater", "white"); +export const UpdateLogger = /* #__PURE__*/ new Logger("Updater", "white"); export let isOutdated = false; export let isNewer = false; export let updateError: any;