Skip to content

[FancyZones] Update primary desktop data on virtual desktop switch#7994

Merged
vldmr11080 merged 4 commits intomicrosoft:masterfrom
vldmr11080:handle-zeroed-guiod-after-vd-switch
Nov 17, 2020
Merged

[FancyZones] Update primary desktop data on virtual desktop switch#7994
vldmr11080 merged 4 commits intomicrosoft:masterfrom
vldmr11080:handle-zeroed-guiod-after-vd-switch

Conversation

@vldmr11080
Copy link
Contributor

@vldmr11080 vldmr11080 commented Nov 11, 2020

Summary of the Pull Request

  1. If user never made a new virtual desktop, there will be no information about virtual desktop id in the registry, and zeroed-GUID will be used. Migrate persisted information for zeroed-GUID in primary desktop when virtual desktop switch occurs.
  2. Don't use IVirtualDesktopManager::GetWindowDesktopId on GetForegroundWindow during initialization. If PowerToys start right away when system starts, GetForegroundWindow might not contain valid HWND, and we could end up with zeroed-GUID.

PR Checklist

Validation Steps Performed

  1. Clear the registry (both Software\Microsoft\Windows\CurrentVersion\Explorer\SessionInfo\\VirtualDesktops and Software\Microsoft\Windows\CurrentVersion\Explorer\VirtualDesktops) and persisted FancyZones settings (C:\Users<user-name>\AppData\Local\Microsoft\PowerToys\FancyZones), reboot the machine.
  2. Start PowerToys and apply either template or custom layout on primary desktop.
  3. Verify that layout information is persisted, and zeroed GUID is used to define primary desktop.
  4. Create new virtual desktop.
  5. Verify that layout information for primary desktop is preserved, but GUID is updated to the real one.

// 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))
Copy link
Contributor Author

@vldmr11080 vldmr11080 Nov 11, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@enricogior @stefansjfw One possible option, that we should consider is when window is created (FancyZones::WindowCreated callback) we do the check if we still have zeroed GUID, and if that is the case we migrate it based on IVirtualDesktopManager::GetWindowDesktopId and HWND of that newly created window. Creation of new window is something that will happen right as system is started, which means we would migrate primary desktop data quite quickly. Example:

// IFancyZonesCallback
IFACEMETHODIMP_(void)
FancyZones::WindowCreated(HWND window) noexcept
{
    auto& fancyZonesData = FancyZonesDataInstance();
    if (!fancyZonesData.HasValidPrimaryDesktopId())
    {
        GUID desktopId{};
        if (VirtualDesktopUtils::GetWindowDesktopId(window, &desktopId))
        {
            fancyZonesData.UpdatePrimaryDesktopData(desktopId);
        }
    }
    ...
}

I'm aware that this method is called quite often, however check HasValidPrimaryDesktopId is fairly simple, and code within if statement will be called only once.

@ghost ghost added FancyZones-VirtualDesktop Issue that deal with FZ and VDs Product-FancyZones Refers to the FancyZones PowerToy labels Nov 11, 2020
@enricogior
Copy link
Contributor

@vldmr11080
can you add

L"Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\SessionInfo\\%d\\VirtualDesktops"

to

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";
}

Copy link
Contributor

@enricogior enricogior left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did some testing and the changes work as expected.

@vldmr11080 vldmr11080 merged commit 9ccd977 into microsoft:master Nov 17, 2020
@vldmr11080 vldmr11080 deleted the handle-zeroed-guiod-after-vd-switch branch November 17, 2020 11:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

FancyZones-VirtualDesktop Issue that deal with FZ and VDs Product-FancyZones Refers to the FancyZones PowerToy

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants