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

Side by Side Diff: lib/icon.js

Issue 29334223: Issue 3532 - Generate animation images at runtime (Closed)
Patch Set: Addressed further feedback Created Jan. 22, 2016, 4:47 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | lib/notificationHelper.js » ('j') | lib/notificationHelper.js » ('J')
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 const numberOfFrames = 10; 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,
23 1, 1, 1, 1, 1,
24 0.9, 0.8, 0.7, 0.6, 0.5, 0.4, 0.3, 0.2, 0.1, 0];
25 const numberOfFrames = frameOpacities.length;
26
27 let stopAnimation = null;
28 let animationPlaying = false;
22 let whitelistedState = new ext.PageMap(); 29 let whitelistedState = new ext.PageMap();
23 let notificationType = null;
24 let animationInterval = null;
25 let animationStep = 0;
26 30
27 function getIconPath(whitelisted) 31 let loadImage =
32 /**
33 * Loads an image and draws it onto a canvas
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 {
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)
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);
43 if (size)
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());
45
46 return new Promise(function(resolve, reject) {
47 let image = new Image();
48 image.src = path;
49 image.addEventListener("load", function()
Sebastian Noack 2016/01/23 14:04:09 Nit: How about using arrow functions?
kzar 2016/01/23 14:43:14 Done.
50 {
51 let canvas = document.createElement("canvas");
52 let context = canvas.getContext("2d");
53 canvas.height = image.height;
54 canvas.width = image.width;
55 context.drawImage(image, 0, 0);
56 resolve(canvas);
57 });
58 image.addEventListener("error", function(error) { reject(error); });
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 });
60 };
61
62 function getImageData(size, baseIcon, overlayIcon, opacity,
63 animationStep, frameCache)
28 { 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 }
83
84 return context.getImageData(0, 0, baseIconCanvas.width,
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
29 let filename = "icons/abp-$size"; 99 let filename = "icons/abp-$size";
100 let baseIcon = filename + (greyed ? "-whitelisted" : "") + ".png";
101 let overlayIcon = animationType && (filename + "-notification-" +
102 animationType + ".png");
30 103
31 // If the current page is whitelisted, pick an icon that indicates that
32 // Adblock Plus is disabled, however not when the notification icon has
33 // full opacity, or on Safari where icons are genrally grayscale-only.
34 if (whitelisted && animationStep < numberOfFrames && require("info").platform != "safari")
35 filename += "-whitelisted";
36 104
37 // If the icon is currently animating to indicate a pending notification, 105 // If the icon doesn't need any modifications, or the platform doesn't support
38 // pick the icon for the corresponing notification type and animation frame. 106 // data URLs, we can just use the image's filename with the $size placeholder.
39 if (notificationType && animationStep > 0) 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)
40 { 117 {
41 filename += "-notification-" + notificationType; 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 });
127 }
42 128
43 if (animationStep < numberOfFrames) 129 function runAnimation(animationType)
44 filename += "-" + animationStep; 130 {
131 let frameCache = {};
132 let frameInterval;
133
134 function playAnimation()
135 {
136 animationPlaying = true;
137 let animationStep = 0;
138 frameInterval = setInterval(function()
139 {
140 ext.pages.query({active: true}, function(pages)
141 {
142 pages.forEach(function (page) {
143 setIcon(page, animationType, animationStep++, frameCache);
144 });
145 if (animationStep >= numberOfFrames)
146 {
147 animationStep = 0;
148 clearInterval(frameInterval);
149 animationPlaying = false;
150 }
151 });
152 }, 100);
45 } 153 }
46 154
47 return filename + ".png"; 155 playAnimation();
48 } 156 let animationInterval = setInterval(playAnimation, 10000);
49 157 return function()
50 function setIcon(page)
51 {
52 page.browserAction.setIcon(getIconPath(whitelistedState.get(page)));
53 }
54
55 function runAnimation()
56 {
57 return setInterval(function()
58 { 158 {
59 ext.pages.query({active: true}, function(pages) 159 clearInterval(frameInterval);
60 { 160 clearInterval(animationInterval);
61 let fadeInInterval = setInterval(function() 161 animationPlaying = false;
62 { 162 };
63 animationStep++;
64 pages.forEach(setIcon);
65
66 if (animationStep < numberOfFrames)
67 return;
68
69 setTimeout(function()
70 {
71 let fadeOutInterval = setInterval(function()
72 {
73 animationStep--;
74 pages.forEach(setIcon);
75
76 if (animationStep > 0)
77 return;
78
79 clearInterval(fadeOutInterval);
80 }, 100);
81 },1000);
82
83 clearInterval(fadeInInterval);
84 }, 100);
85 });
86 }, 10000);
87 } 163 }
88 164
89 /** 165 /**
90 * Set the browser action icon for the given page, indicating whether 166 * Set the browser action icon for the given page, indicating whether
91 * adblocking is active there, and considering the icon animation. 167 * adblocking is active there, and considering the icon animation.
92 * 168 *
93 * @param {Page} page The page to set the browser action icon for 169 * @param {Page} page The page to set the browser action icon for
94 * @param {Boolean} whitelisted Whether the page has been whitelisted 170 * @param {Boolean} whitelisted Whether the page has been whitelisted
95 */ 171 */
96 exports.updateIcon = function(page, whitelisted) 172 exports.updateIcon = function(page, whitelisted)
97 { 173 {
98 page.browserAction.setIcon(getIconPath(whitelisted));
99 whitelistedState.set(page, whitelisted); 174 whitelistedState.set(page, whitelisted);
175 if (!animationPlaying)
176 setIcon(page);
100 }; 177 };
101 178
102 /** 179 /**
103 * Starts to animate the browser action icon to indicate a pending notifcation. 180 * Starts to animate the browser action icon to indicate a pending notifcation.
104 * 181 *
105 * @param {string} type The notification type (i.e: "information" or "critical" ) 182 * @param {string} type The notification type (i.e: "information" or "critical" )
106 */ 183 */
107 exports.startIconAnimation = function(type) 184 exports.startIconAnimation = function(type)
108 { 185 {
109 notificationType = type; 186 stopAnimation && stopAnimation();
110 187 stopAnimation = runAnimation(type);
111 if (animationInterval == null)
112 animationInterval = runAnimation();
113 }; 188 };
114 189
115 /** 190 /**
116 * Stops to animate the browser action icon. 191 * Stops to animate the browser action icon.
117 */ 192 */
118 exports.stopIconAnimation = function() 193 exports.stopIconAnimation = function()
119 { 194 {
120 if (animationInterval != null) 195 stopAnimation && stopAnimation();
121 {
122 clearInterval(animationInterval);
123 animationInterval = null;
124 }
125
126 notificationType = null;
127 }; 196 };
OLDNEW
« no previous file with comments | « no previous file | lib/notificationHelper.js » ('j') | lib/notificationHelper.js » ('J')

Powered by Google App Engine
This is Rietveld