-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Anton Kosyakov <anton.kosyakov@typefox.io>
- Loading branch information
Showing
10 changed files
with
233 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/******************************************************************************** | ||
* Copyright (C) 2019 TypeFox and others. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License v. 2.0 which is available at | ||
* http://www.eclipse.org/legal/epl-2.0. | ||
* | ||
* This Source Code may also be made available under the following Secondary | ||
* Licenses when the conditions for such availability set forth in the Eclipse | ||
* Public License v. 2.0 are satisfied: GNU General Public License, version 2 | ||
* with the GNU Classpath Exception which is available at | ||
* https://www.gnu.org/software/classpath/license.html. | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 | ||
********************************************************************************/ | ||
|
||
import { injectable } from 'inversify'; | ||
|
||
/** | ||
* It should be implemented by an extension, e.g. by the monaco extension. | ||
*/ | ||
@injectable() | ||
export class ColorRegistry { | ||
|
||
*getColors(): IterableIterator<string> { } | ||
|
||
getCurrentColor(id: string): string | undefined { | ||
return undefined; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/******************************************************************************** | ||
* Copyright (C) 2019 TypeFox and others. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License v. 2.0 which is available at | ||
* http://www.eclipse.org/legal/epl-2.0. | ||
* | ||
* This Source Code may also be made available under the following Secondary | ||
* Licenses when the conditions for such availability set forth in the Eclipse | ||
* Public License v. 2.0 are satisfied: GNU General Public License, version 2 | ||
* with the GNU Classpath Exception which is available at | ||
* https://www.gnu.org/software/classpath/license.html. | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 | ||
********************************************************************************/ | ||
|
||
import { injectable } from 'inversify'; | ||
import { ColorRegistry } from '@theia/core/lib/browser/color-registry'; | ||
|
||
@injectable() | ||
export class MonacoColorRegistry implements ColorRegistry { | ||
|
||
protected readonly monacoThemeService = monaco.services.StaticServices.standaloneThemeService.get(); | ||
protected readonly monacoColorRegistry = monaco.color.getColorRegistry(); | ||
|
||
*getColors(): IterableIterator<string> { | ||
for (const { id } of this.monacoColorRegistry.getColors()) { | ||
yield id; | ||
} | ||
} | ||
|
||
getCurrentColor(id: string): string | undefined { | ||
const color = this.monacoThemeService.getTheme().getColor(id); | ||
return color && color.toString(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
115 changes: 115 additions & 0 deletions
115
packages/plugin-ext/src/main/browser/webview/webview-theme-data-provider.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
/******************************************************************************** | ||
* Copyright (C) 2019 TypeFox and others. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License v. 2.0 which is available at | ||
* http://www.eclipse.org/legal/epl-2.0. | ||
* | ||
* This Source Code may also be made available under the following Secondary | ||
* Licenses when the conditions for such availability set forth in the Eclipse | ||
* Public License v. 2.0 are satisfied: GNU General Public License, version 2 | ||
* with the GNU Classpath Exception which is available at | ||
* https://www.gnu.org/software/classpath/license.html. | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 | ||
********************************************************************************/ | ||
/*--------------------------------------------------------------------------------------------- | ||
* Copyright (c) Microsoft Corporation. All rights reserved. | ||
* Licensed under the MIT License. See License.txt in the project root for license information. | ||
*--------------------------------------------------------------------------------------------*/ | ||
// copied and modified from https://github.com/microsoft/vscode/blob/ba40bd16433d5a817bfae15f3b4350e18f144af4/src/vs/workbench/contrib/webview/common/themeing.ts | ||
|
||
import { inject, postConstruct, injectable } from 'inversify'; | ||
import { Emitter } from '@theia/core/lib/common/event'; | ||
import { EditorPreferences, EditorConfiguration } from '@theia/editor/lib/browser/editor-preferences'; | ||
import { ThemeService } from '@theia/core/lib/browser/theming'; | ||
import { ColorRegistry } from '@theia/core/lib/browser/color-registry'; | ||
|
||
export type WebviewThemeType = 'vscode-light' | 'vscode-dark' | 'vscode-high-contrast'; | ||
export interface WebviewThemeData { | ||
readonly activeTheme: WebviewThemeType; | ||
readonly styles: { readonly [key: string]: string | number; }; | ||
} | ||
|
||
@injectable() | ||
export class WebviewThemeDataProvider { | ||
|
||
protected readonly onDidChangeThemeDataEmitter = new Emitter<void>(); | ||
readonly onDidChangeThemeData = this.onDidChangeThemeDataEmitter.event; | ||
|
||
@inject(EditorPreferences) | ||
protected readonly editorPreferences: EditorPreferences; | ||
|
||
@inject(ColorRegistry) | ||
protected readonly colorRegistry: ColorRegistry; | ||
|
||
protected themeData: WebviewThemeData | undefined; | ||
|
||
protected readonly editorStyles = new Map<keyof EditorConfiguration>([ | ||
['editor.fontFamily', 'editor-font-family'], | ||
['editor.fontWeight', 'editor-font-weight'], | ||
['editor.fontSize', 'editor-font-size'] | ||
]); | ||
|
||
@postConstruct() | ||
protected init(): void { | ||
ThemeService.get().onThemeChange(() => this.reset()); | ||
|
||
this.editorPreferences.onPreferenceChanged(e => { | ||
if (this.editorStyles.has(e.preferenceName)) { | ||
this.reset(); | ||
} | ||
}); | ||
} | ||
|
||
protected reset(): void { | ||
if (this.themeData) { | ||
this.themeData = undefined; | ||
this.onDidChangeThemeDataEmitter.fire(undefined); | ||
} | ||
} | ||
|
||
getThemeData(): WebviewThemeData { | ||
if (!this.themeData) { | ||
this.themeData = this.computeThemeData(); | ||
} | ||
return this.themeData; | ||
} | ||
|
||
protected computeThemeData(): WebviewThemeData { | ||
const styles: { [key: string]: string | number; } = {}; | ||
// tslint:disable-next-line:no-any | ||
const addStyle = (id: string, rawValue: any) => { | ||
if (rawValue) { | ||
const value = typeof rawValue === 'number' || typeof rawValue === 'string' ? rawValue : String(rawValue); | ||
styles['vscode-' + id.replace('.', '-')] = value; | ||
styles['theia-' + id.replace('.', '-')] = value; | ||
} | ||
}; | ||
|
||
addStyle('font-family', '-apple-system, BlinkMacSystemFont, "Segoe WPC", "Segoe UI", "Ubuntu", "Droid Sans", sans-serif'); | ||
addStyle('font-weight', 'normal'); | ||
addStyle('font-size', '13px'); | ||
this.editorStyles.forEach((value, key) => addStyle(value, this.editorPreferences[key])); | ||
|
||
for (const id of this.colorRegistry.getColors()) { | ||
const color = this.colorRegistry.getCurrentColor(id); | ||
if (color) { | ||
addStyle(id, color.toString()); | ||
} | ||
} | ||
|
||
const activeTheme = this.getActiveTheme(); | ||
return { styles, activeTheme }; | ||
} | ||
|
||
protected getActiveTheme(): WebviewThemeType { | ||
const theme = ThemeService.get().getCurrentTheme(); | ||
switch (theme.type) { | ||
case 'light': return 'vscode-light'; | ||
case 'dark': return 'vscode-dark'; | ||
default: return 'vscode-high-contrast'; | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters