switchToWindow
Makes the given window the active one until the returned DisposableEx is disposed.
Obsidian tracks the focused window in the activeWindow / activeDocument globals and updates them whenever a window takes focus — including when a popout such as the settings window opens. UI it creates for “the current window” reads those globals at creation time and offers no way to name a window: new Notice(...), for instance, is built inside whatever window activeWindow points at, and cannot be moved afterwards. Pointing the globals at the wanted window for exactly that creation, then putting them back, is what this does.
Restore is what makes it safe, so always bind it with using (or dispose it in a finally): leaving the globals pointed at the wrong window would misplace every later window-sensitive Obsidian call.
Import:
import { switchToWindow } from 'obsidian-dev-utils/obsidian/workspace';Example:
using _windowSwitch = switchToWindow(otherWindow);new Notice('This notice belongs to the other window.');Signature:
function switchToWindow(win: Window): DisposableExParameters:
| Parameter | Type | Description |
|---|---|---|
win | Window | The window to make active. |
Returns: DisposableEx — A DisposableEx that restores the previously active window when disposed.
Example:
using _windowSwitch = switchToWindow(otherWindow);new Notice('This notice belongs to the other window.');Links to this page: