Restore foreground application after desktop switching - #113
Open
timburman wants to merge 4 commits into
Open
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR improves virtual desktop switching behavior on newer Windows 11 builds by restoring foreground application focus after a desktop switch. It also introduces new raw and move-foreground APIs while preserving backward compatibility with the existing API surface.
While using VirtualDesktopAccessor on Windows 11 25H2, I noticed that desktop switching through
IVirtualDesktopManagerInternal::switch_desktop()could leave keyboard focus with the shell instead of an application on the destination desktop. After investigating the behavior and testing possible approaches, this implementation restores focus in a way that closely matches native Explorer desktop switching.Restore foreground application after desktop switching
Problem
On Windows 11 25H2, switching desktops through
IVirtualDesktopManagerInternal::switch_desktop()can leave no application focused on the destination desktop. As a result, keyboard input may remain with the shell until the user manually activates a window.Native Explorer desktop switching restores focus automatically.
Solution
After a successful desktop switch:
IApplicationViewCollectionin Z-order.IApplicationView::set_focus(), while retainingSetForegroundWindow()as a compatibility fallback.Additionally, when switching to the current desktop, the implementation detects whether focus has been left on the shell (
Shell_TrayWnd,WorkerW, orProgman) and restores the foreground application only when necessary.The window-selection heuristics have been isolated into a dedicated helper and documented as experimental so they can be refined independently if future Windows versions expose better APIs.
New raw desktop switch API
Rust
DLL
This exposes the original COM desktop switching behavior without automatic focus restoration for callers that require the underlying semantics.
New move-foreground API
Rust
DLL
This wraps Windows'
switch_desktop_and_move_foreground_view()API, allowing the current foreground window to be moved to another desktop while switching to it.During development I initially investigated replacing
switch_desktop()with this API directly, but testing showed that it has different semantics: it moves the currently focused window between desktops rather than restoring focus on the destination desktop. Exposing it as a separate API preserves both behaviors cleanly.Additional Improvements
switch_desktop_raw()to preserve the original COM behavior.Testing
Tested on:
Validation performed:
The project builds successfully and the existing test suite passes.
I'd appreciate any feedback if there are historical or compatibility considerations that would make a different implementation preferable.