From c7c111e1c03cb1876e8ede5ab856273637d876f5 Mon Sep 17 00:00:00 2001 From: Nils Maier Date: Sun, 25 Aug 2019 23:22:47 +0200 Subject: [PATCH] Option to hide contexts Closes #4 --- _locales/en/messages.json | 4 +++ data/prefs.json | 1 + lib/background.ts | 66 ++++++++++++++++++++++++++++----------- windows/prefs.html | 1 + windows/prefs.ts | 1 + 5 files changed, 55 insertions(+), 18 deletions(-) diff --git a/_locales/en/messages.json b/_locales/en/messages.json index fd7502e..ccf2ca5 100644 --- a/_locales/en/messages.json +++ b/_locales/en/messages.json @@ -457,6 +457,10 @@ "description": "", "message": "Browser button should be OneClick!" }, + "pref-hide-context": { + "description": "", + "message": "Do not show general context menu items" + }, "pref-manager-tooltip": { "description": "", "message": "Show tooltips in Manager tabs" diff --git a/data/prefs.json b/data/prefs.json index 8654f33..978951c 100644 --- a/data/prefs.json +++ b/data/prefs.json @@ -6,6 +6,7 @@ "open-manager-on-queue": true, "text-links": true, "add-paused": false, + "hide-context": false, "conflict-action": "uniquify", "nagging": 0, "nagging-next": 6, diff --git a/lib/background.ts b/lib/background.ts index 67da3f3..adaa87a 100644 --- a/lib/background.ts +++ b/lib/background.ts @@ -139,7 +139,14 @@ const menuHandler = new class Menus extends Handler { constructor() { super(); this.onClicked = this.onClicked.bind(this); - menus.create({ + const alls = new Map(); + const mcreate = (options: any) => { + if (options.contexts.includes("all")) { + alls.set(options.id, options.contexts); + } + return menus.create(options); + }; + mcreate({ id: "DTARegular", contexts: ["all", "browser_action", "tools_menu"], icons: { @@ -148,7 +155,7 @@ const menuHandler = new class Menus extends Handler { }, title: _("dta.regular"), }); - menus.create({ + mcreate({ id: "DTATurbo", contexts: ["all", "browser_action", "tools_menu"], icons: { @@ -157,7 +164,7 @@ const menuHandler = new class Menus extends Handler { }, title: _("dta.turbo"), }); - menus.create({ + mcreate({ id: "DTARegularLink", contexts: ["link"], icons: { @@ -166,7 +173,7 @@ const menuHandler = new class Menus extends Handler { }, title: _("dta.regular.link"), }); - menus.create({ + mcreate({ id: "DTATurboLink", contexts: ["link"], icons: { @@ -175,7 +182,7 @@ const menuHandler = new class Menus extends Handler { }, title: _("dta.turbo.link"), }); - menus.create({ + mcreate({ id: "DTARegularImage", contexts: ["image"], icons: { @@ -184,7 +191,7 @@ const menuHandler = new class Menus extends Handler { }, title: _("dta.regular.image"), }); - menus.create({ + mcreate({ id: "DTATurboImage", contexts: ["image"], icons: { @@ -193,7 +200,7 @@ const menuHandler = new class Menus extends Handler { }, title: _("dta.turbo.image"), }); - menus.create({ + mcreate({ id: "DTARegularMedia", contexts: ["video", "audio"], icons: { @@ -202,7 +209,7 @@ const menuHandler = new class Menus extends Handler { }, title: _("dta.regular.media"), }); - menus.create({ + mcreate({ id: "DTATurboMedia", contexts: ["video", "audio"], icons: { @@ -211,7 +218,7 @@ const menuHandler = new class Menus extends Handler { }, title: _("dta.turbo.media"), }); - menus.create({ + mcreate({ id: "DTARegularSelection", contexts: ["selection"], icons: { @@ -220,7 +227,7 @@ const menuHandler = new class Menus extends Handler { }, title: _("dta.regular.selection"), }); - menus.create({ + mcreate({ id: "DTATurboSelection", contexts: ["selection"], icons: { @@ -229,12 +236,12 @@ const menuHandler = new class Menus extends Handler { }, title: _("dta.turbo.selection"), }); - menus.create({ + mcreate({ id: "sep-1", contexts: ["all", "browser_action", "tools_menu"], type: "separator" }); - menus.create({ + mcreate({ id: "DTARegularAll", contexts: ["all", "browser_action", "tools_menu"], icons: { @@ -243,7 +250,7 @@ const menuHandler = new class Menus extends Handler { }, title: _("dta-regular-all"), }); - menus.create({ + mcreate({ id: "DTATurboAll", contexts: ["all", "browser_action", "tools_menu"], icons: { @@ -255,12 +262,12 @@ const menuHandler = new class Menus extends Handler { const sep2ctx = menus.ACTION_MENU_TOP_LEVEL_LIMIT === 6 ? ["all", "tools_menu"] : ["all", "browser_action", "tools_menu"]; - menus.create({ + mcreate({ id: "sep-2", contexts: sep2ctx, type: "separator" }); - menus.create({ + mcreate({ id: "DTAAdd", contexts: ["all", "browser_action", "tools_menu"], icons: { @@ -271,12 +278,12 @@ const menuHandler = new class Menus extends Handler { }, title: _("add-download"), }); - menus.create({ + mcreate({ id: "sep-3", contexts: ["all", "browser_action", "tools_menu"], type: "separator" }); - menus.create({ + mcreate({ id: "DTAManager", contexts: ["all", "browser_action", "tools_menu"], icons: { @@ -285,7 +292,7 @@ const menuHandler = new class Menus extends Handler { }, title: _("manager.short"), }); - menus.create({ + mcreate({ id: "DTAPrefs", contexts: ["all", "browser_action", "tools_menu"], icons: { @@ -296,6 +303,29 @@ const menuHandler = new class Menus extends Handler { }, title: _("prefs.short"), }); + Object.freeze(alls); + + const adjustMenus = (v: boolean) => { + for (const [id, contexts] of alls.entries()) { + const adjusted = v ? + contexts.filter(e => e !== "all") : + contexts; + menus.update(id, { + contexts: adjusted + }); + } + }; + Prefs.get("hide-context", false).then((v: boolean) => { + // This is the initial load, so no need to adjust when visible already + if (!v) { + return; + } + adjustMenus(v); + }); + Prefs.on("hide-context", (prefs, key, value: boolean) => { + adjustMenus(value); + }); + menus.onClicked.addListener(this.onClicked); } diff --git a/windows/prefs.html b/windows/prefs.html index a6f4977..30c7ebd 100644 --- a/windows/prefs.html +++ b/windows/prefs.html @@ -45,6 +45,7 @@ UI +
diff --git a/windows/prefs.ts b/windows/prefs.ts index abe2dbe..16fbb12 100644 --- a/windows/prefs.ts +++ b/windows/prefs.ts @@ -550,6 +550,7 @@ addEventListener("DOMContentLoaded", () => { new BoolPref("pref-global-turbo", "global-turbo"); new BoolPref("pref-queue-notification", "queue-notification"); new BoolPref("pref-finish-notification", "finish-notification"); + new BoolPref("pref-hide-context", "hide-context"); new BoolPref("pref-tooltip", "tooltip"); new BoolPref("pref-open-manager-on-queue", "open-manager-on-queue"); new BoolPref("pref-text-links", "text-links");