2012-05-21 11:12:37 +00: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 10:58:18 +00:00
|
|
|
|
2012-10-31 16:13:28 +00:00
|
|
|
this.EXPORTED_SYMBOLS = ["LightweightThemeConsumer"];
|
2009-09-04 10:58:18 +00:00
|
|
|
|
2012-09-22 19:24:26 +00:00
|
|
|
Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
|
|
|
|
|
|
|
|
XPCOMUtils.defineLazyModuleGetter(this, "LightweightThemeImageOptimizer",
|
|
|
|
"resource://gre/modules/LightweightThemeImageOptimizer.jsm");
|
|
|
|
|
2013-01-07 18:24:08 +00:00
|
|
|
XPCOMUtils.defineLazyModuleGetter(this, "PrivateBrowsingUtils",
|
|
|
|
"resource://gre/modules/PrivateBrowsingUtils.jsm");
|
|
|
|
|
2012-10-31 16:13:28 +00:00
|
|
|
this.LightweightThemeConsumer =
|
|
|
|
function LightweightThemeConsumer(aDocument) {
|
2009-09-04 10:58:18 +00:00
|
|
|
this._doc = aDocument;
|
2012-09-22 19:24:26 +00:00
|
|
|
this._win = aDocument.defaultView;
|
2009-09-04 10:58:18 +00:00
|
|
|
this._footerId = aDocument.documentElement.getAttribute("lightweightthemesfooter");
|
|
|
|
|
2013-03-29 15:29:29 +00:00
|
|
|
if (PrivateBrowsingUtils.isWindowPrivate(this._win) &&
|
|
|
|
!PrivateBrowsingUtils.permanentPrivateBrowsing) {
|
2013-01-07 18:24:08 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-09-22 19:24:26 +00:00
|
|
|
let screen = this._win.screen;
|
|
|
|
this._lastScreenWidth = screen.width;
|
|
|
|
this._lastScreenHeight = screen.height;
|
|
|
|
|
2009-09-04 10:58:18 +00:00
|
|
|
Components.classes["@mozilla.org/observer-service;1"]
|
|
|
|
.getService(Components.interfaces.nsIObserverService)
|
2009-11-24 15:59:53 +00:00
|
|
|
.addObserver(this, "lightweight-theme-styling-update", false);
|
2009-09-04 10:58:18 +00:00
|
|
|
|
|
|
|
var temp = {};
|
|
|
|
Components.utils.import("resource://gre/modules/LightweightThemeManager.jsm", temp);
|
2009-11-06 07:03:22 +00:00
|
|
|
this._update(temp.LightweightThemeManager.currentThemeForDisplay);
|
2012-09-22 19:24:26 +00:00
|
|
|
this._win.addEventListener("resize", this);
|
2009-09-04 10:58:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
LightweightThemeConsumer.prototype = {
|
2012-09-22 19:24:26 +00:00
|
|
|
_lastData: null,
|
|
|
|
_lastScreenWidth: null,
|
|
|
|
_lastScreenHeight: null,
|
|
|
|
|
2009-09-04 10:58:18 +00:00
|
|
|
observe: function (aSubject, aTopic, aData) {
|
2009-11-24 15:59:53 +00:00
|
|
|
if (aTopic != "lightweight-theme-styling-update")
|
2009-09-04 10:58:18 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
this._update(JSON.parse(aData));
|
|
|
|
},
|
|
|
|
|
2012-09-22 19:24:26 +00: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 10:58:18 +00:00
|
|
|
destroy: function () {
|
2013-03-29 15:29:29 +00:00
|
|
|
if (!PrivateBrowsingUtils.isWindowPrivate(this._win) ||
|
|
|
|
PrivateBrowsingUtils.permanentPrivateBrowsing) {
|
2013-02-01 19:31:32 +00:00
|
|
|
Components.classes["@mozilla.org/observer-service;1"]
|
|
|
|
.getService(Components.interfaces.nsIObserverService)
|
|
|
|
.removeObserver(this, "lightweight-theme-styling-update");
|
|
|
|
|
|
|
|
this._win.removeEventListener("resize", this);
|
|
|
|
}
|
2009-09-04 10:58:18 +00:00
|
|
|
|
2012-09-22 19:24:26 +00:00
|
|
|
this._win = this._doc = null;
|
2009-09-04 10:58:18 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
_update: function (aData) {
|
|
|
|
if (!aData)
|
2009-09-22 08:40:12 +00:00
|
|
|
aData = { headerURL: "", footerURL: "", textcolor: "", accentcolor: "" };
|
2009-09-04 10:58:18 +00:00
|
|
|
|
2012-09-22 19:24:26 +00:00
|
|
|
this._lastData = aData;
|
|
|
|
aData = LightweightThemeImageOptimizer.optimize(aData, this._win.screen);
|
|
|
|
|
2009-09-04 10:58:18 +00:00
|
|
|
var root = this._doc.documentElement;
|
|
|
|
var active = !!aData.headerURL;
|
|
|
|
|
|
|
|
if (active) {
|
2009-09-22 08:40:12 +00:00
|
|
|
root.style.color = aData.textcolor || "black";
|
|
|
|
root.style.backgroundColor = aData.accentcolor || "white";
|
2009-09-04 10:58:18 +00:00
|
|
|
let [r, g, b] = _parseRGB(this._doc.defaultView.getComputedStyle(root, "").color);
|
2009-10-02 06:22:08 +00:00
|
|
|
let luminance = 0.2125 * r + 0.7154 * g + 0.0721 * b;
|
|
|
|
root.setAttribute("lwthemetextcolor", luminance <= 110 ? "dark" : "bright");
|
2009-09-04 10:58:18 +00:00
|
|
|
root.setAttribute("lwtheme", "true");
|
|
|
|
} else {
|
|
|
|
root.style.color = "";
|
2009-09-22 08:40:12 +00:00
|
|
|
root.style.backgroundColor = "";
|
2009-09-04 10:58:18 +00: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 08:46:14 +00:00
|
|
|
footer.style.backgroundColor = active ? aData.accentcolor || "white" : "";
|
2009-09-04 10:58:18 +00:00
|
|
|
_setImage(footer, active, aData.footerURL);
|
|
|
|
if (active && aData.footerURL)
|
|
|
|
footer.setAttribute("lwthemefooter", "true");
|
|
|
|
else
|
|
|
|
footer.removeAttribute("lwthemefooter");
|
|
|
|
}
|
|
|
|
|
2009-10-04 09:28:45 +00:00
|
|
|
#ifdef XP_MACOSX
|
2009-10-21 16:02:43 +00:00
|
|
|
if (active)
|
|
|
|
root.setAttribute("drawintitlebar", "true");
|
|
|
|
else
|
|
|
|
root.removeAttribute("drawintitlebar");
|
2009-10-04 09:28:45 +00:00
|
|
|
#endif
|
2009-09-04 10:58:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function _setImage(aElement, aActive, aURL) {
|
|
|
|
aElement.style.backgroundImage =
|
2012-01-18 21:10:39 +00:00
|
|
|
(aActive && aURL) ? 'url("' + aURL.replace(/"/g, '\\"') + '")' : "";
|
2009-09-04 10:58:18 +00:00
|
|
|
}
|
|
|
|
|
2011-02-16 21:41:59 +00:00
|
|
|
function _parseRGB(aColorString) {
|
|
|
|
var rgb = aColorString.match(/^rgba?\((\d+), (\d+), (\d+)/);
|
|
|
|
rgb.shift();
|
|
|
|
return rgb.map(function (x) parseInt(x));
|
2009-09-04 10:58:18 +00:00
|
|
|
}
|