Skip to content

Commit 685499d

Browse files
Automatically enable AdjustIndistinguishableColors if High Contrast mode enabled (#17346)
If High Contrast mode is enabled in the OS settings, we now automatically enable `adjustIndistinguishableColors`. To accomplish this, a new `Automatic` value is added to `adjustIndistinguishableColors`. When it's chosen, color nudging doesn't occur in regular contrast sessions, but we interpret the value as `Indexed` respectively. The new default value is `AutomaticIndexed`. Meaning that regular contrast sessions will see no difference in behavior. However, if they switch to high contrast mode, Windows Terminal will interpret the value as `Indexed` at runtime. This was chosen because `Always` is more performance intensive.    ## References and Relevant Issues #12999 ## Validation Steps Performed ✅ Toggling High Contrast mode immediately triggers an updated terminal instance with `adjustIndistinguishableColors`
1 parent 7a9fb76 commit 685499d

File tree

12 files changed

+50
-8
lines changed

12 files changed

+50
-8
lines changed

src/cascadia/TerminalControl/ControlCore.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -955,6 +955,11 @@ namespace winrt::Microsoft::Terminal::Control::implementation
955955
}
956956
}
957957

958+
void ControlCore::SetHighContrastMode(const bool enabled)
959+
{
960+
_terminal->SetHighContrastMode(enabled);
961+
}
962+
958963
Control::IControlSettings ControlCore::Settings()
959964
{
960965
return *_settings;

src/cascadia/TerminalControl/ControlCore.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ namespace winrt::Microsoft::Terminal::Control::implementation
9494

9595
void UpdateSettings(const Control::IControlSettings& settings, const IControlAppearance& newAppearance);
9696
void ApplyAppearance(const bool focused);
97+
void SetHighContrastMode(const bool enabled);
9798
Control::IControlSettings Settings();
9899
Control::IControlAppearance FocusedAppearance() const;
99100
Control::IControlAppearance UnfocusedAppearance() const;

src/cascadia/TerminalControl/ControlCore.idl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ namespace Microsoft.Terminal.Control
102102
IControlAppearance FocusedAppearance { get; };
103103
IControlAppearance UnfocusedAppearance { get; };
104104
Boolean HasUnfocusedAppearance();
105+
void SetHighContrastMode(Boolean enabled);
105106

106107
UInt64 SwapChainHandle { get; };
107108

src/cascadia/TerminalControl/TermControl.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,12 @@ namespace winrt::Microsoft::Terminal::Control::implementation
257257
return get_self<ControlCore>(_termControl->_core);
258258
}
259259

260+
static Windows::UI::ViewManagement::AccessibilitySettings& _GetAccessibilitySettings()
261+
{
262+
static Windows::UI::ViewManagement::AccessibilitySettings accessibilitySettings;
263+
return accessibilitySettings;
264+
}
265+
260266
TermControl::TermControl(IControlSettings settings,
261267
Control::IControlAppearance unfocusedAppearance,
262268
TerminalConnection::ITerminalConnection connection) :
@@ -276,6 +282,16 @@ namespace winrt::Microsoft::Terminal::Control::implementation
276282

277283
_core = _interactivity.Core();
278284

285+
// If high contrast mode was changed, update the appearance appropriately.
286+
_core.SetHighContrastMode(_GetAccessibilitySettings().HighContrast());
287+
_revokers.HighContrastChanged = _GetAccessibilitySettings().HighContrastChanged(winrt::auto_revoke, [weakThis{ get_weak() }](const Windows::UI::ViewManagement::AccessibilitySettings& a11ySettings, auto&&) {
288+
if (auto termControl = weakThis.get())
289+
{
290+
termControl->_core.SetHighContrastMode(a11ySettings.HighContrast());
291+
termControl->_core.ApplyAppearance(termControl->_focused);
292+
}
293+
});
294+
279295
// This event is specifically triggered by the renderer thread, a BG thread. Use a weak ref here.
280296
_revokers.RendererEnteredErrorState = _core.RendererEnteredErrorState(winrt::auto_revoke, { get_weak(), &TermControl::_RendererEnteredErrorState });
281297

src/cascadia/TerminalControl/TermControl.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,6 @@ namespace winrt::Microsoft::Terminal::Control::implementation
362362
};
363363
bool _InitializeTerminal(const InitializeReason reason);
364364
safe_void_coroutine _restoreInBackground();
365-
void _SetFontSize(int fontSize);
366365
void _TappedHandler(const Windows::Foundation::IInspectable& sender, const Windows::UI::Xaml::Input::TappedRoutedEventArgs& e);
367366
void _KeyDownHandler(const Windows::Foundation::IInspectable& sender, const Windows::UI::Xaml::Input::KeyRoutedEventArgs& e);
368367
void _KeyUpHandler(const Windows::Foundation::IInspectable& sender, const Windows::UI::Xaml::Input::KeyRoutedEventArgs& e);
@@ -394,8 +393,6 @@ namespace winrt::Microsoft::Terminal::Control::implementation
394393
void _SwapChainSizeChanged(const Windows::Foundation::IInspectable& sender, const Windows::UI::Xaml::SizeChangedEventArgs& e);
395394
void _SwapChainScaleChanged(const Windows::UI::Xaml::Controls::SwapChainPanel& sender, const Windows::Foundation::IInspectable& args);
396395

397-
void _TerminalTabColorChanged(const std::optional<til::color> color);
398-
399396
void _ScrollPositionChanged(const IInspectable& sender, const Control::ScrollPositionChangedArgs& args);
400397

401398
bool _CapturePointer(const Windows::Foundation::IInspectable& sender, const Windows::UI::Xaml::Input::PointerRoutedEventArgs& e);
@@ -480,6 +477,7 @@ namespace winrt::Microsoft::Terminal::Control::implementation
480477
// These are set up in _InitializeTerminal
481478
Control::ControlCore::RendererWarning_revoker RendererWarning;
482479
Control::ControlCore::SwapChainChanged_revoker SwapChainChanged;
480+
Windows::UI::ViewManagement::AccessibilitySettings::HighContrastChanged_revoker HighContrastChanged;
483481

484482
Control::ControlInteractivity::OpenHyperlink_revoker interactivityOpenHyperlink;
485483
Control::ControlInteractivity::ScrollPositionChanged_revoker interactivityScrollPositionChanged;

src/cascadia/TerminalCore/ICoreAppearance.idl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ namespace Microsoft.Terminal.Core
2323
{
2424
Never,
2525
Indexed,
26-
Always
26+
Always,
27+
Automatic
2728
};
2829

2930
// TerminalCore declares its own Color struct to avoid depending

src/cascadia/TerminalCore/Terminal.cpp

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,15 @@ void Terminal::UpdateAppearance(const ICoreAppearance& appearance)
143143
renderSettings.SetRenderMode(RenderSettings::Mode::IntenseIsBold, appearance.IntenseIsBold());
144144
renderSettings.SetRenderMode(RenderSettings::Mode::IntenseIsBright, appearance.IntenseIsBright());
145145

146-
switch (appearance.AdjustIndistinguishableColors())
146+
// If AIC is set to Automatic,
147+
// update the value based on if high contrast mode is enabled.
148+
AdjustTextMode deducedAIC = appearance.AdjustIndistinguishableColors();
149+
if (deducedAIC == AdjustTextMode::Automatic)
150+
{
151+
deducedAIC = _highContrastMode ? AdjustTextMode::Indexed : AdjustTextMode::Never;
152+
}
153+
154+
switch (deducedAIC)
147155
{
148156
case AdjustTextMode::Always:
149157
renderSettings.SetRenderMode(RenderSettings::Mode::IndexedDistinguishableColors, false);
@@ -213,6 +221,11 @@ void Terminal::UpdateAppearance(const ICoreAppearance& appearance)
213221
_NotifyScrollEvent();
214222
}
215223

224+
void Terminal::SetHighContrastMode(bool hc) noexcept
225+
{
226+
_highContrastMode = hc;
227+
}
228+
216229
void Terminal::SetCursorStyle(const DispatchTypes::CursorStyle cursorStyle)
217230
{
218231
auto& engine = reinterpret_cast<OutputStateMachineEngine&>(_stateMachine->Engine());

src/cascadia/TerminalCore/Terminal.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ class Microsoft::Terminal::Core::Terminal final :
9292

9393
void UpdateSettings(winrt::Microsoft::Terminal::Core::ICoreSettings settings);
9494
void UpdateAppearance(const winrt::Microsoft::Terminal::Core::ICoreAppearance& appearance);
95+
void SetHighContrastMode(bool hc) noexcept;
9596
void SetFontInfo(const FontInfo& fontInfo);
9697
void SetCursorStyle(const ::Microsoft::Console::VirtualTerminal::DispatchTypes::CursorStyle cursorStyle);
9798
void SetVtChecksumReportSupport(const bool enabled);
@@ -382,6 +383,7 @@ class Microsoft::Terminal::Core::Terminal final :
382383

383384
std::wstring _answerbackMessage;
384385
std::wstring _workingDirectory;
386+
bool _highContrastMode = false;
385387

386388
// This default fake font value is only used to check if the font is a raster font.
387389
// Otherwise, the font is changed to a real value with the renderer via TriggerFontChange.

src/cascadia/TerminalSettingsEditor/Resources/en-US/Resources.resw

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -946,6 +946,10 @@
946946
<value>Always</value>
947947
<comment>An option to choose from for the "adjust indistinguishable colors" setting. When selected, we will adjust the text colors for visibility.</comment>
948948
</data>
949+
<data name="Profile_AdjustIndistinguishableColorsAutomatic.Content" xml:space="preserve">
950+
<value>Automatic</value>
951+
<comment>An option to choose from for the "adjust indistinguishable colors" setting. When selected, we will adjust the text colors for visibility only when the colors are part of this profile's color scheme's color table if and only if high contrast mode is enabled.</comment>
952+
</data>
949953
<data name="Profile_CursorShapeBar.Content" xml:space="preserve">
950954
<value>Bar ( ┃ )</value>
951955
<comment>{Locked="┃"} An option to choose from for the "cursor shape" setting. When selected, the cursor will look like a vertical bar. The character in the parentheses is used to show what it looks like.</comment>

src/cascadia/TerminalSettingsModel/MTSMSettings.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ Author(s):
136136
X(ConvergedAlignment, BackgroundImageAlignment, "backgroundImageAlignment", ConvergedAlignment::Horizontal_Center | ConvergedAlignment::Vertical_Center) \
137137
X(hstring, BackgroundImagePath, "backgroundImage") \
138138
X(Model::IntenseStyle, IntenseTextStyle, "intenseTextStyle", Model::IntenseStyle::Bright) \
139-
X(Core::AdjustTextMode, AdjustIndistinguishableColors, "adjustIndistinguishableColors", Core::AdjustTextMode::Never) \
139+
X(Core::AdjustTextMode, AdjustIndistinguishableColors, "adjustIndistinguishableColors", Core::AdjustTextMode::Automatic) \
140140
X(bool, UseAcrylic, "useAcrylic", false)
141141

142142
// Intentionally omitted Appearance settings:

src/cascadia/TerminalSettingsModel/TerminalSettingsSerializationHelpers.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,11 @@ JSON_ENUM_MAPPER(::winrt::Microsoft::Terminal::Core::CursorStyle)
3535
// - Helper for converting a user-specified adjustTextMode value to its corresponding enum
3636
JSON_ENUM_MAPPER(::winrt::Microsoft::Terminal::Core::AdjustTextMode)
3737
{
38-
JSON_MAPPINGS(3) = {
38+
JSON_MAPPINGS(4) = {
3939
pair_type{ "never", ValueType::Never },
4040
pair_type{ "indexed", ValueType::Indexed },
4141
pair_type{ "always", ValueType::Always },
42+
pair_type{ "automatic", ValueType::Automatic },
4243
};
4344

4445
// Override mapping parser to add boolean parsing

src/cascadia/inc/ControlProperties.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
X(til::color, SelectionBackground, DEFAULT_FOREGROUND) \
1515
X(bool, IntenseIsBold) \
1616
X(bool, IntenseIsBright, true) \
17-
X(winrt::Microsoft::Terminal::Core::AdjustTextMode, AdjustIndistinguishableColors, winrt::Microsoft::Terminal::Core::AdjustTextMode::Never)
17+
X(winrt::Microsoft::Terminal::Core::AdjustTextMode, AdjustIndistinguishableColors, winrt::Microsoft::Terminal::Core::AdjustTextMode::Automatic)
1818

1919
// --------------------------- Control Appearance ---------------------------
2020
// All of these settings are defined in IControlAppearance.

0 commit comments

Comments
 (0)