Make context menu code more robust

This commit is contained in:
Zerebos 2024-02-24 05:53:03 -05:00
parent ad076667c0
commit 95492001b0
1 changed files with 34 additions and 9 deletions

View File

@ -7,11 +7,14 @@ import Webpack from "@modules/webpackmodules";
import ContextMenuPatcher from "@modules/api/contextmenu"; import ContextMenuPatcher from "@modules/api/contextmenu";
import pluginManager from "@modules/pluginmanager"; import pluginManager from "@modules/pluginmanager";
import themeManager from "@modules/thememanager"; import themeManager from "@modules/thememanager";
import Utilities from "@modules/utilities";
const ContextMenu = new ContextMenuPatcher(); const ContextMenu = new ContextMenuPatcher();
const UserSettingsWindow = Webpack.getByProps("open", "updateAccount"); const UserSettingsWindow = Webpack.getByProps("open", "updateAccount");
const SEPARATOR_ITEM = ContextMenu.buildItem({type: "separator"});
export default new class BDContextMenu extends Builtin { export default new class BDContextMenu extends Builtin {
get name() {return "BDContextMenu";} get name() {return "BDContextMenu";}
get category() {return "general";} get category() {return "general";}
@ -20,6 +23,16 @@ export default new class BDContextMenu extends Builtin {
constructor() { constructor() {
super(...arguments); super(...arguments);
this.callback = this.callback.bind(this); this.callback = this.callback.bind(this);
this.UPDATES_ITEM = {
label: Strings.Panels.updates,
action: () => this.openCategory("updates")
};
this.CUSTOM_CSS_ITEM = {
label: Strings.Panels.customcss,
action: () => this.openCategory("customcss")
};
} }
enabled() { enabled() {
@ -31,25 +44,37 @@ export default new class BDContextMenu extends Builtin {
} }
callback(retVal) { callback(retVal) {
const target = Utilities.findInTree(retVal, b => Array.isArray(b) && b.some(e => e?.key?.toLowerCase() === "my_account"), {walkable: ["props", "children"]});
if (!target) return;
// BetterDiscord Settings
const items = Settings.collections.map(c => this.buildCollectionMenu(c)); const items = Settings.collections.map(c => this.buildCollectionMenu(c));
items.push({label: Strings.Panels.updates, action: () => {this.openCategory("updates");}});
if (Settings.get("settings", "customcss", "customcss")) items.push({label: Strings.Panels.customcss, action: () => {this.openCategory("customcss");}}); // Updater
items.push(this.UPDATES_ITEM);
// Custom CSS
if (Settings.get("settings", "customcss", "customcss")) items.push(this.CUSTOM_CSS_ITEM);
// Plugins & Themes
items.push(this.buildAddonMenu(Strings.Panels.plugins, pluginManager)); items.push(this.buildAddonMenu(Strings.Panels.plugins, pluginManager));
items.push(this.buildAddonMenu(Strings.Panels.themes, themeManager)); items.push(this.buildAddonMenu(Strings.Panels.themes, themeManager));
retVal?.props?.children?.props?.children?.[0].push(ContextMenu.buildItem({type: "separator"}));
retVal?.props?.children?.props?.children?.[0].push(ContextMenu.buildItem({type: "submenu", label: "BetterDiscord", items: items})); // Parent SubMenu
target.push(SEPARATOR_ITEM);
target.push(ContextMenu.buildItem({type: "submenu", label: "BetterDiscord", items: items}));
} }
buildCollectionMenu(collection) { buildCollectionMenu(collection) {
return { return {
type: "submenu", type: "submenu",
label: collection.name, label: collection.name,
action: () => {this.openCategory(collection.name);}, action: () => this.openCategory(collection.name),
items: collection.settings.map(category => { items: collection.settings.map(category => {
return { return {
type: "submenu", type: "submenu",
label: category.name, label: category.name,
action: () => {this.openCategory(collection.name);}, action: () => this.openCategory(collection.name),
items: category.settings.filter(s => s.type === "switch" && !s.hidden && s.id !== this.id).map(setting => { items: category.settings.filter(s => s.type === "switch" && !s.hidden && s.id !== this.id).map(setting => {
return { return {
type: "toggle", type: "toggle",
@ -65,7 +90,7 @@ export default new class BDContextMenu extends Builtin {
} }
/** /**
* * TODO: Can this be done better now that it's integrated?
* @param {string} label * @param {string} label
* @param {import("../../modules/addonmanager").default} manager * @param {import("../../modules/addonmanager").default} manager
* @returns * @returns
@ -75,14 +100,14 @@ export default new class BDContextMenu extends Builtin {
return { return {
type: "submenu", type: "submenu",
label: label, label: label,
action: () => {this.openCategory(label.toLowerCase());}, action: () => this.openCategory(label.toLowerCase()),
items: names.map(name => { items: names.map(name => {
return { return {
type: "toggle", type: "toggle",
label: name, label: name,
disabled: manager.getAddon(name)?.partial ?? false, disabled: manager.getAddon(name)?.partial ?? false,
active: manager.isEnabled(name), active: manager.isEnabled(name),
action: () => {manager.toggleAddon(name);} action: () => manager.toggleAddon(name)
}; };
}) })
}; };