Skip to content

Commit 0b5085e

Browse files
authored
Merge pull request element-hq#2531 from vector-im/dbkr/platform
Implement Platforms
2 parents 735c298 + a714edb commit 0b5085e

4 files changed

Lines changed: 120 additions & 0 deletions

File tree

‎package.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
"classnames": "^2.1.2",
5454
"draft-js": "^0.8.1",
5555
"extract-text-webpack-plugin": "^0.9.1",
56+
"favico.js": "^0.3.10",
5657
"filesize": "^3.1.2",
5758
"flux": "~2.0.3",
5859
"gemini-scrollbar": "matrix-org/gemini-scrollbar#b302279",

‎src/vector/index.js‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ if (process.env.NODE_ENV !== 'production') {
4747
var RunModernizrTests = require("./modernizr"); // this side-effects a global
4848
var ReactDOM = require("react-dom");
4949
var sdk = require("matrix-react-sdk");
50+
var PlatformPeg = require("matrix-react-sdk/lib/PlatformPeg");
5051
sdk.loadSkin(require('../component-index'));
5152
var VectorConferenceHandler = require('../VectorConferenceHandler');
5253
var UpdateChecker = require("./updater");
@@ -57,6 +58,7 @@ import UAParser from 'ua-parser-js';
5758
import url from 'url';
5859

5960
import {parseQs, parseQsFromFragment} from './url_utils';
61+
import Platform from './platform';
6062

6163
var lastLocationHashSet = null;
6264

@@ -210,6 +212,9 @@ async function loadApp() {
210212
const fragparts = parseQsFromFragment(window.location);
211213
const params = parseQs(window.location);
212214

215+
// set the platform for react sdk (our Platform object automatically picks the right one)
216+
PlatformPeg.set(new Platform());
217+
213218
// don't try to redirect to the native apps if we're
214219
// verifying a 3pid
215220
const preventRedirect = Boolean(fragparts.params.client_secret);

‎src/vector/platform/WebPlatform.js‎

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
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+
}

‎src/vector/platform/index.js‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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 WebPlatform from './WebPlatform';
20+
21+
let Platform = WebPlatform;
22+
23+
export default Platform;

0 commit comments

Comments
 (0)