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: Created Jan. 21, 2016, 5:35 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 const frameOpacities = [0, 0.2, 0.4, 0.6, 0.8, 20 "use strict";
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, 21
22 0.8, 0.6, 0.4, 0.2, 0]; 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.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.0];
23 const numberOfFrames = frameOpacities.length; 25 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.
26 const safariPlatform = require("info").platform == "safari";
24 27
25 let stopAnimation = null; 28 let frameInterval = null;
26 let animationPlaying = false; 29 let animationInterval = null;
27 let whitelistedState = new ext.PageMap(); 30 let whitelistedState = new ext.PageMap();
28 31
29 function loadImage(path, size) 32 function loadImage(url)
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
30 { 33 {
31 if (!path) 34 return new Promise((resolve, reject) =>
35 {
36 let image = new Image();
37 image.src = url;
38 image.addEventListener("load", () =>
39 {
40 resolve(image);
41 });
42 image.addEventListener("error", () =>
43 {
44 reject("Failed to load image " + url);
45 });
46 });
47 };
48
49 function setIcon(page, notificationType, opacity, frames)
50 {
51 opacity = opacity || 0;
52 let whitelisted = !!whitelistedState.get(page) && !safariPlatform;
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)
32 return Promise.resolve(null); 75 return Promise.resolve(null);
33 if (size)
34 path = path.replace("$size", size.toString());
35 76
36 return new Promise(function(resolve, reject) { 77 return Promise.all([
37 let image = new Image(); 78 loadImage("icons/abp-19.png"),
38 image.src = path; 79 loadImage("icons/abp-19-whitelisted.png"),
39 image.addEventListener("load", function() { resolve(image); }); 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)
98 {
99 let imageData = {};
100 for (let size of [19, 38])
101 {
102 canvas.width = size;
103 canvas.height = size;
104 context.globalAlpha = 1;
105 context.drawImage(images[size]["base"][whitelisted | 0], 0, 0);
106 context.globalAlpha = opacity;
107 context.drawImage(images[size]["overlay"], 0, 0);
108 imageData[size] = context.getImageData(0, 0, size, size);
109 }
110 frames["" + opacity + whitelisted] = imageData;
111 }
112 }
113
114 return frames;
40 }); 115 });
41 } 116 }
42 117
43 function getImageData(size, baseIcon, overlayIcon, opacity, 118 function runAnimation(notificationType)
44 animationStep, frameCache)
45 { 119 {
46 let cacheIndex = baseIcon + size + opacity; 120 function playAnimation(frames)
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);
47 130
48 if (frameCache && frameCache[cacheIndex]) 131 frameInterval = setInterval(() =>
49 return frameCache[cacheIndex]; 132 {
133 let opacity = frameOpacities[animationStep++];
134 for (let page of pages)
135 {
136 if (whitelistedState.has(page))
137 setIcon(page, notificationType, opacity, frames);
138 };
50 139
51 let canvas = document.createElement("canvas"); 140 if (animationStep > numberOfFrames)
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
87 let filename = "icons/abp-$size";
88 let baseIcon = filename + (greyed ? "-whitelisted" : "") + ".png";
89 let overlayIcon = animationType && (filename + "-notification-" +
90 animationType + ".png");
91
92
93 // If the icon doesn't need any modifications, or the platform doesn't support
94 // data URLs, we can just use the image's filename with the $size placeholder.
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)
105 {
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 }
116
117 function runAnimation(animationType)
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 { 141 {
135 animationStep = 0; 142 animationStep = 0;
136 clearInterval(frameInterval); 143 clearInterval(frameInterval);
137 animationPlaying = false; 144 frameInterval = null;
145 ext.pages.onActivated.removeListener(appendActivePage);
138 } 146 }
139 }, 100); 147 }, 100);
140 }); 148 });
141 } 149 }
142 150
143 playAnimation(); 151 renderFrames(notificationType).then(frames =>
144 let animationInterval = setInterval(playAnimation, 10000);
145 return function()
146 { 152 {
147 clearInterval(frameInterval); 153 playAnimation(frames);
148 clearInterval(animationInterval); 154 animationInterval = setInterval(() => { playAnimation(frames); }, 10000);
149 animationPlaying = false; 155 });
150 };
151 } 156 }
152 157
153 /** 158 /**
154 * Set the browser action icon for the given page, indicating whether 159 * Set the browser action icon for the given page, indicating whether
155 * adblocking is active there, and considering the icon animation. 160 * adblocking is active there, and considering the icon animation.
156 * 161 *
157 * @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
158 * @param {Boolean} whitelisted Whether the page has been whitelisted 163 * @param {Boolean} whitelisted Whether the page has been whitelisted
159 */ 164 */
160 exports.updateIcon = function(page, whitelisted) 165 exports.updateIcon = function(page, whitelisted)
161 { 166 {
162 whitelistedState.set(page, whitelisted); 167 whitelistedState.set(page, whitelisted);
163 if (!animationPlaying) 168 if (frameInterval == null)
164 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;
165 }; 183 };
166 184
167 /** 185 /**
168 * 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.
169 * 187 *
170 * @param {string} type The notification type (i.e: "information" or "critical" ) 188 * @param {string} type The notification type (i.e: "information" or "critical" )
171 */ 189 */
172 exports.startIconAnimation = function(type) 190 exports.startIconAnimation = function(type)
173 { 191 {
174 stopAnimation && stopAnimation(); 192 stopIconAnimation();
175 stopAnimation = runAnimation(type); 193 runAnimation(type);
176 }; 194 };
177
178 /**
179 * Stops to animate the browser action icon.
180 */
181 exports.stopIconAnimation = function()
182 {
183 stopAnimation && stopAnimation();
184 };
LEFTRIGHT

Powered by Google App Engine
This is Rietveld