2021-05-22 07:51:16 +02:00
|
|
|
import {React, WebpackModules, Patcher, Utilities, Settings, Events, DataStore} from "modules";
|
2019-06-23 06:11:50 +02:00
|
|
|
|
2019-06-27 22:18:40 +02:00
|
|
|
import AddonList from "./settings/addonlist";
|
2019-06-23 06:11:50 +02:00
|
|
|
import SettingsGroup from "./settings/group";
|
|
|
|
import SettingsTitle from "./settings/title";
|
2020-07-16 07:42:56 +02:00
|
|
|
import Header from "./settings/sidebarheader";
|
2022-02-16 03:23:14 +01:00
|
|
|
import {Filters} from "../modules/webpackmodules";
|
2019-06-23 06:11:50 +02:00
|
|
|
|
|
|
|
export default new class SettingsRenderer {
|
|
|
|
|
|
|
|
constructor() {
|
|
|
|
this.patchSections();
|
2019-06-27 06:33:42 +02:00
|
|
|
Events.on("strings-updated", this.forceUpdate);
|
2019-06-23 06:11:50 +02:00
|
|
|
}
|
|
|
|
|
2020-07-19 01:01:49 +02:00
|
|
|
onDrawerToggle(collection, group, state) {
|
|
|
|
const drawerStates = DataStore.getBDData("drawerStates") || {};
|
|
|
|
if (!drawerStates[collection]) drawerStates[collection] = {};
|
|
|
|
drawerStates[collection][group] = state;
|
|
|
|
DataStore.setBDData("drawerStates", drawerStates);
|
|
|
|
}
|
|
|
|
|
|
|
|
getDrawerState(collection, group, defaultValue) {
|
|
|
|
const drawerStates = DataStore.getBDData("drawerStates") || {};
|
|
|
|
if (!drawerStates[collection]) return defaultValue;
|
|
|
|
if (!drawerStates[collection].hasOwnProperty(group)) return defaultValue;
|
|
|
|
return drawerStates[collection][group];
|
|
|
|
}
|
|
|
|
|
2019-06-26 20:34:01 +02:00
|
|
|
onChange(onChange) {
|
|
|
|
return (collection, category, id) => {
|
|
|
|
const before = Settings.collections.length + Settings.panels.length;
|
|
|
|
onChange(collection, category, id);
|
|
|
|
const after = Settings.collections.length + Settings.panels.length;
|
|
|
|
if (before != after) setTimeout(this.forceUpdate.bind(this), 50);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-07-19 01:01:49 +02:00
|
|
|
buildSettingsPanel(id, title, config, state, onChange, button = null) {
|
2019-06-23 06:11:50 +02:00
|
|
|
config.forEach(section => {
|
|
|
|
section.settings.forEach(item => item.value = state[section.id][item.id]);
|
|
|
|
});
|
2020-07-19 01:01:49 +02:00
|
|
|
return this.getSettingsPanel(id, title, config, this.onChange(onChange), button);
|
2019-06-23 06:11:50 +02:00
|
|
|
}
|
|
|
|
|
2020-07-19 01:01:49 +02:00
|
|
|
getSettingsPanel(id, title, groups, onChange, button = null) {
|
2019-06-23 06:11:50 +02:00
|
|
|
return [React.createElement(SettingsTitle, {text: title, button: button}), groups.map(section => {
|
2020-07-19 01:01:49 +02:00
|
|
|
return React.createElement(SettingsGroup, Object.assign({}, section, {
|
|
|
|
onChange: onChange,
|
|
|
|
onDrawerToggle: state => this.onDrawerToggle(id, section.id, state),
|
|
|
|
shown: this.getDrawerState(id, section.id, section.hasOwnProperty("shown") ? section.shown : true)
|
|
|
|
}));
|
2019-06-23 06:11:50 +02:00
|
|
|
})];
|
|
|
|
}
|
|
|
|
|
2019-06-27 22:18:40 +02:00
|
|
|
getAddonPanel(title, addonList, addonState, options = {}) {
|
|
|
|
return React.createElement(AddonList, Object.assign({}, {
|
2019-06-23 06:11:50 +02:00
|
|
|
title: title,
|
2019-06-27 22:18:40 +02:00
|
|
|
addonList: addonList,
|
|
|
|
addonState: addonState
|
2019-06-23 06:11:50 +02:00
|
|
|
}, options));
|
|
|
|
}
|
|
|
|
|
2022-02-16 03:23:14 +01:00
|
|
|
async patchSections() {
|
|
|
|
const UserSettings = await WebpackModules.getLazy(Filters.byDisplayName("SettingsView"));
|
|
|
|
|
2021-05-22 07:51:16 +02:00
|
|
|
Patcher.after("SettingsManager", UserSettings.prototype, "getPredicateSections", (thisObject, args, returnValue) => {
|
2021-03-06 21:26:48 +01:00
|
|
|
let location = returnValue.findIndex(s => s.section.toLowerCase() == "changelog") - 1;
|
2021-05-22 08:58:59 +02:00
|
|
|
if (location < 0) return;
|
2019-06-23 06:11:50 +02:00
|
|
|
const insert = (section) => {
|
|
|
|
returnValue.splice(location, 0, section);
|
|
|
|
location++;
|
|
|
|
};
|
|
|
|
insert({section: "DIVIDER"});
|
2020-07-16 07:42:56 +02:00
|
|
|
// Header
|
|
|
|
insert({section: "CUSTOM", element: Header});
|
2019-06-23 06:11:50 +02:00
|
|
|
for (const collection of Settings.collections) {
|
|
|
|
if (collection.disabled) continue;
|
|
|
|
insert({
|
|
|
|
section: collection.name,
|
2020-11-07 07:03:29 +01:00
|
|
|
label: collection.name.toString(),
|
|
|
|
className: `bd-${collection.id}-tab`,
|
2020-07-19 01:01:49 +02:00
|
|
|
element: () => this.buildSettingsPanel(collection.id, collection.name, collection.settings, Settings.state[collection.id], Settings.onSettingChange.bind(Settings, collection.id), collection.button ? collection.button : null)
|
2019-06-23 06:11:50 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
for (const panel of Settings.panels.sort((a,b) => a.order > b.order)) {
|
|
|
|
if (panel.clickListener) panel.onClick = (event) => panel.clickListener(thisObject, event, returnValue);
|
2020-11-07 07:03:29 +01:00
|
|
|
if (!panel.className) panel.className = `bd-${panel.id}-tab`;
|
|
|
|
if (typeof(panel.label) !== "string") panel.label = panel.label.toString();
|
2019-06-23 06:11:50 +02:00
|
|
|
insert(panel);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
forceUpdate() {
|
2022-02-16 03:23:14 +01:00
|
|
|
const viewClass = WebpackModules.getByProps("standardSidebarView")?.standardSidebarView.split(" ")[0];
|
2019-06-23 06:11:50 +02:00
|
|
|
const node = document.querySelector(`.${viewClass}`);
|
2019-06-27 06:33:42 +02:00
|
|
|
if (!node) return;
|
2021-05-22 07:51:16 +02:00
|
|
|
const stateNode = Utilities.findInReactTree(Utilities.getReactInstance(node), m => m && m.getPredicateSections, {walkable: ["return", "stateNode"]});
|
2019-06-27 06:33:42 +02:00
|
|
|
if (stateNode) stateNode.forceUpdate();
|
2019-06-23 06:11:50 +02:00
|
|
|
}
|
|
|
|
};
|