mirror of
https://github.com/microsoft/PowerToys.git
synced 2025-12-16 03:37:59 +01:00
[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:
@@ -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);
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -85,6 +85,8 @@ public:
|
||||
|
||||
void LoadFancyZonesData();
|
||||
void SaveFancyZonesData() const;
|
||||
void SaveZoneSettings() const;
|
||||
void SaveAppZoneHistory() const;
|
||||
|
||||
private:
|
||||
#if defined(UNIT_TESTS)
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user