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

Delta Between Two Patch Sets: lib/icon.js

Issue 29334223: Issue 3532 - Generate animation images at runtime (Closed)
Left Patch Set: Addressed further feedback Created Jan. 22, 2016, 4:47 p.m.
Right Patch Set: Addressed final nits Created Jan. 26, 2016, 10:57 p.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 | « chrome/ext/background.js ('k') | lib/notificationHelper.js » ('j') | 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-2016 Eyeo GmbH 3 * Copyright (C) 2006-2016 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
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details. 12 * GNU General Public License for more details.
13 * 13 *
14 * You should have received a copy of the GNU General Public License 14 * You should have received a copy of the GNU General Public License
15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
16 */ 16 */
17 17
18 /** @module icon */ 18 /** @module icon */
19 19
20 "use strict"; 20 "use strict";
21 21
22 const frameOpacities = [0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 22 const frameOpacities = [0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9,
23 1, 1, 1, 1, 1, 23 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0,
24 0.9, 0.8, 0.7, 0.6, 0.5, 0.4, 0.3, 0.2, 0.1, 0]; 24 0.9, 0.8, 0.7, 0.6, 0.5, 0.4, 0.3, 0.2, 0.1, 0.0];
25 const numberOfFrames = frameOpacities.length; 25 const numberOfFrames = frameOpacities.length;
26 const safariPlatform = require("info").platform == "safari";
26 27
27 let stopAnimation = null; 28 let frameInterval = null;
28 let animationPlaying = false; 29 let animationInterval = null;
29 let whitelistedState = new ext.PageMap(); 30 let whitelistedState = new ext.PageMap();
30 31
31 let loadImage = 32 function loadImage(url)
32 /** 33 {
33 * Loads an image and draws it onto a canvas 34 return new Promise((resolve, reject) =>
Sebastian Noack 2016/01/23 14:04:09 Nit: Missing full stop?
kzar 2016/01/23 14:43:14 Done.
34 *
35 * @param {string} path Path of the image to load
36 * @param {number} [int] Size of image to load (replaces "$size" in path)
37 * @return {promise}
Sebastian Noack 2016/01/23 14:04:09 Nit: It's "Promise" (capitalized) since it's a cla
kzar 2016/01/23 14:43:14 Done.
38 */
39 exports.loadImage = function loadImage(path, size)
Sebastian Noack 2016/01/23 14:04:08 Perhaps we should call the function loadCanvas or
Sebastian Noack 2016/01/23 14:04:09 Nit: Named function assigned to a variable?
kzar 2016/01/23 14:43:13 Done.
kzar 2016/01/23 14:43:13 Done.
40 { 35 {
Sebastian Noack 2016/01/23 14:04:08 Nit: It seems that the indentation is off here.
kzar 2016/01/23 14:43:13 Done.
41 if (!path) 36 let image = new Image();
Sebastian Noack 2016/01/23 14:04:09 Ah, now I see why you check for the path being emp
kzar 2016/01/23 14:43:14 Done.
42 return Promise.resolve(null); 37 image.src = url;
43 if (size) 38 image.addEventListener("load", () =>
Sebastian Noack 2016/01/23 14:04:09 I'm not sure whether this logic should be handled
kzar 2016/01/23 14:43:13 Done.
44 path = path.replace("$size", size.toString()); 39 {
40 resolve(image);
41 });
42 image.addEventListener("error", () =>
43 {
44 reject("Failed to load image " + url);
45 });
46 });
47 };
45 48
46 return new Promise(function(resolve, reject) { 49 function setIcon(page, notificationType, opacity, frames)
47 let image = new Image(); 50 {
48 image.src = path; 51 opacity = opacity || 0;
49 image.addEventListener("load", function() 52 let whitelisted = !!whitelistedState.get(page) && !safariPlatform;
Sebastian Noack 2016/01/23 14:04:09 Nit: How about using arrow functions?
kzar 2016/01/23 14:43:14 Done.
53
54 if (!notificationType || !frames)
55 {
56 if (opacity > 0.5)
57 page.browserAction.setIcon("icons/abp-$size-notification-"
58 + notificationType + ".png");
59 else
60 page.browserAction.setIcon("icons/abp-$size" +
61 (whitelisted ? "-whitelisted" : "") + ".png");
62 }
63 else
64 {
65 chrome.browserAction.setIcon({
66 tabId: page._id,
67 imageData: frames["" + opacity + whitelisted]
68 });
69 }
70 }
71
72 function renderFrames(notificationType)
73 {
74 if (safariPlatform)
75 return Promise.resolve(null);
76
77 return Promise.all([
78 loadImage("icons/abp-19.png"),
79 loadImage("icons/abp-19-whitelisted.png"),
80 loadImage("icons/abp-19-notification-" + notificationType + ".png"),
81 loadImage("icons/abp-38.png"),
82 loadImage("icons/abp-38-whitelisted.png"),
83 loadImage("icons/abp-38-notification-" + notificationType + ".png"),
84 ]).then(images =>
85 {
86 let images = {
87 19: { base: [images[0], images[1]], overlay: images[2] },
88 38: { base: [images[3], images[4]], overlay: images[5] }
89 };
90
91 let frames = {};
92 let canvas = document.createElement("canvas");
93 let context = canvas.getContext("2d");
94
95 for (let whitelisted of [false, true])
96 {
97 for (let i = 0, opacity = 0; i <= 10; opacity = ++i / 10)
50 { 98 {
51 let canvas = document.createElement("canvas"); 99 let imageData = {};
52 let context = canvas.getContext("2d"); 100 for (let size of [19, 38])
53 canvas.height = image.height; 101 {
54 canvas.width = image.width; 102 canvas.width = size;
55 context.drawImage(image, 0, 0); 103 canvas.height = size;
56 resolve(canvas); 104 context.globalAlpha = 1;
57 }); 105 context.drawImage(images[size]["base"][whitelisted | 0], 0, 0);
58 image.addEventListener("error", function(error) { reject(error); }); 106 context.globalAlpha = opacity;
Sebastian Noack 2016/01/23 14:17:41 Note that the value passed to the listener here is
kzar 2016/01/23 14:43:14 Done.
59 }); 107 context.drawImage(images[size]["overlay"], 0, 0);
60 }; 108 imageData[size] = context.getImageData(0, 0, size, size);
61 109 }
62 function getImageData(size, baseIcon, overlayIcon, opacity, 110 frames["" + opacity + whitelisted] = imageData;
63 animationStep, frameCache) 111 }
64 {
65 let cacheIndex = baseIcon + size + opacity;
66
67 if (frameCache && frameCache[cacheIndex])
68 return frameCache[cacheIndex];
69
70 let imageData = Promise.all(
71 [loadImage(baseIcon, size), loadImage(overlayIcon, size)]
72 ).then(function(icons)
73 {
74 let baseIconCanvas = icons[0];
75 let overlayIconCanvas = icons[1];
76 let context = baseIconCanvas.getContext("2d");
77
78 if (overlayIconCanvas && opacity)
79 {
80 context.globalAlpha = opacity;
81 context.drawImage(overlayIconCanvas, 0, 0);
82 } 112 }
83 113
84 return context.getImageData(0, 0, baseIconCanvas.width, 114 return frames;
85 baseIconCanvas.height);
86 });
87 if (frameCache)
88 frameCache[cacheIndex] = imageData;
89 return imageData;
90 }
91
92 function setIcon(page, animationType, animationStep, frameCache)
93 {
94 let safari = require("info").platform == "safari";
95 let opacity = animationType ? frameOpacities[animationStep] : 0;
96 let greyed = whitelistedState.get(page) && !safari;
97 let blending = (animationType && opacity > 0 && opacity < 1);
98
99 let filename = "icons/abp-$size";
100 let baseIcon = filename + (greyed ? "-whitelisted" : "") + ".png";
101 let overlayIcon = animationType && (filename + "-notification-" +
102 animationType + ".png");
103
104
105 // If the icon doesn't need any modifications, or the platform doesn't support
106 // data URLs, we can just use the image's filename with the $size placeholder.
107 if (!blending || safari)
108 if (overlayIcon && opacity > 0.5)
109 return page.browserAction.setIcon(overlayIcon);
110 else
111 return page.browserAction.setIcon(baseIcon);
112
113 // Otherwise we must process the images using a canvas and return a data URL
114 // of the result for each size that's required. (19px and 38px are required by
115 // Chrome/Opera.)
116 let imageData = [19, 38].map(function(size)
117 {
118 return getImageData(size, baseIcon, overlayIcon, opacity,
119 animationStep, frameCache);
120 });
121 Promise.all(imageData).then(function(imageData)
122 {
123 chrome.browserAction.setIcon({tabId: page._id,
124 imageData: {19: imageData[0],
125 38: imageData[1]}});
126 }); 115 });
127 } 116 }
128 117
129 function runAnimation(animationType) 118 function runAnimation(notificationType)
130 { 119 {
131 let frameCache = {}; 120 function playAnimation(frames)
132 let frameInterval; 121 {
122 let animationStep = 0;
123 ext.pages.query({active: true}, pages =>
124 {
125 function appendActivePage(page)
126 {
127 pages.push(page);
128 }
129 ext.pages.onActivated.addListener(appendActivePage);
133 130
134 function playAnimation() 131 frameInterval = setInterval(() =>
135 {
136 animationPlaying = true;
137 let animationStep = 0;
138 frameInterval = setInterval(function()
139 {
140 ext.pages.query({active: true}, function(pages)
141 { 132 {
142 pages.forEach(function (page) { 133 let opacity = frameOpacities[animationStep++];
143 setIcon(page, animationType, animationStep++, frameCache); 134 for (let page of pages)
144 }); 135 {
145 if (animationStep >= numberOfFrames) 136 if (whitelistedState.has(page))
137 setIcon(page, notificationType, opacity, frames);
138 };
139
140 if (animationStep > numberOfFrames)
146 { 141 {
147 animationStep = 0; 142 animationStep = 0;
148 clearInterval(frameInterval); 143 clearInterval(frameInterval);
149 animationPlaying = false; 144 frameInterval = null;
145 ext.pages.onActivated.removeListener(appendActivePage);
150 } 146 }
151 }); 147 }, 100);
152 }, 100); 148 });
153 } 149 }
154 150
155 playAnimation(); 151 renderFrames(notificationType).then(frames =>
156 let animationInterval = setInterval(playAnimation, 10000);
157 return function()
158 { 152 {
159 clearInterval(frameInterval); 153 playAnimation(frames);
160 clearInterval(animationInterval); 154 animationInterval = setInterval(() => { playAnimation(frames); }, 10000);
161 animationPlaying = false; 155 });
162 };
163 } 156 }
164 157
165 /** 158 /**
166 * Set the browser action icon for the given page, indicating whether 159 * Set the browser action icon for the given page, indicating whether
167 * adblocking is active there, and considering the icon animation. 160 * adblocking is active there, and considering the icon animation.
168 * 161 *
169 * @param {Page} page The page to set the browser action icon for 162 * @param {Page} page The page to set the browser action icon for
170 * @param {Boolean} whitelisted Whether the page has been whitelisted 163 * @param {Boolean} whitelisted Whether the page has been whitelisted
171 */ 164 */
172 exports.updateIcon = function(page, whitelisted) 165 exports.updateIcon = function(page, whitelisted)
173 { 166 {
174 whitelistedState.set(page, whitelisted); 167 whitelistedState.set(page, whitelisted);
175 if (!animationPlaying) 168 if (frameInterval == null)
176 setIcon(page); 169 setIcon(page);
170 };
171
172 let stopIconAnimation =
173 /**
174 * Stops to animate the browser action icon.
175 */
176 exports.stopIconAnimation = function()
177 {
178 if (frameInterval != null)
179 clearInterval(frameInterval);
180 if (animationInterval != null)
181 clearInterval(animationInterval);
182 frameInterval = animationInterval = null;
177 }; 183 };
178 184
179 /** 185 /**
180 * Starts to animate the browser action icon to indicate a pending notifcation. 186 * Starts to animate the browser action icon to indicate a pending notifcation.
181 * 187 *
182 * @param {string} type The notification type (i.e: "information" or "critical" ) 188 * @param {string} type The notification type (i.e: "information" or "critical" )
183 */ 189 */
184 exports.startIconAnimation = function(type) 190 exports.startIconAnimation = function(type)
185 { 191 {
186 stopAnimation && stopAnimation(); 192 stopIconAnimation();
187 stopAnimation = runAnimation(type); 193 runAnimation(type);
188 }; 194 };
189
190 /**
191 * Stops to animate the browser action icon.
192 */
193 exports.stopIconAnimation = function()
194 {
195 stopAnimation && stopAnimation();
196 };
LEFTRIGHT

Powered by Google App Engine
This is Rietveld