Skip to content

Commit

Permalink
[FanzyZones] app history and zone settings granular saving (#9489)
Browse files Browse the repository at this point in the history
move DeleteFancyZonesRegistryData to proper place
add logging
  • Loading branch information
enricogior committed Feb 4, 2021
1 parent 3d35aed commit db88616
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 16 deletions.
3 changes: 3 additions & 0 deletions src/modules/fancyzones/dll/dllmain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,9 @@ class FancyZonesModule : public PowertoyModuleIface
m_settings = MakeFancyZonesSettings(reinterpret_cast<HINSTANCE>(&__ImageBase), FancyZonesModule::get_name(), FancyZonesModule::get_key());
FancyZonesDataInstance().LoadFancyZonesData();
s_instance = this;

// TODO: consider removing this call since the registry hasn't been used since 0.15
DeleteFancyZonesRegistryData();
}

private:
Expand Down
2 changes: 1 addition & 1 deletion src/modules/fancyzones/lib/FancyZones.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -942,7 +942,7 @@ void FancyZones::AddZoneWindow(HMONITOR monitor, const std::wstring& deviceId) n
if (workArea)
{
m_workAreaHandler.AddWorkArea(m_currentDesktopId, monitor, workArea);
FancyZonesDataInstance().SaveFancyZonesData();
FancyZonesDataInstance().SaveZoneSettings();
}
}
}
Expand Down
44 changes: 32 additions & 12 deletions src/modules/fancyzones/lib/FancyZonesData.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include <sstream>
#include <unordered_set>
#include <common/utils/process_path.h>
#include <common/logger/logger.h>

// Non-localizable strings
namespace NonLocalizable
Expand Down Expand Up @@ -222,38 +223,53 @@ void FancyZonesData::UpdatePrimaryDesktopData(const std::wstring& desktopId)
auto replaceDesktopId = [&desktopId](const std::wstring& deviceId) {
return deviceId.substr(0, deviceId.rfind('_') + 1) + desktopId;
};

std::scoped_lock lock{ dataLock };
bool dirtyFlag = false;

for (auto& [path, perDesktopData] : appZoneHistoryMap)
{
for (auto& data : perDesktopData)
{
if (ExtractVirtualDesktopId(data.deviceId) == NonLocalizable::DefaultGuid)
{
data.deviceId = replaceDesktopId(data.deviceId);
dirtyFlag = true;
}
}
}

std::vector<std::wstring> toReplace{};

for (const auto& [id, data] : deviceInfoMap)
{
if (ExtractVirtualDesktopId(id) == NonLocalizable::DefaultGuid)
{
toReplace.push_back(id);
dirtyFlag = true;
}
}

for (const auto& id : toReplace)
{
auto mapEntry = deviceInfoMap.extract(id);
mapEntry.key() = replaceDesktopId(id);
deviceInfoMap.insert(std::move(mapEntry));
}
SaveFancyZonesData();

// TODO: when updating the primary desktop GUID, the app zone history also needs to be updated
if (dirtyFlag)
{
SaveZoneSettings();
}
}

void FancyZonesData::RemoveDeletedDesktops(const std::vector<std::wstring>& activeDesktops)
{
std::unordered_set<std::wstring> active(std::begin(activeDesktops), std::end(activeDesktops));
std::scoped_lock lock{ dataLock };
bool dirtyFlag = false;

for (auto it = std::begin(deviceInfoMap); it != std::end(deviceInfoMap);)
{
std::wstring desktopId = ExtractVirtualDesktopId(it->first);
Expand All @@ -264,12 +280,17 @@ void FancyZonesData::RemoveDeletedDesktops(const std::vector<std::wstring>& acti
{
RemoveDesktopAppZoneHistory(desktopId);
it = deviceInfoMap.erase(it);
dirtyFlag = true;
continue;
}
}
++it;
}
SaveFancyZonesData();

if (dirtyFlag)
{
SaveAppZoneHistoryAndZoneSettings();
}
}

bool FancyZonesData::IsAnotherWindowOfApplicationInstanceZoned(HWND window, const std::wstring_view& deviceId) const
Expand Down Expand Up @@ -392,7 +413,7 @@ bool FancyZonesData::RemoveAppLastZone(HWND window, const std::wstring_view& dev
{
appZoneHistoryMap.erase(processPath);
}
SaveFancyZonesData();
SaveAppZoneHistory();
return true;
}
else
Expand Down Expand Up @@ -436,7 +457,7 @@ bool FancyZonesData::SetAppLastZones(HWND window, const std::wstring& deviceId,
data.processIdToHandleMap[processId] = window;
data.zoneSetUuid = zoneSetId;
data.zoneIndexSet = zoneIndexSet;
SaveFancyZonesData();
SaveAppZoneHistory();
return true;
}
}
Expand All @@ -460,7 +481,7 @@ bool FancyZonesData::SetAppLastZones(HWND window, const std::wstring& deviceId,
appZoneHistoryMap[processPath] = std::vector<FancyZonesDataTypes::AppZoneHistoryData>{ data };
}

SaveFancyZonesData();
SaveAppZoneHistory();
return true;
}

Expand All @@ -483,7 +504,7 @@ void FancyZonesData::LoadFancyZonesData()
{
if (!std::filesystem::exists(zonesSettingsFileName))
{
SaveFancyZonesData();
SaveAppZoneHistoryAndZoneSettings();
}
else
{
Expand All @@ -493,25 +514,24 @@ void FancyZonesData::LoadFancyZonesData()
deviceInfoMap = JSONHelpers::ParseDeviceInfos(fancyZonesDataJSON);
customZoneSetsMap = JSONHelpers::ParseCustomZoneSets(fancyZonesDataJSON);
}

DeleteFancyZonesRegistryData();
}

void FancyZonesData::SaveFancyZonesData() const
void FancyZonesData::SaveAppZoneHistoryAndZoneSettings() const
{
std::scoped_lock lock{ dataLock };
JSONHelpers::SaveZoneSettings(zonesSettingsFileName, deviceInfoMap, customZoneSetsMap);
JSONHelpers::SaveAppZoneHistory(appZoneHistoryFileName, appZoneHistoryMap);
SaveZoneSettings();
SaveAppZoneHistory();
}

void FancyZonesData::SaveZoneSettings() const
{
Logger::trace("FancyZonesData::SaveZoneSettings()");
std::scoped_lock lock{ dataLock };
JSONHelpers::SaveZoneSettings(zonesSettingsFileName, deviceInfoMap, customZoneSetsMap);
}

void FancyZonesData::SaveAppZoneHistory() const
{
Logger::trace("FancyZonesData::SaveAppZoneHistory()");
std::scoped_lock lock{ dataLock };
JSONHelpers::SaveAppZoneHistory(appZoneHistoryFileName, appZoneHistoryMap);
}
Expand Down
2 changes: 1 addition & 1 deletion src/modules/fancyzones/lib/FancyZonesData.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ class FancyZonesData
json::JsonObject GetPersistFancyZonesJSON();

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

Expand Down
4 changes: 2 additions & 2 deletions src/modules/fancyzones/tests/UnitTests/JsonHelpers.Tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1727,7 +1727,7 @@ namespace FancyZonesUnitTests
data.SetSettingsModulePath(m_moduleName);
const auto& jsonPath = data.zonesSettingsFileName;

data.SaveFancyZonesData();
data.SaveAppZoneHistoryAndZoneSettings();
bool actual = std::filesystem::exists(jsonPath);

Assert::IsTrue(actual);
Expand All @@ -1751,7 +1751,7 @@ namespace FancyZonesUnitTests
// write json with templates to file
json::to_file(jsonPath, expectedJsonObj);

data.SaveFancyZonesData();
data.SaveAppZoneHistoryAndZoneSettings();

// verify that file was written successfully
Assert::IsTrue(std::filesystem::exists(jsonPath));
Expand Down

0 comments on commit db88616

Please sign in to comment.