obsidianmd / obsidian-api

Type definitions for the latest Obsidian API.
https://docs.obsidian.md
MIT License
1.65k stars 192 forks source link

Bug: Focusing issues with webviews in multiple windows #130

Closed pjeby closed 9 months ago

pjeby commented 10 months ago

This applies to anything that uses webviews, not just canvases, but here's a canvas repro:

Steps to reproduce:

I'm working around it with this:

                const {win, doc} = this.webview;
                win.electronWindow?.on("blur", doBlur);
                win.electronWindow?.on("focus", doFocus);
                return () => {
                    win.electronWindow?.off("blur", doBlur);
                    win.electronWindow?.off("focus", doFocus);
                }
                function doBlur() { doc.body.removeClass("is-focused"); }
                function doFocus() { doc.body.addClass("is-focused"); }

I'm guessing that the current blur/focus detection is targeting the document instead of the electron window. I also haven't yet confirmed whether activeWindow and activeDocument are tracking correctly here either, but I would guess they have the same issue, likely it would show up if you alt-tab to a window with an active webview, dialogs might open in a different window, etc. (And I had something like that happen at one point, need to work out a repro though.)

It looks like the window-level focus/blur detection (for the rootsplit and popouts) could maybe just switch to using the electron window since they're conditional on Platform.isDesktopApp anyway. (I think, though, that the document focus stuff might also need some work since focusing into a web view appears to lose is-focused on the current window as well.)

lishid commented 9 months ago

I have a patch that addresses this issue in the upcoming release, thanks!