mirror of
https://github.com/Equicord/Equicord.git
synced 2025-06-15 01:23:03 -04:00
Add QuickCss toggle; add settings listener api
This commit is contained in:
parent
8a8c6a4b52
commit
113f47ca7f
4 changed files with 56 additions and 14 deletions
|
@ -5,6 +5,7 @@ import { mergeDefaults } from '../utils/misc';
|
|||
|
||||
interface Settings {
|
||||
unsafeRequire: boolean;
|
||||
useQuickCss: boolean;
|
||||
plugins: {
|
||||
[plugin: string]: {
|
||||
enabled: boolean;
|
||||
|
@ -15,8 +16,9 @@ interface Settings {
|
|||
|
||||
const DefaultSettings: Settings = {
|
||||
unsafeRequire: false,
|
||||
useQuickCss: true,
|
||||
plugins: {}
|
||||
};
|
||||
} as any;
|
||||
|
||||
for (const plugin in plugins) {
|
||||
DefaultSettings.plugins[plugin] = {
|
||||
|
@ -35,21 +37,26 @@ try {
|
|||
var settings = mergeDefaults({} as Settings, DefaultSettings);
|
||||
}
|
||||
|
||||
const subscriptions = new Set<() => void>();
|
||||
type SubscriptionCallback = ((newValue: any) => void) & { _path?: string; };
|
||||
const subscriptions = new Set<SubscriptionCallback>();
|
||||
|
||||
function makeProxy(settings: Settings, root = settings): Settings {
|
||||
function makeProxy(settings: Settings, root = settings, path = ""): Settings {
|
||||
return new Proxy(settings, {
|
||||
get(target, p) {
|
||||
get(target, p: string) {
|
||||
const v = target[p];
|
||||
if (typeof v === "object" && !Array.isArray(v)) return makeProxy(v, root);
|
||||
if (typeof v === "object" && !Array.isArray(v))
|
||||
return makeProxy(v, root, `${path}${path && "."}${p}`);
|
||||
return v;
|
||||
},
|
||||
set(target, p, v) {
|
||||
set(target, p: string, v) {
|
||||
if (target[p] === v) return true;
|
||||
|
||||
target[p] = v;
|
||||
const setPath = `${path}${path && "."}${p}`;
|
||||
for (const subscription of subscriptions) {
|
||||
subscription();
|
||||
if (!subscription._path || subscription._path === setPath) {
|
||||
subscription(v);
|
||||
}
|
||||
}
|
||||
VencordNative.ipc.invoke(IpcEvents.SET_SETTINGS, JSON.stringify(root, null, 4));
|
||||
return true;
|
||||
|
@ -78,4 +85,16 @@ export function useSettings() {
|
|||
}, []);
|
||||
|
||||
return Settings;
|
||||
}
|
||||
|
||||
// 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;
|
||||
|
||||
export function addSettingsListener<Path extends keyof Settings>(path: Path, onUpdate: (newValue: Settings[Path]) => void): void;
|
||||
export function addSettingsListener<Path extends string>(path: Path, onUpdate: (newValue: ResolvePropDeep<Settings, Path>) => void): void;
|
||||
export function addSettingsListener(path: string, onUpdate: (newValue: any) => void) {
|
||||
(onUpdate as SubscriptionCallback)._path = path;
|
||||
subscriptions.add(onUpdate);
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue