2022-10-21 23:17:06 +00:00
|
|
|
/*
|
|
|
|
* Vencord, a modification for Discord's desktop app
|
|
|
|
* Copyright (c) 2022 Vendicated and contributors
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2023-04-07 00:27:18 +00:00
|
|
|
import { debounce } from "@utils/debounce";
|
|
|
|
import { localStorage } from "@utils/localStorage";
|
2023-05-05 23:36:00 +00:00
|
|
|
import { Logger } from "@utils/Logger";
|
2022-11-28 12:37:55 +00:00
|
|
|
import { mergeDefaults } from "@utils/misc";
|
2023-04-07 00:27:18 +00:00
|
|
|
import { putCloudSettings } from "@utils/settingsSync";
|
2023-01-13 22:15:45 +00:00
|
|
|
import { DefinedSettings, OptionType, SettingsChecks, SettingsDefinition } from "@utils/types";
|
2022-11-28 12:37:55 +00:00
|
|
|
import { React } from "@webpack/common";
|
2022-10-22 16:18:41 +00:00
|
|
|
|
2022-11-28 12:37:55 +00:00
|
|
|
import plugins from "~plugins";
|
2022-08-31 02:07:16 +00:00
|
|
|
|
2022-11-14 17:05:41 +00:00
|
|
|
const logger = new Logger("Settings");
|
2022-10-11 19:48:28 +00:00
|
|
|
export interface Settings {
|
2022-09-30 22:42:50 +00:00
|
|
|
notifyAboutUpdates: boolean;
|
2023-01-24 00:42:57 +00:00
|
|
|
autoUpdate: boolean;
|
2023-03-19 09:21:26 +00:00
|
|
|
autoUpdateNotification: boolean,
|
2022-09-03 15:49:16 +00:00
|
|
|
useQuickCss: boolean;
|
2022-10-11 19:48:28 +00:00
|
|
|
enableReactDevtools: boolean;
|
2022-12-01 02:01:44 +00:00
|
|
|
themeLinks: string[];
|
2023-08-04 17:52:20 +00:00
|
|
|
enabledThemes: string[];
|
2023-01-12 22:48:37 +00:00
|
|
|
frameless: boolean;
|
2023-01-28 22:54:38 +00:00
|
|
|
transparent: boolean;
|
2023-01-24 00:42:57 +00:00
|
|
|
winCtrlQ: boolean;
|
2023-12-06 23:30:41 +00:00
|
|
|
macosVibrancyStyle:
|
|
|
|
| "content"
|
|
|
|
| "fullscreen-ui"
|
|
|
|
| "header"
|
|
|
|
| "hud"
|
|
|
|
| "menu"
|
|
|
|
| "popover"
|
|
|
|
| "selection"
|
|
|
|
| "sidebar"
|
|
|
|
| "titlebar"
|
|
|
|
| "tooltip"
|
|
|
|
| "under-page"
|
|
|
|
| "window"
|
|
|
|
| undefined;
|
|
|
|
macosTranslucency: boolean | undefined;
|
2023-04-09 01:41:55 +00:00
|
|
|
disableMinSize: boolean;
|
2023-02-28 21:17:39 +00:00
|
|
|
winNativeTitleBar: boolean;
|
2022-08-31 02:07:16 +00:00
|
|
|
plugins: {
|
|
|
|
[plugin: string]: {
|
|
|
|
enabled: boolean;
|
|
|
|
[setting: string]: any;
|
|
|
|
};
|
|
|
|
};
|
2023-02-10 21:33:34 +00:00
|
|
|
|
|
|
|
notifications: {
|
|
|
|
timeout: number;
|
|
|
|
position: "top-right" | "bottom-right";
|
|
|
|
useNative: "always" | "never" | "not-focused";
|
2023-04-01 00:47:49 +00:00
|
|
|
logLimit: number;
|
2023-02-10 21:33:34 +00:00
|
|
|
};
|
2023-04-07 00:27:18 +00:00
|
|
|
|
|
|
|
cloud: {
|
|
|
|
authenticated: boolean;
|
|
|
|
url: string;
|
|
|
|
settingsSync: boolean;
|
|
|
|
settingsSyncVersion: number;
|
|
|
|
};
|
2022-08-31 02:07:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const DefaultSettings: Settings = {
|
2022-09-30 22:42:50 +00:00
|
|
|
notifyAboutUpdates: true,
|
2023-01-24 00:42:57 +00:00
|
|
|
autoUpdate: false,
|
2023-03-19 09:21:26 +00:00
|
|
|
autoUpdateNotification: true,
|
2022-09-03 15:49:16 +00:00
|
|
|
useQuickCss: true,
|
2022-12-01 02:01:44 +00:00
|
|
|
themeLinks: [],
|
2023-08-04 17:52:20 +00:00
|
|
|
enabledThemes: [],
|
2022-10-11 19:48:28 +00:00
|
|
|
enableReactDevtools: false,
|
2023-01-12 22:48:37 +00:00
|
|
|
frameless: false,
|
2023-01-28 22:54:38 +00:00
|
|
|
transparent: false,
|
2023-01-24 00:42:57 +00:00
|
|
|
winCtrlQ: false,
|
2023-12-06 23:30:41 +00:00
|
|
|
// Replaced by macosVibrancyStyle
|
|
|
|
macosTranslucency: undefined,
|
|
|
|
macosVibrancyStyle: undefined,
|
2023-04-09 01:41:55 +00:00
|
|
|
disableMinSize: false,
|
2023-02-28 21:17:39 +00:00
|
|
|
winNativeTitleBar: false,
|
2023-02-10 21:33:34 +00:00
|
|
|
plugins: {},
|
|
|
|
|
|
|
|
notifications: {
|
|
|
|
timeout: 5000,
|
|
|
|
position: "bottom-right",
|
2023-04-01 00:47:49 +00:00
|
|
|
useNative: "not-focused",
|
|
|
|
logLimit: 50
|
2023-04-07 00:27:18 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
cloud: {
|
|
|
|
authenticated: false,
|
|
|
|
url: "https://api.vencord.dev/",
|
|
|
|
settingsSync: false,
|
|
|
|
settingsSyncVersion: 0
|
2023-02-10 21:33:34 +00:00
|
|
|
}
|
2022-09-30 22:42:50 +00:00
|
|
|
};
|
2022-08-31 02:07:16 +00:00
|
|
|
|
|
|
|
try {
|
2023-05-02 00:50:51 +00:00
|
|
|
var settings = JSON.parse(VencordNative.settings.get()) as Settings;
|
2022-08-31 02:07:16 +00:00
|
|
|
mergeDefaults(settings, DefaultSettings);
|
|
|
|
} catch (err) {
|
|
|
|
var settings = mergeDefaults({} as Settings, DefaultSettings);
|
2022-11-28 14:44:53 +00:00
|
|
|
logger.error("An error occurred while loading the settings. Corrupt settings file?\n", err);
|
2022-08-31 02:07:16 +00:00
|
|
|
}
|
|
|
|
|
2023-04-07 00:27:18 +00:00
|
|
|
const saveSettingsOnFrequentAction = debounce(async () => {
|
|
|
|
if (Settings.cloud.settingsSync && Settings.cloud.authenticated) {
|
|
|
|
await putCloudSettings();
|
|
|
|
delete localStorage.Vencord_settingsDirty;
|
|
|
|
}
|
|
|
|
}, 60_000);
|
|
|
|
|
2023-08-04 17:52:20 +00:00
|
|
|
type SubscriptionCallback = ((newValue: any, path: string) => void) & { _paths?: Array<string>; };
|
2022-09-03 15:49:16 +00:00
|
|
|
const subscriptions = new Set<SubscriptionCallback>();
|
2022-08-31 02:07:16 +00:00
|
|
|
|
2022-11-28 14:44:53 +00:00
|
|
|
const proxyCache = {} as Record<string, any>;
|
|
|
|
|
2022-10-18 20:53:37 +00:00
|
|
|
// Wraps the passed settings object in a Proxy to nicely handle change listeners and default values
|
2022-10-29 18:45:31 +00:00
|
|
|
function makeProxy(settings: any, root = settings, path = ""): Settings {
|
2022-11-28 14:44:53 +00:00
|
|
|
return proxyCache[path] ??= new Proxy(settings, {
|
2022-09-03 15:49:16 +00:00
|
|
|
get(target, p: string) {
|
2022-08-31 02:07:16 +00:00
|
|
|
const v = target[p];
|
2022-10-18 20:53:37 +00:00
|
|
|
|
|
|
|
// using "in" is important in the following cases to properly handle falsy or nullish values
|
|
|
|
if (!(p in target)) {
|
2022-10-29 18:45:31 +00:00
|
|
|
// Return empty for plugins with no settings
|
|
|
|
if (path === "plugins" && p in plugins)
|
|
|
|
return target[p] = makeProxy({
|
2023-03-01 04:26:13 +00:00
|
|
|
enabled: plugins[p].required ?? plugins[p].enabledByDefault ?? false
|
2022-11-28 14:44:53 +00:00
|
|
|
}, root, `plugins.${p}`);
|
2022-10-29 18:45:31 +00:00
|
|
|
|
2022-10-18 20:53:37 +00:00
|
|
|
// Since the property is not set, check if this is a plugin's setting and if so, try to resolve
|
|
|
|
// the default value.
|
|
|
|
if (path.startsWith("plugins.")) {
|
|
|
|
const plugin = path.slice("plugins.".length);
|
|
|
|
if (plugin in plugins) {
|
|
|
|
const setting = plugins[plugin].options?.[p];
|
|
|
|
if (!setting) return v;
|
|
|
|
if ("default" in setting)
|
|
|
|
// normal setting with a default value
|
2022-10-26 12:28:27 +00:00
|
|
|
return (target[p] = setting.default);
|
|
|
|
if (setting.type === OptionType.SELECT) {
|
|
|
|
const def = setting.options.find(o => o.default);
|
|
|
|
if (def)
|
|
|
|
target[p] = def.value;
|
|
|
|
return def?.value;
|
|
|
|
}
|
2022-10-18 20:53:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Recursively proxy Objects with the updated property path
|
2022-10-13 22:36:44 +00:00
|
|
|
if (typeof v === "object" && !Array.isArray(v) && v !== null)
|
2022-09-03 15:49:16 +00:00
|
|
|
return makeProxy(v, root, `${path}${path && "."}${p}`);
|
2022-10-18 20:53:37 +00:00
|
|
|
|
|
|
|
// primitive or similar, no need to proxy further
|
2022-08-31 02:07:16 +00:00
|
|
|
return v;
|
|
|
|
},
|
2022-10-18 20:53:37 +00:00
|
|
|
|
2022-09-03 15:49:16 +00:00
|
|
|
set(target, p: string, v) {
|
2022-10-18 20:53:37 +00:00
|
|
|
// avoid unnecessary updates to React Components and other listeners
|
2022-08-31 02:07:16 +00:00
|
|
|
if (target[p] === v) return true;
|
|
|
|
|
|
|
|
target[p] = v;
|
2022-10-18 20:53:37 +00:00
|
|
|
// Call any listeners that are listening to a setting of this path
|
2022-09-03 15:49:16 +00:00
|
|
|
const setPath = `${path}${path && "."}${p}`;
|
2023-03-28 18:26:57 +00:00
|
|
|
delete proxyCache[setPath];
|
2022-08-31 02:07:16 +00:00
|
|
|
for (const subscription of subscriptions) {
|
2023-08-04 17:52:20 +00:00
|
|
|
if (!subscription._paths || subscription._paths.includes(setPath)) {
|
2022-09-03 16:01:06 +00:00
|
|
|
subscription(v, setPath);
|
2022-09-03 15:49:16 +00:00
|
|
|
}
|
2022-08-31 02:07:16 +00:00
|
|
|
}
|
2022-10-18 20:53:37 +00:00
|
|
|
// And don't forget to persist the settings!
|
2023-04-07 00:27:18 +00:00
|
|
|
PlainSettings.cloud.settingsSyncVersion = Date.now();
|
|
|
|
localStorage.Vencord_settingsDirty = true;
|
|
|
|
saveSettingsOnFrequentAction();
|
2023-05-02 00:50:51 +00:00
|
|
|
VencordNative.settings.set(JSON.stringify(root, null, 4));
|
2022-08-31 02:07:16 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-10-13 22:36:44 +00:00
|
|
|
/**
|
|
|
|
* Same as {@link Settings} but unproxied. You should treat this as readonly,
|
|
|
|
* as modifying properties on this will not save to disk or call settings
|
|
|
|
* listeners.
|
2022-10-18 20:53:37 +00:00
|
|
|
* WARNING: default values specified in plugin.options will not be ensured here. In other words,
|
|
|
|
* settings for which you specified a default value may be uninitialised. If you need proper
|
|
|
|
* handling for default values, use {@link Settings}
|
2022-10-13 22:36:44 +00:00
|
|
|
*/
|
|
|
|
export const PlainSettings = settings;
|
2022-08-31 02:07:16 +00:00
|
|
|
/**
|
|
|
|
* A smart settings object. Altering props automagically saves
|
|
|
|
* the updated settings to disk.
|
2022-10-13 22:36:44 +00:00
|
|
|
* This recursively proxies objects. If you need the object non proxied, use {@link PlainSettings}
|
2022-08-31 02:07:16 +00:00
|
|
|
*/
|
|
|
|
export const Settings = makeProxy(settings);
|
2022-10-18 20:53:37 +00:00
|
|
|
|
2022-08-31 02:07:16 +00:00
|
|
|
/**
|
|
|
|
* Settings hook for React components. Returns a smart settings
|
|
|
|
* object that automagically triggers a rerender if any properties
|
|
|
|
* are altered
|
2022-12-02 15:43:37 +00:00
|
|
|
* @param paths An optional list of paths to whitelist for rerenders
|
2022-08-31 02:07:16 +00:00
|
|
|
* @returns Settings
|
|
|
|
*/
|
2023-01-13 22:15:45 +00:00
|
|
|
// TODO: Representing paths as essentially "string[].join('.')" wont allow dots in paths, change to "paths?: string[][]" later
|
2023-03-04 17:40:37 +00:00
|
|
|
export function useSettings(paths?: UseSettings<Settings>[]) {
|
2022-09-30 22:42:50 +00:00
|
|
|
const [, forceUpdate] = React.useReducer(() => ({}), {});
|
2022-08-31 02:07:16 +00:00
|
|
|
|
2024-03-07 16:06:08 +00:00
|
|
|
if (paths) {
|
|
|
|
(forceUpdate as SubscriptionCallback)._paths = paths;
|
|
|
|
}
|
2022-12-02 15:43:37 +00:00
|
|
|
|
2022-08-31 02:07:16 +00:00
|
|
|
React.useEffect(() => {
|
2024-03-07 16:06:08 +00:00
|
|
|
subscriptions.add(forceUpdate);
|
|
|
|
return () => void subscriptions.delete(forceUpdate);
|
2022-08-31 02:07:16 +00:00
|
|
|
}, []);
|
|
|
|
|
|
|
|
return Settings;
|
2022-09-03 15:49:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Resolves a possibly nested prop in the form of "some.nested.prop" to type of T.some.nested.prop
|
|
|
|
type ResolvePropDeep<T, P> = P extends "" ? T :
|
|
|
|
P extends `${infer Pre}.${infer Suf}` ?
|
|
|
|
Pre extends keyof T ? ResolvePropDeep<T[Pre], Suf> : never : P extends keyof T ? T[P] : never;
|
|
|
|
|
2022-09-03 16:01:06 +00:00
|
|
|
/**
|
|
|
|
* Add a settings listener that will be invoked whenever the desired setting is updated
|
|
|
|
* @param path Path to the setting that you want to watch, for example "plugins.Unindent.enabled" will fire your callback
|
|
|
|
* whenever Unindent is toggled. Pass an empty string to get notified for all changes
|
|
|
|
* @param onUpdate Callback function whenever a setting matching path is updated. It gets passed the new value and the path
|
|
|
|
* to the updated setting. This path will be the same as your path argument, unless it was an empty string.
|
2022-09-16 20:59:34 +00:00
|
|
|
*
|
2022-09-03 16:01:06 +00:00
|
|
|
* @example addSettingsListener("", (newValue, path) => console.log(`${path} is now ${newValue}`))
|
|
|
|
* addSettingsListener("plugins.Unindent.enabled", v => console.log("Unindent is now", v ? "enabled" : "disabled"))
|
|
|
|
*/
|
|
|
|
export function addSettingsListener<Path extends keyof Settings>(path: Path, onUpdate: (newValue: Settings[Path], path: Path) => void): void;
|
|
|
|
export function addSettingsListener<Path extends string>(path: Path, onUpdate: (newValue: Path extends "" ? any : ResolvePropDeep<Settings, Path>, path: Path extends "" ? string : Path) => void): void;
|
|
|
|
export function addSettingsListener(path: string, onUpdate: (newValue: any, path: string) => void) {
|
2024-03-07 16:06:08 +00:00
|
|
|
if (path) {
|
2023-10-09 01:01:35 +00:00
|
|
|
((onUpdate as SubscriptionCallback)._paths ??= []).push(path);
|
2024-03-07 16:06:08 +00:00
|
|
|
}
|
|
|
|
|
2022-09-03 15:49:16 +00:00
|
|
|
subscriptions.add(onUpdate);
|
2022-09-03 16:01:06 +00:00
|
|
|
}
|
2022-11-14 17:05:41 +00:00
|
|
|
|
|
|
|
export function migratePluginSettings(name: string, ...oldNames: string[]) {
|
|
|
|
const { plugins } = settings;
|
|
|
|
if (name in plugins) return;
|
|
|
|
|
|
|
|
for (const oldName of oldNames) {
|
|
|
|
if (oldName in plugins) {
|
|
|
|
logger.info(`Migrating settings from old name ${oldName} to ${name}`);
|
|
|
|
plugins[name] = plugins[oldName];
|
|
|
|
delete plugins[oldName];
|
2023-05-02 00:50:51 +00:00
|
|
|
VencordNative.settings.set(JSON.stringify(settings, null, 4));
|
2022-11-14 17:05:41 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-01-13 22:15:45 +00:00
|
|
|
|
2023-06-21 00:00:38 +00:00
|
|
|
export function definePluginSettings<
|
|
|
|
Def extends SettingsDefinition,
|
|
|
|
Checks extends SettingsChecks<Def>,
|
|
|
|
PrivateSettings extends object = {}
|
|
|
|
>(def: Def, checks?: Checks) {
|
|
|
|
const definedSettings: DefinedSettings<Def, Checks, PrivateSettings> = {
|
2023-01-13 22:15:45 +00:00
|
|
|
get store() {
|
|
|
|
if (!definedSettings.pluginName) throw new Error("Cannot access settings before plugin is initialized");
|
|
|
|
return Settings.plugins[definedSettings.pluginName] as any;
|
|
|
|
},
|
|
|
|
use: settings => useSettings(
|
2023-03-04 17:40:37 +00:00
|
|
|
settings?.map(name => `plugins.${definedSettings.pluginName}.${name}`) as UseSettings<Settings>[]
|
2023-01-13 22:15:45 +00:00
|
|
|
).plugins[definedSettings.pluginName] as any,
|
|
|
|
def,
|
2023-06-21 00:00:38 +00:00
|
|
|
checks: checks ?? {} as any,
|
2023-01-13 22:15:45 +00:00
|
|
|
pluginName: "",
|
2023-05-28 20:03:06 +00:00
|
|
|
|
2023-06-21 00:00:38 +00:00
|
|
|
withPrivateSettings<T extends object>() {
|
|
|
|
return this as DefinedSettings<Def, Checks, T>;
|
2023-05-28 20:03:06 +00:00
|
|
|
}
|
2023-01-13 22:15:45 +00:00
|
|
|
};
|
2023-05-28 20:03:06 +00:00
|
|
|
|
2023-01-13 22:15:45 +00:00
|
|
|
return definedSettings;
|
|
|
|
}
|
2023-03-04 17:40:37 +00:00
|
|
|
|
|
|
|
type UseSettings<T extends object> = ResolveUseSettings<T>[keyof T];
|
|
|
|
|
|
|
|
type ResolveUseSettings<T extends object> = {
|
|
|
|
[Key in keyof T]:
|
|
|
|
Key extends string
|
|
|
|
? T[Key] extends Record<string, unknown>
|
|
|
|
// @ts-ignore "Type instantiation is excessively deep and possibly infinite"
|
|
|
|
? UseSettings<T[Key]> extends string ? `${Key}.${UseSettings<T[Key]>}` : never
|
|
|
|
: Key
|
|
|
|
: never;
|
|
|
|
};
|