Skip to content

Commit

Permalink
feat: support listen to the theme changed event (#646)
Browse files Browse the repository at this point in the history
  • Loading branch information
kiwiwong authored Feb 10, 2022
1 parent 1c776c7 commit 2590217
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 2 deletions.
4 changes: 4 additions & 0 deletions src/model/colorTheme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ export enum ColorThemeMode {
light = 'light',
}

export enum ColorThemeEvent {
onChange = 'colorTheme.onChange',
}

export interface IColorTheme {
/**
* The id of component, theme will be applied by this ID
Expand Down
11 changes: 11 additions & 0 deletions src/services/__tests__/colorThemeService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,4 +166,15 @@ describe('The Color Theme Service', () => {
});
expect(colorThemeService.getColorThemeMode()).toBe(ColorThemeMode.dark);
});

test('Listen to the theme changed event', () => {
const fn = jest.fn();
colorThemeService.onChange(fn);
colorThemeService.setTheme(BuiltInColorTheme.id);

expect(fn).toBeCalledTimes(1);
expect(colorThemeService.getColorTheme()!.id).toEqual(
BuiltInColorTheme.id
);
});
});
44 changes: 42 additions & 2 deletions src/services/theme/colorThemeService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,20 @@
*/

import 'reflect-metadata';
import { IColorTheme, ColorThemeMode, ColorScheme } from 'mo/model/colorTheme';
import {
IColorTheme,
ColorThemeMode,
ColorScheme,
ColorThemeEvent,
} from 'mo/model/colorTheme';
import { singleton } from 'tsyringe';
import { editor as monacoEditor } from 'mo/monaco';
import { applyStyleSheetRules } from 'mo/common/css';
import { getThemeData, convertToCSSVars } from './helper';
import logger from 'mo/common/logger';
import { prefixClaName } from 'mo/common/className';
import { searchById, colorLightOrDark } from 'mo/common/utils';
import { GlobalEvent } from 'mo/common/event';

export interface IColorThemeService {
/**
Expand Down Expand Up @@ -57,6 +63,17 @@ export interface IColorThemeService {
* Get the mode('dark' or 'light') of the current Color Theme
*/
getColorThemeMode(): ColorThemeMode;
/**
* Listen to the theme changed event
* @param callback
*/
onChange(
callback: (
prev: IColorTheme,
next: IColorTheme,
themeMode: ColorThemeMode
) => void
): void;
}

/**
Expand All @@ -74,11 +91,15 @@ export const BuiltInColorTheme: IColorTheme = {
export const DEFAULT_THEME_CLASS_NAME = prefixClaName('customize-theme');

@singleton()
export class ColorThemeService implements IColorThemeService {
export class ColorThemeService
extends GlobalEvent
implements IColorThemeService
{
private colorThemes: IColorTheme[] = [BuiltInColorTheme];
private colorTheme: IColorTheme = BuiltInColorTheme;

constructor() {
super();
if (this.colorTheme) {
this.setTheme(this.colorTheme.id);
}
Expand Down Expand Up @@ -130,6 +151,7 @@ export class ColorThemeService implements IColorThemeService {
}

public setTheme(id: string) {
const prevTheme = this.getColorTheme();
const theme = this.getThemeById(id);
if (theme) {
this.colorTheme = { ...theme };
Expand All @@ -140,6 +162,14 @@ export class ColorThemeService implements IColorThemeService {
// Update monaco-editor theme
monacoEditor.defineTheme(DEFAULT_THEME_CLASS_NAME, themeData);
monacoEditor.setTheme(DEFAULT_THEME_CLASS_NAME);

const themeMode = this.getColorThemeMode();
this.emit(
ColorThemeEvent.onChange,
prevTheme,
{ ...this.colorTheme },
themeMode
);
} else {
logger.error(`Can't get the theme by id:` + id);
}
Expand Down Expand Up @@ -180,4 +210,14 @@ export class ColorThemeService implements IColorThemeService {
// Default dark
return ColorThemeMode.dark;
}

public onChange(
callback: (
prev: IColorTheme,
next: IColorTheme,
themeMode: ColorThemeMode
) => void
): void {
this.subscribe(ColorThemeEvent.onChange, callback);
}
}

0 comments on commit 2590217

Please sign in to comment.