Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

window.focus() vs system level focus of the window/tab #5493

Open
mustaqahmed opened this issue Apr 28, 2020 · 8 comments
Open

window.focus() vs system level focus of the window/tab #5493

mustaqahmed opened this issue Apr 28, 2020 · 8 comments
Labels
interop Implementations are not interoperable with each other topic: focus

Comments

@mustaqahmed
Copy link
Contributor

Two points in the spec need attention:

  1. If a tab/window does not have the system level focus (i.e. it is not the topmost one), window.focus() is able to give it system-level focus if there is a user activation. Both Chrome and Firefox work similarly here.
    The spec doesn't cover this part: the note here seems to imply that this is a user-agent defined behavior.

  2. In Chrome, window.focus() is not synchronous. This is unlike what the spec says:

User agents must immediately run the focusing steps...

Chrome has a WPT test failure because of the second point in particular, and a fix in the test needs to consider both points above (bug). (I don't know what other browsers do.)

Do we want to clarify the spec? Fixing the test without covering these in the spec does not sound right.

@domenic
Copy link
Member

domenic commented Apr 30, 2020

See also #2711, #2716, #5049. It would be wonderful if someone could help drive this constellation of issues to a conclusion through specs tests (might need to be manual).

My guess at a model that might work is an explicit per-TLBC boolean "has system focus", with the restriction that only one TLBC will ever have the boolean set to true.

(Maybe per BC would be simpler for spec authors, with it automatically false for non-TLBCs? E.g. the sensors spec could say "if Window X's BC has system focus" and have that always be false for nested BCs.)

Then we'd need to define the interaction of that flag with window.focus(), window.blur(), document.hasFocus, document.activeElement, and the underlying spec concepts.

If a tab/window does not have the system level focus (i.e. it is not the topmost one), window.focus() is able to give it system-level focus if there is a user activation.

How does this work? User activation will focus the tab/window anyway, right? Or are you saying that user activation in window1 can cause window2 to become focused?

In Chrome, window.focus() is not synchronous. This is unlike what the spec says:

Sounds like something that would benefit from cross-browser testing. E.g. if Chrome is in the minority, then we should change Chrome; if it's in the majority, then we should change the spec.

Note that this whole area might benefit from multi-OS testing, so it'll be good to stand up test cases at public URLs with easy instructions for how to run them, so that others can chip in to contribute test results. (I can help with Windows!)

@domenic domenic added the interop Implementations are not interoperable with each other label Apr 30, 2020
@annevk
Copy link
Member

annevk commented May 2, 2020

cc @hsivonen

@mustaqahmed
Copy link
Contributor Author

@domenic In Chrome cross-origin focus requests go through the browser process (may be same for same-origin too, not sure). Do we ever want a blocking call that involves two renderers and the browser? I guess no.

@annevk
Copy link
Member

annevk commented May 5, 2020

We don't, but we should define an API contract that does not depend on process isolation decisions made in individual implementations. So either it always queues a task, or it only queues a task cross-origin/-domain/site, or ...?

@hsivonen
Copy link
Member

hsivonen commented May 5, 2020

I can't opine on this without writing test cases. The problem is I'm not even sure what the full spectrum of issues is to know what set of test cases I should write.

If someone already has test cases, please let me know.

@mustaqahmed
Copy link
Contributor Author

One challenge in testing is that we don't have an API to know if a tab has system level focus.

@domenic
Copy link
Member

domenic commented May 6, 2020

Right, as I said above, manual tests might be necessary.

@rniwa
Copy link

rniwa commented Aug 12, 2020

See also #2711, #2716, #5049. It would be wonderful if someone could help drive this constellation of issues to a conclusion through specs tests (might need to be manual).

My guess at a model that might work is an explicit per-TLBC boolean "has system focus", with the restriction that only one TLBC will ever have the boolean set to true.

I'd caution against putting such an artificial restriction based on the behaviors of existing popular operating systems. It's possible that a future platform would support having multiple windows in the system to be in simultaneously in focus.

domenic pushed a commit that referenced this issue Dec 11, 2020
This is reflected by document.hasFocus(), and blur/focus events.

Fixes #5049. Fixes #2716. Related to #5493.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
interop Implementations are not interoperable with each other topic: focus
Development

No branches or pull requests

5 participants