From 68c7f13f9407285122ae60a390d355d4a9d2181f Mon Sep 17 00:00:00 2001 From: Oj Date: Tue, 6 Apr 2021 17:21:58 +0100 Subject: [PATCH] [PCCompat > Settings] Initial Add --- .../powercord/components/settings.js | 65 +++++++++++++ moduleWrappers/powercord/entities.js | 93 ++++++++++++++++++- 2 files changed, 156 insertions(+), 2 deletions(-) create mode 100644 moduleWrappers/powercord/components/settings.js diff --git a/moduleWrappers/powercord/components/settings.js b/moduleWrappers/powercord/components/settings.js new file mode 100644 index 0000000..eddc846 --- /dev/null +++ b/moduleWrappers/powercord/components/settings.js @@ -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 +}; diff --git a/moduleWrappers/powercord/entities.js b/moduleWrappers/powercord/entities.js index 6ed11f3..42630fb 100644 --- a/moduleWrappers/powercord/entities.js +++ b/moduleWrappers/powercord/entities.js @@ -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) }; } -} \ No newline at end of file +}