electron / electron

:electron: Build cross-platform desktop apps with JavaScript, HTML, and CSS
https://electronjs.org
MIT License
114.13k stars 15.39k forks source link

Infinite focus loop when focusing a window with a BrowserView whose focus handler focuses the BV when there are other such windows open #25429

Open pushkin- opened 4 years ago

pushkin- commented 4 years ago

Preflight Checklist

Issue Details

Actual Behavior

closing out of devtools triggers infinite focus events for a secondary window when the window and the main window have focus event handlers that focus their respective browser view web contents

Expected Behavior

I expect this not to happen.

Closing the detached devtools (has to be detached) puts focus in the main window which triggers the main window's focus handler which focuses the main window's browser view's webcontents. For some reason, doing that causes the focus event of the secondary window to fire, and we get left in a focus loop.

To Reproduce

(async () => {
  const {app, BrowserWindow, BrowserView} = require("electron");

  await app.whenReady()

  const [window, browserView] = await setUpBrowserViewAndWindow();

  window.on("focus", () => {
    browserView.webContents.focus();
    // closing out of the devtools activates the main window which triggers this code, which seems to cause the secondary window's focus event to fire forever
  });

  browserView.webContents.openDevTools({ mode: "detach" });

  browserView.webContents.on("new-window", async (e, url) => {
    e.preventDefault();
    const [win, bv] = await setUpBrowserViewAndWindow();
    e.newGuest = win;

    win.on("focus", () => {
      bv.webContents.focus();
    });
  });

  async function setUpBrowserViewAndWindow() {
    const window = new BrowserWindow();
    const browserView = new BrowserView();

    window.setBrowserView(browserView);
    const contentBounds = window.getContentBounds();
    browserView.setBounds({ x: 0, y: 0, width: contentBounds.width, height: contentBounds.height });

    await browserView.webContents.loadURL("https://google.com");

    return [window, browserView];
  }
})();

Screenshots

electron_focus_issue

Additional Information

It is important to close out of the devtools the normal way: click the X in the devtools window.

This isn't reproducible if you close out of devtools via the taskbar (hover over electron icon, find devtools window image, click X)

pushkin- commented 4 years ago

doing the bv.webContents.focus() operation on a setTimeout(..., 0) works around the issue

pushkin- commented 3 years ago

This affects 11.2.0 and is reproducible without the devtools workflow:

  1. start main.js
  2. click "Open window with BrowserView"
  3. click "Open window"
  4. Focus the BrowserView window
  5. Close the child window
  6. focus switches between the windows forever.

The window close is triggering a focus (explicitly) and that seems to trigger the other window's focus for some reason.

main.js:

const { join } = require("path");
const { app, BrowserWindow, BrowserView } = require('electron')

async function createWindow() {
    const mainWindow = new BrowserWindow();
    const view = new BrowserView({ webPreferences: { nativeWindowOpen: true }});
    mainWindow.setBrowserView(view);

    const bounds = mainWindow.getContentBounds();
    view.setBounds({x: 0, y: 0, width: bounds.width, height: bounds.height});

    mainWindow.on("focus", () => {
        view.webContents.focus();
    });

    await view.webContents.loadFile(join(__dirname, "index.html"));

    view.webContents.on("new-window", (e, url, frameName, disp, opts) => {
        if (frameName === "browserView") {
            e.preventDefault();
            const win = new BrowserWindow(opts);
            e.newGuest = win;
            const bv = new BrowserView();
            win.setBrowserView(bv);

            win.on("focus", () => {
                bv.webContents.focus();
            });

            bv.webContents.loadURL(url);
        }
    });

    app.on("browser-window-created", (e, window) => {
        window.on("closed", () => {
            mainWindow.focus();
        });
    });
}

app.on('ready', createWindow)

index.html:

<!DOCTYPE html>
<html>
  <body>
    <button id="b">Open window with BrowserView</button>
    <button id="b2">Open window</button>

    <script src="./renderer.js"></script>
  </body>
</html>

renderer.js:

document.getElementById("b").onclick = () => {
    window.open("https://google.com", "browserView");
};

document.getElementById("b2").onclick = () => {
    window.open("https://google.com");
};
pushkin- commented 3 years ago

Please make sure that the workflow in this dupe issue is also fixed.

github-actions[bot] commented 2 years ago

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!

pushkin- commented 2 years ago

repros in Electron 21

github-actions[bot] commented 1 year ago

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!

pushkin- commented 1 year ago

repros with v22 gist

click "Open window with BrowserView" click "Open window" Focus the BrowserView window Close the child window focus switches between the windows forever.

github-actions[bot] commented 1 year ago

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!

pushkin- commented 1 year ago

bump

github-actions[bot] commented 1 year ago

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!

pushkin- commented 1 year ago

bump

electron-issue-triage[bot] commented 1 year ago

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!

pushkin- commented 1 year ago

repros with v27

electron-issue-triage[bot] commented 9 months ago

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!

pushkin- commented 9 months ago

repros w v29 alpha 8

electron-issue-triage[bot] commented 6 months ago

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!

pushkin- commented 6 months ago

bump

electron-issue-triage[bot] commented 3 months ago

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!

pushkin- commented 3 months ago

bump

electron-issue-triage[bot] commented 3 days ago

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!

pushkin- commented 3 days ago

bump