From f0fda09734672beb66b0067bf2d0f477695565f2 Mon Sep 17 00:00:00 2001 From: vldmr11080 z Date: Thu, 13 Feb 2020 18:26:28 +0100 Subject: [PATCH 1/9] Introduce handling of new/old work areas (defined by virtual desktop id and monitor) --- src/modules/fancyzones/lib/FancyZones.cpp | 102 +++++++++++++++------ src/modules/fancyzones/lib/JsonHelpers.cpp | 32 ++++++- src/modules/fancyzones/lib/JsonHelpers.h | 1 + 3 files changed, 103 insertions(+), 32 deletions(-) diff --git a/src/modules/fancyzones/lib/FancyZones.cpp b/src/modules/fancyzones/lib/FancyZones.cpp index 1c8443fdb5f3..b96f0aad7f47 100644 --- a/src/modules/fancyzones/lib/FancyZones.cpp +++ b/src/modules/fancyzones/lib/FancyZones.cpp @@ -13,6 +13,7 @@ #include #include #include +#include enum class DisplayChangeType { @@ -136,7 +137,12 @@ struct FancyZones : public winrt::implements& buffer, int bufferCapacity) noexcept; + void RegisterNewWorkArea(GUID virtualDesktopId, HMONITOR monitor) noexcept; + bool IsNewWorkArea(GUID virtualDesktopId, HMONITOR monitor) noexcept; + void OnEditorExitEvent() noexcept; const HINSTANCE m_hinstance{}; @@ -152,7 +158,7 @@ struct FancyZones : public winrt::implements m_zoneWindowMoveSize; // "Active" ZoneWindow, where the move/size is happening. Will update as drag moves between monitors. winrt::com_ptr m_settings{}; GUID m_currentVirtualDesktopId{}; // UUID of the current virtual desktop. Is GUID_NULL until first VD switch per session. - std::unordered_map m_virtualDesktopIds; + std::unordered_map> m_processedWorkAreas; // Work area is defined by monitor and virtual desktop id. wil::unique_handle m_terminateEditorEvent; // Handle of FancyZonesEditor.exe we launch and wait on wil::unique_handle m_terminateVirtualDesktopTrackerEvent; @@ -608,22 +614,21 @@ void FancyZones::AddZoneWindow(HMONITOR monitor, PCWSTR deviceId) noexcept if (SUCCEEDED_LOG(StringFromCLSID(m_currentVirtualDesktopId, &virtualDesktopId))) { std::wstring uniqueId = ZoneWindowUtils::GenerateUniqueId(monitor, deviceId, virtualDesktopId.get()); - bool newVirtualDesktop = true; + JSONHelpers::FancyZonesDataInstance().SetActiveDeviceId(uniqueId); - auto it = m_virtualDesktopIds.find(m_currentVirtualDesktopId); - if (it != end(m_virtualDesktopIds)) - { - newVirtualDesktop = it->second; - JSONHelpers::FancyZonesDataInstance().SetActiveDeviceId(uniqueId); - } + bool newWorkArea = IsNewWorkArea(m_currentVirtualDesktopId, monitor); + const bool flash = m_settings->GetSettings().zoneSetChange_flashZones && newWorkArea; - const bool flash = m_settings->GetSettings().zoneSetChange_flashZones && newVirtualDesktop; auto zoneWindow = MakeZoneWindow(this, m_hinstance, monitor, uniqueId, flash); if (zoneWindow) { m_zoneWindowMap[monitor] = std::move(zoneWindow); } - m_virtualDesktopIds[m_currentVirtualDesktopId] = false; + if (newWorkArea) + { + RegisterNewWorkArea(m_currentVirtualDesktopId, monitor); + JSONHelpers::FancyZonesDataInstance().SaveFancyZonesData(); + } } } @@ -961,31 +966,68 @@ void FancyZones::HandleVirtualDesktopUpdates(HANDLE fancyZonesDestroyedEvent) no { return; } - const int guidSize = sizeof(GUID); - std::unordered_map temp; - temp.reserve(bufferCapacity / guidSize); - for (size_t i = 0; i < bufferCapacity; i += guidSize) - { - GUID* guid = reinterpret_cast(buffer.get() + i); - temp[*guid] = true; - } - std::unique_lock writeLock(m_lock); - for (auto it = begin(m_virtualDesktopIds); it != end(m_virtualDesktopIds);) + RegisterVirtualDesktopUpdates(buffer, bufferCapacity); + } +} + +void FancyZones::RegisterVirtualDesktopUpdates(std::unique_ptr& buffer, int bufferCapacity) noexcept +{ + const int guidSize = sizeof(GUID); + std::unordered_set temp; + temp.reserve(bufferCapacity / guidSize); + for (size_t i = 0; i < bufferCapacity; i += guidSize) + { + GUID* guid = reinterpret_cast(buffer.get() + i); + temp.insert(*guid); + } + std::unique_lock writeLock(m_lock); + for (auto it = begin(m_processedWorkAreas); it != end(m_processedWorkAreas);) + { + auto iter = temp.find(it->first); + if (iter == temp.end()) { - auto iter = temp.find(it->first); - if (iter == temp.end()) - { - it = m_virtualDesktopIds.erase(it); // virtual desktop closed, remove it from map - } - else + // clean up data related to virtual desktop + wil::unique_cotaskmem_string virtualDesktopId; + if (SUCCEEDED_LOG(StringFromCLSID(it->first, &virtualDesktopId))) { - temp.erase(it->first); // virtual desktop already in map, skip it - ++it; + JSONHelpers::FancyZonesDataInstance().RemoveDevicesByVirtualDesktopId(virtualDesktopId.get()); } + it = m_processedWorkAreas.erase(it); // virtual desktop closed, remove it from map } - // register new virtual desktops, if any - m_virtualDesktopIds.insert(begin(temp), end(temp)); + else + { + temp.erase(it->first); // virtual desktop already in map, skip it + ++it; + } + } + // register new virtual desktops, if any + for (const auto& id : temp) + { + m_processedWorkAreas[id] = std::vector(); + } +} + +void FancyZones::RegisterNewWorkArea(GUID virtualDesktopId, HMONITOR monitor) noexcept +{ + if (!m_processedWorkAreas.contains(virtualDesktopId)) + { + m_processedWorkAreas[virtualDesktopId] = { monitor }; } + else + { + m_processedWorkAreas[virtualDesktopId].push_back(monitor); + } +} + +bool FancyZones::IsNewWorkArea(GUID virtualDesktopId, HMONITOR monitor) noexcept +{ + auto it = m_processedWorkAreas.find(virtualDesktopId); + if (it != m_processedWorkAreas.end()) + { + // virtual desktop exists, check if it's processed on given monitor + return std::find(it->second.begin(), it->second.end(), monitor) == it->second.end(); + } + return true; } void FancyZones::OnEditorExitEvent() noexcept diff --git a/src/modules/fancyzones/lib/JsonHelpers.cpp b/src/modules/fancyzones/lib/JsonHelpers.cpp index 790befc6128b..629ebb894285 100644 --- a/src/modules/fancyzones/lib/JsonHelpers.cpp +++ b/src/modules/fancyzones/lib/JsonHelpers.cpp @@ -21,6 +21,12 @@ namespace constexpr int c_blankCustomModelId = 0xFFFA; const wchar_t* FANCY_ZONES_DATA_FILE = L"zones-settings.json"; + + std::wstring ExtractVirtualDesktopId(const std::wstring& deviceId) + { + // Format: __ + return deviceId.substr(deviceId.rfind('_') + 1); + } } namespace JSONHelpers @@ -160,12 +166,33 @@ namespace JSONHelpers if (!deviceInfoMap.contains(deviceId)) { // Creates default entry in map when ZoneWindow is created - deviceInfoMap[deviceId] = DeviceInfoData{ ZoneSetData{ L"null", ZoneSetLayoutType::Blank } }; + deviceInfoMap[deviceId] = DeviceInfoData{ ZoneSetData{ L"null", ZoneSetLayoutType::Blank } }; MigrateDeviceInfoFromRegistry(deviceId); } } + void FancyZonesData::RemoveDevicesByVirtualDesktopId(const std::wstring& virtualDesktopId) + { + bool modified{ false }; + for (auto it = deviceInfoMap.begin(); it != deviceInfoMap.end();) + { + if (ExtractVirtualDesktopId(it->first) == virtualDesktopId) + { + it = deviceInfoMap.erase(it); + modified = true; + } + else + { + ++it; + } + } + if (modified) + { + SaveFancyZonesData(); + } + } + void FancyZonesData::CloneDeviceInfo(const std::wstring& source, const std::wstring& destination) { // Clone information from source device if destination device is uninitialized (Blank). @@ -386,7 +413,8 @@ namespace JSONHelpers for (const auto& [deviceID, deviceData] : deviceInfoMap) { - if (deviceData.activeZoneSet.type != ZoneSetLayoutType::Blank) { + if (deviceData.activeZoneSet.type != ZoneSetLayoutType::Blank) + { DeviceInfosJSON.Append(DeviceInfoJSON::DeviceInfoJSON::ToJson(DeviceInfoJSON{ deviceID, deviceData })); } } diff --git a/src/modules/fancyzones/lib/JsonHelpers.h b/src/modules/fancyzones/lib/JsonHelpers.h index 5ec6b461fbde..de7ba669837b 100644 --- a/src/modules/fancyzones/lib/JsonHelpers.h +++ b/src/modules/fancyzones/lib/JsonHelpers.h @@ -196,6 +196,7 @@ namespace JSONHelpers } void AddDevice(const std::wstring& deviceId); + void RemoveDevicesByVirtualDesktopId(const std::wstring& virtualDesktopId); void CloneDeviceInfo(const std::wstring& source, const std::wstring& destination); int GetAppLastZoneIndex(HWND window, const std::wstring_view& deviceId, const std::wstring_view& zoneSetId) const; From 42323f5dd2f3cacd226405e4d55424e8d0d2638a Mon Sep 17 00:00:00 2001 From: vldmr11080 z Date: Sat, 15 Feb 2020 10:25:41 +0100 Subject: [PATCH 2/9] Save data to JSON storage only once after we register all deleted virtual desktops --- src/modules/fancyzones/lib/FancyZones.cpp | 43 ++++++++++++---------- src/modules/fancyzones/lib/JsonHelpers.cpp | 7 +--- src/modules/fancyzones/lib/JsonHelpers.h | 2 +- 3 files changed, 27 insertions(+), 25 deletions(-) diff --git a/src/modules/fancyzones/lib/FancyZones.cpp b/src/modules/fancyzones/lib/FancyZones.cpp index b96f0aad7f47..4d3f7bf76582 100644 --- a/src/modules/fancyzones/lib/FancyZones.cpp +++ b/src/modules/fancyzones/lib/FancyZones.cpp @@ -139,7 +139,7 @@ struct FancyZones : public winrt::implements& buffer, int bufferCapacity) noexcept; + void RegisterVirtualDesktopUpdates(std::unordered_set& currentVirtualDesktopIds) noexcept; void RegisterNewWorkArea(GUID virtualDesktopId, HMONITOR monitor) noexcept; bool IsNewWorkArea(GUID virtualDesktopId, HMONITOR monitor) noexcept; @@ -616,7 +616,7 @@ void FancyZones::AddZoneWindow(HMONITOR monitor, PCWSTR deviceId) noexcept std::wstring uniqueId = ZoneWindowUtils::GenerateUniqueId(monitor, deviceId, virtualDesktopId.get()); JSONHelpers::FancyZonesDataInstance().SetActiveDeviceId(uniqueId); - bool newWorkArea = IsNewWorkArea(m_currentVirtualDesktopId, monitor); + const bool newWorkArea = IsNewWorkArea(m_currentVirtualDesktopId, monitor); const bool flash = m_settings->GetSettings().zoneSetChange_flashZones && newWorkArea; auto zoneWindow = MakeZoneWindow(this, m_hinstance, monitor, uniqueId, flash); @@ -966,42 +966,47 @@ void FancyZones::HandleVirtualDesktopUpdates(HANDLE fancyZonesDestroyedEvent) no { return; } - RegisterVirtualDesktopUpdates(buffer, bufferCapacity); + const size_t guidSize = sizeof(GUID); + std::unordered_set temp; + temp.reserve(bufferCapacity / guidSize); + for (size_t i = 0; i < bufferCapacity; i += guidSize) + { + GUID* guid = reinterpret_cast(buffer.get() + i); + temp.insert(*guid); + } + RegisterVirtualDesktopUpdates(temp); } } -void FancyZones::RegisterVirtualDesktopUpdates(std::unique_ptr& buffer, int bufferCapacity) noexcept +void FancyZones::RegisterVirtualDesktopUpdates(std::unordered_set& currentVirtualDesktopIds) noexcept { - const int guidSize = sizeof(GUID); - std::unordered_set temp; - temp.reserve(bufferCapacity / guidSize); - for (size_t i = 0; i < bufferCapacity; i += guidSize) - { - GUID* guid = reinterpret_cast(buffer.get() + i); - temp.insert(*guid); - } std::unique_lock writeLock(m_lock); + bool modified{ true }; for (auto it = begin(m_processedWorkAreas); it != end(m_processedWorkAreas);) { - auto iter = temp.find(it->first); - if (iter == temp.end()) + auto iter = currentVirtualDesktopIds.find(it->first); + if (iter == currentVirtualDesktopIds.end()) { - // clean up data related to virtual desktop + // if we couldn't find the GUID in currentVirtualDesktopIds, we must remove it from both m_processedWorkAreas and deviceInfoMap wil::unique_cotaskmem_string virtualDesktopId; if (SUCCEEDED_LOG(StringFromCLSID(it->first, &virtualDesktopId))) { - JSONHelpers::FancyZonesDataInstance().RemoveDevicesByVirtualDesktopId(virtualDesktopId.get()); + modified &= JSONHelpers::FancyZonesDataInstance().RemoveDevicesByVirtualDesktopId(virtualDesktopId.get()); } - it = m_processedWorkAreas.erase(it); // virtual desktop closed, remove it from map + it = m_processedWorkAreas.erase(it); } else { - temp.erase(it->first); // virtual desktop already in map, skip it + currentVirtualDesktopIds.erase(it->first); // virtual desktop already in map, skip it ++it; } } + if (modified) + { + JSONHelpers::FancyZonesDataInstance().SaveFancyZonesData(); + } // register new virtual desktops, if any - for (const auto& id : temp) + for (const auto& id : currentVirtualDesktopIds) { m_processedWorkAreas[id] = std::vector(); } diff --git a/src/modules/fancyzones/lib/JsonHelpers.cpp b/src/modules/fancyzones/lib/JsonHelpers.cpp index 629ebb894285..debb8939e036 100644 --- a/src/modules/fancyzones/lib/JsonHelpers.cpp +++ b/src/modules/fancyzones/lib/JsonHelpers.cpp @@ -172,7 +172,7 @@ namespace JSONHelpers } } - void FancyZonesData::RemoveDevicesByVirtualDesktopId(const std::wstring& virtualDesktopId) + bool FancyZonesData::RemoveDevicesByVirtualDesktopId(const std::wstring& virtualDesktopId) { bool modified{ false }; for (auto it = deviceInfoMap.begin(); it != deviceInfoMap.end();) @@ -187,10 +187,7 @@ namespace JSONHelpers ++it; } } - if (modified) - { - SaveFancyZonesData(); - } + return modified; } void FancyZonesData::CloneDeviceInfo(const std::wstring& source, const std::wstring& destination) diff --git a/src/modules/fancyzones/lib/JsonHelpers.h b/src/modules/fancyzones/lib/JsonHelpers.h index de7ba669837b..91e765af5b76 100644 --- a/src/modules/fancyzones/lib/JsonHelpers.h +++ b/src/modules/fancyzones/lib/JsonHelpers.h @@ -196,7 +196,7 @@ namespace JSONHelpers } void AddDevice(const std::wstring& deviceId); - void RemoveDevicesByVirtualDesktopId(const std::wstring& virtualDesktopId); + bool RemoveDevicesByVirtualDesktopId(const std::wstring& virtualDesktopId); void CloneDeviceInfo(const std::wstring& source, const std::wstring& destination); int GetAppLastZoneIndex(HWND window, const std::wstring_view& deviceId, const std::wstring_view& zoneSetId) const; From fd866ce71fbff04d4513a8e67abca63905a792a3 Mon Sep 17 00:00:00 2001 From: vldmr11080 z Date: Sat, 15 Feb 2020 10:28:26 +0100 Subject: [PATCH 3/9] Update saving mechanism --- src/modules/fancyzones/lib/FancyZones.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/modules/fancyzones/lib/FancyZones.cpp b/src/modules/fancyzones/lib/FancyZones.cpp index 4d3f7bf76582..da6a16d50511 100644 --- a/src/modules/fancyzones/lib/FancyZones.cpp +++ b/src/modules/fancyzones/lib/FancyZones.cpp @@ -981,7 +981,7 @@ void FancyZones::HandleVirtualDesktopUpdates(HANDLE fancyZonesDestroyedEvent) no void FancyZones::RegisterVirtualDesktopUpdates(std::unordered_set& currentVirtualDesktopIds) noexcept { std::unique_lock writeLock(m_lock); - bool modified{ true }; + bool modified{ false }; for (auto it = begin(m_processedWorkAreas); it != end(m_processedWorkAreas);) { auto iter = currentVirtualDesktopIds.find(it->first); @@ -991,7 +991,7 @@ void FancyZones::RegisterVirtualDesktopUpdates(std::unordered_set& current wil::unique_cotaskmem_string virtualDesktopId; if (SUCCEEDED_LOG(StringFromCLSID(it->first, &virtualDesktopId))) { - modified &= JSONHelpers::FancyZonesDataInstance().RemoveDevicesByVirtualDesktopId(virtualDesktopId.get()); + modified |= JSONHelpers::FancyZonesDataInstance().RemoveDevicesByVirtualDesktopId(virtualDesktopId.get()); } it = m_processedWorkAreas.erase(it); } From 5a36651217ffb03fdabb936483c4bec1e6153d06 Mon Sep 17 00:00:00 2001 From: vldmr11080 z Date: Mon, 17 Feb 2020 21:23:42 +0100 Subject: [PATCH 4/9] Remove resolution from unique identifier --- src/modules/fancyzones/lib/FancyZones.cpp | 2 +- src/modules/fancyzones/lib/ZoneWindow.cpp | 10 +++------- src/modules/fancyzones/lib/ZoneWindow.h | 2 +- .../fancyzones/tests/UnitTests/ZoneWindow.Spec.cpp | 7 +++---- 4 files changed, 8 insertions(+), 13 deletions(-) diff --git a/src/modules/fancyzones/lib/FancyZones.cpp b/src/modules/fancyzones/lib/FancyZones.cpp index da6a16d50511..06c8fc2dd541 100644 --- a/src/modules/fancyzones/lib/FancyZones.cpp +++ b/src/modules/fancyzones/lib/FancyZones.cpp @@ -613,7 +613,7 @@ void FancyZones::AddZoneWindow(HMONITOR monitor, PCWSTR deviceId) noexcept wil::unique_cotaskmem_string virtualDesktopId; if (SUCCEEDED_LOG(StringFromCLSID(m_currentVirtualDesktopId, &virtualDesktopId))) { - std::wstring uniqueId = ZoneWindowUtils::GenerateUniqueId(monitor, deviceId, virtualDesktopId.get()); + std::wstring uniqueId = ZoneWindowUtils::GenerateUniqueId(deviceId, virtualDesktopId.get()); JSONHelpers::FancyZonesDataInstance().SetActiveDeviceId(uniqueId); const bool newWorkArea = IsNewWorkArea(m_currentVirtualDesktopId, monitor); diff --git a/src/modules/fancyzones/lib/ZoneWindow.cpp b/src/modules/fancyzones/lib/ZoneWindow.cpp index a70ffc5dfa1f..da4d00aaad2e 100644 --- a/src/modules/fancyzones/lib/ZoneWindow.cpp +++ b/src/modules/fancyzones/lib/ZoneWindow.cpp @@ -63,19 +63,15 @@ namespace ZoneWindowUtils return customZoneSetsTmpFileName; } - std::wstring GenerateUniqueId(HMONITOR monitor, PCWSTR deviceId, PCWSTR virtualDesktopId) + std::wstring GenerateUniqueId(PCWSTR deviceId, PCWSTR virtualDesktopId) { wchar_t uniqueId[256]{}; // Parsed deviceId + resolution + virtualDesktopId - MONITORINFOEXW mi; - mi.cbSize = sizeof(mi); - if (virtualDesktopId && GetMonitorInfo(monitor, &mi)) + if (virtualDesktopId) { wchar_t parsedId[256]{}; ParseDeviceId(deviceId, parsedId, 256); - - Rect const monitorRect(mi.rcMonitor); - StringCchPrintf(uniqueId, ARRAYSIZE(uniqueId), L"%s_%d_%d_%s", parsedId, monitorRect.width(), monitorRect.height(), virtualDesktopId); + StringCchPrintf(uniqueId, ARRAYSIZE(uniqueId), L"%s_%s", parsedId, virtualDesktopId); } return std::wstring{ uniqueId }; } diff --git a/src/modules/fancyzones/lib/ZoneWindow.h b/src/modules/fancyzones/lib/ZoneWindow.h index 93a195ae6507..69dff4b6d2cc 100644 --- a/src/modules/fancyzones/lib/ZoneWindow.h +++ b/src/modules/fancyzones/lib/ZoneWindow.h @@ -7,7 +7,7 @@ namespace ZoneWindowUtils const std::wstring& GetActiveZoneSetTmpPath(); const std::wstring& GetAppliedZoneSetTmpPath(); const std::wstring& GetCustomZoneSetsTmpPath(); - std::wstring GenerateUniqueId(HMONITOR monitor, PCWSTR deviceId, PCWSTR virtualDesktopId); + std::wstring GenerateUniqueId(PCWSTR deviceId, PCWSTR virtualDesktopId); } interface __declspec(uuid("{7F017528-8110-4FB3-BE41-F472969C2560}")) IZoneWindow : public IUnknown diff --git a/src/modules/fancyzones/tests/UnitTests/ZoneWindow.Spec.cpp b/src/modules/fancyzones/tests/UnitTests/ZoneWindow.Spec.cpp index 8a5418bedeea..909892b7e6d9 100644 --- a/src/modules/fancyzones/tests/UnitTests/ZoneWindow.Spec.cpp +++ b/src/modules/fancyzones/tests/UnitTests/ZoneWindow.Spec.cpp @@ -173,11 +173,11 @@ namespace FancyZonesUnitTests TEST_METHOD(CreateZoneWindowNoDeviceId) { // Generate unique id without device id - std::wstring uniqueId = ZoneWindowUtils::GenerateUniqueId(m_monitor, nullptr, m_virtualDesktopId.c_str()); + std::wstring uniqueId = ZoneWindowUtils::GenerateUniqueId(nullptr, m_virtualDesktopId.c_str()); m_zoneWindow = MakeZoneWindow(m_hostPtr, m_hInst, m_monitor, uniqueId, false); const std::wstring expectedWorkArea = std::to_wstring(m_monitorInfo.rcMonitor.right) + L"_" + std::to_wstring(m_monitorInfo.rcMonitor.bottom); - const std::wstring expectedUniqueId = L"FallbackDevice_" + std::to_wstring(m_monitorInfo.rcMonitor.right) + L"_" + std::to_wstring(m_monitorInfo.rcMonitor.bottom) + L"_" + m_virtualDesktopId; + const std::wstring expectedUniqueId = L"FallbackDevice_" + m_virtualDesktopId; Assert::IsNotNull(m_zoneWindow.get()); Assert::IsFalse(m_zoneWindow->IsDragEnabled()); @@ -189,10 +189,9 @@ namespace FancyZonesUnitTests TEST_METHOD(CreateZoneWindowNoDesktopId) { // Generate unique id without virtual desktop id - std::wstring uniqueId = ZoneWindowUtils::GenerateUniqueId(m_monitor, m_deviceId.c_str(), nullptr); + std::wstring uniqueId = ZoneWindowUtils::GenerateUniqueId(m_deviceId.c_str(), nullptr); m_zoneWindow = MakeZoneWindow(m_hostPtr, m_hInst, m_monitor, uniqueId, false); - const std::wstring expectedWorkArea = std::to_wstring(m_monitorInfo.rcMonitor.right) + L"_" + std::to_wstring(m_monitorInfo.rcMonitor.bottom); Assert::IsNotNull(m_zoneWindow.get()); Assert::IsFalse(m_zoneWindow->IsDragEnabled()); Assert::IsTrue(m_zoneWindow->UniqueId().empty()); From 8e898025494e5da35bbd59c1d24053b45efd0e51 Mon Sep 17 00:00:00 2001 From: vldmr11080 z Date: Mon, 17 Feb 2020 21:28:06 +0100 Subject: [PATCH 5/9] Update comments related to new unique identifier --- src/modules/fancyzones/lib/JsonHelpers.cpp | 2 +- src/modules/fancyzones/lib/ZoneWindow.cpp | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/modules/fancyzones/lib/JsonHelpers.cpp b/src/modules/fancyzones/lib/JsonHelpers.cpp index debb8939e036..ca8690697859 100644 --- a/src/modules/fancyzones/lib/JsonHelpers.cpp +++ b/src/modules/fancyzones/lib/JsonHelpers.cpp @@ -24,7 +24,7 @@ namespace std::wstring ExtractVirtualDesktopId(const std::wstring& deviceId) { - // Format: __ + // Format: _ return deviceId.substr(deviceId.rfind('_') + 1); } } diff --git a/src/modules/fancyzones/lib/ZoneWindow.cpp b/src/modules/fancyzones/lib/ZoneWindow.cpp index da4d00aaad2e..4ebff171b83f 100644 --- a/src/modules/fancyzones/lib/ZoneWindow.cpp +++ b/src/modules/fancyzones/lib/ZoneWindow.cpp @@ -65,7 +65,8 @@ namespace ZoneWindowUtils std::wstring GenerateUniqueId(PCWSTR deviceId, PCWSTR virtualDesktopId) { - wchar_t uniqueId[256]{}; // Parsed deviceId + resolution + virtualDesktopId + // Format: _ + wchar_t uniqueId[256]{}; if (virtualDesktopId) { From da7f52d2375861f6a6b154a23474bc89d04e0c16 Mon Sep 17 00:00:00 2001 From: vldmr11080 z Date: Mon, 17 Feb 2020 22:24:51 +0100 Subject: [PATCH 6/9] Don't handle NULL GUID as it is default one --- src/modules/fancyzones/lib/JsonHelpers.cpp | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/modules/fancyzones/lib/JsonHelpers.cpp b/src/modules/fancyzones/lib/JsonHelpers.cpp index ca8690697859..a01d899352a4 100644 --- a/src/modules/fancyzones/lib/JsonHelpers.cpp +++ b/src/modules/fancyzones/lib/JsonHelpers.cpp @@ -21,6 +21,7 @@ namespace constexpr int c_blankCustomModelId = 0xFFFA; const wchar_t* FANCY_ZONES_DATA_FILE = L"zones-settings.json"; + const wchar_t* DEFAULT_GUID = L"{00000000-0000-0000-0000-000000000000}"; std::wstring ExtractVirtualDesktopId(const std::wstring& deviceId) { @@ -174,6 +175,10 @@ namespace JSONHelpers bool FancyZonesData::RemoveDevicesByVirtualDesktopId(const std::wstring& virtualDesktopId) { + if (virtualDesktopId == DEFAULT_GUID) + { + return false; + } bool modified{ false }; for (auto it = deviceInfoMap.begin(); it != deviceInfoMap.end();) { From f8a42b5233aaee645465cc670d2f179c110fd4fa Mon Sep 17 00:00:00 2001 From: vldmr11080 z Date: Mon, 17 Feb 2020 21:23:42 +0100 Subject: [PATCH 7/9] Revert "Remove resolution from unique identifier" This reverts commit 5a36651217ffb03fdabb936483c4bec1e6153d06. --- src/modules/fancyzones/lib/FancyZones.cpp | 2 +- src/modules/fancyzones/lib/ZoneWindow.cpp | 10 +++++++--- src/modules/fancyzones/lib/ZoneWindow.h | 2 +- .../fancyzones/tests/UnitTests/ZoneWindow.Spec.cpp | 7 ++++--- 4 files changed, 13 insertions(+), 8 deletions(-) diff --git a/src/modules/fancyzones/lib/FancyZones.cpp b/src/modules/fancyzones/lib/FancyZones.cpp index 06c8fc2dd541..da6a16d50511 100644 --- a/src/modules/fancyzones/lib/FancyZones.cpp +++ b/src/modules/fancyzones/lib/FancyZones.cpp @@ -613,7 +613,7 @@ void FancyZones::AddZoneWindow(HMONITOR monitor, PCWSTR deviceId) noexcept wil::unique_cotaskmem_string virtualDesktopId; if (SUCCEEDED_LOG(StringFromCLSID(m_currentVirtualDesktopId, &virtualDesktopId))) { - std::wstring uniqueId = ZoneWindowUtils::GenerateUniqueId(deviceId, virtualDesktopId.get()); + std::wstring uniqueId = ZoneWindowUtils::GenerateUniqueId(monitor, deviceId, virtualDesktopId.get()); JSONHelpers::FancyZonesDataInstance().SetActiveDeviceId(uniqueId); const bool newWorkArea = IsNewWorkArea(m_currentVirtualDesktopId, monitor); diff --git a/src/modules/fancyzones/lib/ZoneWindow.cpp b/src/modules/fancyzones/lib/ZoneWindow.cpp index 4ebff171b83f..f9055a701293 100644 --- a/src/modules/fancyzones/lib/ZoneWindow.cpp +++ b/src/modules/fancyzones/lib/ZoneWindow.cpp @@ -63,16 +63,20 @@ namespace ZoneWindowUtils return customZoneSetsTmpFileName; } - std::wstring GenerateUniqueId(PCWSTR deviceId, PCWSTR virtualDesktopId) + std::wstring GenerateUniqueId(HMONITOR monitor, PCWSTR deviceId, PCWSTR virtualDesktopId) { // Format: _ wchar_t uniqueId[256]{}; - if (virtualDesktopId) + MONITORINFOEXW mi; + mi.cbSize = sizeof(mi); + if (virtualDesktopId && GetMonitorInfo(monitor, &mi)) { wchar_t parsedId[256]{}; ParseDeviceId(deviceId, parsedId, 256); - StringCchPrintf(uniqueId, ARRAYSIZE(uniqueId), L"%s_%s", parsedId, virtualDesktopId); + + Rect const monitorRect(mi.rcMonitor); + StringCchPrintf(uniqueId, ARRAYSIZE(uniqueId), L"%s_%d_%d_%s", parsedId, monitorRect.width(), monitorRect.height(), virtualDesktopId); } return std::wstring{ uniqueId }; } diff --git a/src/modules/fancyzones/lib/ZoneWindow.h b/src/modules/fancyzones/lib/ZoneWindow.h index 69dff4b6d2cc..93a195ae6507 100644 --- a/src/modules/fancyzones/lib/ZoneWindow.h +++ b/src/modules/fancyzones/lib/ZoneWindow.h @@ -7,7 +7,7 @@ namespace ZoneWindowUtils const std::wstring& GetActiveZoneSetTmpPath(); const std::wstring& GetAppliedZoneSetTmpPath(); const std::wstring& GetCustomZoneSetsTmpPath(); - std::wstring GenerateUniqueId(PCWSTR deviceId, PCWSTR virtualDesktopId); + std::wstring GenerateUniqueId(HMONITOR monitor, PCWSTR deviceId, PCWSTR virtualDesktopId); } interface __declspec(uuid("{7F017528-8110-4FB3-BE41-F472969C2560}")) IZoneWindow : public IUnknown diff --git a/src/modules/fancyzones/tests/UnitTests/ZoneWindow.Spec.cpp b/src/modules/fancyzones/tests/UnitTests/ZoneWindow.Spec.cpp index 909892b7e6d9..8a5418bedeea 100644 --- a/src/modules/fancyzones/tests/UnitTests/ZoneWindow.Spec.cpp +++ b/src/modules/fancyzones/tests/UnitTests/ZoneWindow.Spec.cpp @@ -173,11 +173,11 @@ namespace FancyZonesUnitTests TEST_METHOD(CreateZoneWindowNoDeviceId) { // Generate unique id without device id - std::wstring uniqueId = ZoneWindowUtils::GenerateUniqueId(nullptr, m_virtualDesktopId.c_str()); + std::wstring uniqueId = ZoneWindowUtils::GenerateUniqueId(m_monitor, nullptr, m_virtualDesktopId.c_str()); m_zoneWindow = MakeZoneWindow(m_hostPtr, m_hInst, m_monitor, uniqueId, false); const std::wstring expectedWorkArea = std::to_wstring(m_monitorInfo.rcMonitor.right) + L"_" + std::to_wstring(m_monitorInfo.rcMonitor.bottom); - const std::wstring expectedUniqueId = L"FallbackDevice_" + m_virtualDesktopId; + const std::wstring expectedUniqueId = L"FallbackDevice_" + std::to_wstring(m_monitorInfo.rcMonitor.right) + L"_" + std::to_wstring(m_monitorInfo.rcMonitor.bottom) + L"_" + m_virtualDesktopId; Assert::IsNotNull(m_zoneWindow.get()); Assert::IsFalse(m_zoneWindow->IsDragEnabled()); @@ -189,9 +189,10 @@ namespace FancyZonesUnitTests TEST_METHOD(CreateZoneWindowNoDesktopId) { // Generate unique id without virtual desktop id - std::wstring uniqueId = ZoneWindowUtils::GenerateUniqueId(m_deviceId.c_str(), nullptr); + std::wstring uniqueId = ZoneWindowUtils::GenerateUniqueId(m_monitor, m_deviceId.c_str(), nullptr); m_zoneWindow = MakeZoneWindow(m_hostPtr, m_hInst, m_monitor, uniqueId, false); + const std::wstring expectedWorkArea = std::to_wstring(m_monitorInfo.rcMonitor.right) + L"_" + std::to_wstring(m_monitorInfo.rcMonitor.bottom); Assert::IsNotNull(m_zoneWindow.get()); Assert::IsFalse(m_zoneWindow->IsDragEnabled()); Assert::IsTrue(m_zoneWindow->UniqueId().empty()); From 0c2c7f38b31f3233c2c7e0bb6002d066727e6f2c Mon Sep 17 00:00:00 2001 From: vldmr11080 z Date: Mon, 17 Feb 2020 21:28:06 +0100 Subject: [PATCH 8/9] Revert "Update comments related to new unique identifier" This reverts commit 8e898025494e5da35bbd59c1d24053b45efd0e51. --- src/modules/fancyzones/lib/JsonHelpers.cpp | 2 +- src/modules/fancyzones/lib/ZoneWindow.cpp | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/src/modules/fancyzones/lib/JsonHelpers.cpp b/src/modules/fancyzones/lib/JsonHelpers.cpp index a01d899352a4..6d7452e0755d 100644 --- a/src/modules/fancyzones/lib/JsonHelpers.cpp +++ b/src/modules/fancyzones/lib/JsonHelpers.cpp @@ -25,7 +25,7 @@ namespace std::wstring ExtractVirtualDesktopId(const std::wstring& deviceId) { - // Format: _ + // Format: __ return deviceId.substr(deviceId.rfind('_') + 1); } } diff --git a/src/modules/fancyzones/lib/ZoneWindow.cpp b/src/modules/fancyzones/lib/ZoneWindow.cpp index f9055a701293..a70ffc5dfa1f 100644 --- a/src/modules/fancyzones/lib/ZoneWindow.cpp +++ b/src/modules/fancyzones/lib/ZoneWindow.cpp @@ -65,8 +65,7 @@ namespace ZoneWindowUtils std::wstring GenerateUniqueId(HMONITOR monitor, PCWSTR deviceId, PCWSTR virtualDesktopId) { - // Format: _ - wchar_t uniqueId[256]{}; + wchar_t uniqueId[256]{}; // Parsed deviceId + resolution + virtualDesktopId MONITORINFOEXW mi; mi.cbSize = sizeof(mi); From dc728d3323aa16dedbf9d6b09f750056552a718b Mon Sep 17 00:00:00 2001 From: Serafima Date: Tue, 18 Feb 2020 13:04:14 +0300 Subject: [PATCH 9/9] removed MigrateDeviceInfoFromRegistry --- src/modules/fancyzones/lib/JsonHelpers.cpp | 27 ---------------------- src/modules/fancyzones/lib/JsonHelpers.h | 2 -- 2 files changed, 29 deletions(-) diff --git a/src/modules/fancyzones/lib/JsonHelpers.cpp b/src/modules/fancyzones/lib/JsonHelpers.cpp index 6d7452e0755d..1c6a8ac8cfec 100644 --- a/src/modules/fancyzones/lib/JsonHelpers.cpp +++ b/src/modules/fancyzones/lib/JsonHelpers.cpp @@ -168,8 +168,6 @@ namespace JSONHelpers { // Creates default entry in map when ZoneWindow is created deviceInfoMap[deviceId] = DeviceInfoData{ ZoneSetData{ L"null", ZoneSetLayoutType::Blank } }; - - MigrateDeviceInfoFromRegistry(deviceId); } } @@ -549,31 +547,6 @@ namespace JSONHelpers } } - void FancyZonesData::MigrateDeviceInfoFromRegistry(const std::wstring& deviceId) - { - wchar_t key[256]; - StringCchPrintf(key, ARRAYSIZE(key), L"%s\\%s", RegistryHelpers::REG_SETTINGS, deviceId.c_str()); - - wchar_t activeZoneSetId[256]; - activeZoneSetId[0] = '\0'; - DWORD bufferSize = sizeof(activeZoneSetId); - DWORD showSpacing = 1; - DWORD spacing = 16; - DWORD zoneCount = 3; - DWORD size = sizeof(DWORD); - - SHRegGetUSValueW(key, L"ActiveZoneSetId", nullptr, &activeZoneSetId, &bufferSize, FALSE, nullptr, 0); - SHRegGetUSValueW(key, L"ShowSpacing", nullptr, &showSpacing, &size, FALSE, nullptr, 0); - SHRegGetUSValueW(key, L"Spacing", nullptr, &spacing, &size, FALSE, nullptr, 0); - SHRegGetUSValueW(key, L"ZoneCount", nullptr, &zoneCount, &size, FALSE, nullptr, 0); - - if (appliedZoneSetsMap.contains(std::wstring{ activeZoneSetId })) - { - deviceInfoMap[deviceId] = DeviceInfoData{ appliedZoneSetsMap.at(std::wstring{ activeZoneSetId }), static_cast(showSpacing), static_cast(spacing), static_cast(zoneCount) }; - SaveFancyZonesData(); - } - } - void FancyZonesData::MigrateCustomZoneSetsFromRegistry() { wchar_t key[256]; diff --git a/src/modules/fancyzones/lib/JsonHelpers.h b/src/modules/fancyzones/lib/JsonHelpers.h index 91e765af5b76..faba4d11dee4 100644 --- a/src/modules/fancyzones/lib/JsonHelpers.h +++ b/src/modules/fancyzones/lib/JsonHelpers.h @@ -222,8 +222,6 @@ namespace JSONHelpers void LoadFancyZonesData(); void SaveFancyZonesData() const; - void MigrateDeviceInfoFromRegistry(const std::wstring& deviceId); - private: void TmpMigrateAppliedZoneSetsFromRegistry(); void MigrateCustomZoneSetsFromRegistry();