Skip to content

Commit 2590217

Browse files
authored
feat: support listen to the theme changed event (#646)
1 parent 1c776c7 commit 2590217

File tree

3 files changed

+57
-2
lines changed

3 files changed

+57
-2
lines changed

src/model/colorTheme.ts

+4
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ export enum ColorThemeMode {
2121
light = 'light',
2222
}
2323

24+
export enum ColorThemeEvent {
25+
onChange = 'colorTheme.onChange',
26+
}
27+
2428
export interface IColorTheme {
2529
/**
2630
* The id of component, theme will be applied by this ID

src/services/__tests__/colorThemeService.test.ts

+11
Original file line numberDiff line numberDiff line change
@@ -166,4 +166,15 @@ describe('The Color Theme Service', () => {
166166
});
167167
expect(colorThemeService.getColorThemeMode()).toBe(ColorThemeMode.dark);
168168
});
169+
170+
test('Listen to the theme changed event', () => {
171+
const fn = jest.fn();
172+
colorThemeService.onChange(fn);
173+
colorThemeService.setTheme(BuiltInColorTheme.id);
174+
175+
expect(fn).toBeCalledTimes(1);
176+
expect(colorThemeService.getColorTheme()!.id).toEqual(
177+
BuiltInColorTheme.id
178+
);
179+
});
169180
});

src/services/theme/colorThemeService.ts

+42-2
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,20 @@
44
*/
55

66
import 'reflect-metadata';
7-
import { IColorTheme, ColorThemeMode, ColorScheme } from 'mo/model/colorTheme';
7+
import {
8+
IColorTheme,
9+
ColorThemeMode,
10+
ColorScheme,
11+
ColorThemeEvent,
12+
} from 'mo/model/colorTheme';
813
import { singleton } from 'tsyringe';
914
import { editor as monacoEditor } from 'mo/monaco';
1015
import { applyStyleSheetRules } from 'mo/common/css';
1116
import { getThemeData, convertToCSSVars } from './helper';
1217
import logger from 'mo/common/logger';
1318
import { prefixClaName } from 'mo/common/className';
1419
import { searchById, colorLightOrDark } from 'mo/common/utils';
20+
import { GlobalEvent } from 'mo/common/event';
1521

1622
export interface IColorThemeService {
1723
/**
@@ -57,6 +63,17 @@ export interface IColorThemeService {
5763
* Get the mode('dark' or 'light') of the current Color Theme
5864
*/
5965
getColorThemeMode(): ColorThemeMode;
66+
/**
67+
* Listen to the theme changed event
68+
* @param callback
69+
*/
70+
onChange(
71+
callback: (
72+
prev: IColorTheme,
73+
next: IColorTheme,
74+
themeMode: ColorThemeMode
75+
) => void
76+
): void;
6077
}
6178

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

7693
@singleton()
77-
export class ColorThemeService implements IColorThemeService {
94+
export class ColorThemeService
95+
extends GlobalEvent
96+
implements IColorThemeService
97+
{
7898
private colorThemes: IColorTheme[] = [BuiltInColorTheme];
7999
private colorTheme: IColorTheme = BuiltInColorTheme;
80100

81101
constructor() {
102+
super();
82103
if (this.colorTheme) {
83104
this.setTheme(this.colorTheme.id);
84105
}
@@ -130,6 +151,7 @@ export class ColorThemeService implements IColorThemeService {
130151
}
131152

132153
public setTheme(id: string) {
154+
const prevTheme = this.getColorTheme();
133155
const theme = this.getThemeById(id);
134156
if (theme) {
135157
this.colorTheme = { ...theme };
@@ -140,6 +162,14 @@ export class ColorThemeService implements IColorThemeService {
140162
// Update monaco-editor theme
141163
monacoEditor.defineTheme(DEFAULT_THEME_CLASS_NAME, themeData);
142164
monacoEditor.setTheme(DEFAULT_THEME_CLASS_NAME);
165+
166+
const themeMode = this.getColorThemeMode();
167+
this.emit(
168+
ColorThemeEvent.onChange,
169+
prevTheme,
170+
{ ...this.colorTheme },
171+
themeMode
172+
);
143173
} else {
144174
logger.error(`Can't get the theme by id:` + id);
145175
}
@@ -180,4 +210,14 @@ export class ColorThemeService implements IColorThemeService {
180210
// Default dark
181211
return ColorThemeMode.dark;
182212
}
213+
214+
public onChange(
215+
callback: (
216+
prev: IColorTheme,
217+
next: IColorTheme,
218+
themeMode: ColorThemeMode
219+
) => void
220+
): void {
221+
this.subscribe(ColorThemeEvent.onChange, callback);
222+
}
183223
}

0 commit comments

Comments
 (0)