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, 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.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, | 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]; | 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 stopAnimation = null; | 28 let frameInterval = null; |
29 let animationPlaying = false; | 29 let animationInterval = null; |
30 let whitelistedState = new ext.PageMap(); | 30 let whitelistedState = new ext.PageMap(); |
31 | 31 |
32 let loadImage = | 32 function loadImage(url) |
33 /** | |
34 * Loads an image, returning a promise | |
Sebastian Noack
2016/01/26 16:12:37
Nit: The return value is already doumented below.
kzar
2016/01/26 17:10:54
Acknowledged.
| |
35 * | |
36 * @param {string} url URL string of the image to load | |
37 * @return {Promise} | |
38 */ | |
39 exports.loadImage = function(url) | |
40 { | 33 { |
41 return new Promise((resolve, reject) => { | 34 return new Promise((resolve, reject) => |
35 { | |
42 let image = new Image(); | 36 let image = new Image(); |
43 image.src = url; | 37 image.src = url; |
44 image.addEventListener("load", () => | 38 image.addEventListener("load", () => |
45 { | 39 { |
46 resolve(image); | 40 resolve(image); |
47 }); | 41 }); |
48 image.addEventListener("error", () => { | 42 image.addEventListener("error", () => |
43 { | |
49 reject("Failed to load image " + url); | 44 reject("Failed to load image " + url); |
50 }); | 45 }); |
51 }); | 46 }); |
52 }; | 47 }; |
53 | 48 |
54 function setIcon(page, notificationType, opacity, frames) | 49 function setIcon(page, notificationType, opacity, frames) |
55 { | 50 { |
56 opacity = opacity || 0; | 51 opacity = opacity || 0; |
57 let greyed = whitelistedState.get(page) && !safariPlatform && true || false; | 52 let whitelisted = !!whitelistedState.get(page) && !safariPlatform; |
58 | 53 |
59 if (!notificationType || !frames) | 54 if (!notificationType || !frames) |
60 { | 55 { |
61 if (notificationType && opacity > 0.5) | 56 if (opacity > 0.5) |
62 page.browserAction.setIcon("icons/abp-$size-notification-" | 57 page.browserAction.setIcon("icons/abp-$size-notification-" |
63 + notificationType + ".png"); | 58 + notificationType + ".png"); |
64 else | 59 else |
65 page.browserAction.setIcon("icons/abp-$size" + | 60 page.browserAction.setIcon("icons/abp-$size" + |
66 (greyed ? "-whitelisted" : "") + ".png"); | 61 (whitelisted ? "-whitelisted" : "") + ".png"); |
67 } | 62 } |
68 else | 63 else |
69 { | 64 { |
70 chrome.browserAction.setIcon({ | 65 chrome.browserAction.setIcon({ |
71 tabId: page._id, | 66 tabId: page._id, |
72 imageData: frames["" + opacity + greyed] | 67 imageData: frames["" + opacity + whitelisted] |
73 }); | 68 }); |
74 } | 69 } |
75 } | 70 } |
76 | 71 |
77 function renderFrames(notificationType) | 72 function renderFrames(notificationType) |
78 { | 73 { |
79 if (safariPlatform) | 74 if (safariPlatform) |
80 return Promise.resolve(null); | 75 return Promise.resolve(null); |
81 | 76 |
82 return Promise.all([ | 77 return Promise.all([ |
83 loadImage("icons/abp-19.png"), | 78 loadImage("icons/abp-19.png"), |
84 loadImage("icons/abp-19-whitelisted.png"), | 79 loadImage("icons/abp-19-whitelisted.png"), |
85 loadImage("icons/abp-19-notification-" + notificationType + ".png"), | 80 loadImage("icons/abp-19-notification-" + notificationType + ".png"), |
86 loadImage("icons/abp-38.png"), | 81 loadImage("icons/abp-38.png"), |
87 loadImage("icons/abp-38-whitelisted.png"), | 82 loadImage("icons/abp-38-whitelisted.png"), |
88 loadImage("icons/abp-38-notification-" + notificationType + ".png"), | 83 loadImage("icons/abp-38-notification-" + notificationType + ".png"), |
89 ]).then(images => | 84 ]).then(images => |
90 { | 85 { |
91 let images = { | 86 let images = { |
92 19: { base: [images[0], images[1]], overlay: images[2] }, | 87 19: { base: [images[0], images[1]], overlay: images[2] }, |
93 38: { base: [images[3], images[4]], overlay: images[5] } | 88 38: { base: [images[3], images[4]], overlay: images[5] } |
94 }; | 89 }; |
95 | 90 |
96 let frames = {}; | 91 let frames = {}; |
97 let canvas = document.createElement("canvas"); | 92 let canvas = document.createElement("canvas"); |
98 let context = canvas.getContext("2d"); | 93 let context = canvas.getContext("2d"); |
99 | 94 |
100 for (let size of [19, 38]) | 95 for (let whitelisted of [false, true]) |
101 { | 96 { |
102 canvas.width = size; | 97 for (let i = 0, opacity = 0; i <= 10; opacity = ++i / 10) |
103 canvas.height = size; | |
104 for (let whitelisted of [false, true]) | |
105 { | 98 { |
106 for (let opacity of [0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1]) | 99 let imageData = {}; |
100 for (let size of [19, 38]) | |
107 { | 101 { |
102 canvas.width = size; | |
103 canvas.height = size; | |
108 context.globalAlpha = 1; | 104 context.globalAlpha = 1; |
109 context.drawImage(images[size]["base"][whitelisted | 0], 0, 0); | 105 context.drawImage(images[size]["base"][whitelisted | 0], 0, 0); |
110 context.globalAlpha = opacity; | 106 context.globalAlpha = opacity; |
111 context.drawImage(images[size]["overlay"], 0, 0); | 107 context.drawImage(images[size]["overlay"], 0, 0); |
112 let frameKey = "" + opacity + whitelisted; | 108 imageData[size] = context.getImageData(0, 0, size, size); |
113 if (!(frameKey in frames)) | |
Sebastian Noack
2016/01/26 16:12:37
If you iterate over the sizes in the inner loop yo
kzar
2016/01/26 17:10:54
Done.
| |
114 frames[frameKey] = {}; | |
115 frames[frameKey][size] = context.getImageData(0, 0, size, size); | |
116 } | 109 } |
110 frames["" + opacity + whitelisted] = imageData; | |
117 } | 111 } |
118 } | 112 } |
119 | 113 |
120 return frames; | 114 return frames; |
121 }); | 115 }); |
122 } | 116 } |
123 | 117 |
124 function runAnimation(notificationType) | 118 function runAnimation(notificationType) |
125 { | 119 { |
126 function playAnimation(frames) | 120 function playAnimation(frames) |
127 { | 121 { |
128 animationPlaying = true; | |
129 let animationStep = 0; | 122 let animationStep = 0; |
130 ext.pages.query({active: true}, pages => | 123 ext.pages.query({active: true}, pages => |
131 { | 124 { |
132 function appendActivePage(page) | 125 function appendActivePage(page) |
133 { | 126 { |
134 pages.push(page); | 127 pages.push(page); |
135 } | 128 } |
136 ext.pages.onActivated.addListener(appendActivePage); | 129 ext.pages.onActivated.addListener(appendActivePage); |
137 | 130 |
138 frameInterval = setInterval(() => | 131 frameInterval = setInterval(() => |
139 { | 132 { |
140 let opacity = frameOpacities[animationStep++]; | 133 let opacity = frameOpacities[animationStep++]; |
141 pages.forEach(page => { | 134 for (let page of pages) |
135 { | |
142 if (whitelistedState.has(page)) | 136 if (whitelistedState.has(page)) |
143 setIcon(page, notificationType, opacity, frames); | 137 setIcon(page, notificationType, opacity, frames); |
144 }); | 138 }; |
145 | 139 |
146 if (animationStep > numberOfFrames) | 140 if (animationStep > numberOfFrames) |
147 { | 141 { |
148 animationStep = 0; | 142 animationStep = 0; |
149 clearInterval(frameInterval); | 143 clearInterval(frameInterval); |
150 animationPlaying = false; | 144 frameInterval = null; |
151 ext.pages.onActivated.removeListener(appendActivePage); | 145 ext.pages.onActivated.removeListener(appendActivePage); |
152 } | 146 } |
153 }, 100); | 147 }, 100); |
154 }); | 148 }); |
155 } | 149 } |
156 | 150 |
157 renderFrames(notificationType).then(frames => | 151 renderFrames(notificationType).then(frames => |
158 { | 152 { |
159 playAnimation(frames); | 153 playAnimation(frames); |
160 let animationInterval = setInterval(() => { playAnimation(frames); }, | 154 animationInterval = setInterval(() => { playAnimation(frames); }, 10000); |
161 10000); | |
162 return () => | |
163 { | |
164 clearInterval(frameInterval); | |
165 clearInterval(animationInterval); | |
166 animationPlaying = false; | |
167 }; | |
168 }); | 155 }); |
169 } | 156 } |
170 | 157 |
171 /** | 158 /** |
172 * Set the browser action icon for the given page, indicating whether | 159 * Set the browser action icon for the given page, indicating whether |
173 * adblocking is active there, and considering the icon animation. | 160 * adblocking is active there, and considering the icon animation. |
174 * | 161 * |
175 * @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 |
176 * @param {Boolean} whitelisted Whether the page has been whitelisted | 163 * @param {Boolean} whitelisted Whether the page has been whitelisted |
177 */ | 164 */ |
178 exports.updateIcon = function(page, whitelisted) | 165 exports.updateIcon = function(page, whitelisted) |
179 { | 166 { |
180 whitelistedState.set(page, whitelisted); | 167 whitelistedState.set(page, whitelisted); |
181 if (!animationPlaying) | 168 if (frameInterval == null) |
182 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; | |
183 }; | 183 }; |
184 | 184 |
185 /** | 185 /** |
186 * 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. |
187 * | 187 * |
188 * @param {string} type The notification type (i.e: "information" or "critical" ) | 188 * @param {string} type The notification type (i.e: "information" or "critical" ) |
189 */ | 189 */ |
190 exports.startIconAnimation = function(type) | 190 exports.startIconAnimation = function(type) |
191 { | 191 { |
192 stopAnimation && stopAnimation(); | 192 stopIconAnimation(); |
193 stopAnimation = runAnimation(type); | 193 runAnimation(type); |
194 }; | 194 }; |
195 | |
196 /** | |
197 * Stops to animate the browser action icon. | |
198 */ | |
199 exports.stopIconAnimation = function() | |
200 { | |
201 stopAnimation && stopAnimation(); | |
202 }; | |
LEFT | RIGHT |