2012-05-21 15:12:37 +04: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 14:58:18 +04:00
|
|
|
|
2012-10-31 20:13:28 +04:00
|
|
|
this.EXPORTED_SYMBOLS = ["LightweightThemeConsumer"];
|
2009-09-04 14:58:18 +04:00
|
|
|
|
2014-06-03 00:59:38 +04:00
|
|
|
const {utils: Cu} = Components;
|
|
|
|
|
|
|
|
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
|
|
|
|
Cu.import("resource://gre/modules/Services.jsm");
|
2015-04-07 11:09:49 +03:00
|
|
|
Cu.import("resource://gre/modules/AppConstants.jsm");
|
2012-09-22 23:24:26 +04:00
|
|
|
|
|
|
|
XPCOMUtils.defineLazyModuleGetter(this, "LightweightThemeImageOptimizer",
|
2014-01-08 08:14:08 +04:00
|
|
|
"resource://gre/modules/addons/LightweightThemeImageOptimizer.jsm");
|
2012-09-22 23:24:26 +04:00
|
|
|
|
2013-01-07 22:24:08 +04:00
|
|
|
XPCOMUtils.defineLazyModuleGetter(this, "PrivateBrowsingUtils",
|
|
|
|
"resource://gre/modules/PrivateBrowsingUtils.jsm");
|
|
|
|
|
2012-10-31 20:13:28 +04:00
|
|
|
this.LightweightThemeConsumer =
|
|
|
|
function LightweightThemeConsumer(aDocument) {
|
2009-09-04 14:58:18 +04:00
|
|
|
this._doc = aDocument;
|
2012-09-22 23:24:26 +04:00
|
|
|
this._win = aDocument.defaultView;
|
2009-09-04 14:58:18 +04:00
|
|
|
this._footerId = aDocument.documentElement.getAttribute("lightweightthemesfooter");
|
|
|
|
|
2013-03-29 19:29:29 +04:00
|
|
|
if (PrivateBrowsingUtils.isWindowPrivate(this._win) &&
|
|
|
|
!PrivateBrowsingUtils.permanentPrivateBrowsing) {
|
2013-01-07 22:24:08 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-09-22 23:24:26 +04:00
|
|
|
let screen = this._win.screen;
|
|
|
|
this._lastScreenWidth = screen.width;
|
|
|
|
this._lastScreenHeight = screen.height;
|
|
|
|
|
2014-06-03 00:59:38 +04:00
|
|
|
Services.obs.addObserver(this, "lightweight-theme-styling-update", false);
|
2009-09-04 14:58:18 +04:00
|
|
|
|
|
|
|
var temp = {};
|
2014-06-03 00:59:38 +04:00
|
|
|
Cu.import("resource://gre/modules/LightweightThemeManager.jsm", temp);
|
2009-11-06 10:03:22 +03:00
|
|
|
this._update(temp.LightweightThemeManager.currentThemeForDisplay);
|
2012-09-22 23:24:26 +04:00
|
|
|
this._win.addEventListener("resize", this);
|
2009-09-04 14:58:18 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
LightweightThemeConsumer.prototype = {
|
2012-09-22 23:24:26 +04:00
|
|
|
_lastData: null,
|
|
|
|
_lastScreenWidth: null,
|
|
|
|
_lastScreenHeight: null,
|
2014-03-21 00:55:49 +04:00
|
|
|
// Whether the active lightweight theme should be shown on the window.
|
2013-10-22 14:20:38 +04:00
|
|
|
_enabled: true,
|
2014-03-21 00:55:49 +04:00
|
|
|
// Whether a lightweight theme is enabled.
|
2014-02-05 01:17:00 +04:00
|
|
|
_active: false,
|
2012-09-22 23:24:26 +04:00
|
|
|
|
2013-10-22 14:20:38 +04:00
|
|
|
enable: function() {
|
|
|
|
this._enabled = true;
|
|
|
|
this._update(this._lastData);
|
|
|
|
},
|
|
|
|
|
|
|
|
disable: function() {
|
|
|
|
// Dance to keep the data, but reset the applied styles:
|
|
|
|
let lastData = this._lastData
|
|
|
|
this._update(null);
|
|
|
|
this._enabled = false;
|
|
|
|
this._lastData = lastData;
|
|
|
|
},
|
|
|
|
|
2014-06-03 00:59:38 +04:00
|
|
|
getData: function() {
|
|
|
|
return this._enabled ? Cu.cloneInto(this._lastData, this._win) : null;
|
|
|
|
},
|
|
|
|
|
2009-09-04 14:58:18 +04:00
|
|
|
observe: function (aSubject, aTopic, aData) {
|
2009-11-24 18:59:53 +03:00
|
|
|
if (aTopic != "lightweight-theme-styling-update")
|
2009-09-04 14:58:18 +04:00
|
|
|
return;
|
|
|
|
|
|
|
|
this._update(JSON.parse(aData));
|
|
|
|
},
|
|
|
|
|
2012-09-22 23:24:26 +04:00
|
|
|
handleEvent: function (aEvent) {
|
|
|
|
let {width, height} = this._win.screen;
|
|
|
|
|
|
|
|
if (this._lastScreenWidth != width || this._lastScreenHeight != height) {
|
|
|
|
this._lastScreenWidth = width;
|
|
|
|
this._lastScreenHeight = height;
|
2014-03-21 00:55:49 +04:00
|
|
|
if (!this._active)
|
|
|
|
return;
|
2012-09-22 23:24:26 +04:00
|
|
|
this._update(this._lastData);
|
2014-03-21 00:55:49 +04:00
|
|
|
Services.obs.notifyObservers(this._win, "lightweight-theme-optimized",
|
|
|
|
JSON.stringify(this._lastData));
|
2012-09-22 23:24:26 +04:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2009-09-04 14:58:18 +04:00
|
|
|
destroy: function () {
|
2013-03-29 19:29:29 +04:00
|
|
|
if (!PrivateBrowsingUtils.isWindowPrivate(this._win) ||
|
|
|
|
PrivateBrowsingUtils.permanentPrivateBrowsing) {
|
2014-06-03 00:59:38 +04:00
|
|
|
Services.obs.removeObserver(this, "lightweight-theme-styling-update");
|
2013-02-01 23:31:32 +04:00
|
|
|
|
|
|
|
this._win.removeEventListener("resize", this);
|
|
|
|
}
|
2009-09-04 14:58:18 +04:00
|
|
|
|
2012-09-22 23:24:26 +04:00
|
|
|
this._win = this._doc = null;
|
2009-09-04 14:58:18 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
_update: function (aData) {
|
2013-07-01 19:12:23 +04:00
|
|
|
if (!aData) {
|
2009-09-22 12:40:12 +04:00
|
|
|
aData = { headerURL: "", footerURL: "", textcolor: "", accentcolor: "" };
|
2013-07-01 19:12:23 +04:00
|
|
|
this._lastData = aData;
|
|
|
|
} else {
|
|
|
|
this._lastData = aData;
|
|
|
|
aData = LightweightThemeImageOptimizer.optimize(aData, this._win.screen);
|
|
|
|
}
|
2013-10-22 14:20:38 +04:00
|
|
|
if (!this._enabled)
|
|
|
|
return;
|
2012-09-22 23:24:26 +04:00
|
|
|
|
2014-02-05 01:17:00 +04:00
|
|
|
let root = this._doc.documentElement;
|
|
|
|
let active = !!aData.headerURL;
|
|
|
|
let stateChanging = (active != this._active);
|
2009-09-04 14:58:18 +04:00
|
|
|
|
2015-04-01 17:16:07 +03:00
|
|
|
// We need to clear these either way: either because the theme is being removed,
|
|
|
|
// or because we are applying a new theme and the data might be bogus CSS,
|
|
|
|
// so if we don't reset first, it'll keep the old value.
|
|
|
|
root.style.removeProperty("color");
|
|
|
|
root.style.removeProperty("background-color");
|
2009-09-04 14:58:18 +04:00
|
|
|
if (active) {
|
2009-09-22 12:40:12 +04:00
|
|
|
root.style.color = aData.textcolor || "black";
|
|
|
|
root.style.backgroundColor = aData.accentcolor || "white";
|
2009-09-04 14:58:18 +04:00
|
|
|
let [r, g, b] = _parseRGB(this._doc.defaultView.getComputedStyle(root, "").color);
|
2009-10-02 10:22:08 +04:00
|
|
|
let luminance = 0.2125 * r + 0.7154 * g + 0.0721 * b;
|
|
|
|
root.setAttribute("lwthemetextcolor", luminance <= 110 ? "dark" : "bright");
|
2009-09-04 14:58:18 +04:00
|
|
|
root.setAttribute("lwtheme", "true");
|
|
|
|
} else {
|
|
|
|
root.removeAttribute("lwthemetextcolor");
|
|
|
|
root.removeAttribute("lwtheme");
|
|
|
|
}
|
|
|
|
|
2014-02-05 01:17:00 +04:00
|
|
|
this._active = active;
|
|
|
|
|
2009-09-04 14:58:18 +04:00
|
|
|
_setImage(root, active, aData.headerURL);
|
|
|
|
if (this._footerId) {
|
|
|
|
let footer = this._doc.getElementById(this._footerId);
|
2010-09-04 12:46:14 +04:00
|
|
|
footer.style.backgroundColor = active ? aData.accentcolor || "white" : "";
|
2009-09-04 14:58:18 +04:00
|
|
|
_setImage(footer, active, aData.footerURL);
|
|
|
|
if (active && aData.footerURL)
|
|
|
|
footer.setAttribute("lwthemefooter", "true");
|
|
|
|
else
|
|
|
|
footer.removeAttribute("lwthemefooter");
|
|
|
|
}
|
|
|
|
|
2014-02-05 01:17:00 +04:00
|
|
|
// On OS X, we extend the lightweight theme into the titlebar, which means setting
|
|
|
|
// the chromemargin attribute. Some XUL applications already draw in the titlebar,
|
|
|
|
// so we need to save the chromemargin value before we overwrite it with the value
|
|
|
|
// that lets us draw in the titlebar. We stash this value on the root attribute so
|
|
|
|
// that XUL applications have the ability to invalidate the saved value.
|
2015-04-07 11:09:49 +03:00
|
|
|
if (AppConstants.platform == "macosx" && stateChanging) {
|
2014-02-05 01:17:00 +04:00
|
|
|
if (!root.hasAttribute("chromemargin-nonlwtheme")) {
|
|
|
|
root.setAttribute("chromemargin-nonlwtheme", root.getAttribute("chromemargin"));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (active) {
|
|
|
|
root.setAttribute("chromemargin", "0,-1,-1,-1");
|
|
|
|
} else {
|
|
|
|
let defaultChromemargin = root.getAttribute("chromemargin-nonlwtheme");
|
|
|
|
if (defaultChromemargin) {
|
|
|
|
root.setAttribute("chromemargin", defaultChromemargin);
|
|
|
|
} else {
|
|
|
|
root.removeAttribute("chromemargin");
|
|
|
|
}
|
|
|
|
}
|
2013-03-01 22:48:18 +04:00
|
|
|
}
|
2014-06-03 00:59:38 +04:00
|
|
|
Services.obs.notifyObservers(this._win, "lightweight-theme-window-updated",
|
|
|
|
JSON.stringify(aData));
|
2009-09-04 14:58:18 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function _setImage(aElement, aActive, aURL) {
|
|
|
|
aElement.style.backgroundImage =
|
2012-01-19 01:10:39 +04:00
|
|
|
(aActive && aURL) ? 'url("' + aURL.replace(/"/g, '\\"') + '")' : "";
|
2009-09-04 14:58:18 +04:00
|
|
|
}
|
|
|
|
|
2011-02-17 00:41:59 +03:00
|
|
|
function _parseRGB(aColorString) {
|
|
|
|
var rgb = aColorString.match(/^rgba?\((\d+), (\d+), (\d+)/);
|
|
|
|
rgb.shift();
|
2015-09-24 15:20:04 +03:00
|
|
|
return rgb.map(x => parseInt(x));
|
2009-09-04 14:58:18 +04:00
|
|
|
}
|