Files
vscode/src/vs/platform/theme/electron-main/themeMainService.ts
T
Martin Aeschlimann 9245f60f40 Add hc light theme kind
2022-03-08 17:05:33 +01:00

95 lines
3.2 KiB
TypeScript

/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { BrowserWindow, nativeTheme } from 'electron';
import { isMacintosh, isWindows } from 'vs/base/common/platform';
import { createDecorator } from 'vs/platform/instantiation/common/instantiation';
import { IStateMainService } from 'vs/platform/state/electron-main/state';
import { IPartsSplash } from 'vs/platform/theme/common/themeService';
const DEFAULT_BG_LIGHT = '#FFFFFF';
const DEFAULT_BG_DARK = '#1E1E1E';
const DEFAULT_BG_HC_BLACK = '#000000';
const DEFAULT_BG_HC_LIGHT = '#FFFFFF';
const THEME_STORAGE_KEY = 'theme';
const THEME_BG_STORAGE_KEY = 'themeBackground';
const THEME_WINDOW_SPLASH = 'windowSplash';
export const IThemeMainService = createDecorator<IThemeMainService>('themeMainService');
export interface IThemeMainService {
readonly _serviceBrand: undefined;
getBackgroundColor(): string;
saveWindowSplash(windowId: number | undefined, splash: IPartsSplash): void;
getWindowSplash(): IPartsSplash | undefined;
}
export class ThemeMainService implements IThemeMainService {
declare readonly _serviceBrand: undefined;
constructor(@IStateMainService private stateMainService: IStateMainService) { }
getBackgroundColor(): string {
if ((isWindows || isMacintosh) && nativeTheme.shouldUseInvertedColorScheme) {
return DEFAULT_BG_HC_BLACK;
}
let background = this.stateMainService.getItem<string | null>(THEME_BG_STORAGE_KEY, null);
if (!background) {
let baseTheme: string;
if ((isWindows || isMacintosh) && nativeTheme.shouldUseInvertedColorScheme) {
baseTheme = 'hc-black';
} else {
baseTheme = this.stateMainService.getItem<string>(THEME_STORAGE_KEY, 'vs-dark').split(' ')[0];
}
switch (baseTheme) {
case 'vs': background = DEFAULT_BG_LIGHT; break;
case 'hc-black': background = DEFAULT_BG_HC_BLACK; break;
case 'hc-light': background = DEFAULT_BG_HC_LIGHT; break;
default: background = DEFAULT_BG_DARK;
}
}
if (isMacintosh && background.toUpperCase() === DEFAULT_BG_DARK) {
background = '#171717'; // https://github.com/electron/electron/issues/5150
}
return background;
}
saveWindowSplash(windowId: number | undefined, splash: IPartsSplash): void {
// Update in storage
this.stateMainService.setItems([
{ key: THEME_STORAGE_KEY, data: splash.baseTheme },
{ key: THEME_BG_STORAGE_KEY, data: splash.colorInfo.background },
{ key: THEME_WINDOW_SPLASH, data: splash }
]);
// Update in opened windows
if (typeof windowId === 'number') {
this.updateBackgroundColor(windowId, splash);
}
}
private updateBackgroundColor(windowId: number, splash: IPartsSplash): void {
for (const window of BrowserWindow.getAllWindows()) {
if (window.id === windowId) {
window.setBackgroundColor(splash.colorInfo.background);
break;
}
}
}
getWindowSplash(): IPartsSplash | undefined {
return this.stateMainService.getItem<IPartsSplash>(THEME_WINDOW_SPLASH);
}
}