Steps to reproduce
- Create a popout window
- Focus a different, non-Obsidian app window, while keeping the popout visible
- Close the popout
Did you follow the troubleshooting guide? [Y/N]
Y
Expected result
Focus should return to the non-Obsidian window, as it was the last focused window at the OS level
Actual result
Main window for the vault is surfaced and focused, even if it was minimized
Environment
SYSTEM INFO:
Obsidian version: v1.10.6
Installer version: v1.10.6
Operating system: Windows 10 Pro 10.0.19045
Login status: logged in
Language: en
Catalyst license: supporter
Insider build toggle: off
Live preview: on
Base theme: light
Community theme: none
Snippets enabled: 0
Restricted mode: on
RECOMMENDATIONS:
none
Additional information
It appears the issue is that during the updateLayout() after closing a window, Obsidian detects the fact the active leaf is no longer attached and attempts to remedy this by finding a leaf to activate and focusing it. (And always finds the most recent tab in the main window, because the window close operation resets the activeWindow to the main window.) This interferes with the normal OS behavior of returning focus to the previously-focused window when a window is closed.
This can probably be fixed by having the window close() method simply do a setActiveLeaf(getMostRecentLeaf()) without setting focus, so that the normal process of refocusing the tab when an OS-level focusing occurs will do the focusing at the appropriate time. Here’s some code I tried in the debugger to see if the approach would work:
function fixFocus() { app.workspace.setActiveLeaf(app.workspace.getMostRecentLeaf()) }
app.workspace.on("window-close", fixFocus)
With this code in place, Obsidian doesn’t steal focus, and appears to otherwise behave normally. Without it (or if disabled with app.workspace.off("window-close", fixFocus)) the focus-stealing behavior returns.
(It’s not clear to me if on other OSes normal focus behavior is to stay with the same app on window close, but even so, this workaround shouldn’t interfere with that, as in that case the OS will focus the appropriate window right away, and then the active leaf will be switched and/or focused as needed.)