Skip to content

Commit 41738c3

Browse files
committed
Add PWA Platform with PWA-specific badge controls
Signed-off-by: Michael Telatynski <7t3chguy@gmail.com>
1 parent 3869a4d commit 41738c3

3 files changed

Lines changed: 40 additions & 2 deletions

File tree

src/@types/global.d.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@ declare global {
3131
// https://developer.mozilla.org/en-US/docs/Web/API/InstallTrigger
3232
InstallTrigger: any;
3333
}
34+
35+
interface Navigator {
36+
// PWA badging extensions https://w3c.github.io/badging/
37+
setAppBadge?(count: number): Promise<void>;
38+
clearAppBadge?(): Promise<void>;
39+
}
3440
}
3541

3642
// add method which is missing from the node typing

src/vector/init.tsx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import * as React from "react";
2626
import * as languageHandler from "matrix-react-sdk/src/languageHandler";
2727
import SettingsStore from "matrix-react-sdk/src/settings/SettingsStore";
2828
import ElectronPlatform from "./platform/ElectronPlatform";
29+
import PWAPlatform from "./platform/PWAPlatform";
2930
import WebPlatform from "./platform/WebPlatform";
3031
import PlatformPeg from "matrix-react-sdk/src/PlatformPeg";
3132
import SdkConfig from "matrix-react-sdk/src/SdkConfig";
@@ -39,8 +40,10 @@ export const rageshakePromise = initRageshake();
3940
export function preparePlatform() {
4041
if (window.ipcRenderer) {
4142
console.log("Using Electron platform");
42-
const plaf = new ElectronPlatform();
43-
PlatformPeg.set(plaf);
43+
PlatformPeg.set(new ElectronPlatform());
44+
} else if (window.matchMedia('(display-mode: standalone)').matches) {
45+
console.log("Using PWA platform");
46+
PlatformPeg.set(new PWAPlatform());
4447
} else {
4548
console.log("Using Web platform");
4649
PlatformPeg.set(new WebPlatform());

src/vector/platform/PWAPlatform.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
Copyright 2020 New Vector Ltd
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
import WebPlatform from "./WebPlatform";
18+
19+
export default class PWAPlatform extends WebPlatform {
20+
setNotificationCount(count: number) {
21+
if (!navigator.setAppBadge) return super.setNotificationCount(count);
22+
if (this.notificationCount === count) return;
23+
this.notificationCount = count;
24+
25+
navigator.setAppBadge(count).catch(e => {
26+
console.error("Failed to update PWA app badge", e);
27+
});
28+
}
29+
}

0 commit comments

Comments
 (0)