From 5ddbda0172d80bfbb2529987ba9020848e8771f7 Mon Sep 17 00:00:00 2001 From: Martin Aeschlimann Date: Mon, 2 Dec 2019 16:05:53 +0100 Subject: [PATCH] add theming API --- src/vs/vscode.proposed.d.ts | 36 ++++++++++++++++++ .../api/browser/extensionHost.contribution.ts | 1 + .../api/browser/mainThreadTheming.ts | 33 ++++++++++++++++ .../workbench/api/common/extHost.api.impl.ts | 13 ++++++- .../workbench/api/common/extHost.protocol.ts | 12 +++++- src/vs/workbench/api/common/extHostTheming.ts | 38 +++++++++++++++++++ src/vs/workbench/api/common/extHostTypes.ts | 17 +++++++++ 7 files changed, 146 insertions(+), 4 deletions(-) create mode 100644 src/vs/workbench/api/browser/mainThreadTheming.ts create mode 100644 src/vs/workbench/api/common/extHostTheming.ts diff --git a/src/vs/vscode.proposed.d.ts b/src/vs/vscode.proposed.d.ts index 5d2f4c6e87c..6dbee43c706 100644 --- a/src/vs/vscode.proposed.d.ts +++ b/src/vs/vscode.proposed.d.ts @@ -1412,4 +1412,40 @@ declare module 'vscode' { } //#endregion + + //#region color theme access + + /** + * Represents a color theme kind. + */ + export enum ColorThemeKind { + Light = 1, + Dark = 2, + HighContrast = 3 + } + + /** + * Represents a color theme. + */ + export interface ColorTheme { + + /** + * The kind of this color theme: light, dark or high contrast. + */ + readonly kind: ColorThemeKind; + } + + export namespace window { + /** + * The currently active color theme as configured in the settings. The active + * theme can be changed via the `workbench.colorTheme` setting. + */ + export let activeColorTheme: ColorTheme; + + /** + * An [event](#Event) which fires when the active theme changes or one of it's colors chnage. + */ + export const onDidChangeActiveColorTheme: Event; + } + } diff --git a/src/vs/workbench/api/browser/extensionHost.contribution.ts b/src/vs/workbench/api/browser/extensionHost.contribution.ts index 2905c524113..dbdaa9b0509 100644 --- a/src/vs/workbench/api/browser/extensionHost.contribution.ts +++ b/src/vs/workbench/api/browser/extensionHost.contribution.ts @@ -47,6 +47,7 @@ import './mainThreadStatusBar'; import './mainThreadStorage'; import './mainThreadTelemetry'; import './mainThreadTerminalService'; +import './mainThreadTheming'; import './mainThreadTreeViews'; import './mainThreadDownloadService'; import './mainThreadUrls'; diff --git a/src/vs/workbench/api/browser/mainThreadTheming.ts b/src/vs/workbench/api/browser/mainThreadTheming.ts new file mode 100644 index 00000000000..3caaa560e30 --- /dev/null +++ b/src/vs/workbench/api/browser/mainThreadTheming.ts @@ -0,0 +1,33 @@ +/*--------------------------------------------------------------------------------------------- + * Copyright (c) Microsoft Corporation. All rights reserved. + * Licensed under the MIT License. See License.txt in the project root for license information. + *--------------------------------------------------------------------------------------------*/ + +import { MainContext, IExtHostContext, ExtHostThemingShape, ExtHostContext, MainThreadThemingShape } from '../common/extHost.protocol'; +import { extHostNamedCustomer } from 'vs/workbench/api/common/extHostCustomers'; +import { IDisposable } from 'vs/base/common/lifecycle'; +import { IThemeService } from 'vs/platform/theme/common/themeService'; + +@extHostNamedCustomer(MainContext.MainThreadTheming) +export class MainThreadTheming implements MainThreadThemingShape { + + private readonly _themeService: IThemeService; + private readonly _proxy: ExtHostThemingShape; + private readonly _themeChangeListener: IDisposable; + + constructor( + extHostContext: IExtHostContext, + @IThemeService themeService: IThemeService + ) { + this._themeService = themeService; + this._proxy = extHostContext.getProxy(ExtHostContext.ExtHostTheming); + + this._themeChangeListener = this._themeService.onThemeChange(e => { + this._proxy.$onColorThemeChange(this._themeService.getTheme().type); + }); + } + + dispose(): void { + this._themeChangeListener.dispose(); + } +} diff --git a/src/vs/workbench/api/common/extHost.api.impl.ts b/src/vs/workbench/api/common/extHost.api.impl.ts index 9e32aedf532..cfec00d5d01 100644 --- a/src/vs/workbench/api/common/extHost.api.impl.ts +++ b/src/vs/workbench/api/common/extHost.api.impl.ts @@ -67,6 +67,7 @@ import { ILogService } from 'vs/platform/log/common/log'; import { IURITransformerService } from 'vs/workbench/api/common/extHostUriTransformerService'; import { IExtHostRpcService } from 'vs/workbench/api/common/extHostRpcService'; import { IExtHostInitDataService } from 'vs/workbench/api/common/extHostInitDataService'; +import { ExtHostTheming } from 'vs/workbench/api/common/extHostTheming'; export interface IExtensionApiFactory { (extension: IExtensionDescription, registry: ExtensionDescriptionRegistry, configProvider: ExtHostConfigProvider): typeof vscode; @@ -122,7 +123,8 @@ export function createApiFactoryAndRegisterActors(accessor: ServicesAccessor): I const extHostComment = rpcProtocol.set(ExtHostContext.ExtHostComments, new ExtHostComments(rpcProtocol, extHostCommands, extHostDocuments)); const extHostWindow = rpcProtocol.set(ExtHostContext.ExtHostWindow, new ExtHostWindow(rpcProtocol)); const extHostProgress = rpcProtocol.set(ExtHostContext.ExtHostProgress, new ExtHostProgress(rpcProtocol.getProxy(MainContext.MainThreadProgress))); - const extHostLabelService = rpcProtocol.set(ExtHostContext.ExtHosLabelService, new ExtHostLabelService(rpcProtocol)); + const extHostLabelService = rpcProtocol.set(ExtHostContext.ExtHostLabelService, new ExtHostLabelService(rpcProtocol)); + const extHostTheming = rpcProtocol.set(ExtHostContext.ExtHostTheming, new ExtHostTheming(rpcProtocol)); // Check that no named customers are missing const expected: ProxyIdentifier[] = values(ExtHostContext); @@ -550,6 +552,12 @@ export function createApiFactoryAndRegisterActors(accessor: ServicesAccessor): I }, createInputBox(): vscode.InputBox { return extHostQuickOpen.createInputBox(extension.identifier); + }, + get activeColorTheme(): vscode.ColorTheme { + return extHostTheming.activeColorTheme; + }, + onDidChangeActiveColorTheme(listener, thisArg?, disposables?) { + return extHostTheming.onDidChangeActiveColorTheme(listener, thisArg, disposables); } }; @@ -939,7 +947,8 @@ export function createApiFactoryAndRegisterActors(accessor: ServicesAccessor): I DebugConsoleMode: extHostTypes.DebugConsoleMode, Decoration: extHostTypes.Decoration, WebviewContentState: extHostTypes.WebviewContentState, - UIKind: UIKind + UIKind: UIKind, + ColorThemeKind: extHostTypes.ColorThemeKind }; }; } diff --git a/src/vs/workbench/api/common/extHost.protocol.ts b/src/vs/workbench/api/common/extHost.protocol.ts index 4bf6846bd20..fc81b8a2d59 100644 --- a/src/vs/workbench/api/common/extHost.protocol.ts +++ b/src/vs/workbench/api/common/extHost.protocol.ts @@ -1385,6 +1385,12 @@ export interface ExtHostStorageShape { $acceptValue(shared: boolean, key: string, value: object | undefined): void; } +export interface ExtHostThemingShape { + $onColorThemeChange(themeType: string): void; +} + +export interface MainThreadThemingShape extends IDisposable { +} // --- proxy identifiers export const MainContext = { @@ -1425,7 +1431,8 @@ export const MainContext = { MainThreadSearch: createMainId('MainThreadSearch'), MainThreadTask: createMainId('MainThreadTask'), MainThreadWindow: createMainId('MainThreadWindow'), - MainThreadLabelService: createMainId('MainThreadLabelService') + MainThreadLabelService: createMainId('MainThreadLabelService'), + MainThreadTheming: createMainId('MainThreadTheming') }; export const ExtHostContext = { @@ -1459,5 +1466,6 @@ export const ExtHostContext = { ExtHostStorage: createMainId('ExtHostStorage'), ExtHostUrls: createExtId('ExtHostUrls'), ExtHostOutputService: createMainId('ExtHostOutputService'), - ExtHosLabelService: createMainId('ExtHostLabelService') + ExtHostLabelService: createMainId('ExtHostLabelService'), + ExtHostTheming: createMainId('ExtHostTheming') }; diff --git a/src/vs/workbench/api/common/extHostTheming.ts b/src/vs/workbench/api/common/extHostTheming.ts new file mode 100644 index 00000000000..04c3d517fdc --- /dev/null +++ b/src/vs/workbench/api/common/extHostTheming.ts @@ -0,0 +1,38 @@ +/*--------------------------------------------------------------------------------------------- + * Copyright (c) Microsoft Corporation. All rights reserved. + * Licensed under the MIT License. See License.txt in the project root for license information. + *--------------------------------------------------------------------------------------------*/ + +import { ColorTheme, ColorThemeKind } from './extHostTypes'; +import { IExtHostRpcService } from 'vs/workbench/api/common/extHostRpcService'; +import { ExtHostThemingShape } from 'vs/workbench/api/common/extHost.protocol'; +import { Emitter, Event } from 'vs/base/common/event'; + +export class ExtHostTheming implements ExtHostThemingShape { + + readonly _serviceBrand: undefined; + + private _actual: ColorTheme; + private _onDidChangeActiveColorTheme: Emitter; + + constructor( + @IExtHostRpcService _extHostRpc: IExtHostRpcService + ) { + this._actual = new ColorTheme(ColorThemeKind.Dark); + this._onDidChangeActiveColorTheme = new Emitter(); + } + + public get activeColorTheme(): ColorTheme { + return this._actual; + } + + $onColorThemeChange(type: string): void { + let kind = type === 'light' ? ColorThemeKind.Light : type === 'dark' ? ColorThemeKind.Dark : ColorThemeKind.HighContrast; + this._actual = new ColorTheme(kind); + this._onDidChangeActiveColorTheme.fire(this._actual); + } + + public get onDidChangeActiveColorTheme(): Event { + return this._onDidChangeActiveColorTheme.event; + } +} diff --git a/src/vs/workbench/api/common/extHostTypes.ts b/src/vs/workbench/api/common/extHostTypes.ts index 8f89d246b5a..18d93b3edef 100644 --- a/src/vs/workbench/api/common/extHostTypes.ts +++ b/src/vs/workbench/api/common/extHostTypes.ts @@ -2503,3 +2503,20 @@ export enum WebviewContentState { Unchanged = 2, Dirty = 3, } + + +//#region Theming + +@es5ClassCompat +export class ColorTheme implements vscode.ColorTheme { + constructor(public readonly kind: ColorThemeKind) { + } +} + +export enum ColorThemeKind { + Light = 1, + Dark = 2, + HighContrast = 3 +} + +//#endregion Theming -- GitLab