Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Unified Diff: lib/icon.js

Issue 29995555: Issue 7253 - Pre-render icons for badge on Chromium (Closed) Base URL: https://hg.adblockplus.org/adblockpluschrome/
Patch Set: Remove icons from metadata.chrome Created Feb. 3, 2019, 8:32 a.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: lib/icon.js
===================================================================
--- a/lib/icon.js
+++ b/lib/icon.js
@@ -15,27 +15,30 @@
* along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
*/
/** @module icon */
"use strict";
const {filterNotifier} = require("../adblockpluscore/lib/filterNotifier");
+const info = require("info");
const frameOpacities = [0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9,
1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0,
0.9, 0.8, 0.7, 0.6, 0.5, 0.4, 0.3, 0.2, 0.1, 0.0];
const numberOfFrames = frameOpacities.length;
let stopRequested = false;
let canUpdateIcon = true;
let notRunning = Promise.resolve();
let whitelistedState = new ext.PageMap();
+let icons = [null, null];
+
function loadImage(url)
{
return new Promise((resolve, reject) =>
{
let image = new Image();
image.src = url;
image.addEventListener("load", () =>
{
@@ -43,32 +46,70 @@
});
image.addEventListener("error", () =>
{
reject("Failed to load image " + url);
});
});
}
+function renderIcons()
+{
+ Promise.all([
+ loadImage("icons/abp-16.png"),
+ loadImage("icons/abp-16-whitelisted.png"),
+ loadImage("icons/abp-32.png"),
+ loadImage("icons/abp-32-whitelisted.png")
+ ])
+ .then(images =>
+ {
+ let imageMap = {
+ 16: [images[0], images[1]],
+ 32: [images[2], images[3]]
+ };
+
+ let canvas = document.createElement("canvas");
+ let context = canvas.getContext("2d");
+
+ for (let whitelisted = 0; whitelisted <= 1; whitelisted++)
+ {
+ let imageData = {};
+ let sizes = [16, 32];
Sebastian Noack 2019/02/03 08:47:16 Not: This can be inlined below.
Manish Jethani 2019/02/03 09:09:35 Do you mean unroll the loop?
Manish Jethani 2019/02/03 09:50:12 Never mind, I have rewritten the function.
Sebastian Noack 2019/02/03 10:07:53 Not unrolling it but avoiding the temporary variab
Manish Jethani 2019/02/03 10:37:55 I see, I remember JSHydra.
+
+ for (let size of sizes)
+ {
+ canvas.width = size;
+ canvas.height = size;
+ context.globalAlpha = 1;
+ context.drawImage(imageMap[size][whitelisted], 0, 0);
+ imageData[size] = context.getImageData(0, 0, size, size);
+ }
+
+ icons[whitelisted] = imageData;
+ }
+ });
+}
+
function setIcon(page, notificationType, opacity, frames)
{
opacity = opacity || 0;
let whitelisted = !!whitelistedState.get(page);
if (!notificationType || !frames)
{
if (opacity > 0.5)
{
page.browserAction.setIcon("/icons/abp-$size-notification-" +
notificationType + ".png");
}
else
{
page.browserAction.setIcon("/icons/abp-$size" +
- (whitelisted ? "-whitelisted" : "") + ".png");
+ (whitelisted ? "-whitelisted" : "") + ".png",
+ icons[whitelisted | 0]);
}
}
else
{
browser.browserAction.setIcon({
tabId: page.id,
imageData: frames["" + opacity + whitelisted]
});
@@ -83,52 +124,44 @@
});
function renderFrames(notificationType)
{
return Promise.all([
loadImage("icons/abp-16.png"),
loadImage("icons/abp-16-whitelisted.png"),
loadImage("icons/abp-16-notification-" + notificationType + ".png"),
- loadImage("icons/abp-19.png"),
- loadImage("icons/abp-19-whitelisted.png"),
- loadImage("icons/abp-19-notification-" + notificationType + ".png"),
loadImage("icons/abp-20.png"),
loadImage("icons/abp-20-whitelisted.png"),
loadImage("icons/abp-20-notification-" + notificationType + ".png"),
loadImage("icons/abp-32.png"),
loadImage("icons/abp-32-whitelisted.png"),
loadImage("icons/abp-32-notification-" + notificationType + ".png"),
- loadImage("icons/abp-38.png"),
- loadImage("icons/abp-38-whitelisted.png"),
- loadImage("icons/abp-38-notification-" + notificationType + ".png"),
loadImage("icons/abp-40.png"),
loadImage("icons/abp-40-whitelisted.png"),
loadImage("icons/abp-40-notification-" + notificationType + ".png")
]).then(images =>
{
let imageMap = {
16: {base: [images[0], images[1]], overlay: images[2]},
- 19: {base: [images[3], images[4]], overlay: images[5]},
- 20: {base: [images[6], images[7]], overlay: images[8]},
- 32: {base: [images[9], images[10]], overlay: images[11]},
- 38: {base: [images[12], images[13]], overlay: images[14]},
- 40: {base: [images[15], images[16]], overlay: images[17]}
+ 20: {base: [images[3], images[4]], overlay: images[5]},
+ 32: {base: [images[6], images[7]], overlay: images[8]},
+ 40: {base: [images[9], images[10]], overlay: images[11]}
};
let frames = {};
let canvas = document.createElement("canvas");
let context = canvas.getContext("2d");
for (let whitelisted of [false, true])
{
for (let i = 0, opacity = 0; i <= 10; opacity = ++i / 10)
{
let imageData = {};
- let sizes = [16, 19, 20, 32, 38, 40];
+ let sizes = [16, 20, 32, 40];
Sebastian Noack 2019/02/03 08:47:16 Not: While at it, same here.
for (let size of sizes)
{
canvas.width = size;
canvas.height = size;
context.globalAlpha = 1;
context.drawImage(imageMap[size]["base"][whitelisted | 0], 0, 0);
context.globalAlpha = opacity;
context.drawImage(imageMap[size]["overlay"], 0, 0);
@@ -232,8 +265,12 @@
return;
}
animateIcon(type, frames);
}, 10000);
});
});
};
+
+// Pre-render icons on Chromium (#7253).
+if (info.platform == "chromium")
+ renderIcons();
« ext/background.js ('K') | « icons/abp-38-whitelisted.png ('k') | metadata.chrome » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld