mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-04-05 02:36:19 +02:00
[FancyZones] Update primary desktop data on virtual desktop switch (#7994)
* Update primary desktop data after virtual desktop switch * Don't remove zeroed-GUID inside RemoveDeletedDesktops method * Minor refactoring in VirtualDesktopUtils * Use std::vector::empty check instead of comparing size with 0
This commit is contained in:
@@ -8,6 +8,7 @@ namespace NonLocalizable
|
||||
const wchar_t RegCurrentVirtualDesktop[] = L"CurrentVirtualDesktop";
|
||||
const wchar_t RegVirtualDesktopIds[] = L"VirtualDesktopIDs";
|
||||
const wchar_t RegKeyVirtualDesktops[] = L"Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\VirtualDesktops";
|
||||
const wchar_t RegKeyVirtualDesktopsFromSession[] = L"Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\SessionInfo\\%d\\VirtualDesktops";
|
||||
}
|
||||
|
||||
namespace VirtualDesktopUtils
|
||||
@@ -53,20 +54,21 @@ namespace VirtualDesktopUtils
|
||||
bool GetDesktopIdFromCurrentSession(GUID* desktopId)
|
||||
{
|
||||
DWORD sessionId;
|
||||
ProcessIdToSessionId(GetCurrentProcessId(), &sessionId);
|
||||
if (!ProcessIdToSessionId(GetCurrentProcessId(), &sessionId))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
wchar_t sessionKeyPath[256]{};
|
||||
RETURN_IF_FAILED(
|
||||
StringCchPrintfW(
|
||||
sessionKeyPath,
|
||||
ARRAYSIZE(sessionKeyPath),
|
||||
L"Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\SessionInfo\\%d\\VirtualDesktops",
|
||||
sessionId));
|
||||
if (FAILED(StringCchPrintfW(sessionKeyPath, ARRAYSIZE(sessionKeyPath), NonLocalizable::RegKeyVirtualDesktopsFromSession, sessionId)))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
wil::unique_hkey key{};
|
||||
GUID value{};
|
||||
if (RegOpenKeyExW(HKEY_CURRENT_USER, sessionKeyPath, 0, KEY_ALL_ACCESS, &key) == ERROR_SUCCESS)
|
||||
{
|
||||
GUID value{};
|
||||
DWORD size = sizeof(GUID);
|
||||
if (RegQueryValueExW(key.get(), NonLocalizable::RegCurrentVirtualDesktop, 0, nullptr, reinterpret_cast<BYTE*>(&value), &size) == ERROR_SUCCESS)
|
||||
{
|
||||
@@ -86,17 +88,10 @@ namespace VirtualDesktopUtils
|
||||
{
|
||||
return true;
|
||||
}
|
||||
// First fallback scenario is to try obtaining virtual desktop id through IVirtualDesktopManager
|
||||
// interface. Use foreground window (the window with which the user is currently working) to determine
|
||||
// current virtual desktop.
|
||||
else if (GetWindowDesktopId(GetForegroundWindow(), desktopId))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
// Second fallback scenario is to get array of virtual desktops stored in registry, but not kept per
|
||||
// session. Note that we are taking first element from virtual desktop array, which is primary desktop.
|
||||
// If user has more than one virtual desktop, one of previous functions should return correct value,
|
||||
// as desktop switch occurred in current session.
|
||||
// Fallback scenario is to get array of virtual desktops stored in registry, but not kept per session.
|
||||
// Note that we are taking first element from virtual desktop array, which is primary desktop.
|
||||
// If user has more than one virtual desktop, previous function should return correct value, as desktop
|
||||
// switch occurred in current session.
|
||||
else
|
||||
{
|
||||
std::vector<GUID> ids{};
|
||||
|
||||
Reference in New Issue
Block a user