From 485d278b8c7968f3b37181f8040370c2084f068d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ivan=20Sto=C5=A1i=C4=87?= Date: Tue, 5 Jan 2021 14:00:39 +0100 Subject: [PATCH] [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 74a4a1e467dc78f17b142d3667529db98365e8b3. * Only save files if the contents are about to be changed * Restore FileWatcher --- src/modules/fancyzones/lib/FancyZones.cpp | 4 +--- src/modules/fancyzones/lib/FancyZonesData.cpp | 19 +++++++++++---- src/modules/fancyzones/lib/FancyZonesData.h | 2 ++ src/modules/fancyzones/lib/JsonHelpers.cpp | 24 +++++++++++-------- src/modules/fancyzones/lib/JsonHelpers.h | 8 +++---- 5 files changed, 34 insertions(+), 23 deletions(-) diff --git a/src/modules/fancyzones/lib/FancyZones.cpp b/src/modules/fancyzones/lib/FancyZones.cpp index dc18dd6419..86d0d643ec 100644 --- a/src/modules/fancyzones/lib/FancyZones.cpp +++ b/src/modules/fancyzones/lib/FancyZones.cpp @@ -53,9 +53,7 @@ public: PostMessageW(m_window, WM_PRIV_LOCATIONCHANGE, NULL, NULL); }), m_fileWatcher(FancyZonesDataInstance().GetZonesSettingsFileName(), [this]() { - // This is commented out until we figure out why calling UpdateZoneSets() in response to this message - // causes Win+Arrow to break. - //PostMessageW(m_window, WM_PRIV_FILE_UPDATE, NULL, NULL); + PostMessageW(m_window, WM_PRIV_FILE_UPDATE, NULL, NULL); }) { m_settings->SetCallback(this); diff --git a/src/modules/fancyzones/lib/FancyZonesData.cpp b/src/modules/fancyzones/lib/FancyZonesData.cpp index 7880714642..a1b79b06ca 100644 --- a/src/modules/fancyzones/lib/FancyZonesData.cpp +++ b/src/modules/fancyzones/lib/FancyZonesData.cpp @@ -553,11 +553,20 @@ void FancyZonesData::LoadFancyZonesData() void FancyZonesData::SaveFancyZonesData() const { std::scoped_lock lock{ dataLock }; - JSONHelpers::SaveFancyZonesData(zonesSettingsFileName, - appZoneHistoryFileName, - deviceInfoMap, - customZoneSetsMap, - appZoneHistoryMap); + JSONHelpers::SaveZoneSettings(zonesSettingsFileName, deviceInfoMap, customZoneSetsMap); + JSONHelpers::SaveAppZoneHistory(appZoneHistoryFileName, 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) diff --git a/src/modules/fancyzones/lib/FancyZonesData.h b/src/modules/fancyzones/lib/FancyZonesData.h index 62360c5b04..e88573bca7 100644 --- a/src/modules/fancyzones/lib/FancyZonesData.h +++ b/src/modules/fancyzones/lib/FancyZonesData.h @@ -85,6 +85,8 @@ public: void LoadFancyZonesData(); void SaveFancyZonesData() const; + void SaveZoneSettings() const; + void SaveAppZoneHistory() const; private: #if defined(UNIT_TESTS) diff --git a/src/modules/fancyzones/lib/JsonHelpers.cpp b/src/modules/fancyzones/lib/JsonHelpers.cpp index 503914ccaf..192c667949 100644 --- a/src/modules/fancyzones/lib/JsonHelpers.cpp +++ b/src/modules/fancyzones/lib/JsonHelpers.cpp @@ -532,17 +532,10 @@ namespace JSONHelpers } } - void SaveFancyZonesData(const std::wstring& zonesSettingsFileName, - const std::wstring& appZoneHistoryFileName, - const TDeviceInfoMap& deviceInfoMap, - const TCustomZoneSetsMap& customZoneSetsMap, - const TAppZoneHistoryMap& appZoneHistoryMap) - + void SaveZoneSettings(const std::wstring& zonesSettingsFileName, const TDeviceInfoMap& deviceInfoMap, const TCustomZoneSetsMap& customZoneSetsMap) { json::JsonObject root{}; - json::JsonObject appZoneHistoryRoot{}; - appZoneHistoryRoot.SetNamedValue(NonLocalizable::AppZoneHistoryStr, JSONHelpers::SerializeAppZoneHistory(appZoneHistoryMap)); root.SetNamedValue(NonLocalizable::DevicesStr, JSONHelpers::SerializeDeviceInfos(deviceInfoMap)); root.SetNamedValue(NonLocalizable::CustomZoneSetsStr, JSONHelpers::SerializeCustomZoneSets(customZoneSetsMap)); @@ -550,10 +543,21 @@ namespace JSONHelpers if (!before.has_value() || before.value().Stringify() != root.Stringify()) { Trace::FancyZones::DataChanged(); + json::to_file(zonesSettingsFileName, root); } + } - json::to_file(zonesSettingsFileName, root); - json::to_file(appZoneHistoryFileName, appZoneHistoryRoot); + void SaveAppZoneHistory(const std::wstring& appZoneHistoryFileName, const TAppZoneHistoryMap& appZoneHistoryMap) + { + 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) diff --git a/src/modules/fancyzones/lib/JsonHelpers.h b/src/modules/fancyzones/lib/JsonHelpers.h index 81f5911391..1a838a46e6 100644 --- a/src/modules/fancyzones/lib/JsonHelpers.h +++ b/src/modules/fancyzones/lib/JsonHelpers.h @@ -67,11 +67,9 @@ namespace JSONHelpers }; json::JsonObject GetPersistFancyZonesJSON(const std::wstring& zonesSettingsFileName, const std::wstring& appZoneHistoryFileName); - void SaveFancyZonesData(const std::wstring& zonesSettingsFileName, - const std::wstring& appZoneHistoryFileName, - const TDeviceInfoMap& deviceInfoMap, - const TCustomZoneSetsMap& customZoneSetsMap, - const TAppZoneHistoryMap& appZoneHistoryMap); + + void SaveZoneSettings(const std::wstring& zonesSettingsFileName, const TDeviceInfoMap& deviceInfoMap, const TCustomZoneSetsMap& customZoneSetsMap); + void SaveAppZoneHistory(const std::wstring& appZoneHistoryFileName, const TAppZoneHistoryMap& appZoneHistoryMap); TAppZoneHistoryMap ParseAppZoneHistory(const json::JsonObject& fancyZonesDataJSON); json::JsonArray SerializeAppZoneHistory(const TAppZoneHistoryMap& appZoneHistoryMap);