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: Created Jan. 21, 2016, 5:35 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
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 const frameOpacities = [0, 0.2, 0.4, 0.6, 0.8,
Sebastian Noack 2016/01/21 18:11:01 This has been copied from the original implementat
kzar 2016/01/21 21:07:30 Haha, I did actually change it to 10 steps before
21 1, 1, 1, 1, 1,
22 0.8, 0.6, 0.4, 0.2, 0];
23 const numberOfFrames = frameOpacities.length;
Sebastian Noack 2016/01/21 18:11:01 While rewriting most of this file, this is a good
kzar 2016/01/21 21:07:30 Done.
Sebastian Noack 2016/01/22 15:59:15 For reference, jsHydra converts const (like let) t
kzar 2016/01/22 16:49:34 Acknowledged.
21 24
25 let stopAnimation = null;
26 let animationPlaying = false;
22 let whitelistedState = new ext.PageMap(); 27 let whitelistedState = new ext.PageMap();
23 let notificationType = null;
24 let animationInterval = null;
25 let animationStep = 0;
26 28
27 function getIconPath(whitelisted) 29 function loadImage(path, size)
Sebastian Noack 2016/01/21 18:11:01 Awesome, using a Promise here. But note that we lo
kzar 2016/01/21 21:07:30 Done. (Kinda... I put loadImage across, but couldn
28 { 30 {
31 if (!path)
32 return Promise.resolve(null);
33 if (size)
34 path = path.replace("$size", size.toString());
35
36 return new Promise(function(resolve, reject) {
37 let image = new Image();
38 image.src = path;
39 image.addEventListener("load", function() { resolve(image); });
40 });
41 }
42
43 function getImageData(size, baseIcon, overlayIcon, opacity,
44 animationStep, frameCache)
45 {
46 let cacheIndex = baseIcon + size + opacity;
47
48 if (frameCache && frameCache[cacheIndex])
49 return frameCache[cacheIndex];
50
51 let canvas = document.createElement("canvas");
52 let context = canvas.getContext("2d");
53
54 let imageData = Promise.all(
55 [loadImage(baseIcon, size), loadImage(overlayIcon, size)]
56 ).then(function(icons)
57 {
58 baseIcon = icons[0];
59 overlayIcon = icons[1];
60
61 canvas.width = baseIcon.width;
62 canvas.height = baseIcon.height;
63
64 context.globalAlpha = 1;
65 context.drawImage(baseIcon, 0, 0);
66
67 if (overlayIcon && opacity)
68 {
69 context.globalAlpha = opacity;
70 context.drawImage(overlayIcon, 0, 0);
71 }
72
73 return context.getImageData(0, 0, canvas.width, canvas.height);
74 });
75 if (frameCache)
76 frameCache[cacheIndex] = imageData;
77 return imageData;
78 }
79
80 function setIcon(page, animationType, animationStep, frameCache)
81 {
82 let safari = require("info").platform == "safari";
83 let opacity = animationType ? frameOpacities[animationStep] : 0;
84 let greyed = whitelistedState.get(page) && !safari;
85 let blending = (animationType && opacity > 0 && opacity < 1);
86
29 let filename = "icons/abp-$size"; 87 let filename = "icons/abp-$size";
88 let baseIcon = filename + (greyed ? "-whitelisted" : "") + ".png";
89 let overlayIcon = animationType && (filename + "-notification-" +
90 animationType + ".png");
30 91
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 92
37 // If the icon is currently animating to indicate a pending notification, 93 // 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. 94 // data URLs, we can just use the image's filename with the $size placeholder.
39 if (notificationType && animationStep > 0) 95 if (!blending || safari)
96 if (overlayIcon && opacity > 0.5)
97 return page.browserAction.setIcon(overlayIcon);
98 else
99 return page.browserAction.setIcon(baseIcon);
100
101 // Otherwise we must process the images using a canvas and return a data URL
102 // of the result for each size that's required. (19px and 38px are required by
103 // Chrome/Opera.)
104 let imageData = [19, 38].map(function(size)
40 { 105 {
41 filename += "-notification-" + notificationType; 106 return getImageData(size, baseIcon, overlayIcon, opacity,
107 animationStep, frameCache);
108 });
109 Promise.all(imageData).then(function(imageData)
110 {
111 chrome.browserAction.setIcon({tabId: page._id,
112 imageData: {19: imageData[0],
113 38: imageData[1]}});
114 });
115 }
42 116
43 if (animationStep < numberOfFrames) 117 function runAnimation(animationType)
44 filename += "-" + animationStep; 118 {
119 let frameCache = {};
120 let frameInterval;
121
122 function playAnimation()
123 {
124 animationPlaying = true;
125 ext.pages.query({active: true}, function(pages)
126 {
127 let animationStep = 0;
128 frameInterval = setInterval(function()
129 {
130 pages.forEach(function (page) {
131 setIcon(page, animationType, animationStep++, frameCache);
132 });
133 if (animationStep >= numberOfFrames)
134 {
135 animationStep = 0;
136 clearInterval(frameInterval);
137 animationPlaying = false;
138 }
139 }, 100);
140 });
45 } 141 }
46 142
47 return filename + ".png"; 143 playAnimation();
48 } 144 let animationInterval = setInterval(playAnimation, 10000);
49 145 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 { 146 {
59 ext.pages.query({active: true}, function(pages) 147 clearInterval(frameInterval);
60 { 148 clearInterval(animationInterval);
61 let fadeInInterval = setInterval(function() 149 animationPlaying = false;
62 { 150 };
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 } 151 }
88 152
89 /** 153 /**
90 * Set the browser action icon for the given page, indicating whether 154 * Set the browser action icon for the given page, indicating whether
91 * adblocking is active there, and considering the icon animation. 155 * adblocking is active there, and considering the icon animation.
92 * 156 *
93 * @param {Page} page The page to set the browser action icon for 157 * @param {Page} page The page to set the browser action icon for
94 * @param {Boolean} whitelisted Whether the page has been whitelisted 158 * @param {Boolean} whitelisted Whether the page has been whitelisted
95 */ 159 */
96 exports.updateIcon = function(page, whitelisted) 160 exports.updateIcon = function(page, whitelisted)
97 { 161 {
98 page.browserAction.setIcon(getIconPath(whitelisted));
99 whitelistedState.set(page, whitelisted); 162 whitelistedState.set(page, whitelisted);
163 if (!animationPlaying)
164 setIcon(page);
100 }; 165 };
101 166
102 /** 167 /**
103 * Starts to animate the browser action icon to indicate a pending notifcation. 168 * Starts to animate the browser action icon to indicate a pending notifcation.
104 * 169 *
105 * @param {string} type The notification type (i.e: "information" or "critical" ) 170 * @param {string} type The notification type (i.e: "information" or "critical" )
106 */ 171 */
107 exports.startIconAnimation = function(type) 172 exports.startIconAnimation = function(type)
108 { 173 {
109 notificationType = type; 174 stopAnimation && stopAnimation();
110 175 stopAnimation = runAnimation(type);
111 if (animationInterval == null)
112 animationInterval = runAnimation();
113 }; 176 };
114 177
115 /** 178 /**
116 * Stops to animate the browser action icon. 179 * Stops to animate the browser action icon.
117 */ 180 */
118 exports.stopIconAnimation = function() 181 exports.stopIconAnimation = function()
119 { 182 {
120 if (animationInterval != null) 183 stopAnimation && stopAnimation();
121 {
122 clearInterval(animationInterval);
123 animationInterval = null;
124 }
125
126 notificationType = null;
127 }; 184 };
OLDNEW
« background.js ('K') | « background.js ('k') | metadata.chrome » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld