Skip to content

Commit

Permalink
[monaco] apply theme on initialization
Browse files Browse the repository at this point in the history
Signed-off-by: Anton Kosyakov <anton.kosyakov@typefox.io>
  • Loading branch information
akosyakov committed Jul 17, 2019
1 parent a7cf531 commit fa15670
Showing 1 changed file with 22 additions and 17 deletions.
39 changes: 22 additions & 17 deletions packages/monaco/src/browser/textmate/monaco-textmate-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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;
}
Expand Down

0 comments on commit fa15670

Please sign in to comment.