From 58f5369adc1514e7de355e47c3bb28b008d6563c Mon Sep 17 00:00:00 2001 From: Sorin Davidoi Date: Tue, 15 Dec 2020 23:22:24 +0100 Subject: [PATCH] feat(platform-utils): Get and react to changes to the system theme (#161) These changes will allow the WebExtension (and later the desktop application) to respect the system theme. I've added the Electron implementation until I realized that the required API [has been implemented but not released yet](https://www.electronjs.org/docs/api/native-theme/history). Let me know if you I should remove the code. Part of https://github.com/bitwarden/browser/issues/1256. https://www.electronjs.org/docs/api/native-theme Co-authored-by: Chad Scharf <3904944+cscharf@users.noreply.github.com> --- src/abstractions/platformUtils.service.ts | 2 ++ src/cli/services/cliPlatformUtils.service.ts | 7 +++++++ .../services/electronPlatformUtils.service.ts | 12 ++++++++++++ 3 files changed, 21 insertions(+) diff --git a/src/abstractions/platformUtils.service.ts b/src/abstractions/platformUtils.service.ts index c717b8962b..918528eaf2 100644 --- a/src/abstractions/platformUtils.service.ts +++ b/src/abstractions/platformUtils.service.ts @@ -34,5 +34,7 @@ export abstract class PlatformUtilsService { readFromClipboard: (options?: any) => Promise; supportsBiometric: () => Promise; authenticateBiometric: () => Promise; + getDefaultSystemTheme: () => 'light' | 'dark'; + onDefaultSystemThemeChange: (callback: ((theme: 'light' | 'dark') => unknown)) => unknown; supportsSecureStorage: () => boolean; } diff --git a/src/cli/services/cliPlatformUtils.service.ts b/src/cli/services/cliPlatformUtils.service.ts index 934ee8a7b0..a182854ddf 100644 --- a/src/cli/services/cliPlatformUtils.service.ts +++ b/src/cli/services/cliPlatformUtils.service.ts @@ -146,6 +146,13 @@ export class CliPlatformUtilsService implements PlatformUtilsService { return Promise.resolve(false); } + getDefaultSystemTheme() { + return 'light' as 'light' | 'dark'; + } + + onDefaultSystemThemeChange() { + } + supportsSecureStorage(): boolean { return false; } diff --git a/src/electron/services/electronPlatformUtils.service.ts b/src/electron/services/electronPlatformUtils.service.ts index f4dfacf980..0ea3f2188a 100644 --- a/src/electron/services/electronPlatformUtils.service.ts +++ b/src/electron/services/electronPlatformUtils.service.ts @@ -3,6 +3,7 @@ import { ipcRenderer, remote, shell, + // nativeTheme, } from 'electron'; import * as fs from 'fs'; @@ -220,6 +221,17 @@ export class ElectronPlatformUtilsService implements PlatformUtilsService { }); } + getDefaultSystemTheme() { + return 'light' as 'light' | 'dark'; + // return nativeTheme.shouldUseDarkColors ? 'dark' : 'light'; + } + + onDefaultSystemThemeChange(callback: ((theme: 'light' | 'dark') => unknown)) { + // nativeTheme.on('updated', () => { + // callback(this.getDefaultSystemTheme()); + // }); + } + supportsSecureStorage(): boolean { return true; }