From 596b6e68a95b13c1809e16db7f89b0491f48f804 Mon Sep 17 00:00:00 2001 From: Alessio Occhipinti Date: Thu, 10 Sep 2020 21:38:44 +0200 Subject: [PATCH] feat: wrap accent settings inside variant name (#504) --- src/commands/set-accent.ts | 27 ++++++++++++++++++++------- src/core/extension-manager.ts | 13 +++++++++++-- 2 files changed, 31 insertions(+), 9 deletions(-) diff --git a/src/commands/set-accent.ts b/src/commands/set-accent.ts index d56d94e..680e518 100644 --- a/src/commands/set-accent.ts +++ b/src/commands/set-accent.ts @@ -38,17 +38,30 @@ const quickPick = async (): Promise => { }; export const command = async (): Promise => { + const pkg = extensionManager.getPackageJSON(); + const currentThemeID = workspace.getConfiguration().get('workbench.colorTheme'); + const isMaterialTheme = Boolean(pkg.contributes.themes.find(theme => theme.label === currentThemeID)); + + if (!isMaterialTheme) { + return; + } + const themeConfig = extensionManager.getConfig(); const currentColorCustomizationsConfig: any = workspace.getConfiguration().get('workbench.colorCustomizations'); const accent = await quickPick(); + let config = {}; - const config = accent === PURGE_KEY ? { - ...currentColorCustomizationsConfig, - ...getThemeColorCustomizationsConfig() - } : { - ...currentColorCustomizationsConfig, - ...getThemeColorCustomizationsConfig(themeConfig.accents[accent]) - }; + if (accent === PURGE_KEY) { + const {[currentThemeID]: _, ...rest} = currentColorCustomizationsConfig; + config = rest; + } else { + config = { + ...currentColorCustomizationsConfig, + [`[${currentThemeID}]`]: { + ...getThemeColorCustomizationsConfig(themeConfig.accents[accent]) + } + }; + } await updateColorCustomizationsConfig(config); await settingsManager.updateSetting('accent', accent); diff --git a/src/core/extension-manager.ts b/src/core/extension-manager.ts index 0b2a9bd..2db6a65 100644 --- a/src/core/extension-manager.ts +++ b/src/core/extension-manager.ts @@ -8,6 +8,15 @@ type MaterialThemeConfig = { changelog?: { lastversion?: string }; }; +type PackageJSON = { + version: string; + contributes: { + themes: Array<{ + label: string; + }>; + }; +}; + type InstallationType = { firstInstall: boolean; update: boolean; @@ -15,7 +24,7 @@ type InstallationType = { export interface IExtensionManager { init: () => Promise; - getPackageJSON: () => Record; + getPackageJSON: () => PackageJSON; getConfig: () => MaterialThemeConfig; getInstallationType: () => Record; updateConfig: (config: Partial) => Promise; @@ -33,7 +42,7 @@ class ExtensionManager implements IExtensionManager { this.userConfigFileUri = extensionFolderUri.with({path: posix.join(extensionFolderUri.path, USER_CONFIG_FILE_NAME)}); } - getPackageJSON(): Record { + getPackageJSON(): PackageJSON { return extensions.getExtension(MATERIAL_THEME_EXT_ID).packageJSON; }