From 95492001b086f2e46b8d43f1ff6aa36a68d2eb01 Mon Sep 17 00:00:00 2001 From: Zerebos Date: Sat, 24 Feb 2024 05:53:03 -0500 Subject: [PATCH] Make context menu code more robust --- renderer/src/builtins/general/contextmenu.js | 43 ++++++++++++++++---- 1 file changed, 34 insertions(+), 9 deletions(-) diff --git a/renderer/src/builtins/general/contextmenu.js b/renderer/src/builtins/general/contextmenu.js index b5047728..25b0d2f5 100644 --- a/renderer/src/builtins/general/contextmenu.js +++ b/renderer/src/builtins/general/contextmenu.js @@ -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) }; }) };