|
| 1 | +// @flow |
| 2 | + |
| 3 | +/* |
| 4 | +Copyright 2016 Aviral Dasgupta and OpenMarket Ltd |
| 5 | +
|
| 6 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +you may not use this file except in compliance with the License. |
| 8 | +You may obtain a copy of the License at |
| 9 | +
|
| 10 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +
|
| 12 | +Unless required by applicable law or agreed to in writing, software |
| 13 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +See the License for the specific language governing permissions and |
| 16 | +limitations under the License. |
| 17 | +*/ |
| 18 | + |
| 19 | +import BasePlatform from 'matrix-react-sdk/lib/BasePlatform'; |
| 20 | +import Favico from 'favico.js'; |
| 21 | +import dis from 'matrix-react-sdk/lib/dispatcher.js'; |
| 22 | +import q from 'q'; |
| 23 | + |
| 24 | +export default class WebPlatform extends BasePlatform { |
| 25 | + constructor() { |
| 26 | + super(); |
| 27 | + // The 'animations' are really low framerate and look terrible. |
| 28 | + // Also it re-starts the animationb every time you set the badge, |
| 29 | + // and we set the state each time, even if the value hasn't changed, |
| 30 | + // so we'd need to fix that if enabling the animation. |
| 31 | + this.favicon = new Favico({animation: 'none'}); |
| 32 | + this._updateFavicon(); |
| 33 | + } |
| 34 | + |
| 35 | + _updateFavicon() { |
| 36 | + try { |
| 37 | + // This needs to be in in a try block as it will throw |
| 38 | + // if there are more than 100 badge count changes in |
| 39 | + // its internal queue |
| 40 | + let bgColor = "#d00", |
| 41 | + notif = this.notificationCount; |
| 42 | + |
| 43 | + if (this.errorDidOccur) { |
| 44 | + notif = notif || "×"; |
| 45 | + bgColor = "#f00"; |
| 46 | + } |
| 47 | + |
| 48 | + this.favicon.badge(notif, { |
| 49 | + bgColor: bgColor |
| 50 | + }); |
| 51 | + } catch (e) { |
| 52 | + console.warn(`Failed to set badge count: ${e.message}`); |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + setNotificationCount(count: number) { |
| 57 | + super.setNotificationCount(count); |
| 58 | + this._updateFavicon(); |
| 59 | + } |
| 60 | + |
| 61 | + setErrorStatus(errorDidOccur: boolean) { |
| 62 | + super.setErrorStatus(errorDidOccur); |
| 63 | + this._updateFavicon(); |
| 64 | + } |
| 65 | + |
| 66 | + displayNotification(title: string, msg: string, avatarUrl: string) { |
| 67 | + const notification = new global.Notification( |
| 68 | + title, |
| 69 | + { |
| 70 | + body: msg, |
| 71 | + icon: avatarUrl, |
| 72 | + tag: "vector", |
| 73 | + silent: true, // we play our own sounds |
| 74 | + } |
| 75 | + ); |
| 76 | + |
| 77 | + notification.onclick = function() { |
| 78 | + dis.dispatch({ |
| 79 | + action: 'view_room', |
| 80 | + room_id: room.roomId |
| 81 | + }); |
| 82 | + global.focus(); |
| 83 | + }; |
| 84 | + |
| 85 | + // Chrome only dismisses notifications after 20s, which |
| 86 | + // is waaaaay too long |
| 87 | + global.setTimeout(function() { |
| 88 | + notification.close(); |
| 89 | + }, 5 * 1000); |
| 90 | + } |
| 91 | +} |
0 commit comments