#54378: fix: keep focus in a child WebContentsView when its window regains focus
Description of Change
After this change a child WebContentsView that had focus keeps it when its BrowserWindow is reactivated (Alt+Tab back, taskbar click) on Windows and Linux, instead of focus jumping to the window's own page.
On reactivation the views focus manager restores focus to the view the user last clicked into, and BrowserWindow::OnWindowFocus() then focused the window's primary webContents unconditionally, pulling focus out of the child. Apps compensated with win.on('focus', () => view.webContents.focus()), the pattern behind the focus loop in #25429.
OnWindowFocus() now leaves focus alone when the focus manager already has a focused view outside the primary web contents view, and behaves as before otherwise; macOS is untouched. A spec focuses a child view, deactivates and reactivates the window, and expects the child to still be focused: it fails 3/3 on main and passes 3/3 here under Xvfb, and the existing focus specs pass.
Checklist
- I have built and tested this change
- I have filled out the PR description
- I have reviewed and verified the changes
- tests are changed or added
Release Notes
Notes: Fixed a child WebContentsView losing focus when its window is reactivated.
Backports
Semver Impact
Semantic Versioning helps users understand the impact of updates:
- Major (X.y.z): Breaking changes that may require code modifications
- Minor (x.Y.z): New features that maintain backward compatibility
- Patch (x.y.Z): Bug fixes that don't change the API
- None: Changes that don't affect using facing parts of Electron