From 6e8388e6835468283cd64956aba3dbf5bb4332cf Mon Sep 17 00:00:00 2001 From: Nicholas Bennett <46642544+bennettnicholas@users.noreply.github.com> Date: Thu, 15 Oct 2020 10:09:20 -0600 Subject: [PATCH] Auto detect background image (#7849) ## Summary of the Pull Request Added watch on desktopImagePath to check when the path equals "DesktopWallpaper" If it does equal "DesktopWallpaper" it replaces the path with a path to the desktop's wallpaper *I am a student and this is my first pull request for Terminal so please give feedback no matter how small. It's the best way I can learn. ## PR Checklist * [X] Closes #7295 * [X] CLA signed. If not, go over [here](https://cla.opensource.microsoft.com/microsoft/Terminal) and sign the CLA * [?] Tests added/passed * [X] Documentation updated. If checked, please file a pull request on [our docs repo](https://github.com/MicrosoftDocs/terminal) and link it here: https://github.com/MicrosoftDocs/terminal/pull/155 * [?] Schema updated. (Not sure if this is needed, also not sure where this would be) * [X] 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: #7295 (Have only talked with the people on the issue, which I don't think has any core contributors) ## Detailed Description of the Pull Request / Additional comments I am using SystemParametersInfo for SPI_GETDESKWALLPAPER which puts the path into a WCHAR and that is then inserted as the BackgroundImagePath. I do not think an additional test would add value. The SPI_GETDESKTOPWALLPAPER uses the computers local wallpaper path and puts it into a WCHAR, which then I feed into BackgroundImagePath() as it's new path. I don't think there adds value in making a static path of the desktop background and testing that, given that static tests are already done for "BackgroundImage()". ## Validation Steps Performed (Manual Validation - Test False Value) 1. Ran Terminal 2. Set setting ["backgroundImage": ""] under profiles->defaults 3. Verified terminal's background is not the desktops wallpaper. (Manual Validation - Test True Value) 1. Ran Terminal 2. Set setting ["backgroundImage": "DesktopWallpaper"] under profiles->defaults 3. Verified the background image matches the desktop background image. (Manual Validation - Multiple Tabs True Value) 1. Ran Terminal 2. Set setting ["backgroundImage": "DesktopWallpaper"] under profiles->defaults 3. Verified the background image matches the desktop background image. 4. Opened new tabs 5. Verified the background image matches the desktop background image for each tab. --- .../actions/spell-check/dictionary/apis.txt | 2 ++ .../DeserializationTests.cpp | 23 ++++++++++++++++ .../TerminalSettingsModel/Profile.cpp | 27 +++++++++++++++++-- 3 files changed, 50 insertions(+), 2 deletions(-) diff --git a/.github/actions/spell-check/dictionary/apis.txt b/.github/actions/spell-check/dictionary/apis.txt index fcfd3c730a5..caaf8debf00 100644 --- a/.github/actions/spell-check/dictionary/apis.txt +++ b/.github/actions/spell-check/dictionary/apis.txt @@ -45,6 +45,8 @@ RSHIFT rx serializer SIZENS +GETDESKWALLPAPER +UPDATEINIFILE spsc STDCPP syscall diff --git a/src/cascadia/LocalTests_SettingsModel/DeserializationTests.cpp b/src/cascadia/LocalTests_SettingsModel/DeserializationTests.cpp index 283646a6f02..4cbbef06869 100644 --- a/src/cascadia/LocalTests_SettingsModel/DeserializationTests.cpp +++ b/src/cascadia/LocalTests_SettingsModel/DeserializationTests.cpp @@ -55,6 +55,7 @@ namespace SettingsModelLocalTests TEST_METHOD(TestHelperFunctions); TEST_METHOD(TestProfileBackgroundImageWithEnvVar); + TEST_METHOD(TestProfileBackgroundImageWithDesktopWallpaper); TEST_METHOD(TestCloseOnExitParsing); TEST_METHOD(TestCloseOnExitCompatibilityShim); @@ -1400,6 +1401,28 @@ namespace SettingsModelLocalTests VERIFY_ARE_NOT_EQUAL(0u, settings->_profiles.Size()); VERIFY_ARE_EQUAL(expectedPath, settings->_profiles.GetAt(0).ExpandedBackgroundImagePath()); } + void DeserializationTests::TestProfileBackgroundImageWithDesktopWallpaper() + { + const winrt::hstring expectedBackgroundImagePath{ winrt::to_hstring("DesktopWallpaper") }; + + const std::string settingsJson{ R"( + { + "profiles": [ + { + "name": "profile0", + "backgroundImage": "DesktopWallpaper" + } + ] + })" }; + + VerifyParseSucceeded(settingsJson); + + auto settings = winrt::make_self(); + settings->_ParseJsonString(settingsJson, false); + settings->LayerJson(settings->_userSettings); + VERIFY_ARE_EQUAL(expectedBackgroundImagePath, settings->_profiles.GetAt(0).BackgroundImagePath()); + VERIFY_ARE_NOT_EQUAL(expectedBackgroundImagePath, settings->_profiles.GetAt(0).ExpandedBackgroundImagePath()); + } void DeserializationTests::TestCloseOnExitParsing() { const std::string settingsJson{ R"( diff --git a/src/cascadia/TerminalSettingsModel/Profile.cpp b/src/cascadia/TerminalSettingsModel/Profile.cpp index d3419d34255..bf3fdcf5ef2 100644 --- a/src/cascadia/TerminalSettingsModel/Profile.cpp +++ b/src/cascadia/TerminalSettingsModel/Profile.cpp @@ -58,6 +58,8 @@ static constexpr std::string_view RetroTerminalEffectKey{ "experimental.retroTer static constexpr std::string_view AntialiasingModeKey{ "antialiasingMode" }; static constexpr std::string_view TabColorKey{ "tabColor" }; +static const winrt::hstring DesktopWallpaperEnum{ L"DesktopWallpaper" }; + Profile::Profile() { } @@ -244,17 +246,38 @@ void Profile::LayerJson(const Json::Value& json) } // Method Description: +// - Either Returns this profile's background image path, if one is set, expanding // - Returns this profile's background image path, if one is set, expanding // any environment variables in the path, if there are any. +// - Or if "DesktopWallpaper" is set, then gets the path to the desktops wallpaper. // Return Value: -// - This profile's expanded background image path / the empty string. +// - This profile's expanded background image path / desktops's wallpaper path /the empty string. winrt::hstring Profile::ExpandedBackgroundImagePath() const { if (_BackgroundImagePath.empty()) { return _BackgroundImagePath; } - return winrt::hstring{ wil::ExpandEnvironmentStringsW(_BackgroundImagePath.c_str()) }; + // checks if the user would like to copy their desktop wallpaper + // if so, replaces the path with the desktop wallpaper's path + else if (_BackgroundImagePath == to_hstring(DesktopWallpaperEnum)) + { + WCHAR desktopWallpaper[MAX_PATH]; + + // "The returned string will not exceed MAX_PATH characters" as of 2020 + if (SystemParametersInfo(SPI_GETDESKWALLPAPER, MAX_PATH, desktopWallpaper, SPIF_UPDATEINIFILE)) + { + return winrt::hstring{ (desktopWallpaper) }; + } + else + { + return winrt::hstring{ L"" }; + } + } + else + { + return winrt::hstring{ wil::ExpandEnvironmentStringsW(_BackgroundImagePath.c_str()) }; + } } winrt::hstring Profile::EvaluatedStartingDirectory() const