2022-10-21 19:17:06 -04: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/>.
|
|
|
|
*/
|
|
|
|
|
2024-03-13 16:45:45 -04:00
|
|
|
import { SettingsStore as SettingsStoreClass } from "@shared/SettingsStore";
|
2023-04-06 20:27:18 -04:00
|
|
|
import { debounce } from "@utils/debounce";
|
|
|
|
import { localStorage } from "@utils/localStorage";
|
2023-05-05 19:36:00 -04:00
|
|
|
import { Logger } from "@utils/Logger";
|
2022-11-28 07:37:55 -05:00
|
|
|
import { mergeDefaults } from "@utils/misc";
|
2023-04-06 20:27:18 -04:00
|
|
|
import { putCloudSettings } from "@utils/settingsSync";
|
2023-01-13 17:15:45 -05:00
|
|
|
import { DefinedSettings, OptionType, SettingsChecks, SettingsDefinition } from "@utils/types";
|
2022-11-28 07:37:55 -05:00
|
|
|
import { React } from "@webpack/common";
|
2022-10-22 12:18:41 -04:00
|
|
|
|
2022-11-28 07:37:55 -05:00
|
|
|
import plugins from "~plugins";
|
2022-08-30 22:07:16 -04:00
|
|
|
|
2022-11-14 12:05:41 -05:00
|
|
|
const logger = new Logger("Settings");
|
2022-10-11 15:48:28 -04:00
|
|
|
export interface Settings {
|
2022-09-30 18:42:50 -04:00
|
|
|
notifyAboutUpdates: boolean;
|
2023-01-23 19:42:57 -05:00
|
|
|
autoUpdate: boolean;
|
2023-03-19 05:21:26 -04:00
|
|
|
autoUpdateNotification: boolean,
|
2022-09-03 11:49:16 -04:00
|
|
|
useQuickCss: boolean;
|
2022-10-11 15:48:28 -04:00
|
|
|
enableReactDevtools: boolean;
|
2022-11-30 21:01:44 -05:00
|
|
|
themeLinks: string[];
|
2023-08-04 13:52:20 -04:00
|
|
|
enabledThemes: string[];
|
2023-01-12 17:48:37 -05:00
|
|
|
frameless: boolean;
|
2023-01-28 17:54:38 -05:00
|
|
|
transparent: boolean;
|
2023-01-23 19:42:57 -05:00
|
|
|
winCtrlQ: boolean;
|
2023-12-06 18:30:41 -05:00
|
|
|
macosVibrancyStyle:
|
|
|
|
| "content"
|
|
|
|
| "fullscreen-ui"
|
|
|
|
| "header"
|
|
|
|
| "hud"
|
|
|
|
| "menu"
|
|
|
|
| "popover"
|
|
|
|
| "selection"
|
|
|
|
| "sidebar"
|
|
|
|
| "titlebar"
|
|
|
|
| "tooltip"
|
|
|
|
| "under-page"
|
|
|
|
| "window"
|
|
|
|
| undefined;
|
2023-04-08 21:41:55 -04:00
|
|
|
disableMinSize: boolean;
|
2023-02-28 16:17:39 -05:00
|
|
|
winNativeTitleBar: boolean;
|
2022-08-30 22:07:16 -04:00
|
|
|
plugins: {
|
|
|
|
[plugin: string]: {
|
|
|
|
enabled: boolean;
|
|
|
|
[setting: string]: any;
|
|
|
|
};
|
|
|
|
};
|
2023-02-10 16:33:34 -05:00
|
|
|
|
|
|
|
notifications: {
|
|
|
|
timeout: number;
|
|
|
|
position: "top-right" | "bottom-right";
|
|
|
|
useNative: "always" | "never" | "not-focused";
|
2023-03-31 20:47:49 -04:00
|
|
|
logLimit: number;
|
2023-02-10 16:33:34 -05:00
|
|
|
};
|
2023-04-06 20:27:18 -04:00
|
|
|
|
|
|
|
cloud: {
|
|
|
|
authenticated: boolean;
|
|
|
|
url: string;
|
|
|
|
settingsSync: boolean;
|
|
|
|
settingsSyncVersion: number;
|
|
|
|
};
|
2022-08-30 22:07:16 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
const DefaultSettings: Settings = {
|
2022-09-30 18:42:50 -04:00
|
|
|
notifyAboutUpdates: true,
|
2023-01-23 19:42:57 -05:00
|
|
|
autoUpdate: false,
|
2023-03-19 05:21:26 -04:00
|
|
|
autoUpdateNotification: true,
|
2022-09-03 11:49:16 -04:00
|
|
|
useQuickCss: true,
|
2022-11-30 21:01:44 -05:00
|
|
|
themeLinks: [],
|
2023-08-04 13:52:20 -04:00
|
|
|
enabledThemes: [],
|
2022-10-11 15:48:28 -04:00
|
|
|
enableReactDevtools: false,
|
2023-01-12 17:48:37 -05:00
|
|
|
frameless: false,
|
2023-01-28 17:54:38 -05:00
|
|
|
transparent: false,
|
2023-01-23 19:42:57 -05:00
|
|
|
winCtrlQ: false,
|
2023-12-06 18:30:41 -05:00
|
|
|
macosVibrancyStyle: undefined,
|
2023-04-08 21:41:55 -04:00
|
|
|
disableMinSize: false,
|
2023-02-28 16:17:39 -05:00
|
|
|
winNativeTitleBar: false,
|
2023-02-10 16:33:34 -05:00
|
|
|
plugins: {},
|
|
|
|
|
|
|
|
notifications: {
|
|
|
|
timeout: 5000,
|
|
|
|
position: "bottom-right",
|
2023-03-31 20:47:49 -04:00
|
|
|
useNative: "not-focused",
|
|
|
|
logLimit: 50
|
2023-04-06 20:27:18 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
cloud: {
|
|
|
|
authenticated: false,
|
|
|
|
url: "https://api.vencord.dev/",
|
|
|
|
settingsSync: false,
|
|
|
|
settingsSyncVersion: 0
|
2023-02-10 16:33:34 -05:00
|
|
|
}
|
2022-09-30 18:42:50 -04:00
|
|
|
};
|
2022-08-30 22:07:16 -04:00
|
|
|
|
2024-03-13 16:45:45 -04:00
|
|
|
const settings = VencordNative.settings.get();
|
|
|
|
mergeDefaults(settings, DefaultSettings);
|
2022-08-30 22:07:16 -04:00
|
|
|
|
2023-04-06 20:27:18 -04:00
|
|
|
const saveSettingsOnFrequentAction = debounce(async () => {
|
|
|
|
if (Settings.cloud.settingsSync && Settings.cloud.authenticated) {
|
|
|
|
await putCloudSettings();
|
|
|
|
delete localStorage.Vencord_settingsDirty;
|
|
|
|
}
|
|
|
|
}, 60_000);
|
|
|
|
|
2022-10-18 16:53:37 -04:00
|
|
|
|
2024-03-13 16:45:45 -04:00
|
|
|
export const SettingsStore = new SettingsStoreClass(settings, {
|
|
|
|
readOnly: true,
|
|
|
|
getDefaultValue({
|
|
|
|
target,
|
|
|
|
key,
|
|
|
|
path
|
|
|
|
}) {
|
|
|
|
const v = target[key];
|
|
|
|
if (!plugins) return v; // plugins not initialised yet. this means this path was reached by being called on the top level
|
|
|
|
|
|
|
|
if (path === "plugins" && key in plugins)
|
|
|
|
return target[key] = {
|
|
|
|
enabled: plugins[key].required ?? plugins[key].enabledByDefault ?? false
|
|
|
|
};
|
|
|
|
|
|
|
|
// 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?.[key];
|
|
|
|
if (!setting) return v;
|
|
|
|
|
|
|
|
if ("default" in setting)
|
|
|
|
// normal setting with a default value
|
|
|
|
return (target[key] = setting.default);
|
|
|
|
|
|
|
|
if (setting.type === OptionType.SELECT) {
|
|
|
|
const def = setting.options.find(o => o.default);
|
|
|
|
if (def)
|
|
|
|
target[key] = def.value;
|
|
|
|
return def?.value;
|
2022-09-03 11:49:16 -04:00
|
|
|
}
|
2022-08-30 22:07:16 -04:00
|
|
|
}
|
|
|
|
}
|
2024-03-13 16:45:45 -04:00
|
|
|
return v;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
SettingsStore.addGlobalChangeListener((_, path) => {
|
|
|
|
SettingsStore.plain.cloud.settingsSyncVersion = Date.now();
|
|
|
|
localStorage.Vencord_settingsDirty = true;
|
|
|
|
saveSettingsOnFrequentAction();
|
|
|
|
VencordNative.settings.set(SettingsStore.plain, path);
|
|
|
|
});
|
2022-08-30 22:07:16 -04:00
|
|
|
|
2022-10-13 18:36:44 -04: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 16:53:37 -04: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 18:36:44 -04:00
|
|
|
*/
|
|
|
|
export const PlainSettings = settings;
|
2022-08-30 22:07:16 -04:00
|
|
|
/**
|
|
|
|
* A smart settings object. Altering props automagically saves
|
|
|
|
* the updated settings to disk.
|
2022-10-13 18:36:44 -04:00
|
|
|
* This recursively proxies objects. If you need the object non proxied, use {@link PlainSettings}
|
2022-08-30 22:07:16 -04:00
|
|
|
*/
|
2024-03-13 16:45:45 -04:00
|
|
|
export const Settings = SettingsStore.store;
|
2022-10-18 16:53:37 -04:00
|
|
|
|
2022-08-30 22:07:16 -04:00
|
|
|
/**
|
|
|
|
* Settings hook for React components. Returns a smart settings
|
|
|
|
* object that automagically triggers a rerender if any properties
|
|
|
|
* are altered
|
2022-12-02 10:43:37 -05:00
|
|
|
* @param paths An optional list of paths to whitelist for rerenders
|
2022-08-30 22:07:16 -04:00
|
|
|
* @returns Settings
|
|
|
|
*/
|
2023-01-13 17:15:45 -05:00
|
|
|
// TODO: Representing paths as essentially "string[].join('.')" wont allow dots in paths, change to "paths?: string[][]" later
|
2023-03-04 12:40:37 -05:00
|
|
|
export function useSettings(paths?: UseSettings<Settings>[]) {
|
2022-09-30 18:42:50 -04:00
|
|
|
const [, forceUpdate] = React.useReducer(() => ({}), {});
|
2022-08-30 22:07:16 -04:00
|
|
|
|
|
|
|
React.useEffect(() => {
|
2024-03-13 16:45:45 -04:00
|
|
|
if (paths) {
|
|
|
|
paths.forEach(p => SettingsStore.addChangeListener(p, forceUpdate));
|
|
|
|
return () => paths.forEach(p => SettingsStore.removeChangeListener(p, forceUpdate));
|
|
|
|
} else {
|
|
|
|
SettingsStore.addGlobalChangeListener(forceUpdate);
|
|
|
|
return () => SettingsStore.removeGlobalChangeListener(forceUpdate);
|
|
|
|
}
|
2022-08-30 22:07:16 -04:00
|
|
|
}, []);
|
|
|
|
|
2024-03-13 16:45:45 -04:00
|
|
|
return SettingsStore.store;
|
2022-09-03 12:01:06 -04:00
|
|
|
}
|
2022-11-14 12:05:41 -05:00
|
|
|
|
|
|
|
export function migratePluginSettings(name: string, ...oldNames: string[]) {
|
2024-03-13 16:45:45 -04:00
|
|
|
const { plugins } = SettingsStore.plain;
|
2022-11-14 12:05:41 -05:00
|
|
|
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];
|
2024-03-13 16:45:45 -04:00
|
|
|
SettingsStore.markAsChanged();
|
2022-11-14 12:05:41 -05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-01-13 17:15:45 -05:00
|
|
|
|
2023-06-20 20:00:38 -04: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 17:15:45 -05: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 12:40:37 -05:00
|
|
|
settings?.map(name => `plugins.${definedSettings.pluginName}.${name}`) as UseSettings<Settings>[]
|
2023-01-13 17:15:45 -05:00
|
|
|
).plugins[definedSettings.pluginName] as any,
|
|
|
|
def,
|
2023-06-20 20:00:38 -04:00
|
|
|
checks: checks ?? {} as any,
|
2023-01-13 17:15:45 -05:00
|
|
|
pluginName: "",
|
2023-05-28 16:03:06 -04:00
|
|
|
|
2023-06-20 20:00:38 -04:00
|
|
|
withPrivateSettings<T extends object>() {
|
|
|
|
return this as DefinedSettings<Def, Checks, T>;
|
2023-05-28 16:03:06 -04:00
|
|
|
}
|
2023-01-13 17:15:45 -05:00
|
|
|
};
|
2023-05-28 16:03:06 -04:00
|
|
|
|
2023-01-13 17:15:45 -05:00
|
|
|
return definedSettings;
|
|
|
|
}
|
2023-03-04 12:40:37 -05: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;
|
|
|
|
};
|