4
4
*/
5
5
6
6
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' ;
8
13
import { singleton } from 'tsyringe' ;
9
14
import { editor as monacoEditor } from 'mo/monaco' ;
10
15
import { applyStyleSheetRules } from 'mo/common/css' ;
11
16
import { getThemeData , convertToCSSVars } from './helper' ;
12
17
import logger from 'mo/common/logger' ;
13
18
import { prefixClaName } from 'mo/common/className' ;
14
19
import { searchById , colorLightOrDark } from 'mo/common/utils' ;
20
+ import { GlobalEvent } from 'mo/common/event' ;
15
21
16
22
export interface IColorThemeService {
17
23
/**
@@ -57,6 +63,17 @@ export interface IColorThemeService {
57
63
* Get the mode('dark' or 'light') of the current Color Theme
58
64
*/
59
65
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 ;
60
77
}
61
78
62
79
/**
@@ -74,11 +91,15 @@ export const BuiltInColorTheme: IColorTheme = {
74
91
export const DEFAULT_THEME_CLASS_NAME = prefixClaName ( 'customize-theme' ) ;
75
92
76
93
@singleton ( )
77
- export class ColorThemeService implements IColorThemeService {
94
+ export class ColorThemeService
95
+ extends GlobalEvent
96
+ implements IColorThemeService
97
+ {
78
98
private colorThemes : IColorTheme [ ] = [ BuiltInColorTheme ] ;
79
99
private colorTheme : IColorTheme = BuiltInColorTheme ;
80
100
81
101
constructor ( ) {
102
+ super ( ) ;
82
103
if ( this . colorTheme ) {
83
104
this . setTheme ( this . colorTheme . id ) ;
84
105
}
@@ -130,6 +151,7 @@ export class ColorThemeService implements IColorThemeService {
130
151
}
131
152
132
153
public setTheme ( id : string ) {
154
+ const prevTheme = this . getColorTheme ( ) ;
133
155
const theme = this . getThemeById ( id ) ;
134
156
if ( theme ) {
135
157
this . colorTheme = { ...theme } ;
@@ -140,6 +162,14 @@ export class ColorThemeService implements IColorThemeService {
140
162
// Update monaco-editor theme
141
163
monacoEditor . defineTheme ( DEFAULT_THEME_CLASS_NAME , themeData ) ;
142
164
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
+ ) ;
143
173
} else {
144
174
logger . error ( `Can't get the theme by id:` + id ) ;
145
175
}
@@ -180,4 +210,14 @@ export class ColorThemeService implements IColorThemeService {
180
210
// Default dark
181
211
return ColorThemeMode . dark ;
182
212
}
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
+ }
183
223
}
0 commit comments