From fa15670e79ccb8478b4b54b59b943df1dfddd7df Mon Sep 17 00:00:00 2001 From: Anton Kosyakov Date: Tue, 16 Jul 2019 21:16:40 +0000 Subject: [PATCH] [monaco] apply theme on initialization Signed-off-by: Anton Kosyakov --- .../textmate/monaco-textmate-service.ts | 39 +++++++++++-------- 1 file changed, 22 insertions(+), 17 deletions(-) diff --git a/packages/monaco/src/browser/textmate/monaco-textmate-service.ts b/packages/monaco/src/browser/textmate/monaco-textmate-service.ts index edf4c50eb5893..52df93361c77a 100644 --- a/packages/monaco/src/browser/textmate/monaco-textmate-service.ts +++ b/packages/monaco/src/browser/textmate/monaco-textmate-service.ts @@ -16,7 +16,7 @@ import { injectable, inject, named } from 'inversify'; import { Registry, IOnigLib, IRawGrammar, parseRawGrammar } from 'vscode-textmate'; -import { ILogger, ContributionProvider, Emitter } from '@theia/core'; +import { ILogger, ContributionProvider, Emitter, DisposableCollection, Disposable } from '@theia/core'; import { FrontendApplicationContribution, isBasicWasmSupported } from '@theia/core/lib/browser'; import { ThemeService } from '@theia/core/lib/browser/theming'; import { LanguageGrammarDefinitionContribution, getEncodedLanguageId } from './textmate-contribution'; @@ -98,28 +98,33 @@ export class MonacoTextmateService implements FrontendApplicationContribution { } }); - this.themeService.onThemeChange(themeChange => { - if (themeChange.oldTheme && themeChange.oldTheme.editorTheme) { - document.body.classList.remove(themeChange.oldTheme.editorTheme); - } - const currentEditorTheme = this.currentEditorTheme; - document.body.classList.add(currentEditorTheme); - - // first update registry to run tokenization with the proper theme - const theme = this.monacoThemeRegistry.getTheme(currentEditorTheme); - if (theme) { - this.grammarRegistry.setTheme(theme); - } - - // then trigger tokenization by setting monaco theme - monaco.editor.setTheme(currentEditorTheme); - }); + this.updateTheme(); + this.themeService.onThemeChange(() => this.updateTheme()); for (const { id } of monaco.languages.getLanguages()) { monaco.languages.onLanguage(id, () => this.activateLanguage(id)); } } + protected readonly toDisposeOnUpdateTheme = new DisposableCollection(); + + protected updateTheme(): void { + this.toDisposeOnUpdateTheme.dispose(); + + const currentEditorTheme = this.currentEditorTheme; + document.body.classList.add(currentEditorTheme); + this.toDisposeOnUpdateTheme.push(Disposable.create(() => document.body.classList.remove(currentEditorTheme))); + + // first update registry to run tokenization with the proper theme + const theme = this.monacoThemeRegistry.getTheme(currentEditorTheme); + if (theme) { + this.grammarRegistry.setTheme(theme); + } + + // then trigger tokenization by setting monaco theme + monaco.editor.setTheme(currentEditorTheme); + } + protected get currentEditorTheme(): string { return this.themeService.getCurrentTheme().editorTheme || MonacoThemeRegistry.DARK_DEFAULT_THEME; }