Skip to content

Commit 56d9874

Browse files
authored
fix: theme fallback logic (#220)
* fix: add ExtensionDidContributes event for themes (#214) * fix: remove theme fallback logic (#188)
1 parent e4c34f1 commit 56d9874

File tree

4 files changed

+31
-29
lines changed

4 files changed

+31
-29
lines changed

packages/core-common/src/types/extension.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,3 +83,5 @@ export interface IExtensionActivateEventPayload {
8383
}
8484

8585
export class ExtensionActivateEvent extends BasicEvent<IExtensionActivateEventPayload> {}
86+
87+
export class ExtensionDidContributes extends BasicEvent<void> {}

packages/extension/src/browser/extension.service.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { IWorkspaceService } from '@opensumi/ide-workspace';
33
import { IDialogService, IMessageService } from '@opensumi/ide-overlay';
44
import { IProgressService } from '@opensumi/ide-core-browser/lib/progress';
55
import { IExtensionStorageService } from '@opensumi/ide-extension-storage';
6-
import { localize, OnEvent, WithEventBus, ProgressLocation } from '@opensumi/ide-core-common';
6+
import { localize, OnEvent, WithEventBus, ProgressLocation, ExtensionDidContributes } from '@opensumi/ide-core-common';
77
import { FileSearchServicePath, IFileSearchService } from '@opensumi/ide-file-search/lib/common';
88
import {
99
AppConfig,
@@ -474,6 +474,7 @@ export class ExtensionServiceImpl extends WithEventBus implements ExtensionServi
474474
await this.activationEventService.fireEvent('onCommand', command);
475475
return args;
476476
});
477+
this.eventBus.fire(new ExtensionDidContributes());
477478
}
478479

479480
/**

packages/theme/src/browser/theme.contribution.ts

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -59,18 +59,6 @@ export class ThemeContribution implements MenuContribution, CommandContribution,
5959
this.registerDefaultTokenStyles();
6060
this.registerDefaultTokenType();
6161
this.registerDefaultTokenModifier();
62-
63-
this.initializeDefaultTheme();
64-
}
65-
66-
private initializeDefaultTheme() {
67-
const globalProvider = this.preferenceService.getProvider(PreferenceScope.User);
68-
if (globalProvider) {
69-
const themeId = globalProvider.get<string>(COLOR_THEME_SETTING);
70-
if (!themeId || themeId === DEFAULT_THEME_ID) {
71-
this.themeService.applyTheme(DEFAULT_THEME_ID);
72-
}
73-
}
7462
}
7563

7664
private registerDefaultTokenModifier() {

packages/theme/src/browser/workbench.theme.service.ts

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ import {
2929
isLinux,
3030
isWindows,
3131
IThemeColor,
32+
OnEvent,
33+
ExtensionDidContributes,
3234
} from '@opensumi/ide-core-common';
3335
import { Autowired, Injectable } from '@opensumi/di';
3436
import { getColorRegistry } from '../common/color-registry';
@@ -99,18 +101,42 @@ export class WorkbenchThemeService extends WithEventBus implements IThemeService
99101
this.applyPlatformClass();
100102
}
101103

104+
@OnEvent(ExtensionDidContributes)
105+
onDidExtensionContributes() {
106+
const themeMap = this.getAvailableThemeInfos().reduce((pre: Map<string, string>, cur: ThemeInfo) => {
107+
if (!pre.has(cur.themeId)) {
108+
pre.set(cur.themeId, cur.name);
109+
}
110+
return pre;
111+
}, new Map());
112+
113+
const themeId = this.preferenceService.get<string>(COLOR_THEME_SETTING);
114+
if (themeId && themeId !== DEFAULT_THEME_ID && themeMap.has(themeId)) {
115+
this.applyTheme(themeId);
116+
} else {
117+
this.applyTheme(DEFAULT_THEME_ID);
118+
}
119+
120+
this.preferenceSettings.setEnumLabels(COLOR_THEME_SETTING, Object.fromEntries(themeMap.entries()));
121+
}
122+
102123
public registerThemes(themeContributions: ThemeContribution[], extPath: URI) {
103124
const disposables = new DisposableCollection();
104125
disposables.push({
105126
dispose: () => this.doSetPreferenceSchema(),
106127
});
128+
const preferenceThemeId = this.preferenceService.get<string>(COLOR_THEME_SETTING);
107129

108130
themeContributions.forEach((contribution) => {
109131
const themeExtContribution = { basePath: extPath, contribution };
110132
const themeId = getThemeId(contribution);
111133

112134
this.themeContributionRegistry.set(themeId, themeExtContribution);
113135

136+
if (preferenceThemeId === themeId) {
137+
this.applyTheme(preferenceThemeId);
138+
}
139+
114140
disposables.push({
115141
dispose: () => {
116142
this.themeContributionRegistry.delete(themeId);
@@ -149,6 +175,7 @@ export class WorkbenchThemeService extends WithEventBus implements IThemeService
149175
const currentThemeType = this.currentTheme.type;
150176

151177
this.toggleBaseThemeClass(prevThemeType, currentThemeType);
178+
152179
this.doApplyTheme(this.currentTheme);
153180
}
154181

@@ -252,22 +279,6 @@ export class WorkbenchThemeService extends WithEventBus implements IThemeService
252279
},
253280
true,
254281
);
255-
256-
const themeMap = this.getAvailableThemeInfos().reduce((pre: Map<string, string>, cur: ThemeInfo) => {
257-
if (!pre.has(cur.themeId)) {
258-
pre.set(cur.themeId, cur.name);
259-
}
260-
return pre;
261-
}, new Map());
262-
263-
const themeId = this.preferenceService.get<string>(COLOR_THEME_SETTING);
264-
if (themeId && themeId !== DEFAULT_THEME_ID && themeMap.has(themeId)) {
265-
this.applyTheme(themeId);
266-
} else {
267-
this.applyTheme(DEFAULT_THEME_ID);
268-
}
269-
270-
this.preferenceSettings.setEnumLabels(COLOR_THEME_SETTING, Object.fromEntries(themeMap.entries()));
271282
}
272283

273284
private get colorCustomizations(): IColorCustomizations {

0 commit comments

Comments
 (0)