mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-04-06 11:16:51 +02:00
[FancyZones] Reset zone settings after restart fix. (#13322)
* updated virtual desktop retrieving * save with null-guid * moved guid utils * moved DeviceIdData related functions * replaced strings with data structs * default value * clean up * save app zone history with zones settings * compare with null guid * updated tests * refactoring * logs * sync vd ids * logs * refactoring * check virtual desktop id * OnDisplayChange call * compare device ids in editor
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
#include "pch.h"
|
||||
#include "VirtualDesktop.h"
|
||||
|
||||
#include <common/logger/logger.h>
|
||||
|
||||
// Non-Localizable strings
|
||||
namespace NonLocalizable
|
||||
{
|
||||
@@ -17,6 +19,7 @@ IServiceProvider* GetServiceProvider()
|
||||
IServiceProvider* provider{ nullptr };
|
||||
if (FAILED(CoCreateInstance(CLSID_ImmersiveShell, nullptr, CLSCTX_LOCAL_SERVER, __uuidof(provider), (PVOID*)&provider)))
|
||||
{
|
||||
Logger::error("Failed to get ServiceProvider for VirtualDesktopManager");
|
||||
return nullptr;
|
||||
}
|
||||
return provider;
|
||||
@@ -28,6 +31,7 @@ IVirtualDesktopManager* GetVirtualDesktopManager()
|
||||
IServiceProvider* serviceProvider = GetServiceProvider();
|
||||
if (serviceProvider == nullptr || FAILED(serviceProvider->QueryService(__uuidof(manager), &manager)))
|
||||
{
|
||||
Logger::error("Failed to get VirtualDesktopManager");
|
||||
return nullptr;
|
||||
}
|
||||
return manager;
|
||||
@@ -77,14 +81,6 @@ std::optional<GUID> GetDesktopIdFromCurrentSession()
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
bool GetZoneWindowDesktopId(IWorkArea* zoneWindow, GUID* desktopId)
|
||||
{
|
||||
// Format: <device-id>_<resolution>_<virtual-desktop-id>
|
||||
std::wstring uniqueId = zoneWindow->UniqueId();
|
||||
std::wstring virtualDesktopId = uniqueId.substr(uniqueId.rfind('_') + 1);
|
||||
return SUCCEEDED(CLSIDFromString(virtualDesktopId.c_str(), desktopId));
|
||||
}
|
||||
|
||||
HKEY OpenVirtualDesktopsRegKey()
|
||||
{
|
||||
HKEY hKey{ nullptr };
|
||||
@@ -124,18 +120,7 @@ void VirtualDesktop::UnInit()
|
||||
}
|
||||
}
|
||||
|
||||
std::optional<GUID> VirtualDesktop::GetWindowDesktopId(HWND topLevelWindow) const
|
||||
{
|
||||
GUID desktopId{};
|
||||
if (m_vdManager && SUCCEEDED(m_vdManager->GetWindowDesktopId(topLevelWindow, &desktopId)))
|
||||
{
|
||||
return desktopId;
|
||||
}
|
||||
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
std::optional<GUID> VirtualDesktop::GetCurrentVirtualDesktopId() const
|
||||
std::optional<GUID> VirtualDesktop::GetCurrentVirtualDesktopIdFromRegistry() const
|
||||
{
|
||||
// On newer Windows builds, the current virtual desktop is persisted to
|
||||
// a totally different reg key. Look there first.
|
||||
@@ -160,19 +145,17 @@ std::optional<GUID> VirtualDesktop::GetCurrentVirtualDesktopId() const
|
||||
// switch occurred in current session.
|
||||
else
|
||||
{
|
||||
auto ids = GetVirtualDesktopIds();
|
||||
auto ids = GetVirtualDesktopIdsFromRegistry();
|
||||
if (ids.has_value() && ids->size() > 0)
|
||||
{
|
||||
return ids->at(0);
|
||||
}
|
||||
}
|
||||
|
||||
desktopId = GetDesktopIdByTopLevelWindows();
|
||||
|
||||
return desktopId;
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
std::optional<std::vector<GUID>> VirtualDesktop::GetVirtualDesktopIds(HKEY hKey) const
|
||||
std::optional<std::vector<GUID>> VirtualDesktop::GetVirtualDesktopIdsFromRegistry(HKEY hKey) const
|
||||
{
|
||||
if (!hKey)
|
||||
{
|
||||
@@ -205,9 +188,9 @@ std::optional<std::vector<GUID>> VirtualDesktop::GetVirtualDesktopIds(HKEY hKey)
|
||||
return temp;
|
||||
}
|
||||
|
||||
std::optional<std::vector<GUID>> VirtualDesktop::GetVirtualDesktopIds() const
|
||||
std::optional<std::vector<GUID>> VirtualDesktop::GetVirtualDesktopIdsFromRegistry() const
|
||||
{
|
||||
return GetVirtualDesktopIds(GetVirtualDesktopsRegKey());
|
||||
return GetVirtualDesktopIdsFromRegistry(GetVirtualDesktopsRegKey());
|
||||
}
|
||||
|
||||
bool VirtualDesktop::IsWindowOnCurrentDesktop(HWND window) const
|
||||
@@ -232,25 +215,50 @@ std::optional<GUID> VirtualDesktop::GetDesktopId(HWND window) const
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
std::optional<GUID> VirtualDesktop::GetDesktopIdByTopLevelWindows() const
|
||||
std::vector<std::pair<HWND, GUID>> VirtualDesktop::GetWindowsRelatedToDesktops() const
|
||||
{
|
||||
using result_t = std::vector<HWND>;
|
||||
result_t result;
|
||||
result_t windows;
|
||||
|
||||
auto callback = [](HWND window, LPARAM data) -> BOOL {
|
||||
result_t& result = *reinterpret_cast<result_t*>(data);
|
||||
result.push_back(window);
|
||||
return TRUE;
|
||||
};
|
||||
EnumWindows(callback, reinterpret_cast<LPARAM>(&result));
|
||||
EnumWindows(callback, reinterpret_cast<LPARAM>(&windows));
|
||||
|
||||
for (const auto window : result)
|
||||
std::vector<std::pair<HWND, GUID>> result;
|
||||
for (auto window : windows)
|
||||
{
|
||||
auto desktop = GetDesktopId(window);
|
||||
if (desktop.has_value())
|
||||
{
|
||||
result.push_back({ window, *desktop });
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
std::optional<GUID> VirtualDesktop::GetDesktopIdByTopLevelWindows() const
|
||||
{
|
||||
using result_t = std::vector<HWND>;
|
||||
result_t windows;
|
||||
|
||||
auto callback = [](HWND window, LPARAM data) -> BOOL {
|
||||
result_t& result = *reinterpret_cast<result_t*>(data);
|
||||
result.push_back(window);
|
||||
return TRUE;
|
||||
};
|
||||
EnumWindows(callback, reinterpret_cast<LPARAM>(&windows));
|
||||
|
||||
for (const auto window : windows)
|
||||
{
|
||||
std::optional<GUID> id = GetDesktopId(window);
|
||||
if (id.has_value())
|
||||
{
|
||||
// Otherwise keep checking other windows
|
||||
return id;
|
||||
return *id;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user