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-07 08:22:22 +03:00
|
|
|
const toolkitVariableMap = [
|
2018-03-27 14:40:06 +03:00
|
|
|
["--lwt-accent-color", {
|
|
|
|
lwtProperty: "accentcolor",
|
|
|
|
processColor(rgbaChannels, element) {
|
2018-04-11 22:05:13 +03:00
|
|
|
if (!rgbaChannels || rgbaChannels.a == 0) {
|
2018-03-27 14:40:06 +03:00
|
|
|
return "white";
|
|
|
|
}
|
|
|
|
// Remove the alpha channel
|
|
|
|
const {r, g, b} = rgbaChannels;
|
|
|
|
return `rgb(${r}, ${g}, ${b})`;
|
|
|
|
}
|
|
|
|
}],
|
|
|
|
["--lwt-text-color", {
|
|
|
|
lwtProperty: "textcolor",
|
|
|
|
processColor(rgbaChannels, element) {
|
|
|
|
if (!rgbaChannels) {
|
2018-05-11 20:48:53 +03:00
|
|
|
rgbaChannels = {r: 0, g: 0, b: 0};
|
2018-03-27 14:40:06 +03:00
|
|
|
}
|
2018-05-11 20:48:53 +03:00
|
|
|
// Remove the alpha channel
|
|
|
|
const {r, g, b} = rgbaChannels;
|
2018-04-26 12:48:22 +03:00
|
|
|
const luminance = _getLuminance(r, g, b);
|
2018-03-27 14:40:06 +03:00
|
|
|
element.setAttribute("lwthemetextcolor", luminance <= 110 ? "dark" : "bright");
|
2018-05-11 20:48:53 +03:00
|
|
|
return `rgba(${r}, ${g}, ${b})`;
|
2018-03-27 14:40:06 +03:00
|
|
|
}
|
|
|
|
}],
|
2018-04-18 18:54:38 +03:00
|
|
|
["--arrowpanel-background", {
|
2018-03-27 14:40:06 +03:00
|
|
|
lwtProperty: "popup"
|
|
|
|
}],
|
2018-04-18 18:54:38 +03:00
|
|
|
["--arrowpanel-color", {
|
2018-03-27 14:40:06 +03:00
|
|
|
lwtProperty: "popup_text"
|
|
|
|
}],
|
2018-04-18 18:54:38 +03:00
|
|
|
["--arrowpanel-border-color", {
|
2018-03-27 14:40:06 +03:00
|
|
|
lwtProperty: "popup_border"
|
|
|
|
}],
|
2018-02-11 01:24:50 +03:00
|
|
|
["--lwt-toolbar-field-background-color", {
|
|
|
|
lwtProperty: "toolbar_field"
|
|
|
|
}],
|
|
|
|
["--lwt-toolbar-field-color", {
|
2018-04-26 12:48:22 +03:00
|
|
|
lwtProperty: "toolbar_field_text",
|
|
|
|
processColor(rgbaChannels, element) {
|
|
|
|
if (!rgbaChannels) {
|
|
|
|
element.removeAttribute("lwt-toolbar-field-brighttext");
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
const {r, g, b, a} = rgbaChannels;
|
|
|
|
const luminance = _getLuminance(r, g, b);
|
|
|
|
if (luminance <= 110) {
|
|
|
|
element.removeAttribute("lwt-toolbar-field-brighttext");
|
|
|
|
} else {
|
|
|
|
element.setAttribute("lwt-toolbar-field-brighttext", "true");
|
|
|
|
}
|
|
|
|
return `rgba(${r}, ${g}, ${b}, ${a})`;
|
|
|
|
}
|
2018-02-11 01:24:50 +03:00
|
|
|
}],
|
2018-02-07 08:22:22 +03:00
|
|
|
];
|
2018-03-27 14:40:06 +03:00
|
|
|
|
|
|
|
// Get the theme variables from the app resource directory.
|
|
|
|
// This allows per-app variables.
|
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-04-20 15:21:00 +03:00
|
|
|
|
|
|
|
this._win.addEventListener("resolutionchange", this);
|
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) {
|
2018-04-20 15:21:00 +03:00
|
|
|
case "resolutionchange":
|
|
|
|
if (this._active) {
|
|
|
|
this._update(this._lastData);
|
|
|
|
}
|
|
|
|
break;
|
2018-02-15 20:03:25 +03:00
|
|
|
case "unload":
|
|
|
|
Services.obs.removeObserver(this, "lightweight-theme-styling-update");
|
2018-04-20 15:21:00 +03:00
|
|
|
this._win.removeEventListener("resolutionchange", this);
|
2018-02-15 20:03:25 +03:00
|
|
|
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;
|
2009-09-04 14:58:18 +04:00
|
|
|
|
2018-02-15 16:37:30 +03:00
|
|
|
if (aData.headerURL) {
|
|
|
|
root.setAttribute("lwtheme-image", "true");
|
|
|
|
} else {
|
|
|
|
root.removeAttribute("lwtheme-image");
|
|
|
|
}
|
|
|
|
|
2018-04-16 11:24:48 +03:00
|
|
|
let active = aData.accentcolor || aData.headerURL;
|
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
|
|
|
|
2018-05-11 20:48:53 +03:00
|
|
|
if (active) {
|
|
|
|
root.setAttribute("lwtheme", "true");
|
|
|
|
} else {
|
|
|
|
root.removeAttribute("lwtheme");
|
|
|
|
root.removeAttribute("lwthemetextcolor");
|
|
|
|
}
|
|
|
|
|
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));
|
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
|
|
|
}
|
|
|
|
|
2018-03-27 14:40:06 +03:00
|
|
|
function _setProperties(root, active, themeData) {
|
2018-02-07 08:22:22 +03:00
|
|
|
for (let map of [toolkitVariableMap, ThemeVariableMap]) {
|
2018-03-27 14:40:06 +03:00
|
|
|
for (let [cssVarName, definition] of map) {
|
|
|
|
const {
|
|
|
|
lwtProperty,
|
|
|
|
optionalElementID,
|
|
|
|
processColor,
|
|
|
|
isColor = true
|
|
|
|
} = definition;
|
2018-02-07 08:22:22 +03:00
|
|
|
let elem = optionalElementID ? root.ownerDocument.getElementById(optionalElementID)
|
|
|
|
: root;
|
2018-03-27 14:40:06 +03:00
|
|
|
|
|
|
|
let val = themeData[lwtProperty];
|
|
|
|
if (isColor) {
|
|
|
|
val = _sanitizeCSSColor(root.ownerDocument, val);
|
|
|
|
if (processColor) {
|
|
|
|
val = processColor(_parseRGBA(val), elem);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_setProperty(elem, active, cssVarName, val);
|
2018-02-07 08:22:22 +03:00
|
|
|
}
|
2017-06-15 20:20:26 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-27 14:40:06 +03:00
|
|
|
function _sanitizeCSSColor(doc, cssColor) {
|
|
|
|
if (!cssColor) {
|
|
|
|
return null;
|
2018-02-11 20:32:33 +03:00
|
|
|
}
|
2018-04-11 22:05:13 +03:00
|
|
|
const HTML_NS = "http://www.w3.org/1999/xhtml";
|
|
|
|
// style.color normalizes color values and makes invalid ones black, so a
|
2018-03-27 14:40:06 +03:00
|
|
|
// simple round trip gets us a sanitized color value.
|
2018-04-11 22:05:13 +03:00
|
|
|
let div = doc.createElementNS(HTML_NS, "div");
|
|
|
|
div.style.color = "black";
|
|
|
|
let span = doc.createElementNS(HTML_NS, "span");
|
2018-03-27 14:40:06 +03:00
|
|
|
span.style.color = cssColor;
|
2018-04-11 22:05:13 +03:00
|
|
|
div.appendChild(span);
|
2018-03-27 14:40:06 +03:00
|
|
|
cssColor = doc.defaultView.getComputedStyle(span).color;
|
|
|
|
return cssColor;
|
|
|
|
}
|
2018-02-11 20:32:33 +03:00
|
|
|
|
2018-03-27 14:40:06 +03:00
|
|
|
function _parseRGBA(aColorString) {
|
|
|
|
if (!aColorString) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
var rgba = aColorString.replace(/(rgba?\()|(\)$)/g, "").split(",");
|
|
|
|
rgba = rgba.map(x => parseFloat(x));
|
|
|
|
return {
|
|
|
|
r: rgba[0],
|
|
|
|
g: rgba[1],
|
|
|
|
b: rgba[2],
|
2018-04-11 22:05:13 +03:00
|
|
|
a: 3 in rgba ? rgba[3] : 1,
|
2018-03-27 14:40:06 +03:00
|
|
|
};
|
2018-02-11 20:32:33 +03:00
|
|
|
}
|
2018-04-26 12:48:22 +03:00
|
|
|
|
|
|
|
function _getLuminance(r, g, b) {
|
|
|
|
return 0.2125 * r + 0.7154 * g + 0.0721 * b;
|
|
|
|
}
|