From 71725d829be6d43c06e9b7444c5db85b70a9398a Mon Sep 17 00:00:00 2001 From: baku Date: Mon, 9 Jan 2017 11:05:13 +0100 Subject: [PATCH] Hide/Show icon should be shown only if we have opened tabs --- index.js | 31 ++++++++++++++++++++++++++++++- webextension/js/popup.js | 17 +++++++++++++++++ 2 files changed, 47 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 18b7fd2..5db22ac 100644 --- a/index.js +++ b/index.js @@ -42,7 +42,35 @@ let ContainerService = // Map of identities. ContextualIdentityService.getIdentities().forEach(identity => { - this._identitiesState[identity.userContextId] = {hiddenTabUrls: []}; + this._identitiesState[identity.userContextId] = { + hiddenTabUrls: [], + openTabs: 0, + }; + }); + + // It can happen that this jsm is loaded after the opening a container tab. + for (let tab of tabs) { + let xulTab = viewFor(tab); + let userContextId = parseInt(xulTab.getAttribute('usercontextid') || 0, 10); + if (userContextId) { + ++this._identitiesState[userContextId].openTabs; + } + } + + tabs.on("open", tab => { + let xulTab = viewFor(tab); + let userContextId = parseInt(xulTab.getAttribute('usercontextid') || 0, 10); + if (userContextId) { + ++this._identitiesState[userContextId].openTabs; + } + }); + + tabs.on("close", tab => { + let xulTab = viewFor(tab); + let userContextId = parseInt(xulTab.getAttribute('usercontextid') || 0, 10); + if (userContextId && this._identitiesState[userContextId].openTabs) { + --this._identitiesState[userContextId].openTabs; + } }); // WebExtension startup @@ -65,6 +93,7 @@ let ContainerService = color: identity.color, userContextId: identity.userContextId, hasHiddenTabs: !!this._identitiesState[identity.userContextId].hiddenTabUrls.length, + hasOpenTabs: !!this._identitiesState[identity.userContextId].openTabs, }; }, diff --git a/webextension/js/popup.js b/webextension/js/popup.js index 6f03947..4cd5746 100644 --- a/webextension/js/popup.js +++ b/webextension/js/popup.js @@ -10,6 +10,17 @@ function showOrHideContainerTabs(userContextId, hasHiddenTabs) { method: hasHiddenTabs ? 'showTabs' : 'hideTabs', userContextId: userContextId }).then(() => { + return browser.runtime.sendMessage({ + method: 'getIdentity', + userContextId: userContextId + }); + }).then((identity) => { + if (!identity.hasHiddenTabs && !identity.hasOpenTabs) { + hideorshowIcon.style.display = "none"; + } else { + hideorshowIcon.style.display = ""; + } + hideorshowIcon.src = hasHiddenTabs ? CONTAINER_HIDE_SRC : CONTAINER_UNHIDE_SRC; }).then(resolve); }); @@ -79,6 +90,12 @@ browser.runtime.sendMessage({method: 'queryIdentities'}).then(identities=> { `; identitiesListElement.innerHTML += identityRow; + + // No tabs, no icon. + if (!identity.hasHiddenTabs && !identity.hasOpenTabs) { + const hideorshowIcon = document.querySelector(`#uci-${identity.userContextId}-hideorshow-icon`); + hideorshowIcon.style.display = "none"; + } }); const rows = identitiesListElement.querySelectorAll('tr');