| 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-2015 Eyeo GmbH | 3 * Copyright (C) 2006-2015 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 iconAnimation = { | 18 let whitelistedState = new ext.PageMap(); |
| 19 _icons: new ext.PageMap(), | 19 let notificationType = null; |
| 20 _animatedPages: new ext.PageMap(), | 20 let animationInterval = null; |
| 21 _step: 0, | 21 let animationStep = 0; |
| 22 | 22 |
| 23 update: function(type) | 23 function getIconPath(whitelisted) |
| 24 { | |
| 25 let filename = "icons/abp-$size"; | |
| 26 | |
| 27 // If the current page is whitelisted, pick an icon that indicates that | |
| 28 // Adblock Plus is disabled, however not when the notification icon has | |
| 29 // full opacity, or on Safari where icons are genrally grayscale-only. | |
| 30 if (whitelisted && animationStep < 10 && require("info").platform != "safari") | |
| 31 filename += "-whitelisted"; | |
| 32 | |
| 33 // If the icon is currently animating to indicate a pending notification, | |
| 34 // pick the icon for the corresponing notification type and animation frame. | |
| 35 if (notificationType && animationStep > 0) | |
| 24 { | 36 { |
| 25 if (type == this._type) | 37 filename += "-notification-" + notificationType; |
| 26 return; | |
| 27 | 38 |
| 28 if (!this._type) | 39 if (animationStep < 10) |
| 29 this._start(); | 40 filename += "-" + animationStep; |
| 41 } | |
| 30 | 42 |
| 31 this._type = type; | 43 return filename + ".png"; |
| 32 }, | 44 } |
| 33 stop: function() | 45 |
| 46 function setIcon(page) | |
| 47 { | |
| 48 page.browserAction.setIcon(getIconPath(whitelistedState.get(page))); | |
| 49 } | |
| 50 | |
| 51 function runAnimation() | |
| 52 { | |
| 53 return setInterval(function() | |
| 34 { | 54 { |
| 35 clearInterval(this._interval); | 55 ext.pages.query({active: true}, function(pages) |
| 56 { | |
| 57 let fadeInInterval = setInterval(function() | |
| 58 { | |
| 59 animationStep++; | |
| 60 pages.forEach(setIcon); | |
| 36 | 61 |
| 37 delete this._interval; | 62 if (animationStep < 10) |
|
Felix Dahlke
2015/02/12 04:52:56
We're checking for `animationStep < 10` three time
Sebastian Noack
2015/02/12 11:15:29
Done.
| |
| 38 delete this._type; | |
| 39 | |
| 40 this._animatedPages.clear(); | |
| 41 }, | |
| 42 registerPage: function(page, icon) | |
| 43 { | |
| 44 this._icons.set(page, icon); | |
| 45 | |
| 46 if (this._animatedPages.has(page)) | |
| 47 this._updateIcon(page); | |
| 48 }, | |
| 49 _start: function() | |
| 50 { | |
| 51 this._interval = setInterval(function() | |
| 52 { | |
| 53 ext.pages.query({active: true}, function(pages) | |
| 54 { | |
| 55 if (pages.length == 0) | |
| 56 return; | 63 return; |
| 57 | 64 |
| 58 for (var i = 0; i < pages.length; i++) | 65 setTimeout(function() |
| 59 this._animatedPages.set(pages[i], null); | 66 { |
| 67 let fadeOutInterval = setInterval(function() | |
| 68 { | |
| 69 animationStep--; | |
| 70 pages.forEach(setIcon); | |
| 60 | 71 |
| 61 var interval = setInterval(function() | 72 if (animationStep > 0) |
| 62 { | 73 return; |
| 63 this._step++; | |
| 64 pages.forEach(this._updateIcon.bind(this)); | |
| 65 | 74 |
| 66 if (this._step < 10) | 75 clearInterval(fadeOutInterval); |
| 67 return; | 76 }, 100); |
| 77 },1000); | |
| 68 | 78 |
| 69 clearInterval(interval); | 79 clearInterval(fadeInInterval); |
| 70 setTimeout(function() | 80 }, 100); |
| 71 { | 81 }); |
| 72 interval = setInterval(function() | 82 }, 15000); |
| 73 { | 83 } |
| 74 this._step--; | |
| 75 pages.forEach(this._updateIcon.bind(this)); | |
| 76 | 84 |
| 77 if (this._step > 0) | 85 /** |
| 78 return; | 86 * Set the browser action icon for the given page, indicating whether |
| 87 * adblocking is active there, and considering the icon animation. | |
| 88 * | |
| 89 * @param {Page} [page] The page to set the browser action icon for | |
| 90 * @param {Boolean} [whitelisted] Whether the page has been whitelisted | |
| 91 */ | |
| 92 function updateIcon(page, whitelisted) | |
| 93 { | |
| 94 page.browserAction.setIcon(getIconPath(whitelisted)); | |
| 95 whitelistedState.set(page, whitelisted); | |
| 96 } | |
| 97 exports.updateIcon = updateIcon; | |
| 79 | 98 |
| 80 clearInterval(interval); | 99 /** |
| 81 this._animatedPages.clear(); | 100 * Starts to animate the browser action icon to indicate a pending notifcation. |
| 82 }.bind(this), 100); | 101 * |
| 83 }.bind(this), 1000); | 102 * @param {string} [type] The notifaction type (i.e: "information" or "critical" ) |
| 84 }.bind(this), 100); | 103 */ |
| 85 }.bind(this)); | 104 function startIconAnimation(type) |
| 86 }.bind(this), 15000); | 105 { |
| 87 }, | 106 notificationType = type; |
| 88 _updateIcon: function(page) | 107 |
| 108 if (animationInterval == null) | |
| 109 animationInterval = runAnimation(); | |
| 110 } | |
| 111 exports.startIconAnimation = startIconAnimation; | |
| 112 | |
| 113 /** | |
| 114 * Stops to animate the browser action icon. | |
| 115 */ | |
| 116 function stopIconAnimation() | |
|
Felix Dahlke
2015/02/12 04:52:56
Now that I'm giving this code a good read - don't
Sebastian Noack
2015/02/12 11:15:29
To keep things simple we let the current transitio
Felix Dahlke
2015/02/12 14:14:30
You're right, I somehow thought we'd abort the tra
| |
| 117 { | |
| 118 if (animationInterval != null) | |
| 89 { | 119 { |
| 90 var path = this._icons.get(page); | 120 clearInterval(animationInterval); |
| 121 animationInterval = null; | |
| 122 } | |
| 91 | 123 |
| 92 if (!path) | 124 notificationType = null; |
| 93 return; | 125 } |
| 94 | 126 exports.stopIconAnimation = stopIconAnimation; |
| 95 if (this._step > 0) | |
| 96 { | |
| 97 var suffix = "-notification-" + this._type; | |
| 98 | |
| 99 if (this._step < 10) | |
| 100 suffix += "-" + this._step; | |
| 101 | |
| 102 path = path.replace(/(?=\..+$)/, suffix); | |
| 103 } | |
| 104 | |
| 105 page.browserAction.setIcon(path); | |
| 106 } | |
| 107 }; | |
| OLD | NEW |