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
|
|
|
|
2012-10-31 01:35:11 -07:00
|
|
|
let EXPORTED_SYMBOLS = ["LightweightThemeConsumer"];
|
2009-09-04 03:58:18 -07:00
|
|
|
|
2012-09-22 12:24:26 -07:00
|
|
|
Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
|
|
|
|
|
|
|
|
XPCOMUtils.defineLazyModuleGetter(this, "LightweightThemeImageOptimizer",
|
|
|
|
"resource://gre/modules/LightweightThemeImageOptimizer.jsm");
|
|
|
|
|
2012-10-31 01:35:11 -07:00
|
|
|
function LightweightThemeConsumer(aDocument) {
|
2009-09-04 03:58:18 -07:00
|
|
|
this._doc = aDocument;
|
2012-09-22 12:24:26 -07:00
|
|
|
this._win = aDocument.defaultView;
|
2009-09-04 03:58:18 -07:00
|
|
|
this._footerId = aDocument.documentElement.getAttribute("lightweightthemesfooter");
|
|
|
|
|
2012-09-22 12:24:26 -07:00
|
|
|
let screen = this._win.screen;
|
|
|
|
this._lastScreenWidth = screen.width;
|
|
|
|
this._lastScreenHeight = screen.height;
|
|
|
|
|
2009-09-04 03:58:18 -07:00
|
|
|
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);
|
2012-09-22 12:24:26 -07:00
|
|
|
this._win.addEventListener("resize", this);
|
2009-09-04 03:58:18 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
LightweightThemeConsumer.prototype = {
|
2012-09-22 12:24:26 -07:00
|
|
|
_lastData: null,
|
|
|
|
_lastScreenWidth: null,
|
|
|
|
_lastScreenHeight: null,
|
|
|
|
|
2009-09-04 03:58:18 -07:00
|
|
|
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));
|
|
|
|
},
|
|
|
|
|
2012-09-22 12:24:26 -07:00
|
|
|
handleEvent: function (aEvent) {
|
|
|
|
let {width, height} = this._win.screen;
|
|
|
|
|
|
|
|
if (this._lastScreenWidth != width || this._lastScreenHeight != height) {
|
|
|
|
this._lastScreenWidth = width;
|
|
|
|
this._lastScreenHeight = height;
|
|
|
|
this._update(this._lastData);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2009-09-04 03:58:18 -07:00
|
|
|
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
|
|
|
|
2012-09-22 12:24:26 -07:00
|
|
|
this._win.removeEventListener("resize", this);
|
|
|
|
this._win = this._doc = null;
|
2009-09-04 03:58:18 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
_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
|
|
|
|
2012-09-22 12:24:26 -07:00
|
|
|
this._lastData = aData;
|
|
|
|
aData = LightweightThemeImageOptimizer.optimize(aData, this._win.screen);
|
|
|
|
|
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
|
|
|
}
|