From 3fec83b84a0983e35e88c5c390301c19dc77e766 Mon Sep 17 00:00:00 2001 From: Leonardo Montini Date: Thu, 16 Jun 2022 16:29:55 +0200 Subject: [PATCH] Add command to toggle between light/dark color themes (#151554) * Added quick input action to toggle between preferred color themes * Removed quickpick in favor of command to toggle between ligh/dark and high-contrast counterparts * Moved toggle logic to themes.contribution * togglePreferredTheme -> toggleLightDarkThemes Co-authored-by: Leonardo Montini Co-authored-by: Martin Aeschlimann --- .../themes/browser/themes.contribution.ts | 49 ++++++++++++++++++- 1 file changed, 47 insertions(+), 2 deletions(-) diff --git a/src/vs/workbench/contrib/themes/browser/themes.contribution.ts b/src/vs/workbench/contrib/themes/browser/themes.contribution.ts index a42283c1457..4e5f6525fb0 100644 --- a/src/vs/workbench/contrib/themes/browser/themes.contribution.ts +++ b/src/vs/workbench/contrib/themes/browser/themes.contribution.ts @@ -8,7 +8,7 @@ import { KeyMod, KeyChord, KeyCode } from 'vs/base/common/keyCodes'; import { MenuRegistry, MenuId, Action2, registerAction2 } from 'vs/platform/actions/common/actions'; import { Registry } from 'vs/platform/registry/common/platform'; import { CATEGORIES } from 'vs/workbench/common/actions'; -import { IWorkbenchThemeService, IWorkbenchTheme, ThemeSettingTarget, IWorkbenchColorTheme, IWorkbenchFileIconTheme, IWorkbenchProductIconTheme } from 'vs/workbench/services/themes/common/workbenchThemeService'; +import { IWorkbenchThemeService, IWorkbenchTheme, ThemeSettingTarget, IWorkbenchColorTheme, IWorkbenchFileIconTheme, IWorkbenchProductIconTheme, ThemeSettings } from 'vs/workbench/services/themes/common/workbenchThemeService'; import { VIEWLET_ID, IExtensionsViewPaneContainer } from 'vs/workbench/contrib/extensions/common/extensions'; import { IExtensionGalleryService, IExtensionManagementService, IGalleryExtension } from 'vs/platform/extensionManagement/common/extensionManagement'; import { IColorRegistry, Extensions as ColorRegistryExtensions } from 'vs/platform/theme/common/colorRegistry'; @@ -34,6 +34,7 @@ import { IInstantiationService, ServicesAccessor } from 'vs/platform/instantiati import { KeybindingWeight } from 'vs/platform/keybinding/common/keybindingsRegistry'; import { CommandsRegistry } from 'vs/platform/commands/common/commands'; import { FileIconThemeData } from 'vs/workbench/services/themes/browser/fileIconThemeData'; +import { IConfigurationService } from 'vs/platform/configuration/common/configuration'; export const manageExtensionIcon = registerIcon('theme-selection-manage-extension', Codicon.gear, localize('manageExtensionIcon', 'Icon for the \'Manage\' action in the theme selection quick pick.')); @@ -583,6 +584,51 @@ registerAction2(class extends Action2 { } }); +const toggleLightDarkThemesCommandId = 'workbench.action.toggleLightDarkThemes'; + +registerAction2(class extends Action2 { + + constructor() { + super({ + id: toggleLightDarkThemesCommandId, + title: { value: localize('toggleLightDarkThemes.label', "Toggle between Light/Dark Themes"), original: 'Toggle between Light/Dark Themes' }, + category: CATEGORIES.Preferences, + f1: true, + }); + } + + override async run(accessor: ServicesAccessor) { + const themeService = accessor.get(IWorkbenchThemeService); + const configurationService = accessor.get(IConfigurationService); + + const currentTheme = themeService.getColorTheme(); + let newSettingsId: string = ThemeSettings.PREFERRED_DARK_THEME; + switch (currentTheme.type) { + case ColorScheme.LIGHT: + newSettingsId = ThemeSettings.PREFERRED_DARK_THEME; + break; + case ColorScheme.DARK: + newSettingsId = ThemeSettings.PREFERRED_LIGHT_THEME; + break; + case ColorScheme.HIGH_CONTRAST_LIGHT: + newSettingsId = ThemeSettings.PREFERRED_HC_DARK_THEME; + break; + case ColorScheme.HIGH_CONTRAST_DARK: + newSettingsId = ThemeSettings.PREFERRED_HC_LIGHT_THEME; + break; + } + + const themeSettingId: string = configurationService.getValue(newSettingsId); + + if (themeSettingId && typeof themeSettingId === 'string') { + const theme = (await themeService.getColorThemes()).find(t => t.settingsId === themeSettingId); + if (theme) { + themeService.setColorTheme(theme.id, 'auto'); + } + } + } +}); + MenuRegistry.appendMenuItem(MenuId.MenubarPreferencesMenu, { group: '4_themes', command: { @@ -610,7 +656,6 @@ MenuRegistry.appendMenuItem(MenuId.MenubarPreferencesMenu, { order: 3 }); - MenuRegistry.appendMenuItem(MenuId.GlobalActivity, { group: '4_themes', command: {