Refresh of container icon+name into the awesome bar

This commit is contained in:
baku 2017-01-22 18:44:26 +01:00
parent 72ec2a5731
commit bed0a23c78
2 changed files with 96 additions and 37 deletions

117
index.js
View file

@ -28,6 +28,7 @@ const IDENTITY_ICONS = [
const { attachTo } = require("sdk/content/mod"); const { attachTo } = require("sdk/content/mod");
const { ContextualIdentityService } = require("resource://gre/modules/ContextualIdentityService.jsm"); const { ContextualIdentityService } = require("resource://gre/modules/ContextualIdentityService.jsm");
const { getFavicon } = require("sdk/places/favicon"); const { getFavicon } = require("sdk/places/favicon");
const { modelFor } = require("sdk/model/core");
const self = require("sdk/self"); const self = require("sdk/self");
const { Style } = require("sdk/stylesheet/style"); const { Style } = require("sdk/stylesheet/style");
const tabs = require("sdk/tabs"); const tabs = require("sdk/tabs");
@ -110,16 +111,17 @@ const ContainerService = {
this._hideAllPanels(); this._hideAllPanels();
}); });
tabs.on("activate", () => { tabs.on("activate", tab => {
this._hideAllPanels(); this._hideAllPanels();
this._restyleTab(tab).catch(() => {});
}); });
// Modify CSS and other stuff for each window. // Modify CSS and other stuff for each window.
this.configureWindows(); this.configureWindows().catch(() => {});
windows.browserWindows.on("open", window => { windows.browserWindows.on("open", window => {
this.configureWindow(viewFor(window)); this.configureWindow(viewFor(window)).catch(() => {});
}); });
windows.browserWindows.on("close", window => { windows.browserWindows.on("close", window => {
@ -469,9 +471,11 @@ const ContainerService = {
openTabs: 0 openTabs: 0
}; };
this._refreshNeeded(); this._refreshNeeded().then(() => {
return this._convert(identity);
return Promise.resolve(this._convert(identity)); }).catch(() => {
return this._convert(identity);
});
}, },
updateIdentity(args) { updateIdentity(args) {
@ -493,8 +497,11 @@ const ContainerService = {
identity.name, identity.name,
icon, color); icon, color);
this._refreshNeeded(); this._refreshNeeded().then(() => {
return Promise.resolve(updated); return updated;
}).catch(() => {
return updated;
});
}, },
removeIdentity(args) { removeIdentity(args) {
@ -508,16 +515,21 @@ const ContainerService = {
const removed = ContextualIdentityService.remove(args.userContextId); const removed = ContextualIdentityService.remove(args.userContextId);
this._refreshNeeded(); this._refreshNeeded().then(() => {
return Promise.resolve(removed); return removed;
}).catch(() => {
return removed;
});
}, },
// Styling the window // Styling the window
configureWindows() { configureWindows() {
const promises = [];
for (let window of windows.browserWindows) { // eslint-disable-line prefer-const for (let window of windows.browserWindows) { // eslint-disable-line prefer-const
this.configureWindow(viewFor(window)); promises.push(this.configureWindow(viewFor(window)));
} }
return Promise.all(promises);
}, },
configureWindow(window) { configureWindow(window) {
@ -526,7 +538,7 @@ const ContainerService = {
this._windowMap[id] = new ContainerWindow(window); this._windowMap[id] = new ContainerWindow(window);
} }
this._windowMap[id].configure(); return this._windowMap[id].configure();
}, },
closeWindow(window) { closeWindow(window) {
@ -535,8 +547,7 @@ const ContainerService = {
}, },
_refreshNeeded() { _refreshNeeded() {
// FIXME: color/name propagation return this.configureWindows();
this.configureWindows();
}, },
_hideAllPanels() { _hideAllPanels() {
@ -544,6 +555,30 @@ const ContainerService = {
this._windowMap[id].hidePanel(); this._windowMap[id].hidePanel();
} }
}, },
_restyleTab(tab) {
if (!tab) {
return Promise.resolve(null);
}
const userContextId = ContainerService._getUserContextIdFromTab(tab);
return ContainerService.getIdentity({userContextId}).then(identity => {
if (!identity) {
return;
}
const hbox = viewFor(tab.window).document.getElementById("userContext-icons");
hbox.setAttribute("data-identity-color", identity.color);
const label = viewFor(tab.window).document.getElementById("userContext-label");
label.setAttribute("value", identity.name);
label.style.color = ContainerService._fromNameToColor(identity.color);
const indicator = viewFor(tab.window).document.getElementById("userContext-indicator");
indicator.setAttribute("data-identity-icon", identity.image);
indicator.style.listStyleImage = "";
});
},
}; };
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
@ -566,6 +601,13 @@ ContainerWindow.prototype = {
}, },
configure() { configure() {
return Promise.all([
this._configurePlusButtonMenu(),
this._configureActiveTab(),
]);
},
_configurePlusButtonMenu() {
const tabsElement = this._window.document.getElementById("tabbrowser-tabs"); const tabsElement = this._window.document.getElementById("tabbrowser-tabs");
const button = this._window.document.getAnonymousElementByAttribute(tabsElement, "anonid", "tabs-newtab-button"); const button = this._window.document.getAnonymousElementByAttribute(tabsElement, "anonid", "tabs-newtab-button");
@ -585,7 +627,28 @@ ContainerWindow.prototype = {
this._repositionPopup(); this._repositionPopup();
ContainerService.queryIdentities().then(identities => { button.addEventListener("click", () => {
this._panelElement.hidden = false;
});
button.addEventListener("mouseover", () => {
this._repositionPopup();
this._panelElement.hidden = false;
});
button.addEventListener("mouseout", () => {
this._createTimeout();
});
this._panelElement.addEventListener("mouseout", (e) => {
if (e.target !== this._panelElement) {
this._createTimeout();
return;
}
this._repositionPopup();
});
return ContainerService.queryIdentities().then(identities => {
identities.forEach(identity => { identities.forEach(identity => {
const menuItemElement = this._window.document.createElementNS(XUL_NS, "menuitem"); const menuItemElement = this._window.document.createElementNS(XUL_NS, "menuitem");
this._panelElement.appendChild(menuItemElement); this._panelElement.appendChild(menuItemElement);
@ -619,27 +682,11 @@ ContainerWindow.prototype = {
}).catch(() => { }).catch(() => {
this.hidePanel(); this.hidePanel();
}); });
},
button.addEventListener("click", () => { _configureActiveTab() {
this._panelElement.hidden = false; const tab = modelFor(this._window).tabs.activeTab;
}); return ContainerService._restyleTab(tab);
button.addEventListener("mouseover", () => {
this._repositionPopup();
this._panelElement.hidden = false;
});
button.addEventListener("mouseout", () => {
this._createTimeout();
});
this._panelElement.addEventListener("mouseout", (e) => {
if (e.target !== this._panelElement) {
this._createTimeout();
return;
}
this._repositionPopup();
});
}, },
// This function puts the popup in the correct place. // This function puts the popup in the correct place.

View file

@ -420,8 +420,8 @@ Logic.registerPanel(P_CONTAINER_EDIT, {
method: identity.userContextId ? "updateIdentity" : "createIdentity", method: identity.userContextId ? "updateIdentity" : "createIdentity",
userContextId: identity.userContextId || 0, userContextId: identity.userContextId || 0,
name: document.getElementById("edit-container-panel-name-input").value || Logic.generateIdentityName(), name: document.getElementById("edit-container-panel-name-input").value || Logic.generateIdentityName(),
icon: identity.image || "fingerprint", icon: this._randomIcon(),
color: identity.color || "green", color: this._randomColor(),
}).then(() => { }).then(() => {
return Logic.refreshIdentities(); return Logic.refreshIdentities();
}).then(() => { }).then(() => {
@ -441,6 +441,18 @@ Logic.registerPanel(P_CONTAINER_EDIT, {
return Promise.resolve(null); return Promise.resolve(null);
}, },
// These 2 methods are for debugging only. We should remove them when we
// finish the UI.
_randomIcon() {
const images = ["fingerprint", "briefcase", "dollar", "cart", "cirlce"];
return images[Math.floor(Math.random() * images.length)];
},
_randomColor() {
const colors = ["blue", "turquoise", "green", "yellow", "orange", "red", "pink", "purple" ];
return colors[Math.floor(Math.random() * colors.length)];
},
}); });
// P_CONTAINER_DELETE: Delete a container. // P_CONTAINER_DELETE: Delete a container.