[FancyZones] Refactor/improve saving JSON files (#8947)

* Divide SaveFancyZonesData into two functions

* Removed unused function, continued refactoring

* Use SaveAppZoneHistory in some places

* Revert "Use SaveAppZoneHistory in some places"

This reverts commit 74a4a1e467.

* Only save files if the contents are about to be changed

* Restore FileWatcher
This commit is contained in:
Ivan Stošić
2021-01-05 14:00:39 +01:00
committed by GitHub
parent d4a4203f95
commit 485d278b8c
5 changed files with 34 additions and 23 deletions

View File

@@ -53,9 +53,7 @@ public:
PostMessageW(m_window, WM_PRIV_LOCATIONCHANGE, NULL, NULL); PostMessageW(m_window, WM_PRIV_LOCATIONCHANGE, NULL, NULL);
}), }),
m_fileWatcher(FancyZonesDataInstance().GetZonesSettingsFileName(), [this]() { m_fileWatcher(FancyZonesDataInstance().GetZonesSettingsFileName(), [this]() {
// This is commented out until we figure out why calling UpdateZoneSets() in response to this message PostMessageW(m_window, WM_PRIV_FILE_UPDATE, NULL, NULL);
// causes Win+Arrow to break.
//PostMessageW(m_window, WM_PRIV_FILE_UPDATE, NULL, NULL);
}) })
{ {
m_settings->SetCallback(this); m_settings->SetCallback(this);

View File

@@ -553,11 +553,20 @@ void FancyZonesData::LoadFancyZonesData()
void FancyZonesData::SaveFancyZonesData() const void FancyZonesData::SaveFancyZonesData() const
{ {
std::scoped_lock lock{ dataLock }; std::scoped_lock lock{ dataLock };
JSONHelpers::SaveFancyZonesData(zonesSettingsFileName, JSONHelpers::SaveZoneSettings(zonesSettingsFileName, deviceInfoMap, customZoneSetsMap);
appZoneHistoryFileName, JSONHelpers::SaveAppZoneHistory(appZoneHistoryFileName, appZoneHistoryMap);
deviceInfoMap, }
customZoneSetsMap,
appZoneHistoryMap); void FancyZonesData::SaveZoneSettings() const
{
std::scoped_lock lock{ dataLock };
JSONHelpers::SaveZoneSettings(zonesSettingsFileName, deviceInfoMap, customZoneSetsMap);
}
void FancyZonesData::SaveAppZoneHistory() const
{
std::scoped_lock lock{ dataLock };
JSONHelpers::SaveAppZoneHistory(appZoneHistoryFileName, appZoneHistoryMap);
} }
void FancyZonesData::RemoveDesktopAppZoneHistory(const std::wstring& desktopId) void FancyZonesData::RemoveDesktopAppZoneHistory(const std::wstring& desktopId)

View File

@@ -85,6 +85,8 @@ public:
void LoadFancyZonesData(); void LoadFancyZonesData();
void SaveFancyZonesData() const; void SaveFancyZonesData() const;
void SaveZoneSettings() const;
void SaveAppZoneHistory() const;
private: private:
#if defined(UNIT_TESTS) #if defined(UNIT_TESTS)

View File

@@ -532,17 +532,10 @@ namespace JSONHelpers
} }
} }
void SaveFancyZonesData(const std::wstring& zonesSettingsFileName, void SaveZoneSettings(const std::wstring& zonesSettingsFileName, const TDeviceInfoMap& deviceInfoMap, const TCustomZoneSetsMap& customZoneSetsMap)
const std::wstring& appZoneHistoryFileName,
const TDeviceInfoMap& deviceInfoMap,
const TCustomZoneSetsMap& customZoneSetsMap,
const TAppZoneHistoryMap& appZoneHistoryMap)
{ {
json::JsonObject root{}; json::JsonObject root{};
json::JsonObject appZoneHistoryRoot{};
appZoneHistoryRoot.SetNamedValue(NonLocalizable::AppZoneHistoryStr, JSONHelpers::SerializeAppZoneHistory(appZoneHistoryMap));
root.SetNamedValue(NonLocalizable::DevicesStr, JSONHelpers::SerializeDeviceInfos(deviceInfoMap)); root.SetNamedValue(NonLocalizable::DevicesStr, JSONHelpers::SerializeDeviceInfos(deviceInfoMap));
root.SetNamedValue(NonLocalizable::CustomZoneSetsStr, JSONHelpers::SerializeCustomZoneSets(customZoneSetsMap)); root.SetNamedValue(NonLocalizable::CustomZoneSetsStr, JSONHelpers::SerializeCustomZoneSets(customZoneSetsMap));
@@ -550,10 +543,21 @@ namespace JSONHelpers
if (!before.has_value() || before.value().Stringify() != root.Stringify()) if (!before.has_value() || before.value().Stringify() != root.Stringify())
{ {
Trace::FancyZones::DataChanged(); Trace::FancyZones::DataChanged();
json::to_file(zonesSettingsFileName, root);
} }
}
json::to_file(zonesSettingsFileName, root); void SaveAppZoneHistory(const std::wstring& appZoneHistoryFileName, const TAppZoneHistoryMap& appZoneHistoryMap)
json::to_file(appZoneHistoryFileName, appZoneHistoryRoot); {
json::JsonObject root{};
root.SetNamedValue(NonLocalizable::AppZoneHistoryStr, JSONHelpers::SerializeAppZoneHistory(appZoneHistoryMap));
auto before = json::from_file(appZoneHistoryFileName);
if (!before.has_value() || before.value().Stringify() != root.Stringify())
{
json::to_file(appZoneHistoryFileName, root);
}
} }
TAppZoneHistoryMap ParseAppZoneHistory(const json::JsonObject& fancyZonesDataJSON) TAppZoneHistoryMap ParseAppZoneHistory(const json::JsonObject& fancyZonesDataJSON)

View File

@@ -67,11 +67,9 @@ namespace JSONHelpers
}; };
json::JsonObject GetPersistFancyZonesJSON(const std::wstring& zonesSettingsFileName, const std::wstring& appZoneHistoryFileName); json::JsonObject GetPersistFancyZonesJSON(const std::wstring& zonesSettingsFileName, const std::wstring& appZoneHistoryFileName);
void SaveFancyZonesData(const std::wstring& zonesSettingsFileName,
const std::wstring& appZoneHistoryFileName, void SaveZoneSettings(const std::wstring& zonesSettingsFileName, const TDeviceInfoMap& deviceInfoMap, const TCustomZoneSetsMap& customZoneSetsMap);
const TDeviceInfoMap& deviceInfoMap, void SaveAppZoneHistory(const std::wstring& appZoneHistoryFileName, const TAppZoneHistoryMap& appZoneHistoryMap);
const TCustomZoneSetsMap& customZoneSetsMap,
const TAppZoneHistoryMap& appZoneHistoryMap);
TAppZoneHistoryMap ParseAppZoneHistory(const json::JsonObject& fancyZonesDataJSON); TAppZoneHistoryMap ParseAppZoneHistory(const json::JsonObject& fancyZonesDataJSON);
json::JsonArray SerializeAppZoneHistory(const TAppZoneHistoryMap& appZoneHistoryMap); json::JsonArray SerializeAppZoneHistory(const TAppZoneHistoryMap& appZoneHistoryMap);