From ce71c0c0bd6667573e0e611222dc415770ba3909 Mon Sep 17 00:00:00 2001 From: Thomas Rittson <31796059+eliykat@users.noreply.github.com> Date: Thu, 30 Sep 2021 06:37:36 +1000 Subject: [PATCH] Add theme enums and platformUtilsService helper (#497) * Use enum for themes, add getEffectiveTheme * Update electron and cli to use theme refactor --- common/src/abstractions/platformUtils.service.ts | 6 ++++-- common/src/enums/themeType.ts | 7 +++++++ .../services/electronMainMessaging.service.ts | 6 ++++-- .../services/electronPlatformUtils.service.ts | 16 ++++++++++++++-- .../src/cli/services/cliPlatformUtils.service.ts | 7 ++++++- 5 files changed, 35 insertions(+), 7 deletions(-) create mode 100644 common/src/enums/themeType.ts diff --git a/common/src/abstractions/platformUtils.service.ts b/common/src/abstractions/platformUtils.service.ts index 5e0f311a12..7f792e614b 100644 --- a/common/src/abstractions/platformUtils.service.ts +++ b/common/src/abstractions/platformUtils.service.ts @@ -1,4 +1,5 @@ import { DeviceType } from '../enums/deviceType'; +import { ThemeType } from '../enums/themeType'; export abstract class PlatformUtilsService { identityClientId: string; @@ -28,7 +29,8 @@ export abstract class PlatformUtilsService { readFromClipboard: (options?: any) => Promise; supportsBiometric: () => Promise; authenticateBiometric: () => Promise; - getDefaultSystemTheme: () => Promise<'light' | 'dark'>; - onDefaultSystemThemeChange: (callback: ((theme: 'light' | 'dark') => unknown)) => unknown; + getDefaultSystemTheme: () => Promise; + onDefaultSystemThemeChange: (callback: ((theme: ThemeType.Light | ThemeType.Dark) => unknown)) => unknown; + getEffectiveTheme: () => Promise; supportsSecureStorage: () => boolean; } diff --git a/common/src/enums/themeType.ts b/common/src/enums/themeType.ts new file mode 100644 index 0000000000..40e7c2ddfd --- /dev/null +++ b/common/src/enums/themeType.ts @@ -0,0 +1,7 @@ +export enum ThemeType { + System = 'system', + Light = 'light', + Dark = 'dark', + Nord = 'nord', + SolarizedDark = 'solarizedDark', +} diff --git a/electron/src/services/electronMainMessaging.service.ts b/electron/src/services/electronMainMessaging.service.ts index 6f11b0bdbf..d58d3b09a2 100644 --- a/electron/src/services/electronMainMessaging.service.ts +++ b/electron/src/services/electronMainMessaging.service.ts @@ -3,6 +3,8 @@ import { promises as fs } from 'fs'; import { MessagingService } from 'jslib-common/abstractions/messaging.service'; import { RendererMenuItem } from '../utils'; +import { ThemeType } from 'jslib-common/enums/themeType'; + import { WindowMain } from '../window.main'; export class ElectronMainMessagingService implements MessagingService { @@ -12,7 +14,7 @@ export class ElectronMainMessagingService implements MessagingService { }); ipcMain.handle('systemTheme', () => { - return nativeTheme.shouldUseDarkColors ? 'dark' : 'light'; + return nativeTheme.shouldUseDarkColors ? ThemeType.Dark : ThemeType.Light; }); ipcMain.handle('showMessageBox', (event, options) => { @@ -42,7 +44,7 @@ export class ElectronMainMessagingService implements MessagingService { }); nativeTheme.on('updated', () => { - windowMain.win?.webContents.send('systemThemeUpdated', nativeTheme.shouldUseDarkColors ? 'dark' : 'light'); + windowMain.win?.webContents.send('systemThemeUpdated', nativeTheme.shouldUseDarkColors ? ThemeType.Dark : ThemeType.Light); }); } diff --git a/electron/src/services/electronPlatformUtils.service.ts b/electron/src/services/electronPlatformUtils.service.ts index 07f9660396..7ffe3e3f94 100644 --- a/electron/src/services/electronPlatformUtils.service.ts +++ b/electron/src/services/electronPlatformUtils.service.ts @@ -10,12 +10,15 @@ import { } from '../utils'; import { DeviceType } from 'jslib-common/enums/deviceType'; +import { ThemeType } from 'jslib-common/enums/themeType'; import { I18nService } from 'jslib-common/abstractions/i18n.service'; 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'; + import { ElectronConstants } from '../electronConstants'; export class ElectronPlatformUtilsService implements PlatformUtilsService { @@ -192,8 +195,17 @@ export class ElectronPlatformUtilsService implements PlatformUtilsService { return ipcRenderer.invoke('systemTheme'); } - onDefaultSystemThemeChange(callback: ((theme: 'light' | 'dark') => unknown)) { - ipcRenderer.on('systemThemeUpdated', (event, theme: 'light' | 'dark') => callback(theme)); + onDefaultSystemThemeChange(callback: ((theme: ThemeType.Light | ThemeType.Dark) => unknown)) { + ipcRenderer.on('systemThemeUpdated', (event, theme: ThemeType.Light | ThemeType.Dark) => callback(theme)); + } + + async getEffectiveTheme() { + const theme = await this.storageService.get(ConstantsService.themeKey); + if (theme == null || theme === ThemeType.System) { + return this.getDefaultSystemTheme(); + } else { + return theme; + } } supportsSecureStorage(): boolean { diff --git a/node/src/cli/services/cliPlatformUtils.service.ts b/node/src/cli/services/cliPlatformUtils.service.ts index 0b81b2e854..dc2d7e1b54 100644 --- a/node/src/cli/services/cliPlatformUtils.service.ts +++ b/node/src/cli/services/cliPlatformUtils.service.ts @@ -1,6 +1,7 @@ import * as child_process from 'child_process'; import { DeviceType } from 'jslib-common/enums/deviceType'; +import { ThemeType } from 'jslib-common/enums/themeType'; import { PlatformUtilsService } from 'jslib-common/abstractions/platformUtils.service'; @@ -135,13 +136,17 @@ export class CliPlatformUtilsService implements PlatformUtilsService { } getDefaultSystemTheme() { - return Promise.resolve('light' as 'light' | 'dark'); + return Promise.resolve(ThemeType.Light as ThemeType.Light | ThemeType.Dark); } onDefaultSystemThemeChange() { /* noop */ } + getEffectiveTheme() { + return Promise.resolve(ThemeType.Light); + } + supportsSecureStorage(): boolean { return false; }