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

BrowserWindow.isFocused() can return true when it's not (Windows only) #20464

Open
3 tasks done
Nantris opened this issue Oct 7, 2019 · 32 comments
Open
3 tasks done

BrowserWindow.isFocused() can return true when it's not (Windows only) #20464

Nantris opened this issue Oct 7, 2019 · 32 comments

Comments

@Nantris
Copy link
Contributor

Nantris commented Oct 7, 2019

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: 6.0.11
  • Operating System: Windows 7 & Windows 10 (1803)
  • Last Known Working Electron version: Unknown

Expected Behavior

Window should be focused and allow typing

Actual Behavior

Window reports being focused, but it's not.

Strangely, if I call the exact same method I used to create the demo video below via a global hotkey, the window is properly focused.

Another indication that focus truly isn't being transferred to the window is that Windows Aero darkens the glass effect on the focused window, and whatever previous window was focused does not lighten as it should when losing focus.

To Reproduce

Unknown. I have other windows in my application that work perfectly using the same, very basic:

win.show();

I also tried

win.showInactive();
win.focus();

Screenshots

https://recordit.co/ydntE6o9dt

You can see in the video the window reports being focused and the input as the document.activeElement. The input appears to be focused (it has the blue outline and blinking caret) - but it's not actually focused - or at least that's the only way it will allow me to type into the input.

When I move focus to another window, it correctly almost always detects that it does not have focus. It failed once to report lost focus when I moved focus to Sublime Text)

When I re-focus the window with my mouse, rather than programmatically, only then does it actually become focused.

Additional Information

I tested Mac 10.13 and Linux Mint 19 and the same code works as intended there.

@MarshallOfSound
Copy link
Member

Can you test on the latest nightly? I made some visibility and focus tracking changes a few weeks back

@MarshallOfSound MarshallOfSound added the blocked/need-info ❌ Cannot proceed without more information label Oct 9, 2019
@Nantris
Copy link
Contributor Author

Nantris commented Oct 10, 2019

Thanks for the response @MarshallOfSound. Unfortunately the issue persists in 8.0.0-nightly.20191009

@Nantris
Copy link
Contributor Author

Nantris commented Jan 30, 2020

@MarshallOfSound does #21963 add anything helpful diagnostically?

@sofianguy sofianguy added 6-1-x and removed 6-0-x labels Feb 19, 2020
@MMcKester
Copy link

I still have this issu with the latest public Electron version. blur simply doesn't unfocus the window. isFocused() always returns true

@electron-triage
Copy link

The Electron version reported on this issue is no longer supported. See our supported versions documentation.

If this is still reproducible on a supported version, please open a new issue with any other new information that a maintainer should know.

Thank you for taking the time to report this issue and helping to make Electron better! Your help is appreciated.

@Nantris
Copy link
Contributor Author

Nantris commented Mar 9, 2021

This is still an issue in 11.3.0. It's an edge case, but it's a real issue. Do we really need to open a new issue? @MarshallOfSound

@andy-asana
Copy link

I am also seeing this on MacOS. Here is the code I'm using (pretty simple):

// win is a BrowserWindow
if (win.isFocused()) {
  win.blur();
  console.log(win.isFocused()); // still shows "true"
}

@pushkin-
Copy link

@andy-asana what if you put the isFocused() call on a setTimeout(). I don't know if blurring is a fully synchronous operation.

@Nantris
Copy link
Contributor Author

Nantris commented Mar 22, 2022

@pushkin- it doesn't help from my experience.

@RaisinTen
Copy link
Member

This was also an issue in macOS, so I sent a fix for it in #33734. :)

@Nantris
Copy link
Contributor Author

Nantris commented May 22, 2022

@sofianguy @nornagon @MarshallOfSound

It's been almost 3 years and this issue persists. People continue to comment on other issues that are closed (like #20400,) that I strongly believe are caused by this as the underlying issue. Could we get some attention on this?

@github-actions
Copy link
Contributor

github-actions bot commented Oct 8, 2022

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 Oct 8, 2022
@github-actions github-actions bot removed the stale label Oct 9, 2022
@nornagon
Copy link
Member

Am I missing some repro steps somewhere in this thread...? Would love to take a look but it's going to be challenging without a consistent (or at least semi-consistent) repro case.

@Nantris
Copy link
Contributor Author

Nantris commented Oct 26, 2022

Thanks for the comment @nornagon!

I'll try to create a repro when I can, but my backlog is huge, and it may not be possible to recreate a ready to go repro.

IIRC, the situation where I could 100% consistently reproduce this was clicking a Chrome notification (on Windows 7) when the last focused window before clicking a notification was the Electron app. In that case, Electron always thought it had retained the focus when it had not.

@ghbob
Copy link

ghbob commented Oct 26, 2022

I had the same issue with a current Electron and a current Windows 10.
As far as my tests went the easiest way to repdroduce it is switching to any application that waits for keyboard input (e.g. Notepad), trying to detect the missing focus (win.isFocused()), and trying to refocus it (win.show(), win.focus(), ...). Electron then thinks it's focused while any keyboard input is still happening in Notepad.
Like Slapbox mentioned, Windows also still highlights the other app in the taskbar.

@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 25, 2023
@Nantris
Copy link
Contributor Author

Nantris commented Jan 25, 2023

Still relevant last I checked.

@github-actions github-actions bot removed the stale label Jan 26, 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 27, 2023
@alex-titarenko
Copy link

alex-titarenko commented Apr 27, 2023

bump

@Nantris
Copy link
Contributor Author

Nantris commented Apr 27, 2023

I wonder if people can repro this on Windows 11 22H2? I strongly suspect the answer is yes, but I thought it might not repro since the way taskbar focus is handled seemed to change a bit.

I'm no longer sure how to repro this now that Windows 7 is dead, but I do believe the underlying issue remains.

@github-actions github-actions bot removed the stale label Apr 28, 2023
@proy1121
Copy link

this issue still remains same
Can anyone suggest any solution for it ?

@Nantris
Copy link
Contributor Author

Nantris commented Jul 19, 2023

@proy1121 can you report your OS so we can have an up to date idea of where this still occurs? Perhaps it occurs even on the latest Windows builds but the way I reproed it was particular to Windows 7.

@kcolton
Copy link

kcolton commented Jul 26, 2023

I am getting something like this on windows 11 home 22H2 22621.1992 with Electron 25.3.2 and as simple of a boilerplate as you can get:

https://gist.github.com/kcolton/6bb551200578c1629a83e578f5d9073e

const { app, BrowserWindow, webContents} = require('electron')

const createWindow = () => {
  console.debug('creating window')
  const window = new BrowserWindow({
    width: 800,
    height: 600
  })
  window.loadFile('index.html')

  setInterval(() => {
    console.debug(
      'IS FOCUSED?', window.isFocused(), 'IS VISIBLE:', window.isVisible(),
      'IS ENABLED:', window.isEnabled(), 'FOCUSED WINDOW:', !!BrowserWindow.getFocusedWindow(), 'WEB CONTENTS GET FOCUSED WEBCONTENTS:', !!webContents.getFocusedWebContents()
    )
  }, 2000)
}

app.whenReady().then(() => {
  console.debug('app webReady')
  createWindow()
})

app.on('window-all-closed', () => {
  if (process.platform !== 'darwin') app.quit()
})

Here are my repro steps. They are not 100%, but pretty frequent. (Screenshare: https://jmp.sh/Fa7IWQrV)

  • Run start script
  • Start spamming clicks on a textarea on another program (doesn't matter what) right when electron is starting
  • The window will report that it's focused when it's clearly not

Once I manually focus on the app once it will start picking up it's true focus state accurately again.

Some more observations:

  • Both window.isFocused() and document.hasFocus() report same false positive
  • When the bug is encountered the window will still show on top of the window you had been clicking on. But clicking on other windows will go on top.
  • This happened with earlier versions of electron too. Earliest I tried was 20.x
  • I have not noticed any difference in behavior between various combination of show, paintWhenInitiallyHidden, backgroundThrottling, manually calling .show() etc as long as you are spamming click on another apps textarea at the time that either .show() or .focus() is called

@Nantris
Copy link
Contributor Author

Nantris commented Jul 26, 2023

Great work on this @kcolton!

@electron-issue-triage
Copy link

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!

@alex-titarenko
Copy link

bump

@Nantris
Copy link
Contributor Author

Nantris commented Oct 25, 2023

I can't easily prove this is unresolved, but I very strongly suspect it is not.

@electron-issue-triage
Copy link

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!

@Nantris
Copy link
Contributor Author

Nantris commented Jan 24, 2024

Not stale.

@electron-issue-triage
Copy link

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-
Copy link

bump

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
No open projects
6.1.x
Unsorted Issues
Development

No branches or pull requests