1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/toolkit/modules/LightweightThemeConsumer.jsm Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,174 @@ 1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.7 + 1.8 +this.EXPORTED_SYMBOLS = ["LightweightThemeConsumer"]; 1.9 + 1.10 +Components.utils.import("resource://gre/modules/XPCOMUtils.jsm"); 1.11 +Components.utils.import("resource://gre/modules/Services.jsm"); 1.12 + 1.13 +XPCOMUtils.defineLazyModuleGetter(this, "LightweightThemeImageOptimizer", 1.14 + "resource://gre/modules/addons/LightweightThemeImageOptimizer.jsm"); 1.15 + 1.16 +XPCOMUtils.defineLazyModuleGetter(this, "PrivateBrowsingUtils", 1.17 + "resource://gre/modules/PrivateBrowsingUtils.jsm"); 1.18 + 1.19 +this.LightweightThemeConsumer = 1.20 + function LightweightThemeConsumer(aDocument) { 1.21 + this._doc = aDocument; 1.22 + this._win = aDocument.defaultView; 1.23 + this._footerId = aDocument.documentElement.getAttribute("lightweightthemesfooter"); 1.24 + 1.25 + if (PrivateBrowsingUtils.isWindowPrivate(this._win) && 1.26 + !PrivateBrowsingUtils.permanentPrivateBrowsing) { 1.27 + return; 1.28 + } 1.29 + 1.30 + let screen = this._win.screen; 1.31 + this._lastScreenWidth = screen.width; 1.32 + this._lastScreenHeight = screen.height; 1.33 + 1.34 + Components.classes["@mozilla.org/observer-service;1"] 1.35 + .getService(Components.interfaces.nsIObserverService) 1.36 + .addObserver(this, "lightweight-theme-styling-update", false); 1.37 + 1.38 + var temp = {}; 1.39 + Components.utils.import("resource://gre/modules/LightweightThemeManager.jsm", temp); 1.40 + this._update(temp.LightweightThemeManager.currentThemeForDisplay); 1.41 + this._win.addEventListener("resize", this); 1.42 +} 1.43 + 1.44 +LightweightThemeConsumer.prototype = { 1.45 + _lastData: null, 1.46 + _lastScreenWidth: null, 1.47 + _lastScreenHeight: null, 1.48 + // Whether the active lightweight theme should be shown on the window. 1.49 + _enabled: true, 1.50 + // Whether a lightweight theme is enabled. 1.51 + _active: false, 1.52 + 1.53 + enable: function() { 1.54 + this._enabled = true; 1.55 + this._update(this._lastData); 1.56 + }, 1.57 + 1.58 + disable: function() { 1.59 + // Dance to keep the data, but reset the applied styles: 1.60 + let lastData = this._lastData 1.61 + this._update(null); 1.62 + this._enabled = false; 1.63 + this._lastData = lastData; 1.64 + }, 1.65 + 1.66 + observe: function (aSubject, aTopic, aData) { 1.67 + if (aTopic != "lightweight-theme-styling-update") 1.68 + return; 1.69 + 1.70 + this._update(JSON.parse(aData)); 1.71 + }, 1.72 + 1.73 + handleEvent: function (aEvent) { 1.74 + let {width, height} = this._win.screen; 1.75 + 1.76 + if (this._lastScreenWidth != width || this._lastScreenHeight != height) { 1.77 + this._lastScreenWidth = width; 1.78 + this._lastScreenHeight = height; 1.79 + if (!this._active) 1.80 + return; 1.81 + this._update(this._lastData); 1.82 + Services.obs.notifyObservers(this._win, "lightweight-theme-optimized", 1.83 + JSON.stringify(this._lastData)); 1.84 + } 1.85 + }, 1.86 + 1.87 + destroy: function () { 1.88 + if (!PrivateBrowsingUtils.isWindowPrivate(this._win) || 1.89 + PrivateBrowsingUtils.permanentPrivateBrowsing) { 1.90 + Components.classes["@mozilla.org/observer-service;1"] 1.91 + .getService(Components.interfaces.nsIObserverService) 1.92 + .removeObserver(this, "lightweight-theme-styling-update"); 1.93 + 1.94 + this._win.removeEventListener("resize", this); 1.95 + } 1.96 + 1.97 + this._win = this._doc = null; 1.98 + }, 1.99 + 1.100 + _update: function (aData) { 1.101 + if (!aData) { 1.102 + aData = { headerURL: "", footerURL: "", textcolor: "", accentcolor: "" }; 1.103 + this._lastData = aData; 1.104 + } else { 1.105 + this._lastData = aData; 1.106 + aData = LightweightThemeImageOptimizer.optimize(aData, this._win.screen); 1.107 + } 1.108 + if (!this._enabled) 1.109 + return; 1.110 + 1.111 + let root = this._doc.documentElement; 1.112 + let active = !!aData.headerURL; 1.113 + let stateChanging = (active != this._active); 1.114 + 1.115 + if (active) { 1.116 + root.style.color = aData.textcolor || "black"; 1.117 + root.style.backgroundColor = aData.accentcolor || "white"; 1.118 + let [r, g, b] = _parseRGB(this._doc.defaultView.getComputedStyle(root, "").color); 1.119 + let luminance = 0.2125 * r + 0.7154 * g + 0.0721 * b; 1.120 + root.setAttribute("lwthemetextcolor", luminance <= 110 ? "dark" : "bright"); 1.121 + root.setAttribute("lwtheme", "true"); 1.122 + } else { 1.123 + root.style.color = ""; 1.124 + root.style.backgroundColor = ""; 1.125 + root.removeAttribute("lwthemetextcolor"); 1.126 + root.removeAttribute("lwtheme"); 1.127 + } 1.128 + 1.129 + this._active = active; 1.130 + 1.131 + _setImage(root, active, aData.headerURL); 1.132 + if (this._footerId) { 1.133 + let footer = this._doc.getElementById(this._footerId); 1.134 + footer.style.backgroundColor = active ? aData.accentcolor || "white" : ""; 1.135 + _setImage(footer, active, aData.footerURL); 1.136 + if (active && aData.footerURL) 1.137 + footer.setAttribute("lwthemefooter", "true"); 1.138 + else 1.139 + footer.removeAttribute("lwthemefooter"); 1.140 + } 1.141 + 1.142 +#ifdef XP_MACOSX 1.143 + // On OS X, we extend the lightweight theme into the titlebar, which means setting 1.144 + // the chromemargin attribute. Some XUL applications already draw in the titlebar, 1.145 + // so we need to save the chromemargin value before we overwrite it with the value 1.146 + // that lets us draw in the titlebar. We stash this value on the root attribute so 1.147 + // that XUL applications have the ability to invalidate the saved value. 1.148 + if (stateChanging) { 1.149 + if (!root.hasAttribute("chromemargin-nonlwtheme")) { 1.150 + root.setAttribute("chromemargin-nonlwtheme", root.getAttribute("chromemargin")); 1.151 + } 1.152 + 1.153 + if (active) { 1.154 + root.setAttribute("chromemargin", "0,-1,-1,-1"); 1.155 + } else { 1.156 + let defaultChromemargin = root.getAttribute("chromemargin-nonlwtheme"); 1.157 + if (defaultChromemargin) { 1.158 + root.setAttribute("chromemargin", defaultChromemargin); 1.159 + } else { 1.160 + root.removeAttribute("chromemargin"); 1.161 + } 1.162 + } 1.163 + } 1.164 +#endif 1.165 + } 1.166 +} 1.167 + 1.168 +function _setImage(aElement, aActive, aURL) { 1.169 + aElement.style.backgroundImage = 1.170 + (aActive && aURL) ? 'url("' + aURL.replace(/"/g, '\\"') + '")' : ""; 1.171 +} 1.172 + 1.173 +function _parseRGB(aColorString) { 1.174 + var rgb = aColorString.match(/^rgba?\((\d+), (\d+), (\d+)/); 1.175 + rgb.shift(); 1.176 + return rgb.map(function (x) parseInt(x)); 1.177 +}