From baef0ae22b66f7a29bf4a9b102901ac1f3279bc9 Mon Sep 17 00:00:00 2001 From: Martin Aeschlimann Date: Tue, 8 Sep 2020 21:04:00 +0200 Subject: [PATCH] high contrast switching in browser --- .../electron-sandbox/desktop.contribution.ts | 9 +------- .../browser/browserHostColorSchemeService.ts | 7 ++++++- .../themes/browser/workbenchThemeService.ts | 1 + .../themes/common/themeConfiguration.ts | 21 +++++++++++++++++-- 4 files changed, 27 insertions(+), 11 deletions(-) diff --git a/src/vs/workbench/electron-sandbox/desktop.contribution.ts b/src/vs/workbench/electron-sandbox/desktop.contribution.ts index 07fe262c796..693ea6bcae0 100644 --- a/src/vs/workbench/electron-sandbox/desktop.contribution.ts +++ b/src/vs/workbench/electron-sandbox/desktop.contribution.ts @@ -9,7 +9,7 @@ import { SyncActionDescriptor, MenuRegistry, MenuId } from 'vs/platform/actions/ import { IConfigurationRegistry, Extensions as ConfigurationExtensions, ConfigurationScope } from 'vs/platform/configuration/common/configurationRegistry'; import { IWorkbenchActionRegistry, Extensions } from 'vs/workbench/common/actions'; import { KeyMod, KeyCode } from 'vs/base/common/keyCodes'; -import { isWindows, isLinux, isMacintosh } from 'vs/base/common/platform'; +import { isLinux, isMacintosh } from 'vs/base/common/platform'; import { ToggleDevToolsAction, ConfigureRuntimeArgumentsAction } from 'vs/workbench/electron-sandbox/actions/developerActions'; import { ZoomResetAction, ZoomOutAction, ZoomInAction, CloseCurrentWindowAction, SwitchWindow, QuickSwitchWindow, ReloadWindowWithExtensionsDisabledAction, NewWindowTabHandler, ShowPreviousWindowTabHandler, ShowNextWindowTabHandler, MoveWindowTabToNewWindowHandler, MergeWindowTabsHandlerHandler, ToggleWindowTabsBarHandler } from 'vs/workbench/electron-sandbox/actions/windowActions'; import { ContextKeyExpr } from 'vs/platform/contextkey/common/contextkey'; @@ -255,13 +255,6 @@ import { IJSONSchema } from 'vs/base/common/jsonSchema'; 'default': false, 'description': nls.localize('closeWhenEmpty', "Controls whether closing the last editor should also close the window. This setting only applies for windows that do not show folders.") }, - 'window.autoDetectHighContrast': { - 'type': 'boolean', - 'default': true, - 'description': nls.localize('autoDetectHighContrast', "If enabled, will automatically change to high contrast theme if the OS is using a high contrast theme, and to dark theme when switching away from a high contrast theme."), - 'scope': ConfigurationScope.APPLICATION, - 'included': isWindows || isMacintosh - }, 'window.doubleClickIconToClose': { 'type': 'boolean', 'default': false, diff --git a/src/vs/workbench/services/themes/browser/browserHostColorSchemeService.ts b/src/vs/workbench/services/themes/browser/browserHostColorSchemeService.ts index 2f16e857fc7..086043eb041 100644 --- a/src/vs/workbench/services/themes/browser/browserHostColorSchemeService.ts +++ b/src/vs/workbench/services/themes/browser/browserHostColorSchemeService.ts @@ -29,6 +29,9 @@ export class BrowserHostColorSchemeService extends Disposable implements IHostCo window.matchMedia('(prefers-color-scheme: dark)').addListener(() => { this._onDidSchemeChangeEvent.fire(); }); + window.matchMedia('(forced-colors: active)').addListener(() => { + this._onDidSchemeChangeEvent.fire(); + }); } get onDidChangeColorScheme(): Event { @@ -36,7 +39,9 @@ export class BrowserHostColorSchemeService extends Disposable implements IHostCo } get colorScheme(): ColorScheme { - if (window.matchMedia(`(prefers-color-scheme: light)`).matches) { + if (window.matchMedia(`(forced-colors: active)`).matches) { + return ColorScheme.HIGH_CONTRAST; + } else if (window.matchMedia(`(prefers-color-scheme: light)`).matches) { return ColorScheme.LIGHT; } else if (window.matchMedia(`(prefers-color-scheme: dark)`).matches) { return ColorScheme.DARK; diff --git a/src/vs/workbench/services/themes/browser/workbenchThemeService.ts b/src/vs/workbench/services/themes/browser/workbenchThemeService.ts index 7aa072ca36e..39c18b0c7d3 100644 --- a/src/vs/workbench/services/themes/browser/workbenchThemeService.ts +++ b/src/vs/workbench/services/themes/browser/workbenchThemeService.ts @@ -438,6 +438,7 @@ export class WorkbenchThemeService implements IWorkbenchThemeService { } } }; + ruleCollector.addRule(`.monaco-workbench { forced-color-adjust: none; }`); themingRegistry.getThemingParticipants().forEach(p => p(themeData, ruleCollector, this.environmentService)); _applyRules([...cssRules].join('\n'), colorThemeRulesClassName); } diff --git a/src/vs/workbench/services/themes/common/themeConfiguration.ts b/src/vs/workbench/services/themes/common/themeConfiguration.ts index cd4ad6203f5..795b03edf96 100644 --- a/src/vs/workbench/services/themes/common/themeConfiguration.ts +++ b/src/vs/workbench/services/themes/common/themeConfiguration.ts @@ -6,7 +6,7 @@ import * as nls from 'vs/nls'; import * as types from 'vs/base/common/types'; import { Registry } from 'vs/platform/registry/common/platform'; -import { IConfigurationRegistry, Extensions as ConfigurationExtensions, IConfigurationPropertySchema, IConfigurationNode } from 'vs/platform/configuration/common/configurationRegistry'; +import { IConfigurationRegistry, Extensions as ConfigurationExtensions, IConfigurationPropertySchema, IConfigurationNode, ConfigurationScope } from 'vs/platform/configuration/common/configurationRegistry'; import { IJSONSchema } from 'vs/base/common/jsonSchema'; import { textmateColorsSchemaId, textmateColorGroupSchemaId } from 'vs/workbench/services/themes/common/colorThemeSchema'; @@ -96,6 +96,13 @@ const productIconThemeSettingSchema: IConfigurationPropertySchema = { errorMessage: nls.localize('productIconThemeError', "Product icon theme is unknown or not installed.") }; +const detectHCSchemeSettingSchema: IConfigurationPropertySchema = { + type: 'boolean', + default: true, + description: nls.localize('autoDetectHighContrast', "If enabled, will automatically change to high contrast theme if the OS is using a high contrast theme."), + scope: ConfigurationScope.APPLICATION +}; + const themeSettingsConfiguration: IConfigurationNode = { id: 'workbench', order: 7.1, @@ -105,7 +112,6 @@ const themeSettingsConfiguration: IConfigurationNode = { [ThemeSettings.PREFERRED_DARK_THEME]: preferredDarkThemeSettingSchema, [ThemeSettings.PREFERRED_LIGHT_THEME]: preferredLightThemeSettingSchema, [ThemeSettings.PREFERRED_HC_THEME]: preferredHCThemeSettingSchema, - [ThemeSettings.DETECT_COLOR_SCHEME]: detectColorSchemeSettingSchema, [ThemeSettings.FILE_ICON_THEME]: fileIconThemeSettingSchema, [ThemeSettings.COLOR_CUSTOMIZATIONS]: colorCustomizationsSchema, [ThemeSettings.PRODUCT_ICON_THEME]: productIconThemeSettingSchema @@ -113,6 +119,17 @@ const themeSettingsConfiguration: IConfigurationNode = { }; configurationRegistry.registerConfiguration(themeSettingsConfiguration); +const themeSettingsWindowConfiguration: IConfigurationNode = { + id: 'window', + order: 8.1, + type: 'object', + properties: { + [ThemeSettings.DETECT_HC]: detectHCSchemeSettingSchema, + [ThemeSettings.DETECT_COLOR_SCHEME]: detectColorSchemeSettingSchema, + } +}; +configurationRegistry.registerConfiguration(themeSettingsWindowConfiguration); + function tokenGroupSettings(description: string): IJSONSchema { return { description, -- GitLab