2012-05-21 04:12:37 -07:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2009-09-04 03:58:18 -07:00
|
|
|
|
|
|
|
let EXPORTED_SYMBOLS = ["LightweightThemeConsumer"];
|
|
|
|
|
|
|
|
function LightweightThemeConsumer(aDocument) {
|
|
|
|
this._doc = aDocument;
|
|
|
|
this._footerId = aDocument.documentElement.getAttribute("lightweightthemesfooter");
|
|
|
|
|
|
|
|
Components.classes["@mozilla.org/observer-service;1"]
|
|
|
|
.getService(Components.interfaces.nsIObserverService)
|
2009-11-24 07:59:53 -08:00
|
|
|
.addObserver(this, "lightweight-theme-styling-update", false);
|
2009-09-04 03:58:18 -07:00
|
|
|
|
|
|
|
var temp = {};
|
|
|
|
Components.utils.import("resource://gre/modules/LightweightThemeManager.jsm", temp);
|
2009-11-05 23:03:22 -08:00
|
|
|
this._update(temp.LightweightThemeManager.currentThemeForDisplay);
|
2009-09-04 03:58:18 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
LightweightThemeConsumer.prototype = {
|
|
|
|
observe: function (aSubject, aTopic, aData) {
|
2009-11-24 07:59:53 -08:00
|
|
|
if (aTopic != "lightweight-theme-styling-update")
|
2009-09-04 03:58:18 -07:00
|
|
|
return;
|
|
|
|
|
|
|
|
this._update(JSON.parse(aData));
|
|
|
|
},
|
|
|
|
|
|
|
|
destroy: function () {
|
|
|
|
Components.classes["@mozilla.org/observer-service;1"]
|
|
|
|
.getService(Components.interfaces.nsIObserverService)
|
2009-11-24 07:59:53 -08:00
|
|
|
.removeObserver(this, "lightweight-theme-styling-update");
|
2009-09-04 03:58:18 -07:00
|
|
|
|
|
|
|
this._doc = null;
|
|
|
|
},
|
|
|
|
|
|
|
|
_update: function (aData) {
|
|
|
|
if (!aData)
|
2009-09-22 01:40:12 -07:00
|
|
|
aData = { headerURL: "", footerURL: "", textcolor: "", accentcolor: "" };
|
2009-09-04 03:58:18 -07:00
|
|
|
|
|
|
|
var root = this._doc.documentElement;
|
|
|
|
var active = !!aData.headerURL;
|
|
|
|
|
|
|
|
if (active) {
|
2009-09-22 01:40:12 -07:00
|
|
|
root.style.color = aData.textcolor || "black";
|
|
|
|
root.style.backgroundColor = aData.accentcolor || "white";
|
2009-09-04 03:58:18 -07:00
|
|
|
let [r, g, b] = _parseRGB(this._doc.defaultView.getComputedStyle(root, "").color);
|
2009-10-01 23:22:08 -07:00
|
|
|
let luminance = 0.2125 * r + 0.7154 * g + 0.0721 * b;
|
|
|
|
root.setAttribute("lwthemetextcolor", luminance <= 110 ? "dark" : "bright");
|
2009-09-04 03:58:18 -07:00
|
|
|
root.setAttribute("lwtheme", "true");
|
|
|
|
} else {
|
|
|
|
root.style.color = "";
|
2009-09-22 01:40:12 -07:00
|
|
|
root.style.backgroundColor = "";
|
2009-09-04 03:58:18 -07:00
|
|
|
root.removeAttribute("lwthemetextcolor");
|
|
|
|
root.removeAttribute("lwtheme");
|
|
|
|
}
|
|
|
|
|
|
|
|
_setImage(root, active, aData.headerURL);
|
|
|
|
if (this._footerId) {
|
|
|
|
let footer = this._doc.getElementById(this._footerId);
|
2010-09-04 01:46:14 -07:00
|
|
|
footer.style.backgroundColor = active ? aData.accentcolor || "white" : "";
|
2009-09-04 03:58:18 -07:00
|
|
|
_setImage(footer, active, aData.footerURL);
|
|
|
|
if (active && aData.footerURL)
|
|
|
|
footer.setAttribute("lwthemefooter", "true");
|
|
|
|
else
|
|
|
|
footer.removeAttribute("lwthemefooter");
|
|
|
|
}
|
|
|
|
|
2009-10-04 02:28:45 -07:00
|
|
|
#ifdef XP_MACOSX
|
2009-10-21 09:02:43 -07:00
|
|
|
if (active)
|
|
|
|
root.setAttribute("drawintitlebar", "true");
|
|
|
|
else
|
|
|
|
root.removeAttribute("drawintitlebar");
|
2009-10-04 02:28:45 -07:00
|
|
|
#endif
|
2009-09-04 03:58:18 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function _setImage(aElement, aActive, aURL) {
|
|
|
|
aElement.style.backgroundImage =
|
2012-01-18 13:10:39 -08:00
|
|
|
(aActive && aURL) ? 'url("' + aURL.replace(/"/g, '\\"') + '")' : "";
|
2009-09-04 03:58:18 -07:00
|
|
|
}
|
|
|
|
|
2011-02-16 13:41:59 -08:00
|
|
|
function _parseRGB(aColorString) {
|
|
|
|
var rgb = aColorString.match(/^rgba?\((\d+), (\d+), (\d+)/);
|
|
|
|
rgb.shift();
|
|
|
|
return rgb.map(function (x) parseInt(x));
|
2009-09-04 03:58:18 -07:00
|
|
|
}
|