toolkit/modules/LightweightThemeConsumer.jsm

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 this.EXPORTED_SYMBOLS = ["LightweightThemeConsumer"];
michael@0 6
michael@0 7 Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
michael@0 8 Components.utils.import("resource://gre/modules/Services.jsm");
michael@0 9
michael@0 10 XPCOMUtils.defineLazyModuleGetter(this, "LightweightThemeImageOptimizer",
michael@0 11 "resource://gre/modules/addons/LightweightThemeImageOptimizer.jsm");
michael@0 12
michael@0 13 XPCOMUtils.defineLazyModuleGetter(this, "PrivateBrowsingUtils",
michael@0 14 "resource://gre/modules/PrivateBrowsingUtils.jsm");
michael@0 15
michael@0 16 this.LightweightThemeConsumer =
michael@0 17 function LightweightThemeConsumer(aDocument) {
michael@0 18 this._doc = aDocument;
michael@0 19 this._win = aDocument.defaultView;
michael@0 20 this._footerId = aDocument.documentElement.getAttribute("lightweightthemesfooter");
michael@0 21
michael@0 22 if (PrivateBrowsingUtils.isWindowPrivate(this._win) &&
michael@0 23 !PrivateBrowsingUtils.permanentPrivateBrowsing) {
michael@0 24 return;
michael@0 25 }
michael@0 26
michael@0 27 let screen = this._win.screen;
michael@0 28 this._lastScreenWidth = screen.width;
michael@0 29 this._lastScreenHeight = screen.height;
michael@0 30
michael@0 31 Components.classes["@mozilla.org/observer-service;1"]
michael@0 32 .getService(Components.interfaces.nsIObserverService)
michael@0 33 .addObserver(this, "lightweight-theme-styling-update", false);
michael@0 34
michael@0 35 var temp = {};
michael@0 36 Components.utils.import("resource://gre/modules/LightweightThemeManager.jsm", temp);
michael@0 37 this._update(temp.LightweightThemeManager.currentThemeForDisplay);
michael@0 38 this._win.addEventListener("resize", this);
michael@0 39 }
michael@0 40
michael@0 41 LightweightThemeConsumer.prototype = {
michael@0 42 _lastData: null,
michael@0 43 _lastScreenWidth: null,
michael@0 44 _lastScreenHeight: null,
michael@0 45 // Whether the active lightweight theme should be shown on the window.
michael@0 46 _enabled: true,
michael@0 47 // Whether a lightweight theme is enabled.
michael@0 48 _active: false,
michael@0 49
michael@0 50 enable: function() {
michael@0 51 this._enabled = true;
michael@0 52 this._update(this._lastData);
michael@0 53 },
michael@0 54
michael@0 55 disable: function() {
michael@0 56 // Dance to keep the data, but reset the applied styles:
michael@0 57 let lastData = this._lastData
michael@0 58 this._update(null);
michael@0 59 this._enabled = false;
michael@0 60 this._lastData = lastData;
michael@0 61 },
michael@0 62
michael@0 63 observe: function (aSubject, aTopic, aData) {
michael@0 64 if (aTopic != "lightweight-theme-styling-update")
michael@0 65 return;
michael@0 66
michael@0 67 this._update(JSON.parse(aData));
michael@0 68 },
michael@0 69
michael@0 70 handleEvent: function (aEvent) {
michael@0 71 let {width, height} = this._win.screen;
michael@0 72
michael@0 73 if (this._lastScreenWidth != width || this._lastScreenHeight != height) {
michael@0 74 this._lastScreenWidth = width;
michael@0 75 this._lastScreenHeight = height;
michael@0 76 if (!this._active)
michael@0 77 return;
michael@0 78 this._update(this._lastData);
michael@0 79 Services.obs.notifyObservers(this._win, "lightweight-theme-optimized",
michael@0 80 JSON.stringify(this._lastData));
michael@0 81 }
michael@0 82 },
michael@0 83
michael@0 84 destroy: function () {
michael@0 85 if (!PrivateBrowsingUtils.isWindowPrivate(this._win) ||
michael@0 86 PrivateBrowsingUtils.permanentPrivateBrowsing) {
michael@0 87 Components.classes["@mozilla.org/observer-service;1"]
michael@0 88 .getService(Components.interfaces.nsIObserverService)
michael@0 89 .removeObserver(this, "lightweight-theme-styling-update");
michael@0 90
michael@0 91 this._win.removeEventListener("resize", this);
michael@0 92 }
michael@0 93
michael@0 94 this._win = this._doc = null;
michael@0 95 },
michael@0 96
michael@0 97 _update: function (aData) {
michael@0 98 if (!aData) {
michael@0 99 aData = { headerURL: "", footerURL: "", textcolor: "", accentcolor: "" };
michael@0 100 this._lastData = aData;
michael@0 101 } else {
michael@0 102 this._lastData = aData;
michael@0 103 aData = LightweightThemeImageOptimizer.optimize(aData, this._win.screen);
michael@0 104 }
michael@0 105 if (!this._enabled)
michael@0 106 return;
michael@0 107
michael@0 108 let root = this._doc.documentElement;
michael@0 109 let active = !!aData.headerURL;
michael@0 110 let stateChanging = (active != this._active);
michael@0 111
michael@0 112 if (active) {
michael@0 113 root.style.color = aData.textcolor || "black";
michael@0 114 root.style.backgroundColor = aData.accentcolor || "white";
michael@0 115 let [r, g, b] = _parseRGB(this._doc.defaultView.getComputedStyle(root, "").color);
michael@0 116 let luminance = 0.2125 * r + 0.7154 * g + 0.0721 * b;
michael@0 117 root.setAttribute("lwthemetextcolor", luminance <= 110 ? "dark" : "bright");
michael@0 118 root.setAttribute("lwtheme", "true");
michael@0 119 } else {
michael@0 120 root.style.color = "";
michael@0 121 root.style.backgroundColor = "";
michael@0 122 root.removeAttribute("lwthemetextcolor");
michael@0 123 root.removeAttribute("lwtheme");
michael@0 124 }
michael@0 125
michael@0 126 this._active = active;
michael@0 127
michael@0 128 _setImage(root, active, aData.headerURL);
michael@0 129 if (this._footerId) {
michael@0 130 let footer = this._doc.getElementById(this._footerId);
michael@0 131 footer.style.backgroundColor = active ? aData.accentcolor || "white" : "";
michael@0 132 _setImage(footer, active, aData.footerURL);
michael@0 133 if (active && aData.footerURL)
michael@0 134 footer.setAttribute("lwthemefooter", "true");
michael@0 135 else
michael@0 136 footer.removeAttribute("lwthemefooter");
michael@0 137 }
michael@0 138
michael@0 139 #ifdef XP_MACOSX
michael@0 140 // On OS X, we extend the lightweight theme into the titlebar, which means setting
michael@0 141 // the chromemargin attribute. Some XUL applications already draw in the titlebar,
michael@0 142 // so we need to save the chromemargin value before we overwrite it with the value
michael@0 143 // that lets us draw in the titlebar. We stash this value on the root attribute so
michael@0 144 // that XUL applications have the ability to invalidate the saved value.
michael@0 145 if (stateChanging) {
michael@0 146 if (!root.hasAttribute("chromemargin-nonlwtheme")) {
michael@0 147 root.setAttribute("chromemargin-nonlwtheme", root.getAttribute("chromemargin"));
michael@0 148 }
michael@0 149
michael@0 150 if (active) {
michael@0 151 root.setAttribute("chromemargin", "0,-1,-1,-1");
michael@0 152 } else {
michael@0 153 let defaultChromemargin = root.getAttribute("chromemargin-nonlwtheme");
michael@0 154 if (defaultChromemargin) {
michael@0 155 root.setAttribute("chromemargin", defaultChromemargin);
michael@0 156 } else {
michael@0 157 root.removeAttribute("chromemargin");
michael@0 158 }
michael@0 159 }
michael@0 160 }
michael@0 161 #endif
michael@0 162 }
michael@0 163 }
michael@0 164
michael@0 165 function _setImage(aElement, aActive, aURL) {
michael@0 166 aElement.style.backgroundImage =
michael@0 167 (aActive && aURL) ? 'url("' + aURL.replace(/"/g, '\\"') + '")' : "";
michael@0 168 }
michael@0 169
michael@0 170 function _parseRGB(aColorString) {
michael@0 171 var rgb = aColorString.match(/^rgba?\((\d+), (\d+), (\d+)/);
michael@0 172 rgb.shift();
michael@0 173 return rgb.map(function (x) parseInt(x));
michael@0 174 }

mercurial