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
|
|
|
|
2017-08-04 23:08:57 +03:00
|
|
|
const {utils: Cu, interfaces: Ci, classes: Cc} = Components;
|
2014-06-03 00:59:38 +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-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
|
|
|
|
2017-06-15 20:20:26 +03:00
|
|
|
const kCSSVarsMap = new Map([
|
|
|
|
["--lwt-background-alignment", "backgroundsAlignment"],
|
|
|
|
["--lwt-background-tiling", "backgroundsTiling"],
|
2017-12-05 22:16:00 +03:00
|
|
|
["--lwt-tab-text", "tab_text"],
|
2017-08-24 16:55:02 +03:00
|
|
|
["--toolbar-bgcolor", "toolbarColor"],
|
2017-08-30 16:23:58 +03:00
|
|
|
["--toolbar-color", "toolbar_text"],
|
2017-08-24 16:55:02 +03:00
|
|
|
["--url-and-searchbar-background-color", "toolbar_field"],
|
2017-11-03 21:00:51 +03:00
|
|
|
["--url-and-searchbar-color", "toolbar_field_text"],
|
2017-12-05 22:16:00 +03:00
|
|
|
["--lwt-toolbar-field-border-color", "toolbar_field_border"],
|
2017-11-03 21:00:51 +03:00
|
|
|
["--tabs-border-color", "toolbar_top_separator"],
|
|
|
|
["--toolbox-border-bottom-color", "toolbar_bottom_separator"],
|
|
|
|
["--urlbar-separator-color", "toolbar_vertical_separator"],
|
2017-06-15 20:20:26 +03: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;
|
|
|
|
|
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);
|
2012-09-22 23:24:26 +04:00
|
|
|
this._win.addEventListener("resize", this);
|
2017-10-15 21:50:30 +03:00
|
|
|
};
|
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:
|
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) {
|
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;
|
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");
|
|
|
|
}
|
|
|
|
|
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(","));
|
|
|
|
}
|
|
|
|
|
|
|
|
function _setProperty(root, active, variableName, value) {
|
|
|
|
if (active && value) {
|
|
|
|
root.style.setProperty(variableName, value);
|
2017-02-22 22:13:09 +03:00
|
|
|
} else {
|
2017-03-31 15:48:44 +03:00
|
|
|
root.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) {
|
|
|
|
for (let [cssVarName, varsKey] of kCSSVarsMap) {
|
|
|
|
_setProperty(root, active, cssVarName, vars[varsKey]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|