Update context menu when a container is created/deleted

This commit is contained in:
tunefish 2018-02-20 01:50:00 +01:00
parent 2a6775ab12
commit 396c96efc9
2 changed files with 29 additions and 13 deletions

View file

@ -325,7 +325,7 @@ const assignManager = {
browser.contextMenus.remove(this.MENU_ASSIGN_ID); browser.contextMenus.remove(this.MENU_ASSIGN_ID);
browser.contextualIdentities.query({}).then((identities) => { browser.contextualIdentities.query({}).then((identities) => {
identities.forEach((identity) => { identities.forEach((identity) => {
browser.contextMenus.remove(this.cookieStoreId2menuId(identity.cookieStoreId)); this.removeContainerMenuEntry(identity);
}); });
}).catch(() => {}); }).catch(() => {});
browser.contextMenus.remove(this.MENU_REOPEN_IN); browser.contextMenus.remove(this.MENU_REOPEN_IN);
@ -335,6 +335,24 @@ const assignManager = {
browser.contextMenus.remove(this.MENU_MOVE_ID); browser.contextMenus.remove(this.MENU_MOVE_ID);
}, },
addContainerMenuEntry(contextualIdentity, contexts) {
browser.contextMenus.create({
id: this.cookieStoreId2menuId(contextualIdentity.cookieStoreId),
title: contextualIdentity.name,
// TODO: colorized icons?
icons: {
"16": contextualIdentity.iconUrl
},
// TODO: hide entry for current container in context menu of tabs
contexts: contexts,
parentId: this.MENU_REOPEN_IN,
});
},
removeContainerMenuEntry(contextualIdentity) {
browser.contextMenus.remove(this.cookieStoreId2menuId(contextualIdentity.cookieStoreId));
},
async calculateContextMenu(tab) { async calculateContextMenu(tab) {
this.removeContextMenu(); this.removeContextMenu();
@ -346,18 +364,8 @@ const assignManager = {
const identities = await browser.contextualIdentities.query({}); const identities = await browser.contextualIdentities.query({});
identities.forEach((identity) => { identities.forEach((identity) => {
browser.contextMenus.create({ this.addContainerMenuEntry(identity, (identity.cookieStoreId !== tab.cookieStoreId) ?
id: this.cookieStoreId2menuId(identity.cookieStoreId), ["all", "tab"] : ["tab"]);
title: identity.name,
// TODO: colorized icons?
icons: {
"16": identity.iconUrl
},
// TODO: hide entry for current container in context menu of tabs
contexts: (identity.cookieStoreId !== tab.cookieStoreId) ?
["all", "tab"] : ["tab"],
parentId: this.MENU_REOPEN_IN,
});
}); });
const siteSettings = await this._getAssignment(tab); const siteSettings = await this._getAssignment(tab);

View file

@ -76,6 +76,14 @@ const messageHandler = {
browser.contextualIdentities.onRemoved.addListener(({contextualIdentity}) => { browser.contextualIdentities.onRemoved.addListener(({contextualIdentity}) => {
const userContextId = backgroundLogic.getUserContextIdFromCookieStoreId(contextualIdentity.cookieStoreId); const userContextId = backgroundLogic.getUserContextIdFromCookieStoreId(contextualIdentity.cookieStoreId);
backgroundLogic.deleteContainer(userContextId, true); backgroundLogic.deleteContainer(userContextId, true);
assignManager.removeContainerMenuEntry(contextualIdentity);
});
}
if (browser.contextualIdentities.onCreated) {
browser.contextualIdentities.onCreated.addListener(({contextualIdentity}) => {
assignManager.addContainerMenuEntry(contextualIdentity, ["all", "tab"])
}); });
} }