Create basic theme service

Part of #4184
This commit is contained in:
Daniel Imms
2022-10-08 11:43:19 -07:00
parent 8db2244f5d
commit c4750eaf77
16 changed files with 120 additions and 54 deletions
+18 -3
View File
@@ -5,10 +5,11 @@
import { IEvent } from 'common/EventEmitter';
import { IRenderDimensions, IRenderer } from 'browser/renderer/shared/Types';
import { IColorSet } from 'browser/Types';
import { IColorSet, ReadonlyColorSet } from 'browser/Types';
import { ISelectionRedrawRequestEvent as ISelectionRequestRedrawEvent, ISelectionRequestScrollLinesEvent } from 'browser/selection/Types';
import { createDecorator } from 'common/services/ServiceRegistry';
import { IDisposable } from 'common/Types';
import { ColorIndex, IDisposable } from 'common/Types';
import { ITheme } from 'common/services/Services';
export const ICharSizeService = createDecorator<ICharSizeService>('CharSizeService');
export interface ICharSizeService {
@@ -73,7 +74,7 @@ export interface IRenderService extends IDisposable {
resize(cols: number, rows: number): void;
hasRenderer(): boolean;
setRenderer(renderer: IRenderer): void;
setColors(colors: IColorSet): void;
setColors(colors: ReadonlyColorSet): void;
handleDevicePixelRatioChange(): void;
handleResize(cols: number, rows: number): void;
handleCharSizeChanged(): void;
@@ -121,3 +122,17 @@ export interface ICharacterJoinerService {
deregister(joinerId: number): boolean;
getJoinedCharacters(row: number): [number, number][];
}
export const IThemeService = createDecorator<IThemeService>('ThemeService');
export interface IThemeService {
serviceBrand: undefined;
readonly colors: ReadonlyColorSet;
setTheme(theme?: ITheme): void;
restoreColor(slot?: ColorIndex): void;
/**
* Allows external modifying of colors in the theme, this is used instead of {@link colors} to
* prevent accidental writes.
*/
modifyColors(callback: (colors: IColorSet) => void): void;
}
+46
View File
@@ -0,0 +1,46 @@
/**
* Copyright (c) 2022 The xterm.js authors. All rights reserved.
* @license MIT
*/
import { ColorManager } from 'browser/ColorManager';
import { IThemeService } from 'browser/services/Services';
import { IColorSet, ReadonlyColorSet } from 'browser/Types';
import { Disposable } from 'common/Lifecycle';
import { IOptionsService, ITheme } from 'common/services/Services';
import { ColorIndex } from 'common/Types';
export class ThemeService extends Disposable implements IThemeService {
public serviceBrand: undefined;
// TODO: Merge color manager into theme service
private _colorManager = new ColorManager();
public get colors(): ReadonlyColorSet { return this._colorManager.colors; }
constructor(
@IOptionsService private readonly _optionsService: IOptionsService
) {
super();
this.register(this._optionsService.onOptionChange(key => {
if (key === 'theme') {
this.setTheme(this._optionsService.rawOptions.theme);
}
this._colorManager.handleOptionsChange(key, this._optionsService.rawOptions[key]);
}));
this._colorManager.setTheme(this._optionsService.rawOptions.theme);
}
public setTheme(theme: ITheme = {}): void {
this._colorManager.setTheme(theme);
}
public restoreColor(slot?: ColorIndex): void {
this._colorManager.restoreColor(slot);
}
public modifyColors(callback: (colors: IColorSet) => void): void {
callback(this._colorManager.colors);
// TODO: Fire event
}
}