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 from webapp does not trigger the focus event of the BrowserWindow #25578

Open
3 tasks done
pushkin- opened this issue Sep 22, 2020 · 15 comments
Open
3 tasks done
Assignees
Labels
9-x-y 10-x-y 11-x-y 12-x-y bug 🪲 component/BrowserWindow has-repro-gist Issue can be reproduced with code at https://gist.github.com/ status/confirmed A maintainer reproduced the bug or agreed with the feature

Comments

@pushkin-
Copy link

Preflight Checklist

  • I have read the Contributing Guidelines for this project.
  • I agree to follow the Code of Conduct that this project adheres to.
  • I have searched the issue tracker for an issue that matches the one I want to file, without success.

Issue Details

  • Electron Version: 10.1.2, 9.0.4, ... 4.0.4
  • Operating System: Windows 10 (v 1909)

Expected Behavior

When I open a window in the webapp that's in my BrowserWindow, I expect that calling win.focus() focuses the BrowserWindow

Actual Behavior

w.focus() doesn't do anything and doesn't emit the focus event

To Reproduce

This is basically this issue which was closed. The part of the issue about win.close() not working appears to have been fixed, so I'm opening a new issue for the focus issue.

  1. Start the app below
  2. click "open window"
  3. click "focus window"
  4. observe that the window doesn't get focused

If you run the jsfiddle in Chrome, the focus button will put focus in the window.

Notice the "focus" event of the window doesn't fire.

(async () => {
  const {app, BrowserWindow, inAppPurchase} = require('electron');
  await app.whenReady();
  const mainWindow = new BrowserWindow({webPreferences: {nativeWindowOpen: true}});
  await mainWindow.loadURL("https://jsfiddle.net/ishats/0shxqkpw/2/");

  app.on("browser-window-created", (e, win) => {
    win.on("focus", () => {
      console.log("doesn't get hit");
    });
  });
})();

@nornagon
Copy link
Member

nornagon commented May 5, 2021

@nornagon nornagon added has-repro-gist Issue can be reproduced with code at https://gist.github.com/ status/confirmed A maintainer reproduced the bug or agreed with the feature and removed platform/windows labels May 5, 2021
@CezaryKulakowski
Copy link
Contributor

CezaryKulakowski commented May 11, 2021

When focus is called on DOM's Window Electron is notified with call to content::WebContentsDelegate:::ActivateContents so following change:

--- a/shell/browser/api/electron_api_web_contents.cc
+++ b/shell/browser/api/electron_api_web_contents.cc
@@ -1151,6 +1151,7 @@ void WebContents::CloseContents(content::WebContents* source) {
 }

 void WebContents::ActivateContents(content::WebContents* source) {
+  web_contents()->Focus();
   for (ExtendedWebContentsObserver& observer : observers_)
     observer.OnActivateContents();
 }

fixes the problem. What do you think about it? I'm worried about possible double calls to WebContents::Focus in some scenarios.

@pushkin-
Copy link
Author

pushkin- commented Oct 6, 2022

repros in Electron 21

@github-actions
Copy link
Contributor

This issue has been automatically marked as stale. If this issue is still affecting you, please leave any comment (for example, "bump"), and we'll keep it open. If you have any new additional information—in particular, if this is still reproducible in the latest version of Electron or in the beta—please include it with your comment!

@github-actions github-actions bot added the stale label Jan 10, 2023
@gpetrov
Copy link

gpetrov commented Jan 10, 2023

Still a problem

@github-actions github-actions bot removed the stale label Jan 11, 2023
@github-actions
Copy link
Contributor

This issue has been automatically marked as stale. If this issue is still affecting you, please leave any comment (for example, "bump"), and we'll keep it open. If you have any new additional information—in particular, if this is still reproducible in the latest version of Electron or in the beta—please include it with your comment!

@github-actions github-actions bot added the stale label Apr 12, 2023
@gpetrov
Copy link

gpetrov commented Apr 12, 2023

Bump

@github-actions github-actions bot removed the stale label Apr 13, 2023
@jaime-rivas
Copy link

Same issue here

@bpasero
Copy link
Contributor

bpasero commented Oct 4, 2023

@codebytere @MarshallOfSound @nornagon any update? we would like to focus child windows via basic window.focus() call. Currently I would otherwise have to go an ugly route from the renderer to the main process to find the web contents and call focus there. Is there anything to make this easier?

@codebytere
Copy link
Member

@bpasero i'm trying to repro to little success - do you have a repro?

I tried https://gist.github.com/7b537954b7575085143d70f767ad5644 and the focus event triggered as expected

@bpasero
Copy link
Contributor

bpasero commented Oct 4, 2023

@codebytere thanks for following up, so far I have only seen it in VS Code where a child window (about:blank) does not receive OS window focus when I call childWindow.focus() on it. If this is not a bug per-se then maybe its a BrowserWindow option I need to change to allow this? Was wondering if some kind of popup-blocker would be in the way.

@pushkin-
Copy link
Author

pushkin- commented Oct 4, 2023

@codebytere in my testing of your gist (on Windows), the focus event gets triggered when the window is first opened. But we don't get a second log when your w.focus() code runs.

@bpasero
Copy link
Contributor

bpasero commented Oct 4, 2023

@codebytere reproduces for me with the gist https://gist.github.com/cb0763b1f310a640a360dd40ef0897e6 in Fiddle using Electron 25.x:

Recording 2023-10-04 at 20 06 33

@codebytere codebytere self-assigned this Oct 4, 2023
@codebytere
Copy link
Member

@bpasero figured out what i was missing - will work on this today.

@jaime-rivas
Copy link

Hi @codebytere, please keep us posted for any updates on this issue when you have a chance. Thank you so much!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
9-x-y 10-x-y 11-x-y 12-x-y bug 🪲 component/BrowserWindow has-repro-gist Issue can be reproduced with code at https://gist.github.com/ status/confirmed A maintainer reproduced the bug or agreed with the feature
Projects
None yet
Development

No branches or pull requests

8 participants