Migrate FancyZones data persisting from Registry to JSON file (#1194)

* Migrate FancyZones data persisting from Registry to JSON file

* Address PR comment: Remove redundant check

* Addres PR comment: Remove unused Dpi and add CmdArgs enum

* Address PR comment: Make methods const and inline

* Address PR comments: Expose GenerateUniqueId function and use const ref instead of passing wstring by value

* Address PR comment: Use lamdba as callback

* Address PR comment: Move GenerateUniqueId to ZoneWindowUtils namespace

* Address PR comment: Use regular comparison instead of std::wstring::compare

* Address PR comment: Use std::wstring_view for tmp file paths

* Address PR comment: Use scoped lock when accessing member data

* Address PR comment: Remove typedefs to increase code readability

* Address PR comment: removed nullptr checks with corresponding tests

* Address PR comment: Move ZoneSet object instead of copying

* Address PR comment: Make FancyZonesData instance const where possible

* Remove unnecessary gutter variable during calculating zone coordinates

* Remove uneeded subclass

* Avoid unnecessary copying and reserve space for vector if possible

* Save FancyZones data after exiting editor

* App zone history (#18)

* added window and zone set ids to app zone history

* Rename JSON file

* Remove AppZoneHistory migration

* Move parsing of ZoneWindow independent temp files outside of it

* Unit tests update (#19)

* check device existence in map
* updated ZoneSet tests
* updated JsonHelpers tests

* Use single zone count information

* Remove uneeded tests

* Remove one more test

* Remove uneeded line

* Address PR comments - Missing whitespace

* Update zoneset data for new virtual desktops (#21)

* update active zone set with actual data

* Introduce Blank zone set (used to indicate that no layout applied yet). Move parsing completely outside of ZoneWindow.

* Fix unit tests to match modifications in implementation

* Fix applying layouts on startup (second monitor)

Co-authored-by: vldmr11080 <57061786+vldmr11080@users.noreply.github.com>
Co-authored-by: Seraphima <zykovas91@gmail.com>
This commit is contained in:
stefansjfw
2020-02-10 14:59:51 +01:00
committed by GitHub
parent a5e3207715
commit 53f830bb38
41 changed files with 8496 additions and 1905 deletions

View File

@@ -7,132 +7,6 @@ namespace RegistryHelpers
static PCWSTR REG_SETTINGS = L"Software\\SuperFancyZones";
static PCWSTR APP_ZONE_HISTORY_SUBKEY = L"AppZoneHistory";
inline PCWSTR GetKey(_In_opt_ PCWSTR monitorId, PWSTR key, size_t keyLength)
{
if (monitorId)
{
StringCchPrintf(key, keyLength, L"%s\\%s", REG_SETTINGS, monitorId);
}
else
{
StringCchPrintf(key, keyLength, L"%s", REG_SETTINGS);
}
return key;
}
inline HKEY OpenKey(_In_opt_ PCWSTR monitorId)
{
HKEY hkey;
wchar_t key[256];
GetKey(monitorId, key, ARRAYSIZE(key));
if (RegOpenKeyExW(HKEY_CURRENT_USER, key, 0, KEY_ALL_ACCESS, &hkey) == ERROR_SUCCESS)
{
return hkey;
}
return nullptr;
}
inline HKEY CreateKey(PCWSTR monitorId)
{
HKEY hkey;
wchar_t key[256]{};
GetKey(monitorId, key, ARRAYSIZE(key));
if (RegCreateKeyExW(HKEY_CURRENT_USER, key, 0, nullptr, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, nullptr, &hkey, nullptr) == ERROR_SUCCESS)
{
return hkey;
}
return nullptr;
}
inline LSTATUS GetAppLastZone(HWND window, PCWSTR appPath, _Out_ PINT iZoneIndex)
{
*iZoneIndex = -1;
LSTATUS res{};
if (auto monitor = MonitorFromWindow(window, MONITOR_DEFAULTTONULL))
{
wchar_t keyPath[256]{};
StringCchPrintf(keyPath, ARRAYSIZE(keyPath), L"%s\\%s\\%x", REG_SETTINGS, APP_ZONE_HISTORY_SUBKEY, monitor);
DWORD zoneIndex;
DWORD dataType = REG_DWORD;
DWORD dataSize = sizeof(DWORD);
res = SHRegGetUSValueW(keyPath, appPath, &dataType, &zoneIndex, &dataSize, FALSE, nullptr, 0);
if (res == ERROR_SUCCESS)
{
*iZoneIndex = static_cast<INT>(zoneIndex);
}
}
return res;
}
// Pass -1 for the zoneIndex to delete the entry from the registry
inline void SaveAppLastZone(HWND window, PCWSTR appPath, DWORD zoneIndex)
{
LSTATUS res{};
if (auto monitor = MonitorFromWindow(window, MONITOR_DEFAULTTONULL))
{
wchar_t keyPath[256]{};
StringCchPrintf(keyPath, ARRAYSIZE(keyPath), L"%s\\%s\\%x", REG_SETTINGS, APP_ZONE_HISTORY_SUBKEY, monitor);
if (zoneIndex == -1)
{
SHDeleteValueW(HKEY_CURRENT_USER, keyPath, appPath);
}
else
{
SHRegSetUSValueW(keyPath, appPath, REG_DWORD, &zoneIndex, sizeof(zoneIndex), SHREGSET_FORCE_HKCU);
}
}
}
inline void GetString(PCWSTR uniqueId, PCWSTR setting, PWSTR value, DWORD cbValue)
{
wchar_t key[256]{};
GetKey(uniqueId, key, ARRAYSIZE(key));
SHRegGetUSValueW(key, setting, nullptr, value, &cbValue, FALSE, nullptr, 0);
}
inline void SetString(PCWSTR uniqueId, PCWSTR setting, PCWSTR value)
{
wchar_t key[256]{};
GetKey(uniqueId, key, ARRAYSIZE(key));
SHRegSetUSValueW(key, setting, REG_SZ, value, sizeof(value) * static_cast<DWORD>(wcslen(value)), SHREGSET_FORCE_HKCU);
}
template<typename t>
inline void GetValue(PCWSTR monitorId, PCWSTR setting, t* value, DWORD size)
{
wchar_t key[256]{};
GetKey(monitorId, key, ARRAYSIZE(key));
SHRegGetUSValueW(key, setting, nullptr, value, &size, FALSE, nullptr, 0);
}
template<typename t>
inline void SetValue(PCWSTR monitorId, PCWSTR setting, t value, DWORD size)
{
wchar_t key[256]{};
GetKey(monitorId, key, ARRAYSIZE(key));
SHRegSetUSValueW(key, setting, REG_BINARY, &value, size, SHREGSET_FORCE_HKCU);
}
inline void DeleteZoneSet(PCWSTR monitorId, GUID guid)
{
wil::unique_cotaskmem_string zoneSetId;
if (SUCCEEDED_LOG(StringFromCLSID(guid, &zoneSetId)))
{
wchar_t key[256]{};
GetKey(monitorId, key, ARRAYSIZE(key));
SHDeleteValueW(HKEY_CURRENT_USER, key, zoneSetId.get());
}
}
inline void DeleteAllZoneSets(PCWSTR monitorId)
{
wchar_t key[256]{};
GetKey(monitorId, key, ARRAYSIZE(key));
SHDeleteKey(HKEY_CURRENT_USER, key);
}
inline HRESULT GetCurrentVirtualDesktop(_Out_ GUID* id)
{
*id = GUID_NULL;