diff --git a/src/cascadia/TerminalApp/TerminalPage.cpp b/src/cascadia/TerminalApp/TerminalPage.cpp index a3eb5d1e7e9..8fc8b329a35 100644 --- a/src/cascadia/TerminalApp/TerminalPage.cpp +++ b/src/cascadia/TerminalApp/TerminalPage.cpp @@ -3786,6 +3786,8 @@ namespace winrt::TerminalApp::implementation TermControl TerminalPage::_SetupControl(const TermControl& term) { + term.WindowActivationChanged(_activated); + // GH#12515: ConPTY assumes it's hidden at the start. If we're not, let it know now. if (_visible) { @@ -5164,9 +5166,7 @@ namespace winrt::TerminalApp::implementation const auto acrylicBrush = Media::AcrylicBrush(); // Make sure that we use the same backdrop source as TermControl. // If we don't, it'll cause the same flickering as in GH #14384 - acrylicBrush.BackgroundSource(_currentWindowSettings().EnableUnfocusedAcrylic() ? - Media::AcrylicBackgroundSource::Backdrop : - Media::AcrylicBackgroundSource::HostBackdrop); + acrylicBrush.BackgroundSource(Media::AcrylicBackgroundSource::Backdrop); acrylicBrush.FallbackColor(bgColor); acrylicBrush.TintColor(bgColor); acrylicBrush.TintOpacity(0.5); @@ -5393,9 +5393,22 @@ namespace winrt::TerminalApp::implementation void TerminalPage::WindowActivated(const bool activated) { - // Stash if we're activated. Use that when we reload - // the settings, change active panes, etc. - _activated = activated; + if (_activated != activated) + { + _activated = activated; + for (const auto& tab : _tabs) + { + if (const auto rootPane = _GetTabImpl(tab)->GetRootPane()) + { + rootPane->WalkTree([activated](const auto& pane) { + if (const auto control = pane->GetTerminalControl()) + { + control.WindowActivationChanged(activated); + } + }); + } + } + } _updateThemeColors(); _adjustProcessPriorityThrottled->Run(); diff --git a/src/cascadia/TerminalControl/TermControl.cpp b/src/cascadia/TerminalControl/TermControl.cpp index 99becd8a614..5efebed3d3a 100644 --- a/src/cascadia/TerminalControl/TermControl.cpp +++ b/src/cascadia/TerminalControl/TermControl.cpp @@ -1103,20 +1103,20 @@ namespace winrt::Microsoft::Terminal::Control::implementation // changed from what was in the original settings. if (_core->UseAcrylic() && !transparentBg) { + const auto backdropStyle = _windowActivated.has_value() || settings.EnableUnfocusedAcrylic() ? + Media::AcrylicBackgroundSource::Backdrop : + Media::AcrylicBackgroundSource::HostBackdrop; + // See if we've already got an acrylic background brush // to avoid the flicker when setting up a new one auto acrylic = RootGrid().Background().try_as(); - // Instantiate a brush if there's not already one there - if (acrylic == nullptr) + if (!acrylic || acrylic.BackgroundSource() != backdropStyle) { acrylic = Media::AcrylicBrush{}; + acrylic.BackgroundSource(backdropStyle); } - const auto backdropStyle = - _core->Settings().EnableUnfocusedAcrylic() ? Media::AcrylicBackgroundSource::Backdrop : Media::AcrylicBackgroundSource::HostBackdrop; - acrylic.BackgroundSource(backdropStyle); - // see GH#1082: Initialize background color so we don't get a // fade/flash when _BackgroundColorChanged is called acrylic.FallbackColor(bgColor); @@ -1124,6 +1124,7 @@ namespace winrt::Microsoft::Terminal::Control::implementation // Apply brush settings acrylic.TintOpacity(_core->Opacity()); + acrylic.AlwaysUseFallback(_ShouldUseAcrylicFallback()); // Apply brush to control if it's not already there if (RootGrid().Background() != acrylic) @@ -1143,6 +1144,11 @@ namespace winrt::Microsoft::Terminal::Control::implementation BackgroundBrush(RootGrid().Background()); } + bool TermControl::_ShouldUseAcrylicFallback() + { + return !_windowActivated.value_or(true) && !_core->Settings().EnableUnfocusedAcrylic(); + } + // Method Description: // - Handler for the core's BackgroundColorChanged event. Updates the color // of our background brush to match. @@ -2877,6 +2883,29 @@ namespace winrt::Microsoft::Terminal::Control::implementation _core->WindowVisibilityChanged(showOrHide); } + void TermControl::WindowActivationChanged(const bool activated) + { + if (_IsClosing() || _windowActivated == activated) + { + return; + } + + // HostBackdrop can get the activation state wrong in XAML Islands. + // Use the top-level window's activation instead (GH#20944). + _windowActivated = activated; + if (const auto acrylic = RootGrid().Background().try_as()) + { + if (acrylic.BackgroundSource() == Media::AcrylicBackgroundSource::HostBackdrop) + { + _InitializeBackgroundBrush(); + } + else + { + acrylic.AlwaysUseFallback(_ShouldUseAcrylicFallback()); + } + } + } + // Method Description: // - Get the modifier keys that are currently pressed. This can be used to // find out which modifiers (ctrl, alt, shift) are pressed in events that diff --git a/src/cascadia/TerminalControl/TermControl.h b/src/cascadia/TerminalControl/TermControl.h index 71af8d691a1..8300ecfaa7e 100644 --- a/src/cascadia/TerminalControl/TermControl.h +++ b/src/cascadia/TerminalControl/TermControl.h @@ -88,6 +88,7 @@ namespace winrt::Microsoft::Terminal::Control::implementation double QuickFixButtonCollapsedWidth(); void WindowVisibilityChanged(const bool showOrHide); + void WindowActivationChanged(const bool activated); void ColorSelection(Control::SelectionColor fg, Control::SelectionColor bg, Core::MatchMode matchMode); @@ -290,6 +291,7 @@ namespace winrt::Microsoft::Terminal::Control::implementation bool _closing{ false }; bool _focused{ false }; + std::optional _windowActivated; bool _initializedTerminal{ false }; bool _quickFixButtonCollapsible{ false }; bool _quickFixesAvailable{ false }; @@ -354,6 +356,7 @@ namespace winrt::Microsoft::Terminal::Control::implementation void _SetBackgroundImage(const IControlAppearance& newAppearance); void _InitializeBackgroundBrush(); + bool _ShouldUseAcrylicFallback(); safe_void_coroutine _coreBackgroundColorChanged(const IInspectable& sender, const IInspectable& args); void _changeBackgroundColor(til::color bg); static bool _isColorLight(til::color bg) noexcept; diff --git a/src/cascadia/TerminalControl/TermControl.idl b/src/cascadia/TerminalControl/TermControl.idl index 4c51342c3a1..63f64f41ecc 100644 --- a/src/cascadia/TerminalControl/TermControl.idl +++ b/src/cascadia/TerminalControl/TermControl.idl @@ -135,6 +135,7 @@ namespace Microsoft.Terminal.Control Single SnapDimensionToGrid(Boolean widthOrHeight, Single dimension); void WindowVisibilityChanged(Boolean showOrHide); + void WindowActivationChanged(Boolean activated); void ScrollViewport(Int32 viewTop); diff --git a/src/cascadia/TerminalSettingsAppAdapterLib/TerminalSettings.cpp b/src/cascadia/TerminalSettingsAppAdapterLib/TerminalSettings.cpp index 3f2b7af4689..98744833384 100644 --- a/src/cascadia/TerminalSettingsAppAdapterLib/TerminalSettings.cpp +++ b/src/cascadia/TerminalSettingsAppAdapterLib/TerminalSettings.cpp @@ -66,6 +66,9 @@ namespace winrt::Microsoft::Terminal::Settings { const auto settings = _CreateWithProfileCommon(appSettings, windowSettings, profile); settings->_UseBackgroundImageForWindow = false; + // Preview controls aren't driven by WindowActivationChanged, so keep them + // on Backdrop instead of HostBackdrop (GH#20944). + settings->_EnableUnfocusedAcrylic = true; return settings; } @@ -75,8 +78,7 @@ namespace winrt::Microsoft::Terminal::Settings // has no unfocused appearance, this behaves like CreateForPreview (the focused appearance). winrt::com_ptr TerminalSettings::CreateForPreviewUnfocused(const Model::CascadiaSettings& appSettings, const Model::WindowSettings& windowSettings, const Model::Profile& profile) { - const auto settings = _CreateWithProfileCommon(appSettings, windowSettings, profile); - settings->_UseBackgroundImageForWindow = false; + const auto settings = CreateForPreview(appSettings, windowSettings, profile); if (const auto& unfocusedAppearance{ profile.UnfocusedAppearance() }) { const auto globals = appSettings.GlobalSettings();