panel: Propagate "focus-in" event to browser host #508

Open
PatTheMav wants to merge 1 commit from PatTheMav/browser-focus-fix into master
PatTheMav commented 2025-11-13 10:04:31 -06:00 (Migrated from github.com)

Description

Fixes the issue of a browser window not regaining keyboard input focus when a user switched away from and back to OBS Studio as their active application.

Motivation and Context

The CEF instance is wrapped in another QWindow instance for Windows and Linux and thus consumes any focus events that are propagated through the application.

To ensure that a wrapped CEF instance regains focus after its wrapping window receives focus, the event needs to be explicitly sent to the browser host instance it wraps.

The least-intrusive way to achieve this within the current code architecture is to install the instance of QCefWidgetInternal owning the QWindow to handle all events sent to the window. If the window itself is the target of the event and it's of type FocusIn, the focus state of the browser host window is set explicitly.

In tests this has shown that it allows keyboard input fields on a loaded website to regain input focus.

How Has This Been Tested?

Tested on macOS 26 (which did not require the fix) and Windows 11 (which did).

Types of changes

  • Bug fix (non-breaking change which fixes an issue)

Checklist:

  • My code has been run through clang-format.
  • I have read the contributing document.
  • My code is not on the master branch.
  • The code has been tested.
  • All commit messages are properly formatted and commits squashed where appropriate.
  • I have included updates to all appropriate documentation.
### Description Fixes the issue of a browser window not regaining keyboard input focus when a user switched away from and back to OBS Studio as their active application. ### Motivation and Context The CEF instance is wrapped in another QWindow instance for Windows and Linux and thus consumes any focus events that are propagated through the application. To ensure that a wrapped CEF instance regains focus after its wrapping window receives focus, the event needs to be explicitly sent to the browser host instance it wraps. The least-intrusive way to achieve this within the current code architecture is to install the instance of `QCefWidgetInternal` owning the `QWindow` to handle all events _sent_ to the window. If the window itself is the target of the event and it's of type `FocusIn`, the focus state of the browser host window is set explicitly. In tests this has shown that it allows keyboard input fields on a loaded website to regain input focus. ### How Has This Been Tested? Tested on macOS 26 (which did not require the fix) and Windows 11 (which did). ### Types of changes - Bug fix (non-breaking change which fixes an issue) ### Checklist: - [x] My code has been run through [clang-format](https://github.com/obsproject/obs-studio/blob/master/.clang-format). - [x] I have read the [**contributing** document](https://github.com/obsproject/obs-studio/blob/master/CONTRIBUTING.rst). - [x] My code is not on the master branch. - [x] The code has been tested. - [x] All commit messages are properly formatted and commits squashed where appropriate. - [x] I have included updates to all appropriate documentation.
WizardCM (Migrated from github.com) requested changes 2025-11-26 02:43:49 -06:00
WizardCM (Migrated from github.com) left a comment

After some testing, I thought this had provided an improvement, however with or without this change, the Auth window for Twitch correctly focuses the Username input on initial open, and then does not if the window is unfocused and tabbed back in.

I tested with CEF 136, so it's likely this fix works fine for 127 (I will have to test again later) but has no effect with the Chrome runtime on modern Chromium versions.

After some testing, I thought this had provided an improvement, however with or without this change, the Auth window for Twitch correctly focuses the Username input on initial open, and then does not if the window is unfocused and tabbed back in. I tested with CEF 136, so it's likely this fix works fine for 127 (I will have to test again later) but has no effect with the Chrome runtime on modern Chromium versions.
@ -186,6 +188,34 @@ QCefWidgetInternal::~QCefWidgetInternal()
closeBrowser();
WizardCM (Migrated from github.com) commented 2025-11-26 02:34:59 -06:00
		host->SetFocus(hasFocus());

Most checks in Qt are functions.

```suggestion host->SetFocus(hasFocus()); ``` Most checks in Qt are functions.
WizardCM (Migrated from github.com) commented 2025-11-26 02:35:56 -06:00
	if (object != window || !cefBrowser) {

You may have uncommitted code where you've renamed it, but currently it's called window.

```suggestion if (object != window || !cefBrowser) { ``` You may have uncommitted code where you've renamed it, but currently it's called `window`.
PatTheMav (Migrated from github.com) reviewed 2025-12-15 13:56:15 -06:00
@ -186,6 +188,34 @@ QCefWidgetInternal::~QCefWidgetInternal()
closeBrowser();
PatTheMav (Migrated from github.com) commented 2025-12-15 13:56:14 -06:00

That should actually simply be true, hasFocus used to be a boolean passed into an explicit setFocus method I moved this code from. So I guess this resolves to a non-zero function pointer and thus implicitly a "truthy" value in this form.

Implicit type conversions are evil like that..

That should actually simply be `true`, `hasFocus` used to be a boolean passed into an explicit `setFocus` method I moved this code from. So I guess this resolves to a non-zero function pointer and thus implicitly a "truthy" value in this form. Implicit type conversions are evil like that..
PatTheMav commented 2026-01-23 11:22:44 -06:00 (Migrated from github.com)

Pushed an update to resolve the compilation errors. I've also did another round of testing just now and I'm not able to reproduce the focus change not happening. Indeed the only way for it to not work is to explicitly click outside of the input field to make it loose focus and then deactivating/activating OBS which of course will not visibly show a focus change anymore.

OBS does ship with CEF 127 right now, so that's what the fix will target. Fixing this for CEF 136 is indeed something that any update to CEF 136 itself needs to address (just like all the other changes).

Pushed an update to resolve the compilation errors. I've also did another round of testing just now and I'm not able to reproduce the focus change _not_ happening. Indeed the only way for it to not work is to explicitly click outside of the input field to make it loose focus and then deactivating/activating OBS which of course will not visibly show a focus change anymore. OBS does ship with CEF 127 right now, so that's what the fix will target. Fixing this for CEF 136 is indeed something that any update to CEF 136 itself needs to address (just like all the other changes).
This pull request is broken due to missing fork information.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'PatTheMav/browser-focus-fix:PatTheMav/browser-focus-fix'
git switch 'PatTheMav/browser-focus-fix'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'master'
git merge --no-ff 'PatTheMav/browser-focus-fix'
git switch 'PatTheMav/browser-focus-fix'
git rebase 'master'
git switch 'master'
git merge --ff-only 'PatTheMav/browser-focus-fix'
git switch 'PatTheMav/browser-focus-fix'
git rebase 'master'
git switch 'master'
git merge --no-ff 'PatTheMav/browser-focus-fix'
git switch 'master'
git merge --squash 'PatTheMav/browser-focus-fix'
git switch 'master'
git merge --ff-only 'PatTheMav/browser-focus-fix'
git switch 'master'
git merge 'PatTheMav/browser-focus-fix'
git push origin 'master'
Sign in to join this conversation.
No reviewers
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
gh-obsproject/obs-browser!508
No description provided.