From f5381ae3e3d48cd2b9cd2d91c5b2a5967fbdaf04 Mon Sep 17 00:00:00 2001 From: vldmr11080 <57061786+vldmr11080@users.noreply.github.com> Date: Tue, 5 May 2020 10:13:50 +0200 Subject: [PATCH] [FancyZones] Clean up resources taken by closed virtual desktops on module startup (#2616) * Clean up resources taken by closed virtual desktops on FancyZones module startup. * Overload GetVirtualDesktopIds to return wide string. * Refactor OnDisplayChange method. --- src/modules/fancyzones/lib/FancyZones.cpp | 15 ++++++----- src/modules/fancyzones/lib/JsonHelpers.cpp | 20 ++++++++++++++ src/modules/fancyzones/lib/JsonHelpers.h | 1 + .../fancyzones/lib/VirtualDesktopUtils.cpp | 26 ++++++++++++++++--- .../fancyzones/lib/VirtualDesktopUtils.h | 3 ++- 5 files changed, 54 insertions(+), 11 deletions(-) diff --git a/src/modules/fancyzones/lib/FancyZones.cpp b/src/modules/fancyzones/lib/FancyZones.cpp index 12095df566..d38a691e4c 100644 --- a/src/modules/fancyzones/lib/FancyZones.cpp +++ b/src/modules/fancyzones/lib/FancyZones.cpp @@ -568,7 +568,7 @@ LRESULT FancyZones::WndProc(HWND window, UINT message, WPARAM wparam, LPARAM lpa else if (message == WM_PRIV_VD_UPDATE) { std::vector ids{}; - if (VirtualDesktopUtils::GetVirtualDekstopIds(ids)) + if (VirtualDesktopUtils::GetVirtualDesktopIds(ids)) { RegisterVirtualDesktopUpdates(ids); } @@ -597,6 +597,7 @@ LRESULT FancyZones::WndProc(HWND window, UINT message, WPARAM wparam, LPARAM lpa return 0; } + void FancyZones::OnDisplayChange(DisplayChangeType changeType) noexcept { if (changeType == DisplayChangeType::VirtualDesktop || @@ -605,13 +606,15 @@ void FancyZones::OnDisplayChange(DisplayChangeType changeType) noexcept GUID currentVirtualDesktopId{}; if (VirtualDesktopUtils::GetCurrentVirtualDesktopId(¤tVirtualDesktopId)) { - std::unique_lock writeLock(m_lock); m_currentVirtualDesktopId = currentVirtualDesktopId; - wil::unique_cotaskmem_string id; - if (changeType == DisplayChangeType::Initialization && - SUCCEEDED_LOG(StringFromCLSID(m_currentVirtualDesktopId, &id))) + } + if (changeType == DisplayChangeType::Initialization) + { + std::vector ids{}; + if (VirtualDesktopUtils::GetVirtualDesktopIds(ids) && !ids.empty()) { - JSONHelpers::FancyZonesDataInstance().UpdatePrimaryDesktopData(id.get()); + JSONHelpers::FancyZonesDataInstance().UpdatePrimaryDesktopData(ids[0]); + JSONHelpers::FancyZonesDataInstance().RemoveDeletedDesktops(ids); } } } diff --git a/src/modules/fancyzones/lib/JsonHelpers.cpp b/src/modules/fancyzones/lib/JsonHelpers.cpp index abcb083258..6ad1eef413 100644 --- a/src/modules/fancyzones/lib/JsonHelpers.cpp +++ b/src/modules/fancyzones/lib/JsonHelpers.cpp @@ -10,6 +10,7 @@ #include #include #include +#include namespace { @@ -348,6 +349,25 @@ namespace JSONHelpers SaveFancyZonesData(); } + void FancyZonesData::RemoveDeletedDesktops(const std::vector& activeDesktops) + { + std::unordered_set active(std::begin(activeDesktops), std::end(activeDesktops)); + std::scoped_lock lock{ dataLock }; + for (auto it = std::begin(deviceInfoMap); it != std::end(deviceInfoMap);) + { + auto foundId = active.find(ExtractVirtualDesktopId(it->first)); + if (foundId == std::end(active)) + { + it = deviceInfoMap.erase(it); + } + else + { + ++it; + } + } + SaveFancyZonesData(); + } + int FancyZonesData::GetAppLastZoneIndex(HWND window, const std::wstring_view& deviceId, const std::wstring_view& zoneSetId) const { std::scoped_lock lock{ dataLock }; diff --git a/src/modules/fancyzones/lib/JsonHelpers.h b/src/modules/fancyzones/lib/JsonHelpers.h index ebdfbc352e..9a10bc9f0c 100644 --- a/src/modules/fancyzones/lib/JsonHelpers.h +++ b/src/modules/fancyzones/lib/JsonHelpers.h @@ -234,6 +234,7 @@ namespace JSONHelpers bool RemoveDevicesByVirtualDesktopId(const std::wstring& virtualDesktopId); void CloneDeviceInfo(const std::wstring& source, const std::wstring& destination); void UpdatePrimaryDesktopData(const std::wstring& desktopId); + void RemoveDeletedDesktops(const std::vector& activeDesktops); int GetAppLastZoneIndex(HWND window, const std::wstring_view& deviceId, const std::wstring_view& zoneSetId) const; bool RemoveAppLastZone(HWND window, const std::wstring_view& deviceId, const std::wstring_view& zoneSetId); diff --git a/src/modules/fancyzones/lib/VirtualDesktopUtils.cpp b/src/modules/fancyzones/lib/VirtualDesktopUtils.cpp index 2c7427cf17..c060dd47ca 100644 --- a/src/modules/fancyzones/lib/VirtualDesktopUtils.cpp +++ b/src/modules/fancyzones/lib/VirtualDesktopUtils.cpp @@ -87,7 +87,7 @@ namespace VirtualDesktopUtils // desktops (only primary desktop) in this session value in registry will be empty. // If this value is empty take first element from array of virtual desktops (not kept per session). std::vector ids{}; - if (!GetVirtualDekstopIds(ids) || ids.empty()) + if (!GetVirtualDesktopIds(ids) || ids.empty()) { return false; } @@ -96,7 +96,7 @@ namespace VirtualDesktopUtils return true; } - bool GetVirtualDekstopIds(HKEY hKey, std::vector& ids) + bool GetVirtualDesktopIds(HKEY hKey, std::vector& ids) { if (!hKey) { @@ -126,9 +126,27 @@ namespace VirtualDesktopUtils return true; } - bool GetVirtualDekstopIds(std::vector& ids) + bool GetVirtualDesktopIds(std::vector& ids) { - return GetVirtualDekstopIds(GetVirtualDesktopsRegKey(), ids); + return GetVirtualDesktopIds(GetVirtualDesktopsRegKey(), ids); + } + + bool GetVirtualDesktopIds(std::vector& ids) + { + std::vector guids{}; + if (GetVirtualDesktopIds(guids)) + { + for (auto& guid : guids) + { + wil::unique_cotaskmem_string guidString; + if (SUCCEEDED(StringFromCLSID(guid, &guidString))) + { + ids.push_back(guidString.get()); + } + } + return true; + } + return false; } HKEY OpenVirtualDesktopsRegKey() diff --git a/src/modules/fancyzones/lib/VirtualDesktopUtils.h b/src/modules/fancyzones/lib/VirtualDesktopUtils.h index 37f0a5c869..5bf5c1778c 100644 --- a/src/modules/fancyzones/lib/VirtualDesktopUtils.h +++ b/src/modules/fancyzones/lib/VirtualDesktopUtils.h @@ -7,7 +7,8 @@ namespace VirtualDesktopUtils bool GetWindowDesktopId(HWND topLevelWindow, GUID* desktopId); bool GetZoneWindowDesktopId(IZoneWindow* zoneWindow, GUID* desktopId); bool GetCurrentVirtualDesktopId(GUID* desktopId); - bool GetVirtualDekstopIds(std::vector& ids); + bool GetVirtualDesktopIds(std::vector& ids); + bool GetVirtualDesktopIds(std::vector& ids); HKEY GetVirtualDesktopsRegKey(); void HandleVirtualDesktopUpdates(HWND window, UINT message, HANDLE terminateEvent); }