Add theme enums and platformUtilsService helper (#497)

* Use enum for themes, add getEffectiveTheme

* Update electron and cli to use theme refactor
This commit is contained in:
Thomas Rittson 2021-09-30 06:37:36 +10:00 committed by GitHub
parent 91b73fa777
commit ce71c0c0bd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 35 additions and 7 deletions

View File

@ -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<string>;
supportsBiometric: () => Promise<boolean>;
authenticateBiometric: () => Promise<boolean>;
getDefaultSystemTheme: () => Promise<'light' | 'dark'>;
onDefaultSystemThemeChange: (callback: ((theme: 'light' | 'dark') => unknown)) => unknown;
getDefaultSystemTheme: () => Promise<ThemeType.Light | ThemeType.Dark>;
onDefaultSystemThemeChange: (callback: ((theme: ThemeType.Light | ThemeType.Dark) => unknown)) => unknown;
getEffectiveTheme: () => Promise<ThemeType>;
supportsSecureStorage: () => boolean;
}

View File

@ -0,0 +1,7 @@
export enum ThemeType {
System = 'system',
Light = 'light',
Dark = 'dark',
Nord = 'nord',
SolarizedDark = 'solarizedDark',
}

View File

@ -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);
});
}

View File

@ -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<ThemeType>(ConstantsService.themeKey);
if (theme == null || theme === ThemeType.System) {
return this.getDefaultSystemTheme();
} else {
return theme;
}
}
supportsSecureStorage(): boolean {

View File

@ -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;
}