Make Windows Ctrl+Q feature optional; add opt-in auto update (#451)

This commit is contained in:
Ven 2023-01-24 01:42:57 +01:00 committed by GitHub
parent 25d32ce292
commit b2ecb02335
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 84 additions and 33 deletions

View file

@ -30,7 +30,7 @@ import "./webpack/patchWebpack";
import { popNotice, showNotice } from "./api/Notices"; import { popNotice, showNotice } from "./api/Notices";
import { PlainSettings, Settings } from "./api/settings"; import { PlainSettings, Settings } from "./api/settings";
import { patches, PMLogger, startAllPlugins } from "./plugins"; 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 { onceReady } from "./webpack";
import { Router } from "./webpack/common"; import { Router } from "./webpack/common";
@ -44,7 +44,27 @@ async function init() {
if (!IS_WEB) { if (!IS_WEB) {
try { try {
const isOutdated = await checkForUpdates(); 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(() => { setTimeout(() => {
showNotice( showNotice(
"A Vencord update is available!", "A Vencord update is available!",
@ -54,7 +74,7 @@ async function init() {
Router.open("VencordUpdater"); Router.open("VencordUpdater");
} }
); );
}, 10000); }, 10_000);
} catch (err) { } catch (err) {
UpdateLogger.error("Failed to check for updates", err); UpdateLogger.error("Failed to check for updates", err);
} }

View file

@ -27,10 +27,12 @@ import plugins from "~plugins";
const logger = new Logger("Settings"); const logger = new Logger("Settings");
export interface Settings { export interface Settings {
notifyAboutUpdates: boolean; notifyAboutUpdates: boolean;
autoUpdate: boolean;
useQuickCss: boolean; useQuickCss: boolean;
enableReactDevtools: boolean; enableReactDevtools: boolean;
themeLinks: string[]; themeLinks: string[];
frameless: boolean; frameless: boolean;
winCtrlQ: boolean;
plugins: { plugins: {
[plugin: string]: { [plugin: string]: {
enabled: boolean; enabled: boolean;
@ -41,10 +43,12 @@ export interface Settings {
const DefaultSettings: Settings = { const DefaultSettings: Settings = {
notifyAboutUpdates: true, notifyAboutUpdates: true,
autoUpdate: false,
useQuickCss: true, useQuickCss: true,
themeLinks: [], themeLinks: [],
enableReactDevtools: false, enableReactDevtools: false,
frameless: false, frameless: false,
winCtrlQ: false,
plugins: {} plugins: {}
}; };

View file

@ -16,6 +16,7 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>. * along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
import { useSettings } from "@api/settings";
import ErrorBoundary from "@components/ErrorBoundary"; import ErrorBoundary from "@components/ErrorBoundary";
import { ErrorCard } from "@components/ErrorCard"; import { ErrorCard } from "@components/ErrorCard";
import { Flex } from "@components/Flex"; import { Flex } from "@components/Flex";
@ -23,7 +24,7 @@ import { handleComponentFailed } from "@components/handleComponentFailed";
import { Link } from "@components/Link"; import { Link } from "@components/Link";
import { classes, useAwaiter } from "@utils/misc"; import { classes, useAwaiter } from "@utils/misc";
import { changes, checkForUpdates, getRepo, isNewer, rebuild, update, updateError, UpdateLogger } from "@utils/updater"; 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"; import gitHash from "~git-hash";
@ -183,6 +184,8 @@ function Newer(props: CommonProps) {
} }
function Updater() { function Updater() {
const settings = useSettings(["notifyAboutUpdates", "autoUpdate"]);
const [repo, err, repoPending] = useAwaiter(getRepo, { fallbackValue: "Loading..." }); const [repo, err, repoPending] = useAwaiter(getRepo, { fallbackValue: "Loading..." });
React.useEffect(() => { React.useEffect(() => {
@ -197,6 +200,23 @@ function Updater() {
return ( return (
<Forms.FormSection> <Forms.FormSection>
<Forms.FormTitle tag="h5">Updater Settings</Forms.FormTitle>
<Switch
value={settings.notifyAboutUpdates}
onChange={(v: boolean) => settings.notifyAboutUpdates = v}
note="Shows a toast on startup"
disabled={settings.autoUpdate}
>
Get notified about new updates
</Switch>
<Switch
value={settings.autoUpdate}
onChange={(v: boolean) => settings.autoUpdate = v}
note="Automatically update Vencord without confirmation prompt"
>
Automatically update
</Switch>
<Forms.FormTitle tag="h5">Repo</Forms.FormTitle> <Forms.FormTitle tag="h5">Repo</Forms.FormTitle>
<Forms.FormText>{repoPending ? repo : err ? "Failed to retrieve - check console" : ( <Forms.FormText>{repoPending ? repo : err ? "Failed to retrieve - check console" : (

View file

@ -97,21 +97,26 @@ function VencordSettings() {
<Switch <Switch
value={settings.enableReactDevtools} value={settings.enableReactDevtools}
onChange={(v: boolean) => settings.enableReactDevtools = v} onChange={(v: boolean) => settings.enableReactDevtools = v}
note="Requires a full restart"> note="Requires a full restart"
>
Enable React Developer Tools Enable React Developer Tools
</Switch> </Switch>
<Switch
value={settings.notifyAboutUpdates}
onChange={(v: boolean) => settings.notifyAboutUpdates = v}
note="Shows a toast on startup">
Get notified about new updates
</Switch>
<Switch <Switch
value={settings.frameless} value={settings.frameless}
onChange={(v: boolean) => settings.frameless = v} onChange={(v: boolean) => settings.frameless = v}
note="Requires a full restart"> note="Requires a full restart"
>
Disable the window frame Disable the window frame
</Switch> </Switch>
{navigator.platform.toLowerCase().startsWith("win") && (
<Switch
value={settings.winCtrlQ}
onChange={(v: boolean) => settings.winCtrlQ = v}
note="Requires a full restart"
>
Register Ctrl+Q as shortcut to close Discord (Alternative to Alt+F4)
</Switch>
)}
</React.Fragment> </React.Fragment>
)} )}

View file

@ -43,10 +43,16 @@ require.main!.filename = join(asarPath, discordPkg.main);
app.setAppPath(asarPath); app.setAppPath(asarPath);
if (!process.argv.includes("--vanilla")) { 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 // Repatch after host updates on Windows
if (process.platform === "win32") { if (process.platform === "win32") {
require("./patchWin32Updater"); require("./patchWin32Updater");
if (settings.winCtrlQ) {
const originalBuild = Menu.buildFromTemplate; const originalBuild = Menu.buildFromTemplate;
Menu.buildFromTemplate = function (template) { Menu.buildFromTemplate = function (template) {
if (template[0]?.label === "&File") { if (template[0]?.label === "&File") {
@ -64,11 +70,7 @@ if (!process.argv.includes("--vanilla")) {
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 { class BrowserWindow extends electron.BrowserWindow {
constructor(options: BrowserWindowConstructorOptions) { constructor(options: BrowserWindowConstructorOptions) {

View file

@ -22,7 +22,7 @@ import IpcEvents from "./IpcEvents";
import Logger from "./Logger"; import Logger from "./Logger";
import { IpcRes } from "./types"; 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 isOutdated = false;
export let isNewer = false; export let isNewer = false;
export let updateError: any; export let updateError: any;