glfw
glfw copied to clipboard
Implement focus callback for browser
Modern browsers implement the Page Visibility API which emits an event visibilitychange
when a browser page is either hidden or made visible. The current visibility of a page can be found via document.hidden
.
This can be used to implement the focus callback in the browser:
document.addEventListener('visibilitychange', (e) => focusCB(!document.hidden));
This is a cool idea, thanks for the issue and PR @20zinnm!
I want to warn you right away, I'm busy this week and won't be able to give it my full attention until next weekend or so. But I will come back to this.
I have one quick thought so far. As you say, the Page Visibility API emits those events when a page is hidden or made visible. But I don't think that corresponds to GLFW's Focus callback directly. The Focus callback is meant to trigger whenever the window is selected. The window might gain/lose focus while always being visible.
There is some overlap, of course. It might be possible to combine Page Visibility API with additional events to detect when the user has actually selected the window. Let me know what you think about this.
Thank you for the prompt response @shurcooL -- I was worried this project was falling by the wayside. I was not familiar with that particular functionality of GLFW, so I apologize for the misunderstanding. If it deals only with selection this is even easier to implement using an event listener for focus
and blur
on the canvas which is implemented in every major browser.