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
|
|
|
|
2018-02-23 22:50:01 +03:00
|
|
|
var EXPORTED_SYMBOLS = ["LightweightThemeConsumer"];
|
2009-09-04 14:58:18 +04:00
|
|
|
|
2018-01-30 02:20:18 +03:00
|
|
|
ChromeUtils.import("resource://gre/modules/XPCOMUtils.jsm");
|
|
|
|
ChromeUtils.import("resource://gre/modules/Services.jsm");
|
|
|
|
ChromeUtils.import("resource://gre/modules/AppConstants.jsm");
|
2012-09-22 23:24:26 +04:00
|
|
|
|
2018-02-12 20:24:27 +03:00
|
|
|
// Get the theme variables from the app resource directory.
|
|
|
|
// This allows per-app variables.
|
2018-02-07 08:22:22 +03:00
|
|
|
const toolkitVariableMap = [
|
|
|
|
["--arrowpanel-background", "popup"],
|
|
|
|
["--arrowpanel-color", "popup_text"],
|
|
|
|
["--arrowpanel-border-color", "popup_border"],
|
|
|
|
];
|
2018-02-12 20:24:27 +03:00
|
|
|
ChromeUtils.import("resource:///modules/ThemeVariableMap.jsm");
|
|
|
|
|
2018-01-30 02:20:18 +03:00
|
|
|
ChromeUtils.defineModuleGetter(this, "LightweightThemeImageOptimizer",
|
2014-01-08 08:14:08 +04:00
|
|
|
"resource://gre/modules/addons/LightweightThemeImageOptimizer.jsm");
|
2012-09-22 23:24:26 +04:00
|
|
|
|
2018-02-23 22:50:01 +03:00
|
|
|
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
|
|
|
|
2017-04-14 22:51:38 +03:00
|
|
|
Services.obs.addObserver(this, "lightweight-theme-styling-update");
|
2009-09-04 14:58:18 +04:00
|
|
|
|
|
|
|
var temp = {};
|
2018-01-30 02:20:18 +03:00
|
|
|
ChromeUtils.import("resource://gre/modules/LightweightThemeManager.jsm", temp);
|
2009-11-06 10:03:22 +03:00
|
|
|
this._update(temp.LightweightThemeManager.currentThemeForDisplay);
|
2018-02-15 20:03:25 +03:00
|
|
|
this._win.addEventListener("unload", this, { once: true });
|
2018-02-23 22:50:01 +03:00
|
|
|
}
|
2009-09-04 14:58:18 +04:00
|
|
|
|
|
|
|
LightweightThemeConsumer.prototype = {
|
2012-09-22 23:24:26 +04:00
|
|
|
_lastData: 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:
|
2017-10-15 21:50:30 +03:00
|
|
|
let lastData = this._lastData;
|
2013-10-22 14:20:38 +04:00
|
|
|
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;
|
|
|
|
|
2017-08-04 23:08:57 +03:00
|
|
|
const { outerWindowID } = this._win
|
|
|
|
.QueryInterface(Ci.nsIInterfaceRequestor)
|
|
|
|
.getInterface(Ci.nsIDOMWindowUtils);
|
|
|
|
|
|
|
|
const parsedData = JSON.parse(aData);
|
2017-08-07 13:52:25 +03:00
|
|
|
if (parsedData && parsedData.window && parsedData.window !== outerWindowID) {
|
2017-08-04 23:08:57 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this._update(parsedData);
|
2009-09-04 14:58:18 +04:00
|
|
|
},
|
|
|
|
|
2016-12-30 02:34:54 +03:00
|
|
|
handleEvent(aEvent) {
|
2018-02-15 20:03:25 +03:00
|
|
|
switch (aEvent.type) {
|
|
|
|
case "unload":
|
|
|
|
Services.obs.removeObserver(this, "lightweight-theme-styling-update");
|
|
|
|
this._win = this._doc = null;
|
|
|
|
break;
|
2012-09-22 23:24:26 +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;
|
2018-02-15 16:37:30 +03:00
|
|
|
let active = !!aData.accentcolor;
|
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");
|
2017-12-18 19:50:50 +03:00
|
|
|
let textcolor = aData.textcolor || "black";
|
2017-03-31 15:48:44 +03:00
|
|
|
_setProperty(root, active, "--lwt-text-color", textcolor);
|
2017-09-29 19:49:55 +03:00
|
|
|
_setProperty(root, active, "--lwt-accent-color", this._sanitizeCSSColor(aData.accentcolor) || "white");
|
2017-12-18 19:50:50 +03:00
|
|
|
|
2009-09-04 14:58:18 +04:00
|
|
|
if (active) {
|
2017-02-28 20:15:04 +03:00
|
|
|
let dummy = this._doc.createElement("dummy");
|
|
|
|
dummy.style.color = textcolor;
|
2017-12-18 19:50:50 +03:00
|
|
|
let [r, g, b] = _parseRGB(this._win.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");
|
|
|
|
}
|
|
|
|
|
2018-02-15 16:37:30 +03:00
|
|
|
if (aData.headerURL) {
|
|
|
|
root.setAttribute("lwtheme-image", "true");
|
|
|
|
} else {
|
|
|
|
root.removeAttribute("lwtheme-image");
|
|
|
|
}
|
|
|
|
|
2014-02-05 01:17:00 +04:00
|
|
|
this._active = active;
|
|
|
|
|
2017-03-07 02:10:39 +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, name, value);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
root.removeAttribute("lwthemeicons");
|
|
|
|
}
|
|
|
|
|
2017-03-03 21:41:28 +03:00
|
|
|
_setImage(root, active, "--lwt-header-image", aData.headerURL);
|
2017-03-07 01:18:57 +03:00
|
|
|
_setImage(root, active, "--lwt-footer-image", aData.footerURL);
|
2017-03-31 15:48:44 +03:00
|
|
|
_setImage(root, active, "--lwt-additional-images", aData.additionalBackgrounds);
|
2017-06-15 20:20:26 +03:00
|
|
|
_setProperties(root, active, aData);
|
2017-03-07 02:10:39 +03:00
|
|
|
|
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-06-03 00:59:38 +04:00
|
|
|
Services.obs.notifyObservers(this._win, "lightweight-theme-window-updated",
|
|
|
|
JSON.stringify(aData));
|
2017-09-29 19:49:55 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
_sanitizeCSSColor(cssColor) {
|
|
|
|
// style.color normalizes color values and rejects invalid ones, so a
|
|
|
|
// simple round trip gets us a sanitized color value.
|
|
|
|
let span = this._doc.createElementNS("http://www.w3.org/1999/xhtml", "span");
|
|
|
|
span.style.color = cssColor;
|
2017-12-18 19:50:50 +03:00
|
|
|
cssColor = this._win.getComputedStyle(span).color;
|
|
|
|
if (cssColor == "rgba(0, 0, 0, 0)" ||
|
|
|
|
!cssColor) {
|
2017-09-29 19:49:55 +03:00
|
|
|
return "";
|
|
|
|
}
|
2017-12-18 19:50:50 +03:00
|
|
|
// Remove alpha channel from color
|
|
|
|
return `rgb(${_parseRGB(cssColor).join(", ")})`;
|
2009-09-04 14:58:18 +04:00
|
|
|
}
|
2017-10-15 21:50:30 +03:00
|
|
|
};
|
2009-09-04 14:58:18 +04:00
|
|
|
|
2017-03-31 15:48:44 +03:00
|
|
|
function _setImage(aRoot, aActive, aVariableName, aURLs) {
|
|
|
|
if (aURLs && !Array.isArray(aURLs)) {
|
|
|
|
aURLs = [aURLs];
|
|
|
|
}
|
|
|
|
_setProperty(aRoot, aActive, aVariableName, aURLs && aURLs.map(v => `url("${v.replace(/"/g, '\\"')}")`).join(","));
|
|
|
|
}
|
|
|
|
|
2018-02-02 17:54:52 +03:00
|
|
|
function _setProperty(elem, active, variableName, value) {
|
2017-03-31 15:48:44 +03:00
|
|
|
if (active && value) {
|
2018-02-02 17:54:52 +03:00
|
|
|
elem.style.setProperty(variableName, value);
|
2017-02-22 22:13:09 +03:00
|
|
|
} else {
|
2018-02-02 17:54:52 +03:00
|
|
|
elem.style.removeProperty(variableName);
|
2017-02-22 22:13:09 +03:00
|
|
|
}
|
2009-09-04 14:58:18 +04:00
|
|
|
}
|
|
|
|
|
2017-06-15 20:20:26 +03:00
|
|
|
function _setProperties(root, active, vars) {
|
2018-02-07 08:22:22 +03:00
|
|
|
for (let map of [toolkitVariableMap, ThemeVariableMap]) {
|
|
|
|
for (let [cssVarName, varsKey, optionalElementID] of map) {
|
|
|
|
let elem = optionalElementID ? root.ownerDocument.getElementById(optionalElementID)
|
|
|
|
: root;
|
|
|
|
_setProperty(elem, active, cssVarName, vars[varsKey]);
|
|
|
|
}
|
2017-06-15 20:20:26 +03: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
|
|
|
}
|