[PCCompat > Settings] Initial Add

This commit is contained in:
Ducko 2021-04-06 17:21:58 +01:00 committed by Keanu
parent f39dab4f40
commit 68c7f13f94
Signed by: keanucode
GPG Key ID: A7431C0D513CA93B
2 changed files with 156 additions and 2 deletions

View File

@ -0,0 +1,65 @@
const { React } = goosemodScope.webpackModules.common;
const OriginalTextInput = goosemodScope.webpackModules.findByDisplayName('TextInput');
const OriginalFormItem = goosemodScope.webpackModules.findByDisplayName('FormItem');
const OriginalFormText = goosemodScope.webpackModules.findByDisplayName('FormText');
const Flex = goosemodScope.webpackModules.findByDisplayName('Flex');
const Margins = goosemodScope.webpackModules.findByProps('marginTop20', 'marginBottom20');
const FormClasses = goosemodScope.webpackModules.findByProps('formText', 'description');
const FormDivider = goosemodScope.webpackModules.findByDisplayName('FormDivider');
const SettingsFormClasses = goosemodScope.webpackModules.findByProps('dividerDefault', 'titleDefault');
class Divider extends React.PureComponent {
render() {
return React.createElement(FormDivider, {
className: SettingsFormClasses.dividerDefault
});
}
}
class FormItem extends React.PureComponent {
render() {
return React.createElement(OriginalFormItem, {
title: this.props.title,
required: this.props.required,
className: [Flex.Direction.VERTICAL, Flex.Justify.START, Flex.Align.STRETCH, Flex.Wrap.NO_WRAP, Margins.marginBottom20].join(' ')
},
this.props.children,
this.props.note && React.createElement(OriginalFormText, {
className: FormClasses.description + (this.props.noteHasMargin ? (' ' + Margins.marginTop8) : '')
}, this.props.note),
React.createElement(Divider)
);
}
}
class TextInput extends React.PureComponent {
render() {
const title = this.props.children;
delete this.props.children;
return React.createElement(FormItem, {
title,
note: this.props.note,
required: this.props.required,
noteHasMargin: true
},
React.createElement(OriginalTextInput, {
...this.props
})
);
}
}
module.exports = {
FormItem,
TextInput
};

View File

@ -46,19 +46,108 @@ export const powercord = {
goosemodScope.showToast(content);
}
},
settings: {
registerSettings: (id, { label, render, category }) => {
const { React } = goosemodScope.webpackModules.common;
const SettingsView = goosemodScope.webpackModules.findByDisplayName('SettingsView');
const FormTitle = goosemodScope.webpackModules.findByDisplayName('FormTitle');
const FormSection = goosemodScope.webpackModules.findByDisplayName('FormSection');
goosemodScope.patcher.inject(id, SettingsView.prototype, 'getPredicateSections', (_, sections) => {
if (!sections.find(c => c.section === 'changelog')) return sections;
const dividers = sections.filter(c => c.section === 'DIVIDER');
const finalLabel = typeof label === 'function' ? label() : label;
sections.splice(sections.indexOf(dividers[dividers.length - 2]) - 2, 0,
{
section: finalLabel,
label: finalLabel,
predicate: () => { },
element: () => React.createElement(FormSection, { },
React.createElement(FormTitle, { tag: 'h2' }, finalLabel),
render({
getSetting: settingStores[category].getSetting,
updateSetting: settingStores[category].updateSetting
})
)
}
);
return sections;
});
},
unregisterSettings: (id) => {
goosemodScope.patcher.uninject(id);
}
}
}
};
const settingStores = {};
class SimpleStore {
constructor() {
console.log('cons', this);
this.store = {};
}
getSetting = (key, defaultValue) => {
console.log('getsetting', this);
return this.store[key] ?? defaultValue;
}
updateSetting = (key, value) => {
console.log('updatesetting', this);
this.store[key] = value;
}
}
export class Plugin {
constructor() {
}
loadStylesheet(path) {
const url = `https://raw.githubusercontent.com/${this.github.repo}/main/${path}`;
return url;
}
delayedConstructor() {
if (this.delayedConstructed) return;
this.delayedConstructed = true;
settingStores[this.entityID] = new SimpleStore();
}
get entityID() {
return this.name;
}
get settings() {
return settingStores[this.entityID];
}
get goosemodHandlers() {
return {
onImport: this.startPlugin.bind(this),
onImport: () => {
this.delayedConstructor();
this.startPlugin.bind(this)();
},
onRemove: this.pluginWillUnload.bind(this)
};
}
}
}