Skip to content

Commit

Permalink
serialize hidden shells
Browse files Browse the repository at this point in the history
  • Loading branch information
carlos-zamora committed Oct 27, 2020
1 parent b7e7ca6 commit 17e5fb4
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -783,7 +783,6 @@ namespace SettingsModelLocalTests
settings->_profiles.Append(*profile4);
settings->_profiles.Append(*profile5);

settings->_ValidateProfilesHaveGuid();
VERIFY_IS_TRUE(settings->_profiles.GetAt(0).HasGuid());
VERIFY_IS_TRUE(settings->_profiles.GetAt(1).HasGuid());
VERIFY_IS_TRUE(settings->_profiles.GetAt(2).HasGuid());
Expand Down Expand Up @@ -836,7 +835,6 @@ namespace SettingsModelLocalTests

auto settings = winrt::make_self<implementation::CascadiaSettings>();
settings->_profiles.Append(*profile1);
settings->_ValidateProfilesHaveGuid();

VERIFY_IS_TRUE(settings->_profiles.GetAt(0).HasGuid());

Expand Down
4 changes: 2 additions & 2 deletions src/cascadia/LocalTests_SettingsModel/SerializationTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -231,8 +231,8 @@ namespace SettingsModelLocalTests
"\"name\": \"NiksaShell\""
"},"
"{"
"\"name\": \"BhojwaniShell\","
"\"source\": \"local\""
"\"hidden\": true,"
"\"name\": \"BhojwaniShell\""
"}"
"]"
"},"
Expand Down
27 changes: 18 additions & 9 deletions src/cascadia/TerminalSettingsModel/CascadiaSettings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ CascadiaSettings::CascadiaSettings() :
CascadiaSettings::CascadiaSettings(const bool addDynamicProfiles) :
_globals{ winrt::make_self<implementation::GlobalAppSettings>() },
_profiles{ winrt::single_threaded_observable_vector<Model::Profile>() },
_hiddenProfiles{ winrt::single_threaded_observable_vector<Model::Profile>() },
_warnings{ winrt::single_threaded_vector<SettingsLoadWarnings>() },
_deserializationErrorMessage{ L"" }
{
Expand Down Expand Up @@ -81,7 +82,14 @@ winrt::Microsoft::Terminal::Settings::Model::CascadiaSettings CascadiaSettings::
settings->_userSettings = _userSettings;
settings->_defaultSettings = _defaultSettings;

_CopyProfileInheritanceTree(settings);
// profile.defaults must be saved to CascadiaSettings
// So let's copy it over manually first
if (_userDefaultProfileSettings)
{
settings->_userDefaultProfileSettings = Profile::CopySettings(_userDefaultProfileSettings);
}
settings->_profiles = _CopyProfileInheritanceTree(_profiles.GetView(), settings->_userDefaultProfileSettings);
settings->_hiddenProfiles = _CopyProfileInheritanceTree(_hiddenProfiles.GetView(), settings->_userDefaultProfileSettings);

return *settings;
}
Expand All @@ -92,14 +100,14 @@ winrt::Microsoft::Terminal::Settings::Model::CascadiaSettings CascadiaSettings::
// - cloneSettings: the CascadiaSettings we're copying the inheritance tree to
// Return Value:
// - <none>
void CascadiaSettings::_CopyProfileInheritanceTree(winrt::com_ptr<CascadiaSettings>& cloneSettings) const
IObservableVector<winrt::Microsoft::Terminal::Settings::Model::Profile> CascadiaSettings::_CopyProfileInheritanceTree(IVectorView<Model::Profile> profiles, winrt::com_ptr<Profile> profileDefaultsClone) const
{
// Our profiles inheritance graph doesn't have a formal root.
// However, if we create a dummy Profile, and set _profiles as the parent,
// we now have a root. So we'll do just that, then copy the inheritance graph
// from the dummyRoot.
auto dummyRootSource{ winrt::make_self<Profile>() };
for (const auto& profile : _profiles)
for (const auto& profile : profiles)
{
winrt::com_ptr<Profile> profileImpl;
profileImpl.copy_from(winrt::get_self<Profile>(profile));
Expand All @@ -109,23 +117,23 @@ void CascadiaSettings::_CopyProfileInheritanceTree(winrt::com_ptr<CascadiaSettin
auto dummyRootClone{ winrt::make_self<Profile>() };
std::unordered_map<void*, winrt::com_ptr<Profile>> visited{};

if (_userDefaultProfileSettings)
if (_userDefaultProfileSettings && profileDefaultsClone)
{
// profile.defaults must be saved to CascadiaSettings
// So let's do that manually first, and add that to visited
cloneSettings->_userDefaultProfileSettings = Profile::CopySettings(_userDefaultProfileSettings);
visited[_userDefaultProfileSettings.get()] = cloneSettings->_userDefaultProfileSettings;
// add profile.defaults to visited
visited[_userDefaultProfileSettings.get()] = profileDefaultsClone;
}

Profile::CloneInheritanceGraph(dummyRootSource, dummyRootClone, visited);

// All of the parents of the dummy root clone are _profiles.
// Get the parents and add them to the settings clone.
IObservableVector<Model::Profile> result{ winrt::single_threaded_observable_vector<Model::Profile>() };
const auto cloneParents{ dummyRootClone->Parents() };
for (const auto& profile : cloneParents)
{
cloneSettings->_profiles.Append(*profile);
result.Append(*profile);
}
return result;
}

// Method Description:
Expand Down Expand Up @@ -441,6 +449,7 @@ void CascadiaSettings::_RemoveHiddenProfiles()
if (_profiles.GetAt(i).Hidden())
{
// remove hidden profile, don't increment 'i'
_hiddenProfiles.Append(std::move(_profiles.GetAt(i)));
_profiles.RemoveAt(i);
}
else
Expand Down
3 changes: 2 additions & 1 deletion src/cascadia/TerminalSettingsModel/CascadiaSettings.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ namespace winrt::Microsoft::Terminal::Settings::Model::implementation
private:
com_ptr<GlobalAppSettings> _globals;
Windows::Foundation::Collections::IObservableVector<Model::Profile> _profiles;
Windows::Foundation::Collections::IObservableVector<Model::Profile> _hiddenProfiles;
Windows::Foundation::Collections::IVector<Model::SettingsLoadWarnings> _warnings;
Windows::Foundation::IReference<SettingsLoadErrors> _loadError;
hstring _deserializationErrorMessage;
Expand All @@ -118,7 +119,7 @@ namespace winrt::Microsoft::Terminal::Settings::Model::implementation
bool _PrependSchemaDirective();
bool _AppendDynamicProfilesToUserSettings();
std::string _ApplyFirstRunChangesToSettingsTemplate(std::string_view settingsTemplate) const;
void _CopyProfileInheritanceTree(com_ptr<CascadiaSettings>& cloneSettings) const;
Windows::Foundation::Collections::IObservableVector<Model::Profile> _CopyProfileInheritanceTree(Windows::Foundation::Collections::IVectorView<Model::Profile> profiles, winrt::com_ptr<Profile> profileDefaultsClone) const;

void _ApplyDefaultsFromUserSettings();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1063,6 +1063,11 @@ Json::Value CascadiaSettings::ToJson() const
const auto prof{ winrt::get_self<implementation::Profile>(entry) };
profilesList.append(prof->ToJson());
}
for (const auto& entry : _hiddenProfiles)
{
const auto prof{ winrt::get_self<implementation::Profile>(entry) };
profilesList.append(prof->ToJson());
}
profiles[JsonKey(ProfilesListKey)] = profilesList;
json[JsonKey(ProfilesKey)] = profiles;

Expand Down

0 comments on commit 17e5fb4

Please sign in to comment.