Skip to content

Commit

Permalink
Auto detect background image (#7849)
Browse files Browse the repository at this point in the history
##  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: MicrosoftDocs/terminal#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": "<some random img path>"] 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.
  • Loading branch information
bennettnicholas authored Oct 15, 2020
1 parent 8bdae31 commit 6e8388e
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 2 deletions.
2 changes: 2 additions & 0 deletions .github/actions/spell-check/dictionary/apis.txt
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ RSHIFT
rx
serializer
SIZENS
GETDESKWALLPAPER
UPDATEINIFILE
spsc
STDCPP
syscall
Expand Down
23 changes: 23 additions & 0 deletions src/cascadia/LocalTests_SettingsModel/DeserializationTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ namespace SettingsModelLocalTests
TEST_METHOD(TestHelperFunctions);

TEST_METHOD(TestProfileBackgroundImageWithEnvVar);
TEST_METHOD(TestProfileBackgroundImageWithDesktopWallpaper);

TEST_METHOD(TestCloseOnExitParsing);
TEST_METHOD(TestCloseOnExitCompatibilityShim);
Expand Down Expand Up @@ -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<implementation::CascadiaSettings>();
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"(
Expand Down
27 changes: 25 additions & 2 deletions src/cascadia/TerminalSettingsModel/Profile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
}
Expand Down Expand Up @@ -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<std::wstring>(_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<std::wstring>(_BackgroundImagePath.c_str()) };
}
}

winrt::hstring Profile::EvaluatedStartingDirectory() const
Expand Down

0 comments on commit 6e8388e

Please sign in to comment.