| Left: | ||
| Right: |
| OLD | NEW |
|---|---|
| 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 "use strict"; | |
|
Sebastian Noack
2016/01/22 15:59:15
Nit: Please add a newline above. That's more consi
kzar
2016/01/22 16:49:35
Done.
| |
| 19 | 20 |
| 20 const numberOfFrames = 10; | 21 let {Utils} = require("utils"); |
| 21 | 22 |
| 23 const frameOpacities = [0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, | |
| 24 1, 1, 1, 1, 1, | |
| 25 0.9, 0.8, 0.7, 0.6, 0.5, 0.4, 0.3, 0.2, 0.1, 0]; | |
| 26 const numberOfFrames = frameOpacities.length; | |
| 27 | |
| 28 let stopAnimation = null; | |
| 29 let animationPlaying = false; | |
| 22 let whitelistedState = new ext.PageMap(); | 30 let whitelistedState = new ext.PageMap(); |
| 23 let notificationType = null; | |
| 24 let animationInterval = null; | |
| 25 let animationStep = 0; | |
| 26 | 31 |
| 27 function getIconPath(whitelisted) | 32 function getImageData(size, baseIcon, overlayIcon, opacity, |
| 33 animationStep, frameCache) | |
| 28 { | 34 { |
| 35 let cacheIndex = baseIcon + size + opacity; | |
| 36 | |
| 37 if (frameCache && frameCache[cacheIndex]) | |
| 38 return frameCache[cacheIndex]; | |
| 39 | |
| 40 let canvas = document.createElement("canvas"); | |
| 41 let context = canvas.getContext("2d"); | |
| 42 | |
| 43 let imageData = Promise.all( | |
| 44 [Utils.loadImage(baseIcon, size), Utils.loadImage(overlayIcon, size)] | |
| 45 ).then(function(icons) | |
| 46 { | |
| 47 baseIcon = icons[0]; | |
| 48 overlayIcon = icons[1]; | |
| 49 | |
| 50 canvas.width = baseIcon.width; | |
| 51 canvas.height = baseIcon.height; | |
| 52 | |
| 53 context.globalAlpha = 1; | |
| 54 context.drawImage(baseIcon, 0, 0); | |
| 55 | |
| 56 if (overlayIcon && opacity) | |
| 57 { | |
| 58 context.globalAlpha = opacity; | |
| 59 context.drawImage(overlayIcon, 0, 0); | |
| 60 } | |
| 61 | |
| 62 return context.getImageData(0, 0, canvas.width, canvas.height); | |
| 63 }); | |
| 64 if (frameCache) | |
| 65 frameCache[cacheIndex] = imageData; | |
| 66 return imageData; | |
| 67 } | |
| 68 | |
| 69 function setIcon(page, animationType, animationStep, frameCache) | |
| 70 { | |
| 71 let safari = require("info").platform == "safari"; | |
| 72 let opacity = animationType ? frameOpacities[animationStep] : 0; | |
| 73 let greyed = whitelistedState.get(page) && !safari; | |
| 74 let blending = (animationType && opacity > 0 && opacity < 1); | |
| 75 | |
| 29 let filename = "icons/abp-$size"; | 76 let filename = "icons/abp-$size"; |
| 77 let baseIcon = filename + (greyed ? "-whitelisted" : "") + ".png"; | |
| 78 let overlayIcon = animationType && (filename + "-notification-" + | |
| 79 animationType + ".png"); | |
| 30 | 80 |
| 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 | 81 |
| 37 // If the icon is currently animating to indicate a pending notification, | 82 // 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. | 83 // data URLs, we can just use the image's filename with the $size placeholder. |
| 39 if (notificationType && animationStep > 0) | 84 if (!blending || safari) |
| 85 if (overlayIcon && opacity > 0.5) | |
| 86 return page.browserAction.setIcon(overlayIcon); | |
| 87 else | |
| 88 return page.browserAction.setIcon(baseIcon); | |
| 89 | |
| 90 // Otherwise we must process the images using a canvas and return a data URL | |
| 91 // of the result for each size that's required. (19px and 38px are required by | |
| 92 // Chrome/Opera.) | |
| 93 let imageData = [19, 38].map(function(size) | |
| 40 { | 94 { |
| 41 filename += "-notification-" + notificationType; | 95 return getImageData(size, baseIcon, overlayIcon, opacity, |
| 96 animationStep, frameCache); | |
| 97 }); | |
| 98 Promise.all(imageData).then(function(imageData) | |
| 99 { | |
| 100 chrome.browserAction.setIcon({tabId: page._id, | |
| 101 imageData: {19: imageData[0], | |
| 102 38: imageData[1]}}); | |
| 103 }); | |
| 104 } | |
| 42 | 105 |
| 43 if (animationStep < numberOfFrames) | 106 function runAnimation(animationType) |
| 44 filename += "-" + animationStep; | 107 { |
| 108 let frameCache = {}; | |
| 109 let frameInterval; | |
| 110 | |
| 111 function playAnimation() | |
| 112 { | |
| 113 animationPlaying = true; | |
| 114 let animationStep = 0; | |
| 115 frameInterval = setInterval(function() | |
| 116 { | |
| 117 ext.pages.query({active: true}, function(pages) | |
| 118 { | |
| 119 pages.forEach(function (page) { | |
| 120 setIcon(page, animationType, animationStep++, frameCache); | |
| 121 }); | |
| 122 if (animationStep >= numberOfFrames) | |
| 123 { | |
| 124 animationStep = 0; | |
| 125 clearInterval(frameInterval); | |
| 126 animationPlaying = false; | |
| 127 } | |
| 128 }); | |
| 129 }, 100); | |
| 45 } | 130 } |
| 46 | 131 |
| 47 return filename + ".png"; | 132 playAnimation(); |
| 48 } | 133 let animationInterval = setInterval(playAnimation, 10000); |
| 49 | 134 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 { | 135 { |
| 59 ext.pages.query({active: true}, function(pages) | 136 clearInterval(frameInterval); |
| 60 { | 137 clearInterval(animationInterval); |
| 61 let fadeInInterval = setInterval(function() | 138 animationPlaying = false; |
| 62 { | 139 }; |
| 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 } | 140 } |
| 88 | 141 |
| 89 /** | 142 /** |
| 90 * Set the browser action icon for the given page, indicating whether | 143 * Set the browser action icon for the given page, indicating whether |
| 91 * adblocking is active there, and considering the icon animation. | 144 * adblocking is active there, and considering the icon animation. |
| 92 * | 145 * |
| 93 * @param {Page} page The page to set the browser action icon for | 146 * @param {Page} page The page to set the browser action icon for |
| 94 * @param {Boolean} whitelisted Whether the page has been whitelisted | 147 * @param {Boolean} whitelisted Whether the page has been whitelisted |
| 95 */ | 148 */ |
| 96 exports.updateIcon = function(page, whitelisted) | 149 exports.updateIcon = function(page, whitelisted) |
| 97 { | 150 { |
| 98 page.browserAction.setIcon(getIconPath(whitelisted)); | |
| 99 whitelistedState.set(page, whitelisted); | 151 whitelistedState.set(page, whitelisted); |
| 152 if (!animationPlaying) | |
| 153 setIcon(page); | |
| 100 }; | 154 }; |
| 101 | 155 |
| 102 /** | 156 /** |
| 103 * Starts to animate the browser action icon to indicate a pending notifcation. | 157 * Starts to animate the browser action icon to indicate a pending notifcation. |
| 104 * | 158 * |
| 105 * @param {string} type The notification type (i.e: "information" or "critical" ) | 159 * @param {string} type The notification type (i.e: "information" or "critical" ) |
| 106 */ | 160 */ |
| 107 exports.startIconAnimation = function(type) | 161 exports.startIconAnimation = function(type) |
| 108 { | 162 { |
| 109 notificationType = type; | 163 stopAnimation && stopAnimation(); |
| 110 | 164 stopAnimation = runAnimation(type); |
| 111 if (animationInterval == null) | |
| 112 animationInterval = runAnimation(); | |
| 113 }; | 165 }; |
| 114 | 166 |
| 115 /** | 167 /** |
| 116 * Stops to animate the browser action icon. | 168 * Stops to animate the browser action icon. |
| 117 */ | 169 */ |
| 118 exports.stopIconAnimation = function() | 170 exports.stopIconAnimation = function() |
| 119 { | 171 { |
| 120 if (animationInterval != null) | 172 stopAnimation && stopAnimation(); |
| 121 { | |
| 122 clearInterval(animationInterval); | |
| 123 animationInterval = null; | |
| 124 } | |
| 125 | |
| 126 notificationType = null; | |
| 127 }; | 173 }; |
| OLD | NEW |