From f63159db59d7cf297e48467dd1997d84b24d97b0 Mon Sep 17 00:00:00 2001 From: Sergey <45919738+serd2011@users.noreply.github.com> Date: Mon, 25 Oct 2021 14:17:18 +0300 Subject: [PATCH] Adds exception handling of uri creation in profile background image update (#11542) ## Summary of the Pull Request Moves baskgroung image update releated code into separate function and adds uri path construction exeption handling. ## References ## PR Checklist * [x] Closes #11361 * [x] CLA signed. If not, go over [here](https://cla.opensource.microsoft.com/microsoft/Terminal) and sign the CLA * [ ] Tests added/passed * [ ] Documentation updated. If checked, please file a pull request on [our docs repo](https://github.com/MicrosoftDocs/terminal) and link it here: #xxx * [ ] Schema updated. * [ ] I've discussed this with core contributors already. If not checked, I'm ready to accept this work might be rejected in favor of a different grand plan. Issue number where discussion took place: #xxx ## Detailed Description of the Pull Request / Additional comments ## Validation Steps Performed Tried to put garbage as a path. Terminal didn't crashed. --- src/cascadia/TerminalControl/TermControl.cpp | 83 ++++++++++++-------- src/cascadia/TerminalControl/TermControl.h | 1 + 2 files changed, 53 insertions(+), 31 deletions(-) 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);