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

Delta Between Two Patch Sets: lib/icon.js

Issue 29995555: Issue 7253 - Pre-render icons for badge on Chromium (Closed) Base URL: https://hg.adblockplus.org/adblockpluschrome/
Left Patch Set: Remove icons from metadata.chrome Created Feb. 3, 2019, 8:32 a.m.
Right Patch Set: Rewrite renderIcons Created Feb. 3, 2019, 9:45 a.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « ext/background.js ('k') | no next file » | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
1 /* 1 /*
2 * This file is part of Adblock Plus <https://adblockplus.org/>, 2 * This file is part of Adblock Plus <https://adblockplus.org/>,
3 * Copyright (C) 2006-present eyeo GmbH 3 * Copyright (C) 2006-present eyeo GmbH
4 * 4 *
5 * Adblock Plus is free software: you can redistribute it and/or modify 5 * Adblock Plus is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 3 as 6 * it under the terms of the GNU General Public License version 3 as
7 * published by the Free Software Foundation. 7 * published by the Free Software Foundation.
8 * 8 *
9 * Adblock Plus is distributed in the hope that it will be useful, 9 * Adblock Plus is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
46 }); 46 });
47 image.addEventListener("error", () => 47 image.addEventListener("error", () =>
48 { 48 {
49 reject("Failed to load image " + url); 49 reject("Failed to load image " + url);
50 }); 50 });
51 }); 51 });
52 } 52 }
53 53
54 function renderIcons() 54 function renderIcons()
55 { 55 {
56 Promise.all([ 56 let paths = [
57 loadImage("icons/abp-16.png"), 57 "icons/abp-16.png", "icons/abp-16-whitelisted.png",
58 loadImage("icons/abp-16-whitelisted.png"), 58 "icons/abp-32.png", "icons/abp-32-whitelisted.png"
59 loadImage("icons/abp-32.png"), 59 ];
60 loadImage("icons/abp-32-whitelisted.png") 60
61 ]) 61 for (let path of paths)
62 .then(images => 62 {
63 { 63 loadImage(path).then(image =>
64 let imageMap = { 64 {
65 16: [images[0], images[1]], 65 let [, size, whitelisted] = /\/abp-(16|32)(-whitelisted)?\./.exec(path);
Manish Jethani 2019/02/03 09:47:55 Note: size is a string, not a number, but that's f
66 32: [images[2], images[3]] 66
67 }; 67 let canvas = document.createElement("canvas");
68 68 let context = canvas.getContext("2d");
69 let canvas = document.createElement("canvas"); 69 let imageData = icons[!!whitelisted | 0] || {};
Sebastian Noack 2019/02/03 10:07:54 Nit: Isn't coercing to boolean (through !!) redund
Manish Jethani 2019/02/03 10:37:55 `"-whitelisted" | 0` is 0. Because "-whitelisted"
70 let context = canvas.getContext("2d"); 70
71 71 canvas.width = size;
72 for (let whitelisted = 0; whitelisted <= 1; whitelisted++) 72 canvas.height = size;
73 { 73 context.globalAlpha = 1;
74 let imageData = {}; 74 context.drawImage(image, 0, 0);
75 let sizes = [16, 32]; 75 imageData[size] = context.getImageData(0, 0, size, size);
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.
76 76
77 for (let size of sizes) 77 icons[!!whitelisted | 0] = imageData;
78 { 78 });
79 canvas.width = size; 79 }
80 canvas.height = size;
81 context.globalAlpha = 1;
82 context.drawImage(imageMap[size][whitelisted], 0, 0);
83 imageData[size] = context.getImageData(0, 0, size, size);
84 }
85
86 icons[whitelisted] = imageData;
87 }
88 });
89 } 80 }
90 81
91 function setIcon(page, notificationType, opacity, frames) 82 function setIcon(page, notificationType, opacity, frames)
92 { 83 {
93 opacity = opacity || 0; 84 opacity = opacity || 0;
94 let whitelisted = !!whitelistedState.get(page); 85 let whitelisted = !!whitelistedState.get(page);
95 86
96 if (!notificationType || !frames) 87 if (!notificationType || !frames)
97 { 88 {
98 if (opacity > 0.5) 89 if (opacity > 0.5)
99 { 90 {
100 page.browserAction.setIcon("/icons/abp-$size-notification-" + 91 page.browserAction.setIconPath("/icons/abp-$size-notification-" +
101 notificationType + ".png"); 92 notificationType + ".png");
93 }
94 else if (icons[whitelisted | 0])
95 {
96 page.browserAction.setIconImageData(icons[whitelisted | 0]);
102 } 97 }
103 else 98 else
104 { 99 {
105 page.browserAction.setIcon("/icons/abp-$size" + 100 page.browserAction.setIconPath("/icons/abp-$size" +
106 (whitelisted ? "-whitelisted" : "") + ".png", 101 (whitelisted ? "-whitelisted" : "") + ".png ");
107 icons[whitelisted | 0]);
108 } 102 }
109 } 103 }
110 else 104 else
111 { 105 {
112 browser.browserAction.setIcon({ 106 browser.browserAction.setIconPath({
113 tabId: page.id, 107 tabId: page.id,
114 imageData: frames["" + opacity + whitelisted] 108 imageData: frames["" + opacity + whitelisted]
115 }); 109 });
116 } 110 }
117 } 111 }
118 112
119 filterNotifier.on("page.WhitelistingStateRevalidate", (page, filter) => 113 filterNotifier.on("page.WhitelistingStateRevalidate", (page, filter) =>
120 { 114 {
121 whitelistedState.set(page, !!filter); 115 whitelistedState.set(page, !!filter);
122 if (canUpdateIcon) 116 if (canUpdateIcon)
(...skipping 26 matching lines...) Expand all
149 143
150 let frames = {}; 144 let frames = {};
151 let canvas = document.createElement("canvas"); 145 let canvas = document.createElement("canvas");
152 let context = canvas.getContext("2d"); 146 let context = canvas.getContext("2d");
153 147
154 for (let whitelisted of [false, true]) 148 for (let whitelisted of [false, true])
155 { 149 {
156 for (let i = 0, opacity = 0; i <= 10; opacity = ++i / 10) 150 for (let i = 0, opacity = 0; i <= 10; opacity = ++i / 10)
157 { 151 {
158 let imageData = {}; 152 let imageData = {};
159 let sizes = [16, 20, 32, 40]; 153 let sizes = [16, 20, 32, 40];
Sebastian Noack 2019/02/03 08:47:16 Not: While at it, same here.
160 for (let size of sizes) 154 for (let size of sizes)
161 { 155 {
162 canvas.width = size; 156 canvas.width = size;
163 canvas.height = size; 157 canvas.height = size;
164 context.globalAlpha = 1; 158 context.globalAlpha = 1;
165 context.drawImage(imageMap[size]["base"][whitelisted | 0], 0, 0); 159 context.drawImage(imageMap[size]["base"][whitelisted | 0], 0, 0);
166 context.globalAlpha = opacity; 160 context.globalAlpha = opacity;
167 context.drawImage(imageMap[size]["overlay"], 0, 0); 161 context.drawImage(imageMap[size]["overlay"], 0, 0);
168 imageData[size] = context.getImageData(0, 0, size, size); 162 imageData[size] = context.getImageData(0, 0, size, size);
169 } 163 }
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after
267 261
268 animateIcon(type, frames); 262 animateIcon(type, frames);
269 }, 10000); 263 }, 10000);
270 }); 264 });
271 }); 265 });
272 }; 266 };
273 267
274 // Pre-render icons on Chromium (#7253). 268 // Pre-render icons on Chromium (#7253).
275 if (info.platform == "chromium") 269 if (info.platform == "chromium")
276 renderIcons(); 270 renderIcons();
LEFTRIGHT

Powered by Google App Engine
This is Rietveld