From a50337abe5d65b6c75f64d581568437beac770c9 Mon Sep 17 00:00:00 2001 From: Piro / YUKI Hiroshi Date: Thu, 25 Aug 2016 21:31:39 +0900 Subject: [PATCH] Support contextual tab coloring on Firefox 51. See also: https://github.com/piroor/treestyletab/issues/1171 --- modules/browser.js | 63 ++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 61 insertions(+), 2 deletions(-) diff --git a/modules/browser.js b/modules/browser.js index df074b47..16d7e4a1 100644 --- a/modules/browser.js +++ b/modules/browser.js @@ -57,6 +57,7 @@ XPCOMUtils.defineLazyModuleGetter(this, 'TabpanelDNDObserver', 'resource://trees XPCOMUtils.defineLazyModuleGetter(this, 'AutoHideBrowser', 'resource://treestyletab-modules/autoHide.js'); XPCOMUtils.defineLazyModuleGetter(this, 'ContentBridge', 'resource://treestyletab-modules/contentBridge.js'); XPCOMUtils.defineLazyModuleGetter(this, 'BrowserUIShowHideObserver', 'resource://treestyletab-modules/browserUIShowHideObserver.js'); +XPCOMUtils.defineLazyModuleGetter(this, 'TabAttributesObserver', 'resource://treestyletab-modules/tabAttributesObserver.js'); XPCOMUtils.defineLazyModuleGetter(this, 'TabContentsObserver', 'resource://treestyletab-modules/tabContentsObserver.js'); XPCOMUtils.defineLazyModuleGetter(this, 'visuallyselectedTabs', 'resource://treestyletab-modules/lib/visuallyselectedTabs.jsm'); @@ -68,6 +69,15 @@ XPCOMUtils.defineLazyGetter(this, 'prefs', function() { Cu.import('resource://treestyletab-modules/lib/prefs.js'); return window['piro.sakura.ne.jp'].prefs; }); +XPCOMUtils.defineLazyGetter(this, 'ContextualIdentityService', function() { + try { + Cu.import('resource://gre/modules/ContextualIdentityService.jsm'); + return ContextualIdentityService; + } + catch(e) { + return null; + } +}); function wait(aMilliSeconds) { return new Promise(function(aResolve, aReject) { @@ -879,6 +889,14 @@ TreeStyleTabBrowser.prototype = inherit(TreeStyleTabWindow.prototype, { this._initTabbrowserContextMenu(); w.TreeStyleTabWindowHelper.updateTabDNDObserver(b); + this.tabsAttributeObserver = new TabAttributesObserver({ + container : b.mTabContainer, + attributes : 'usercontextid,style', + callback : (function(aTab) { + this.onTabContextIdChanged(aTab); + }).bind(this) + }); + this.getAllTabs(b).forEach(this.initTab, this); this.allowSubtreeCollapseExpand = true; // reset attribute @@ -1117,6 +1135,8 @@ TreeStyleTabBrowser.prototype = inherit(TreeStyleTabWindow.prototype, { aTab.__treestyletab__contentBridge = new ContentBridge(aTab, this.mTabBrowser); this.autoHide.notifyStatusToTab(aTab); + + this.onTabContextIdChanged(aTab); }, isTabInitialized : function TSTBrowser_isTabInitialized(aTab) @@ -4190,6 +4210,8 @@ TreeStyleTabBrowser.prototype = inherit(TreeStyleTabWindow.prototype, { } } + this.onTabContextIdChanged(tab); + return restored; }, @@ -4763,10 +4785,11 @@ TreeStyleTabBrowser.prototype = inherit(TreeStyleTabWindow.prototype, { onTabRestored : function TSTBrowser_onTabRestored(aEvent) { - this.updateTabAsParent(aEvent.originalTarget, { + var tab = aEvent.originalTarget; + this.updateTabAsParent(tab, { dontUpdateCount : true }); - delete aEvent.originalTarget.__treestyletab__restoredByUndoCloseTab; + delete tab.__treestyletab__restoredByUndoCloseTab; }, onTabPinned : function TSTBrowser_onTabPinned(aTab) @@ -5106,6 +5129,42 @@ TreeStyleTabBrowser.prototype = inherit(TreeStyleTabWindow.prototype, { return; } }, + + onTabContextIdChanged : function TSTBrowser_onTabContextIdChanged(aTab) + { + if (!ContextualIdentityService || + aTab.__treestyletab__updatingContextualTabColor) + return; + + aTab.__treestyletab__updatingContextualTabColor = true; + setTimeout(function() { + aTab.__treestyletab__updatingContextualTabColor = false; + }, 1); + + var style = aTab.style; + if (!this.isVertical) { + style.backgroundImage = style.backgroundSize = style.backgroundRepeat = style.backgroundPosition = ''; + ContextualIdentityService.setTabStyle(aTab); + return; + } + + var color; + var userContextId = aTab.getAttribute('usercontextid'); + if (userContextId) { + let identity = ContextualIdentityService.getIdentityFromId(userContextId); + color = identity ? identity.color : null ; + } + if (color) { + setTimeout(function() { + style.setProperty('background-image', 'linear-gradient(to bottom, transparent 0, ' + color + ' 5%, ' + color + ' 95%, transparent 100%)', 'important'); + style.setProperty('background-size', '2px auto', 'important'); + style.setProperty('background-repeat', 'no-repeat', 'important'); + let shouldInvert = this.position == 'right' && utils.getTreePref('tabbar.invertTab'); + let position = shouldInvert ? 'right' : 'left' ; + style.setProperty('background-position', position, 'important'); + }, 0); + } + }, onClick : function TSTBrowser_onClick(aEvent) {