blob: f4dc796b573ad048047490c70335cc1e5385bc25 [file] [log] [blame] [view]
David Bienvenu39104d52020-09-11 21:50:421# Windows 10 Virtual Desktop support
2
3Windows 10 introduced Virtual Desktop support. Virtual Desktops are similar to
4Chrome OS and Mac workspaces. A virtual desktop is a collection of windows.
5Every window belongs to a virtual desktop. When a virtual desktop is selected
6to be active, the windows associated with that virtual desktop are displayed on
7the screen. When a virtual desktop is hidden, all of its windows are also
8hidden. This enables the user to create multiple working environments and to
9switch between them. An app (e.g., Chromium) can have windows open on
10different virtual desktops, and thus may need to be Virtual Desktop-aware.
11
12The user-facing Chromium support for virtual desktops consists of two things:
13
14 * When launching the browser with session restore, browser windows are moved
15 to the virtual desktop they were on when the browser shutdown.
16 * When opening a URL with the browser, either open it in a window on the
17 current virtual desktop, or open a new window on the current virtual desktop.
18 Don't open it in a tab in a window on another virtual desktop.
19
20The core UI principles are that windows should be restored to the desktop they
21were shut down on, and opening an app window shouldn't change the current
22virtual desktop. Only the user should be able to change virtual desktops, or
23move windows between virtual desktops.
24
25Windows 10 exposes the COM interface
26[IVirtualDesktopManager](https://docs.microsoft.com/en-us/windows/win32/api/shobjidl_core/nn-shobjidl_core-ivirtualdesktopmanager)
27to access the Virtual Desktop functionality. To make sure that opening a URL
28stays on the current virtual desktop,
John Palmer046f9872021-05-24 01:24:5629[BrowserView::IsOnCurrentWorkspace](https://source.chromium.org/chromium/chromium/src/+/main:chrome/browser/ui/views/frame/browser_view.cc?q=%20BrowserView::IsOnCurrentWorkspace)
David Bienvenu39104d52020-09-11 21:50:4230uses the IVirtualDesktopManager method
31[IsWindowOnCurrentVirtualDesktop](https://docs.microsoft.com/en-us/windows/win32/api/shobjidl_core/nf-shobjidl_core-ivirtualdesktopmanager-iswindowoncurrentvirtualdesktop).
John Palmer046f9872021-05-24 01:24:5632[BrowserMatches](https://source.chromium.org/chromium/chromium/src/+/main:chrome/browser/ui/browser_finder.cc?q=BrowserMatches)
David Bienvenu39104d52020-09-11 21:50:4233in browser_finder.cc only returns a browser window on the current desktop.
34
35To restore browser windows to the desktop they were last open on,
John Palmer046f9872021-05-24 01:24:5636[BrowserDesktopWindowTreeHostWin](https://source.chromium.org/chromium/chromium/src/+/main:chrome/browser/ui/views/frame/browser_desktop_window_tree_host_win.cc)
David Bienvenu39104d52020-09-11 21:50:4237implements GetWorkspace by using the
38[GetWindowDesktopId](https://docs.microsoft.com/en-us/windows/win32/api/shobjidl_core/nf-shobjidl_core-ivirtualdesktopmanager-getwindowdesktopid) method on
39IVirtualDesktopManager, and restores the workspace using
40[MoveWindowToDesktop](https://docs.microsoft.com/en-us/windows/win32/api/shobjidl_core/nf-shobjidl_core-ivirtualdesktopmanager-movewindowtodesktop),
41in its ::Init method.
42
43The actual implementation is a bit more complicated in order to avoid
44calling COM methods on the UI thread, or destroying COM objects on the UI
45thread, since doing so can cause nested message loops and re-entrant calls,
46leading to blocked UI threads and crashes. The
John Palmer046f9872021-05-24 01:24:5647[VirtualDesktopHelper](https://source.chromium.org/chromium/chromium/src/+/main:chrome/browser/ui/views/frame/browser_desktop_window_tree_host_win.cc?q=VirtualDesktopHelper&sq=&ss=chromium%2Fchromium%2Fsrc)
David Bienvenu39104d52020-09-11 21:50:4248class does the workspace handling for BrowserDesktopWindowTreeHostWin, including
49doing all the COM operations on a separate COM task runner. The GetWorkspace
50method is synchronous so VirtualDesktopHelper has to remember and return the
51most recent virtual desktop. Windows has no notification of a window changing
52virtual desktops, so BrowserDesktopWindowTreeHostWin updates the virtual desktop
53of a window whenever it gets focus, and if it has changed, calls
54WindowTreeHost::OnHostWorkspaceChanged. This means that if a window is moved
55to a different virtual desktop, but doesn't get focus before the browser is shut
56down, the browser window will be restored to the previous virtual desktop.
57
58Windows on different virtual desktops share the same coordinate system, so code
59that iterates over windows generally needs to be Virtual Desktop-aware.
60For example, the following places in code ignore windows not on the current
61virtual desktop:
62
John Palmer046f9872021-05-24 01:24:5663 * [LocalProcessWindowFinder::GetProcessWindowAtPoint](https://source.chromium.org/chromium/chromium/src/+/main:ui/display/win/local_process_window_finder_win.cc?q=LocalProcessWindowFinder::ShouldStopIterating&ss=chromium%2Fchromium%2Fsrc)
David Bienvenu39104d52020-09-11 21:50:4264 * third_party/webrtc/modules/desktop_capture/win/window_capture_utils.cc
John Palmer046f9872021-05-24 01:24:5665 * [Native Window occlusion tracker](https://source.chromium.org/chromium/chromium/src/+/main:ui/aura/native_window_occlusion_tracker_win.cc?q=WindowCanOccludeOtherWindowsOnCurrentVirtualDesktop&ss=chromium%2Fchromium%2Fsrc),
David Bienvenu39104d52020-09-11 21:50:4266 when determining if a Chromium window is occluded/covered by other windows.
67 Windows not on the current virtual desktop are considered occluded.
68
69