Skip to content

Commit

Permalink
fix: theme fallback logic (#220)
Browse files Browse the repository at this point in the history
* fix: add ExtensionDidContributes event for themes (#214)

* fix: remove theme fallback logic (#188)
  • Loading branch information
Aaaaash authored Dec 27, 2021
1 parent e4c34f1 commit 56d9874
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 29 deletions.
2 changes: 2 additions & 0 deletions packages/core-common/src/types/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,5 @@ export interface IExtensionActivateEventPayload {
}

export class ExtensionActivateEvent extends BasicEvent<IExtensionActivateEventPayload> {}

export class ExtensionDidContributes extends BasicEvent<void> {}
3 changes: 2 additions & 1 deletion packages/extension/src/browser/extension.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { IWorkspaceService } from '@opensumi/ide-workspace';
import { IDialogService, IMessageService } from '@opensumi/ide-overlay';
import { IProgressService } from '@opensumi/ide-core-browser/lib/progress';
import { IExtensionStorageService } from '@opensumi/ide-extension-storage';
import { localize, OnEvent, WithEventBus, ProgressLocation } from '@opensumi/ide-core-common';
import { localize, OnEvent, WithEventBus, ProgressLocation, ExtensionDidContributes } from '@opensumi/ide-core-common';
import { FileSearchServicePath, IFileSearchService } from '@opensumi/ide-file-search/lib/common';
import {
AppConfig,
Expand Down Expand Up @@ -474,6 +474,7 @@ export class ExtensionServiceImpl extends WithEventBus implements ExtensionServi
await this.activationEventService.fireEvent('onCommand', command);
return args;
});
this.eventBus.fire(new ExtensionDidContributes());
}

/**
Expand Down
12 changes: 0 additions & 12 deletions packages/theme/src/browser/theme.contribution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,18 +59,6 @@ export class ThemeContribution implements MenuContribution, CommandContribution,
this.registerDefaultTokenStyles();
this.registerDefaultTokenType();
this.registerDefaultTokenModifier();

this.initializeDefaultTheme();
}

private initializeDefaultTheme() {
const globalProvider = this.preferenceService.getProvider(PreferenceScope.User);
if (globalProvider) {
const themeId = globalProvider.get<string>(COLOR_THEME_SETTING);
if (!themeId || themeId === DEFAULT_THEME_ID) {
this.themeService.applyTheme(DEFAULT_THEME_ID);
}
}
}

private registerDefaultTokenModifier() {
Expand Down
43 changes: 27 additions & 16 deletions packages/theme/src/browser/workbench.theme.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ import {
isLinux,
isWindows,
IThemeColor,
OnEvent,
ExtensionDidContributes,
} from '@opensumi/ide-core-common';
import { Autowired, Injectable } from '@opensumi/di';
import { getColorRegistry } from '../common/color-registry';
Expand Down Expand Up @@ -99,18 +101,42 @@ export class WorkbenchThemeService extends WithEventBus implements IThemeService
this.applyPlatformClass();
}

@OnEvent(ExtensionDidContributes)
onDidExtensionContributes() {
const themeMap = this.getAvailableThemeInfos().reduce((pre: Map<string, string>, cur: ThemeInfo) => {
if (!pre.has(cur.themeId)) {
pre.set(cur.themeId, cur.name);
}
return pre;
}, new Map());

const themeId = this.preferenceService.get<string>(COLOR_THEME_SETTING);
if (themeId && themeId !== DEFAULT_THEME_ID && themeMap.has(themeId)) {
this.applyTheme(themeId);
} else {
this.applyTheme(DEFAULT_THEME_ID);
}

this.preferenceSettings.setEnumLabels(COLOR_THEME_SETTING, Object.fromEntries(themeMap.entries()));
}

public registerThemes(themeContributions: ThemeContribution[], extPath: URI) {
const disposables = new DisposableCollection();
disposables.push({
dispose: () => this.doSetPreferenceSchema(),
});
const preferenceThemeId = this.preferenceService.get<string>(COLOR_THEME_SETTING);

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

this.themeContributionRegistry.set(themeId, themeExtContribution);

if (preferenceThemeId === themeId) {
this.applyTheme(preferenceThemeId);
}

disposables.push({
dispose: () => {
this.themeContributionRegistry.delete(themeId);
Expand Down Expand Up @@ -149,6 +175,7 @@ export class WorkbenchThemeService extends WithEventBus implements IThemeService
const currentThemeType = this.currentTheme.type;

this.toggleBaseThemeClass(prevThemeType, currentThemeType);

this.doApplyTheme(this.currentTheme);
}

Expand Down Expand Up @@ -252,22 +279,6 @@ export class WorkbenchThemeService extends WithEventBus implements IThemeService
},
true,
);

const themeMap = this.getAvailableThemeInfos().reduce((pre: Map<string, string>, cur: ThemeInfo) => {
if (!pre.has(cur.themeId)) {
pre.set(cur.themeId, cur.name);
}
return pre;
}, new Map());

const themeId = this.preferenceService.get<string>(COLOR_THEME_SETTING);
if (themeId && themeId !== DEFAULT_THEME_ID && themeMap.has(themeId)) {
this.applyTheme(themeId);
} else {
this.applyTheme(DEFAULT_THEME_ID);
}

this.preferenceSettings.setEnumLabels(COLOR_THEME_SETTING, Object.fromEntries(themeMap.entries()));
}

private get colorCustomizations(): IColorCustomizations {
Expand Down

0 comments on commit 56d9874

Please sign in to comment.