Left: | ||
Right: |
LEFT | RIGHT |
---|---|
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, |
Sebastian Noack
2016/01/23 20:38:30
Since, we have 10 frames per second now, you also
kzar
2016/01/23 21:16:56
Done.
| |
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 loadImageAsCanvas = | 32 function loadImage(url) |
32 /** | |
33 * Loads an image and draws it onto a canvas. | |
34 * | |
35 * @param {string} path Path of the image to load | |
Sebastian Noack
2016/01/23 20:38:29
Nit: "url" would be more accurrate than "path" as
kzar
2016/01/23 21:16:56
Done.
| |
36 * @return {Promise} | |
37 */ | |
38 exports.loadImageAsCanvas = function (path, size) | |
Sebastian Noack
2016/01/23 20:38:29
Nit: No space before arguments list.
Sebastian Noack
2016/01/23 20:38:29
The size argument is unused.
kzar
2016/01/23 21:16:55
Done.
kzar
2016/01/23 21:16:56
Done.
| |
39 { | 33 { |
40 return new Promise((resolve, reject) => { | 34 return new Promise((resolve, reject) => |
35 { | |
41 let image = new Image(); | 36 let image = new Image(); |
42 image.src = path; | 37 image.src = url; |
43 image.addEventListener("load", () => | 38 image.addEventListener("load", () => |
44 { | 39 { |
45 let canvas = document.createElement("canvas"); | 40 resolve(image); |
46 let context = canvas.getContext("2d"); | |
47 canvas.height = image.height; | |
48 canvas.width = image.width; | |
49 context.drawImage(image, 0, 0); | |
50 resolve(canvas); | |
51 }); | 41 }); |
52 image.addEventListener("error", () => { | 42 image.addEventListener("error", () => |
53 reject("Failed to load image " + path); | 43 { |
44 reject("Failed to load image " + url); | |
54 }); | 45 }); |
55 }); | 46 }); |
56 }; | 47 }; |
57 | 48 |
58 function getImageData(baseIcon, overlayIcon, opacity, animationStep, frameCache) | 49 function setIcon(page, notificationType, opacity, frames) |
59 { | 50 { |
60 let cacheIndex = baseIcon + overlayIcon + opacity; | 51 opacity = opacity || 0; |
52 let whitelisted = !!whitelistedState.get(page) && !safariPlatform; | |
61 | 53 |
62 if (frameCache && frameCache[cacheIndex]) | 54 if (!notificationType || !frames) |
63 return frameCache[cacheIndex]; | 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 } | |
64 | 71 |
65 let imageData = Promise.all( | 72 function renderFrames(notificationType) |
66 [loadImageAsCanvas(baseIcon), | 73 { |
67 overlayIcon && loadImageAsCanvas(overlayIcon) || Promise.resolve(null)] | 74 if (safariPlatform) |
68 ).then(icons => | 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 => | |
69 { | 85 { |
70 let baseIconCanvas = icons[0]; | 86 let images = { |
71 let overlayIconCanvas = icons[1]; | 87 19: { base: [images[0], images[1]], overlay: images[2] }, |
72 let context = baseIconCanvas.getContext("2d"); | 88 38: { base: [images[3], images[4]], overlay: images[5] } |
89 }; | |
73 | 90 |
74 if (overlayIconCanvas && opacity) | 91 let frames = {}; |
92 let canvas = document.createElement("canvas"); | |
93 let context = canvas.getContext("2d"); | |
94 | |
95 for (let whitelisted of [false, true]) | |
75 { | 96 { |
76 context.globalAlpha = opacity; | 97 for (let i = 0, opacity = 0; i <= 10; opacity = ++i / 10) |
77 context.drawImage(overlayIconCanvas, 0, 0); | 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 } | |
78 } | 112 } |
79 | 113 |
80 return context.getImageData(0, 0, baseIconCanvas.width, | 114 return frames; |
81 baseIconCanvas.height); | |
82 }); | |
83 if (frameCache) | |
Sebastian Noack
2016/01/23 20:38:29
Instead of caching we could probably simplify the
kzar
2016/01/23 21:16:57
I did consider pre-rendering them all, or caching
Sebastian Noack
2016/01/23 21:26:32
The idea is NOT to prerender them on extension int
kzar
2016/01/24 15:43:00
Ah I see, I think you're right. Done.
| |
84 frameCache[cacheIndex] = imageData; | |
85 return imageData; | |
86 } | |
87 | |
88 function setIcon(page, animationType, animationStep, frameCache) | |
89 { | |
90 let safari = require("info").platform == "safari"; | |
91 let opacity = animationType ? frameOpacities[animationStep] : 0; | |
92 let greyed = whitelistedState.get(page) && !safari; | |
93 let blending = (animationType && opacity > 0 && opacity < 1); | |
94 | |
95 let filename = "icons/abp-$size"; | |
96 let baseIcon = filename + (greyed ? "-whitelisted" : "") + ".png"; | |
97 let overlayIcon = animationType && (filename + "-notification-" + | |
98 animationType + ".png"); | |
99 | |
100 | |
101 // If the icon doesn't need any modifications, or the platform doesn't support | |
102 // data URLs, we can just use the image's filename with the $size placeholder. | |
103 if (!blending || safari) | |
104 if (overlayIcon && opacity > 0.5) | |
105 return page.browserAction.setIcon(overlayIcon); | |
106 else | |
107 return page.browserAction.setIcon(baseIcon); | |
108 | |
109 // Otherwise we must process the images using a canvas and return a data URL | |
110 // of the result for each size that's required. (19px and 38px are required by | |
111 // Chrome/Opera.) | |
112 let imageData = [19, 38].map(size => | |
113 { | |
114 return getImageData(baseIcon.replace("$size", size.toString()), | |
115 overlayIcon.replace("$size", size.toString()), | |
116 opacity, animationStep, frameCache); | |
117 }); | |
118 Promise.all(imageData).then(imageData => | |
119 { | |
120 chrome.browserAction.setIcon({tabId: page._id, | |
121 imageData: {19: imageData[0], | |
Sebastian Noack
2016/01/23 20:38:29
How about generating the object as it is used in t
kzar
2016/01/23 21:16:55
That would be nice but I'm not sure how to do it c
Sebastian Noack
2016/01/23 21:26:32
See my other comment, and the code snippet I provi
| |
122 38: imageData[1]}}); | |
123 }); | 115 }); |
124 } | 116 } |
125 | 117 |
126 function runAnimation(animationType) | 118 function runAnimation(notificationType) |
127 { | 119 { |
128 let frameCache = {}; | 120 function playAnimation(frames) |
129 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); | |
130 | 130 |
131 function playAnimation() | 131 frameInterval = setInterval(() => |
132 { | |
133 animationPlaying = true; | |
134 let animationStep = 0; | |
135 frameInterval = setInterval(() => | |
136 { | |
137 ext.pages.query({active: true}, pages => | |
Sebastian Noack
2016/01/23 20:38:29
Querying every 100ms for active tabs will impair t
kzar
2016/01/23 21:16:56
Ah, whoops. I changed this around because I notice
Sebastian Noack
2016/01/23 21:26:33
Well, exceptions shouldn't occur. The easiest way,
kzar
2016/01/23 22:01:06
On second thoughts I think it is worth adding the
| |
138 { | 132 { |
139 pages.forEach(page => { | 133 let opacity = frameOpacities[animationStep++]; |
140 setIcon(page, animationType, animationStep++, frameCache); | 134 for (let page of pages) |
141 }); | 135 { |
142 if (animationStep >= numberOfFrames) | 136 if (whitelistedState.has(page)) |
137 setIcon(page, notificationType, opacity, frames); | |
138 }; | |
139 | |
140 if (animationStep > numberOfFrames) | |
143 { | 141 { |
144 animationStep = 0; | 142 animationStep = 0; |
145 clearInterval(frameInterval); | 143 clearInterval(frameInterval); |
146 animationPlaying = false; | 144 frameInterval = null; |
145 ext.pages.onActivated.removeListener(appendActivePage); | |
147 } | 146 } |
148 }); | 147 }, 100); |
149 }, 100); | 148 }); |
150 } | 149 } |
151 | 150 |
152 playAnimation(); | 151 renderFrames(notificationType).then(frames => |
153 let animationInterval = setInterval(playAnimation, 10000); | |
154 return () => | |
155 { | 152 { |
156 clearInterval(frameInterval); | 153 playAnimation(frames); |
157 clearInterval(animationInterval); | 154 animationInterval = setInterval(() => { playAnimation(frames); }, 10000); |
158 animationPlaying = false; | 155 }); |
159 }; | |
160 } | 156 } |
161 | 157 |
162 /** | 158 /** |
163 * Set the browser action icon for the given page, indicating whether | 159 * Set the browser action icon for the given page, indicating whether |
164 * adblocking is active there, and considering the icon animation. | 160 * adblocking is active there, and considering the icon animation. |
165 * | 161 * |
166 * @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 |
167 * @param {Boolean} whitelisted Whether the page has been whitelisted | 163 * @param {Boolean} whitelisted Whether the page has been whitelisted |
168 */ | 164 */ |
169 exports.updateIcon = function(page, whitelisted) | 165 exports.updateIcon = function(page, whitelisted) |
170 { | 166 { |
171 whitelistedState.set(page, whitelisted); | 167 whitelistedState.set(page, whitelisted); |
172 if (!animationPlaying) | 168 if (frameInterval == null) |
173 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; | |
174 }; | 183 }; |
175 | 184 |
176 /** | 185 /** |
177 * 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. |
178 * | 187 * |
179 * @param {string} type The notification type (i.e: "information" or "critical" ) | 188 * @param {string} type The notification type (i.e: "information" or "critical" ) |
180 */ | 189 */ |
181 exports.startIconAnimation = function(type) | 190 exports.startIconAnimation = function(type) |
182 { | 191 { |
183 stopAnimation && stopAnimation(); | 192 stopIconAnimation(); |
184 stopAnimation = runAnimation(type); | 193 runAnimation(type); |
185 }; | 194 }; |
186 | |
187 /** | |
188 * Stops to animate the browser action icon. | |
189 */ | |
190 exports.stopIconAnimation = function() | |
191 { | |
192 stopAnimation && stopAnimation(); | |
193 }; | |
LEFT | RIGHT |