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 pluginManager from "@modules/pluginmanager";
import themeManager from "@modules/thememanager";
import Utilities from "@modules/utilities";
const ContextMenu = new ContextMenuPatcher();
const UserSettingsWindow = Webpack.getByProps("open", "updateAccount");
const SEPARATOR_ITEM = ContextMenu.buildItem({type: "separator"});
export default new class BDContextMenu extends Builtin {
get name() {return "BDContextMenu";}
get category() {return "general";}
@ -20,6 +23,16 @@ export default new class BDContextMenu extends Builtin {
constructor() {
super(...arguments);
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() {
@ -31,25 +44,37 @@ export default new class BDContextMenu extends Builtin {
}
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));
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.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) {
return {
type: "submenu",
label: collection.name,
action: () => {this.openCategory(collection.name);},
action: () => this.openCategory(collection.name),
items: collection.settings.map(category => {
return {
type: "submenu",
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 => {
return {
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 {import("../../modules/addonmanager").default} manager
* @returns
@ -75,14 +100,14 @@ export default new class BDContextMenu extends Builtin {
return {
type: "submenu",
label: label,
action: () => {this.openCategory(label.toLowerCase());},
action: () => this.openCategory(label.toLowerCase()),
items: names.map(name => {
return {
type: "toggle",
label: name,
disabled: manager.getAddon(name)?.partial ?? false,
active: manager.isEnabled(name),
action: () => {manager.toggleAddon(name);}
action: () => manager.toggleAddon(name)
};
})
};