From 7d71b4b9bab598b4ba38e24ec8cce32811301527 Mon Sep 17 00:00:00 2001 From: Mike Griese Date: Tue, 4 May 2021 16:20:01 -0500 Subject: [PATCH] Only move the window to the current desktop when it isn't on that one already (#10025) ## Summary of the Pull Request This is to mitigate MSFT:33035972. If you call `MoveWindowToDesktop` while an app is set to "Show windows from this app on all desktops", the OS will clear that "Show windows from this app on all desktops" state. But it _won't_ clear that state from the task view, so it'll just plain look broken. We can mitigate this just by checking if we're already on the current desktop first. "Show windows from this app on all desktops" windows will _always_ be on every desktop, so that API will return true, and we can avoid tearing the state. ## References * added in #9954 ## PR Checklist * [x] Closes https://github.com/microsoft/terminal/projects/5#card-60325102 * [x] I work here * [ ] Tests aren't possible * [n/a] Requires documentation to be updated ## Validation Steps Performed * it works again --- src/cascadia/WindowsTerminal/AppHost.cpp | 26 ++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/src/cascadia/WindowsTerminal/AppHost.cpp b/src/cascadia/WindowsTerminal/AppHost.cpp index 5b7ca216f43..aa5f79369df 100644 --- a/src/cascadia/WindowsTerminal/AppHost.cpp +++ b/src/cascadia/WindowsTerminal/AppHost.cpp @@ -781,13 +781,28 @@ void AppHost::_HandleSummon(const winrt::Windows::Foundation::IInspectable& /*se { if (_LazyLoadDesktopManager()) { - GUID currentlyActiveDesktop{ 0 }; - if (VirtualDesktopUtils::GetCurrentVirtualDesktopId(¤tlyActiveDesktop)) + // First thing - make sure that we're not on the current desktop. If + // we are, then don't call MoveWindowToDesktop. This is to mitigate + // MSFT:33035972 + BOOL onCurrentDesktop{ false }; + if (SUCCEEDED(_desktopManager->IsWindowOnCurrentVirtualDesktop(_window->GetHandle(), &onCurrentDesktop)) && onCurrentDesktop) { - LOG_IF_FAILED(_desktopManager->MoveWindowToDesktop(_window->GetHandle(), currentlyActiveDesktop)); + // If we succeeded, and the window was on the current desktop, then do nothing. + } + else + { + // Here, we either failed to check if the window is on the + // current desktop, or it wasn't on that desktop. In both those + // cases, just move the window. + + GUID currentlyActiveDesktop{ 0 }; + if (VirtualDesktopUtils::GetCurrentVirtualDesktopId(¤tlyActiveDesktop)) + { + LOG_IF_FAILED(_desktopManager->MoveWindowToDesktop(_window->GetHandle(), currentlyActiveDesktop)); + } + // If GetCurrentVirtualDesktopId failed, then just leave the window + // where it is. Nothing else to be done :/ } - // If GetCurrentVirtualDesktopId failed, then just leave the window - // where it is. Nothing else to be done :/ } } } @@ -802,7 +817,6 @@ void AppHost::_HandleSummon(const winrt::Windows::Foundation::IInspectable& /*se GUID AppHost::_CurrentDesktopGuid() { GUID currentDesktopGuid{ 0 }; - const auto manager = winrt::create_instance(__uuidof(VirtualDesktopManager)); if (_LazyLoadDesktopManager()) { LOG_IF_FAILED(_desktopManager->GetWindowDesktopId(_window->GetHandle(), ¤tDesktopGuid));