1
0
mirror of https://github.com/bitwarden/browser synced 2026-01-04 17:43:39 +00:00

Use theme enum and platformUtilsService helpers (#2089)

* Use theme enum and platformUtilsService helpers

* Update jslib
This commit is contained in:
Thomas Rittson
2021-10-05 06:30:31 +10:00
committed by GitHub
parent e4260e7df5
commit 6dfb06c5b0
6 changed files with 46 additions and 29 deletions

View File

@@ -2,6 +2,8 @@ import BrowserPlatformUtilsService from './browserPlatformUtils.service';
import { DeviceType } from 'jslib-common/enums/deviceType';
const platformUtilsFactory = () => new BrowserPlatformUtilsService(null, null, null, null);
describe('Browser Utils Service', () => {
describe('getBrowser', () => {
const originalUserAgent = navigator.userAgent;
@@ -27,7 +29,7 @@ describe('Browser Utils Service', () => {
value: 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36',
});
const browserPlatformUtilsService = new BrowserPlatformUtilsService(null, null, null);
const browserPlatformUtilsService = platformUtilsFactory();
expect(browserPlatformUtilsService.getDevice()).toBe(DeviceType.ChromeExtension);
});
@@ -37,7 +39,7 @@ describe('Browser Utils Service', () => {
value: 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:58.0) Gecko/20100101 Firefox/58.0',
});
const browserPlatformUtilsService = new BrowserPlatformUtilsService(null, null, null);
const browserPlatformUtilsService = platformUtilsFactory();
expect(browserPlatformUtilsService.getDevice()).toBe(DeviceType.FirefoxExtension);
});
@@ -52,7 +54,7 @@ describe('Browser Utils Service', () => {
value: {},
});
const browserPlatformUtilsService = new BrowserPlatformUtilsService(null, null, null);
const browserPlatformUtilsService = platformUtilsFactory();
expect(browserPlatformUtilsService.getDevice()).toBe(DeviceType.OperaExtension);
});
@@ -62,7 +64,7 @@ describe('Browser Utils Service', () => {
value: 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.74 Safari/537.36 Edg/79.0.309.43',
});
const browserPlatformUtilsService = new BrowserPlatformUtilsService(null, null, null);
const browserPlatformUtilsService = platformUtilsFactory();
expect(browserPlatformUtilsService.getDevice()).toBe(DeviceType.EdgeExtension);
});
@@ -77,7 +79,7 @@ describe('Browser Utils Service', () => {
value: true,
});
const browserPlatformUtilsService = new BrowserPlatformUtilsService(null, null, null);
const browserPlatformUtilsService = platformUtilsFactory();
expect(browserPlatformUtilsService.getDevice()).toBe(DeviceType.SafariExtension);
Object.defineProperty(window, 'safariAppExtension', {
@@ -92,7 +94,7 @@ describe('Browser Utils Service', () => {
value: 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.97 Safari/537.36 Vivaldi/1.94.1008.40',
});
const browserPlatformUtilsService = new BrowserPlatformUtilsService(null, null, null);
const browserPlatformUtilsService = platformUtilsFactory();
expect(browserPlatformUtilsService.getDevice()).toBe(DeviceType.VivaldiExtension);
});
});

View File

@@ -2,9 +2,13 @@ import { BrowserApi } from '../browser/browserApi';
import { SafariApp } from '../browser/safariApp';
import { DeviceType } from 'jslib-common/enums/deviceType';
import { ThemeType } from 'jslib-common/enums/themeType';
import { MessagingService } from 'jslib-common/abstractions/messaging.service';
import { PlatformUtilsService } from 'jslib-common/abstractions/platformUtils.service';
import { StorageService } from 'jslib-common/abstractions/storage.service';
import { ConstantsService } from 'jslib-common/services/constants.service';
const DialogPromiseExpiration = 600000; // 10 minutes
@@ -16,7 +20,7 @@ export default class BrowserPlatformUtilsService implements PlatformUtilsService
private deviceCache: DeviceType = null;
private prefersColorSchemeDark = window.matchMedia('(prefers-color-scheme: dark)');
constructor(private messagingService: MessagingService,
constructor(private messagingService: MessagingService, private storageService: StorageService,
private clipboardWriteCallback: (clipboardValue: string, clearMs: number) => void,
private biometricCallback: () => Promise<boolean>) { }
@@ -317,13 +321,22 @@ export default class BrowserPlatformUtilsService implements PlatformUtilsService
return false;
}
getDefaultSystemTheme(): Promise<'light' | 'dark'> {
return Promise.resolve(this.prefersColorSchemeDark.matches ? 'dark' : 'light');
getDefaultSystemTheme(): Promise<ThemeType.Light | ThemeType.Dark> {
return Promise.resolve(this.prefersColorSchemeDark.matches ? ThemeType.Dark : ThemeType.Light);
}
onDefaultSystemThemeChange(callback: ((theme: 'light' | 'dark') => unknown)) {
onDefaultSystemThemeChange(callback: ((theme: ThemeType.Light | ThemeType.Dark) => unknown)) {
this.prefersColorSchemeDark.addEventListener('change', ({ matches }) => {
callback(matches ? 'dark' : 'light');
callback(matches ? ThemeType.Dark : ThemeType.Light);
});
}
async getEffectiveTheme() {
const theme = await this.storageService.get<ThemeType>(ConstantsService.themeKey);
if (theme == null || theme === ThemeType.System) {
return this.getDefaultSystemTheme();
} else {
return theme;
}
}
}