Skip to content

Commit

Permalink
[FancyZones] Refactor/improve saving JSON files (#8947)
Browse files Browse the repository at this point in the history
* Divide SaveFancyZonesData into two functions

* Removed unused function, continued refactoring

* Use SaveAppZoneHistory in some places

* Revert "Use SaveAppZoneHistory in some places"

This reverts commit 74a4a1e.

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

* Restore FileWatcher
  • Loading branch information
ivan100sic committed Jan 5, 2021
1 parent d4a4203 commit 485d278
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 23 deletions.
4 changes: 1 addition & 3 deletions src/modules/fancyzones/lib/FancyZones.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,7 @@ struct FancyZones : public winrt::implements<FancyZones, IFancyZones, IFancyZone
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);
Expand Down
19 changes: 14 additions & 5 deletions src/modules/fancyzones/lib/FancyZonesData.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions src/modules/fancyzones/lib/FancyZonesData.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ class FancyZonesData

void LoadFancyZonesData();
void SaveFancyZonesData() const;
void SaveZoneSettings() const;
void SaveAppZoneHistory() const;

private:
#if defined(UNIT_TESTS)
Expand Down
24 changes: 14 additions & 10 deletions src/modules/fancyzones/lib/JsonHelpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -532,28 +532,32 @@ 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));

auto before = json::from_file(zonesSettingsFileName);
if (!before.has_value() || before.value().Stringify() != root.Stringify())
{
Trace::FancyZones::DataChanged();
json::to_file(zonesSettingsFileName, root);
}
}

void SaveAppZoneHistory(const std::wstring& appZoneHistoryFileName, const TAppZoneHistoryMap& appZoneHistoryMap)
{
json::JsonObject root{};

json::to_file(zonesSettingsFileName, root);
json::to_file(appZoneHistoryFileName, appZoneHistoryRoot);
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)
Expand Down
8 changes: 3 additions & 5 deletions src/modules/fancyzones/lib/JsonHelpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 485d278

Please sign in to comment.