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
|
|
|
|
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
|
|
|
|
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
|
|
|
|
2016-12-30 02:34:54 +03:00
|
|
|
enable() {
|
2013-10-22 14:20:38 +04:00
|
|
|
this._enabled = true;
|
|
|
|
this._update(this._lastData);
|
|
|
|
},
|
|
|
|
|
2016-12-30 02:34:54 +03:00
|
|
|
disable() {
|
2013-10-22 14:20:38 +04:00
|
|
|
// Dance to keep the data, but reset the applied styles:
|
|
|
|
let lastData = this._lastData
|
|
|
|
this._update(null);
|
|
|
|
this._enabled = false;
|
|
|
|
this._lastData = lastData;
|
|
|
|
},
|
|
|
|
|
2016-12-30 02:34:54 +03:00
|
|
|
getData() {
|
2014-06-03 00:59:38 +04:00
|
|
|
return this._enabled ? Cu.cloneInto(this._lastData, this._win) : null;
|
|
|
|
},
|
|
|
|
|
2016-12-30 02:34:54 +03:00
|
|
|
observe(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));
|
|
|
|
},
|
|
|
|
|
2016-12-30 02:34:54 +03:00
|
|
|
handleEvent(aEvent) {
|
2012-09-22 23:24:26 +04:00
|
|
|
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
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2016-12-30 02:34:54 +03:00
|
|
|
destroy() {
|
2017-01-17 20:23:29 +03:00
|
|
|
Services.obs.removeObserver(this, "lightweight-theme-styling-update");
|
2013-02-01 23:31:32 +04:00
|
|
|
|
2017-01-17 20:23:29 +03: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
|
|
|
},
|
|
|
|
|
2016-12-30 02:34:54 +03:00
|
|
|
_update(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.
|
2017-02-28 22:19:59 +03:00
|
|
|
root.style.removeProperty("--lwt-text-color");
|
|
|
|
root.style.removeProperty("--lwt-accent-color");
|
2009-09-04 14:58:18 +04:00
|
|
|
if (active) {
|
2017-02-28 20:15:04 +03:00
|
|
|
let textcolor = aData.textcolor || "black";
|
2017-02-28 22:19:59 +03:00
|
|
|
root.style.setProperty("--lwt-text-color", textcolor);
|
|
|
|
root.style.setProperty("--lwt-accent-color", aData.accentcolor || "white");
|
2017-02-28 20:15:04 +03:00
|
|
|
let dummy = this._doc.createElement("dummy");
|
|
|
|
dummy.style.color = textcolor;
|
|
|
|
let [r, g, b] = _parseRGB(this._doc.defaultView.getComputedStyle(dummy).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;
|
|
|
|
|
2017-03-06 23:16:35 +03:00
|
|
|
if (aData.icons) {
|
|
|
|
let activeIcons = active ? Object.keys(aData.icons).join(" ") : "";
|
|
|
|
root.setAttribute("lwthemeicons", activeIcons);
|
|
|
|
for (let [name, value] of Object.entries(aData.icons)) {
|
|
|
|
_setImage(root, active, value, name);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
root.removeAttribute("lwthemeicons");
|
|
|
|
}
|
|
|
|
|
2017-03-03 21:41:28 +03:00
|
|
|
_setImage(root, active, "--lwt-header-image", aData.headerURL);
|
2017-03-06 23:16:35 +03:00
|
|
|
_setImage(root, active, "--lwt-header-image", aData.footerURL);
|
|
|
|
|
2017-03-03 21:41:28 +03:00
|
|
|
if (active && aData.footerURL)
|
|
|
|
root.setAttribute("lwthemefooter", "true");
|
|
|
|
else
|
|
|
|
root.removeAttribute("lwthemefooter");
|
2009-09-04 14:58:18 +04:00
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-03 21:41:28 +03:00
|
|
|
function _setImage(aRoot, aActive, aVariableName, aURL) {
|
2017-02-22 22:13:09 +03:00
|
|
|
if (aActive && aURL) {
|
2017-03-03 21:41:28 +03:00
|
|
|
aRoot.style.setProperty(aVariableName, `url("${aURL.replace(/"/g, '\\"')}")`);
|
2017-02-22 22:13:09 +03:00
|
|
|
} else {
|
2017-03-03 21:41:28 +03:00
|
|
|
aRoot.style.removeProperty(aVariableName);
|
2017-02-22 22:13:09 +03:00
|
|
|
}
|
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
|
|
|
}
|