diff --git a/src/cascadia/TerminalControl/TermControl.cpp b/src/cascadia/TerminalControl/TermControl.cpp index 32bd45f6b..afae49d9d 100644 --- a/src/cascadia/TerminalControl/TermControl.cpp +++ b/src/cascadia/TerminalControl/TermControl.cpp @@ -297,37 +297,7 @@ namespace winrt::Microsoft::Terminal::Control::implementation return; } - if (!newAppearance.BackgroundImage().empty()) - { - Windows::Foundation::Uri imageUri{ newAppearance.BackgroundImage() }; - - // Check if the image brush is already pointing to the image - // in the modified settings; if it isn't (or isn't there), - // set a new image source for the brush - auto imageSource = BackgroundImage().Source().try_as(); - - if (imageSource == nullptr || - imageSource.UriSource() == nullptr || - imageSource.UriSource().RawUri() != imageUri.RawUri()) - { - // Note that BitmapImage handles the image load asynchronously, - // which is especially important since the image - // may well be both large and somewhere out on the - // internet. - Media::Imaging::BitmapImage image(imageUri); - BackgroundImage().Source(image); - } - - // Apply stretch, opacity and alignment settings - BackgroundImage().Stretch(newAppearance.BackgroundImageStretchMode()); - BackgroundImage().Opacity(newAppearance.BackgroundImageOpacity()); - BackgroundImage().HorizontalAlignment(newAppearance.BackgroundImageHorizontalAlignment()); - BackgroundImage().VerticalAlignment(newAppearance.BackgroundImageVerticalAlignment()); - } - else - { - BackgroundImage().Source(nullptr); - } + _SetBackgroundImage(newAppearance); // Update our control settings const auto bg = newAppearance.DefaultBackground(); @@ -412,6 +382,57 @@ namespace winrt::Microsoft::Terminal::Control::implementation } } + // Method Description: + // - Sets background image and applies its settings (stretch, opacity and alignment) + // - Checks path validity + // Arguments: + // - newAppearance + // Return Value: + // - + void TermControl::_SetBackgroundImage(const IControlAppearance& newAppearance) + { + if (newAppearance.BackgroundImage().empty()) + { + BackgroundImage().Source(nullptr); + return; + } + + Windows::Foundation::Uri imageUri{ nullptr }; + try + { + imageUri = Windows::Foundation::Uri{ newAppearance.BackgroundImage() }; + } + catch (...) + { + LOG_CAUGHT_EXCEPTION(); + BackgroundImage().Source(nullptr); + return; + } + + // Check if the image brush is already pointing to the image + // in the modified settings; if it isn't (or isn't there), + // set a new image source for the brush + auto imageSource = BackgroundImage().Source().try_as(); + + if (imageSource == nullptr || + imageSource.UriSource() == nullptr || + imageSource.UriSource().RawUri() != imageUri.RawUri()) + { + // Note that BitmapImage handles the image load asynchronously, + // which is especially important since the image + // may well be both large and somewhere out on the + // internet. + Media::Imaging::BitmapImage image(imageUri); + BackgroundImage().Source(image); + } + + // Apply stretch, opacity and alignment settings + BackgroundImage().Stretch(newAppearance.BackgroundImageStretchMode()); + BackgroundImage().Opacity(newAppearance.BackgroundImageOpacity()); + BackgroundImage().HorizontalAlignment(newAppearance.BackgroundImageHorizontalAlignment()); + BackgroundImage().VerticalAlignment(newAppearance.BackgroundImageVerticalAlignment()); + } + // Method Description: // - Set up each layer's brush used to display the control's background. // - Respects the settings for acrylic, background image and opacity from diff --git a/src/cascadia/TerminalControl/TermControl.h b/src/cascadia/TerminalControl/TermControl.h index 9d0f22e33..8f758fb69 100644 --- a/src/cascadia/TerminalControl/TermControl.h +++ b/src/cascadia/TerminalControl/TermControl.h @@ -202,6 +202,7 @@ namespace winrt::Microsoft::Terminal::Control::implementation void _UpdateSettingsFromUIThread(IControlSettings newSettings); void _UpdateAppearanceFromUIThread(IControlAppearance newAppearance); void _ApplyUISettings(const IControlSettings&); + void _SetBackgroundImage(const IControlAppearance& newAppearance); void _InitializeBackgroundBrush(); void _BackgroundColorChangedHandler(const IInspectable& sender, const IInspectable& args);