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: Remove stopAnimation closure and hard coded opacity range Created Jan. 26, 2016, 6:43 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
(...skipping 13 matching lines...) Expand all
24 0.9, 0.8, 0.7, 0.6, 0.5, 0.4, 0.3, 0.2, 0.1, 0.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 const safariPlatform = require("info").platform == "safari";
27 27
28 let frameInterval = null; 28 let frameInterval = null;
29 let animationInterval = null; 29 let animationInterval = null;
30 let whitelistedState = new ext.PageMap(); 30 let whitelistedState = new ext.PageMap();
31 31
32 function loadImage(url) 32 function loadImage(url)
33 { 33 {
34 return new Promise((resolve, reject) => { 34 return new Promise((resolve, reject) =>
35 {
35 let image = new Image(); 36 let image = new Image();
36 image.src = url; 37 image.src = url;
37 image.addEventListener("load", () => 38 image.addEventListener("load", () =>
Sebastian Noack 2016/01/26 22:35:15 Nit: The indentation of the block below is inconsi
kzar 2016/01/26 22:59:31 Done.
38 { 39 {
39 resolve(image); 40 resolve(image);
40 }); 41 });
41 image.addEventListener("error", () => { 42 image.addEventListener("error", () =>
43 {
42 reject("Failed to load image " + url); 44 reject("Failed to load image " + url);
43 }); 45 });
44 }); 46 });
45 }; 47 };
46 48
47 function setIcon(page, notificationType, opacity, frames) 49 function setIcon(page, notificationType, opacity, frames)
48 { 50 {
49 opacity = opacity || 0; 51 opacity = opacity || 0;
50 let whitelisted = !!whitelistedState.get(page) && !safariPlatform; 52 let whitelisted = !!whitelistedState.get(page) && !safariPlatform;
51 53
52 if (!notificationType || !frames) 54 if (!notificationType || !frames)
53 { 55 {
54 if (notificationType && opacity > 0.5) 56 if (opacity > 0.5)
Sebastian Noack 2016/01/26 22:35:17 The check for notificationType seems to be redunda
kzar 2016/01/26 22:59:32 Done.
55 page.browserAction.setIcon("icons/abp-$size-notification-" 57 page.browserAction.setIcon("icons/abp-$size-notification-"
56 + notificationType + ".png"); 58 + notificationType + ".png");
57 else 59 else
58 page.browserAction.setIcon("icons/abp-$size" + 60 page.browserAction.setIcon("icons/abp-$size" +
59 (whitelisted ? "-whitelisted" : "") + ".png"); 61 (whitelisted ? "-whitelisted" : "") + ".png");
60 } 62 }
61 else 63 else
62 { 64 {
63 chrome.browserAction.setIcon({ 65 chrome.browserAction.setIcon({
64 tabId: page._id, 66 tabId: page._id,
65 imageData: frames["" + opacity + whitelisted] 67 imageData: frames["" + opacity + whitelisted]
66 }); 68 });
Sebastian Noack 2016/01/26 22:35:15 Nit: You should close the braces on the same depth
kzar 2016/01/26 22:59:32 Done.
67 } 69 }
68 } 70 }
69 71
70 function renderFrames(notificationType) 72 function renderFrames(notificationType)
71 { 73 {
72 if (safariPlatform) 74 if (safariPlatform)
73 return Promise.resolve(null); 75 return Promise.resolve(null);
74 76
75 return Promise.all([ 77 return Promise.all([
76 loadImage("icons/abp-19.png"), 78 loadImage("icons/abp-19.png"),
77 loadImage("icons/abp-19-whitelisted.png"), 79 loadImage("icons/abp-19-whitelisted.png"),
78 loadImage("icons/abp-19-notification-" + notificationType + ".png"), 80 loadImage("icons/abp-19-notification-" + notificationType + ".png"),
79 loadImage("icons/abp-38.png"), 81 loadImage("icons/abp-38.png"),
80 loadImage("icons/abp-38-whitelisted.png"), 82 loadImage("icons/abp-38-whitelisted.png"),
81 loadImage("icons/abp-38-notification-" + notificationType + ".png"), 83 loadImage("icons/abp-38-notification-" + notificationType + ".png"),
82 ]).then(images => 84 ]).then(images =>
83 { 85 {
84 let images = { 86 let images = {
Sebastian Noack 2016/01/26 22:35:15 Nit: You declare a variable that is already define
kzar 2016/01/26 22:59:33 I think it's pretty readable in this instance.
85 19: { base: [images[0], images[1]], overlay: images[2] }, 87 19: { base: [images[0], images[1]], overlay: images[2] },
86 38: { base: [images[3], images[4]], overlay: images[5] } 88 38: { base: [images[3], images[4]], overlay: images[5] }
87 }; 89 };
88 90
89 let frames = {}; 91 let frames = {};
90 let canvas = document.createElement("canvas"); 92 let canvas = document.createElement("canvas");
91 let context = canvas.getContext("2d"); 93 let context = canvas.getContext("2d");
92 94
93 for (let whitelisted of [false, true]) 95 for (let whitelisted of [false, true])
94 { 96 {
(...skipping 27 matching lines...) Expand all
122 { 124 {
123 function appendActivePage(page) 125 function appendActivePage(page)
124 { 126 {
125 pages.push(page); 127 pages.push(page);
126 } 128 }
127 ext.pages.onActivated.addListener(appendActivePage); 129 ext.pages.onActivated.addListener(appendActivePage);
128 130
129 frameInterval = setInterval(() => 131 frameInterval = setInterval(() =>
130 { 132 {
131 let opacity = frameOpacities[animationStep++]; 133 let opacity = frameOpacities[animationStep++];
132 pages.forEach(page => { 134 for (let page of pages)
Sebastian Noack 2016/01/26 22:35:16 Nit: Why not a for-of loop?
kzar 2016/01/26 22:59:33 Done.
135 {
133 if (whitelistedState.has(page)) 136 if (whitelistedState.has(page))
134 setIcon(page, notificationType, opacity, frames); 137 setIcon(page, notificationType, opacity, frames);
135 }); 138 };
136 139
137 if (animationStep > numberOfFrames) 140 if (animationStep > numberOfFrames)
138 { 141 {
139 animationStep = 0; 142 animationStep = 0;
140 clearInterval(frameInterval); 143 clearInterval(frameInterval);
141 frameInterval = null; 144 frameInterval = null;
142 ext.pages.onActivated.removeListener(appendActivePage); 145 ext.pages.onActivated.removeListener(appendActivePage);
143 } 146 }
144 }, 100); 147 }, 100);
145 }); 148 });
146 } 149 }
147 150
148 renderFrames(notificationType).then(frames => 151 renderFrames(notificationType).then(frames =>
149 { 152 {
150 playAnimation(frames); 153 playAnimation(frames);
151 animationInterval = setInterval(() => { playAnimation(frames); }, 154 animationInterval = setInterval(() => { playAnimation(frames); }, 10000);
152 10000);
Sebastian Noack 2016/01/26 22:35:16 Nit: Wrapping the line here doesn't seem to be nec
kzar 2016/01/26 22:59:31 Done.
153 }); 155 });
154 } 156 }
155 157
156 /** 158 /**
157 * Set the browser action icon for the given page, indicating whether 159 * Set the browser action icon for the given page, indicating whether
158 * adblocking is active there, and considering the icon animation. 160 * adblocking is active there, and considering the icon animation.
159 * 161 *
160 * @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
161 * @param {Boolean} whitelisted Whether the page has been whitelisted 163 * @param {Boolean} whitelisted Whether the page has been whitelisted
162 */ 164 */
(...skipping 20 matching lines...) Expand all
183 /** 185 /**
184 * 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.
185 * 187 *
186 * @param {string} type The notification type (i.e: "information" or "critical" ) 188 * @param {string} type The notification type (i.e: "information" or "critical" )
187 */ 189 */
188 exports.startIconAnimation = function(type) 190 exports.startIconAnimation = function(type)
189 { 191 {
190 stopIconAnimation(); 192 stopIconAnimation();
191 runAnimation(type); 193 runAnimation(type);
192 }; 194 };
LEFTRIGHT

Powered by Google App Engine
This is Rietveld