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
Windows 10: Window is not put to front on focus() #2867
Comments
I find it interesting you've had this working at all in the past. I've always found it to be relatively inconsistent due to |
Under the hood From #1954 it seems that Windows 10 starts to treat the renderer process of Chromium as foreground process, and thus the browser process becomes a background process, resulting in I can probably fix this by calling P.S. @bpasero Is it possible for you to reach the Windows team to verify my assumption? I know Microsoft is HUGE and it is hard to find the right team, but it can help understand the mysterious behaviors. |
@zcbenz I will try to find a contact, meanwhile if you have a change in mind, I can build electron on my windows 10 to verify the fix. Is it possible that this also causes the issue that I cannot set user tasks anymore to the task bar menu of the window? |
@bpasero Start at the |
@paulcbetts ??? |
@bpasero Open up Outlook and look in the GAL for it :) |
@paulcbetts ah you are talking about an email alias, I was searching electron source code for "win32prg" to fix this myself ;). Yeah I might have some people to ask, might not get back to me until Redmond wakes up though. |
Fixing this Properly actually might be not particularly trivial if you have to do it The Hard Way. Basically:
However, Windows probably has some heuristics around this and there might be a way less involved way to do it (do all processes in a Job share SetForegroundWindow'ability? Maybe!) This is where |
@zcbenz if I understand your previous comment correctly, I should be able to bring a window to the foreground only if the call is made a foreground process. Therefore I understand that to have it working:
As far I am concerned (see #3124), the call is made from the renderer process. If my assumptions are correct, I should get this working using windows 10. I will try it and report the result here. Looking forward to have the feedback from the Windows team :-). |
@antoinepairet is it really possible to make the call from the renderer? My understanding is that the call is just getting proxied, but still executed on the main side. |
@bpasero I do not know the internal details of electron. Here is what I do: In the renderer process, I am loading a DLL using FYI, NW.js also seem to struggle with similar issues: nwjs/nw.js#3387 |
Ah ok that might work, I thought you would go through the official Electron API to focus a window. |
We found a bug in our code. Shame on me. :-s. |
@antoinepairet is your node-ffi code somewhere on GitHub? Just curious... |
@bpasero Sorry for the late response. It's closed source but if you are interested, we can setup a Skype call or Google Hangout for a quick demo and have a look at the code :-). I am on CEST. |
I ran into this today. It's a huge blocker for using BrowserWindow on Windows 10, since if you ever hide a BrowserWindow there is no way to give focus back to it. @zcbenz have you tried calling AllowSetForegroundWindow in the renderer just to see if it works? @bpasero have you made any progress within Microsoft? Would be great to have this fixed. |
After reading #1954 (comment) I have setup the latest Windows insider build and I cannot reproduce the issue anymore. It seems to me that Windows 10 v10565 now behaves again like Windows 8 in terms of Windows and SetForegroundWindow. To verify this assumption I have send a mail on win32prg. |
Ok more insights: There is in fact no difference between the stable Win 10 and the insiders release. However there is a difference with how window.focus() works depending on wether the window is minimized or not. I was likely testing it with a minimized window in the one build and non-minimized in the other. If I call |
Thanks @bpasero |
@zcbenz would it actually be possible to call SetForegroundWindow from the renderer process without going through the main process? win32prg agrees that the workaround in #2867 (comment) is ugly and if you can call SetAllowForegroundWindow from the renderer you might as well just call SetForegroundWindow directly. |
@bpasero We can probably pass the window handle to renderer process and call |
@zcbenz I can look into that and verify the solution if you could send me pointers how to wire that up. |
@bpasero You can references how |
@zcbenz any clue if/how it is possible to get a hold of the renderer window's HWND from the renderer side? |
@bpasero You can send a synchronous message to the main process to get it, an example is the |
It doesn't fix it. My workaround is to have an event triggered in the webapp on notification click and then use this monstrosity to trigger a focus in the electron side: |
Another vote for this. We're facing this issue as well with our Electron app. |
Contrary to a report earlier in this thread, the issue does exist on Windows 8.1. |
Thanks to @robinwassen, https://www.npmjs.com/package/@adeperio/forcefocus this works for me with electron 8 and node 12. |
... for what it's worth, it should probably be in the documentation that BrowserWindow.show() is effectively non-functional for single window applications when running in Windows. BrowserWindow.show() will bring a window to the front only if it shares (X,Y) screenspace with another window from the same application that already has foreground |
This should probably be re-opened as there are definitely issues surrounding focus under Windows. EDIT: Here is the version I use
|
@bpasero You say above:
And later:
This trick seems to work for me. What issues were you running into that made you stop using it? |
@rmuchall 's version seems to work pretty well in Windows at least, better than any other method i've found so far, however, the keyboard focus is not being forced into the window. That might actually be preferable for my application, but it's directly in contradiction of what the code seems to indicate it should do :-) ... though, after using it for a while, there is an odd flicker of the window occasionally that i can't yet explain ... scratch that, it doesn't work 100% reliably when other windows are in front of it, still only works 100% when another window from the same app is already in the fore. |
I can say for sure that messing with the minimized state definitely does weird things with the state of windows that are positioned using Aero-Snap in Windows. |
Hi @ericblade , |
@rmuchall yeah, i can pretty consistently defeat it coming foreground if I have Visual Studio Code or Vivaldi maximized. It does work better/more consistently than most of the other solutions that don't involve native plugins, though. The flashing seems to be some other window in the application coming forward briefly before the correct one does. It may have to do with that my application has many windows... which is part of why i want to bring forward specific ones.. i'm presently using a native node module called 'forcefocus', but it has pretty awful drawbacks, too.. namely it screws up keyboard input. |
@ericblade |
From a user's perspective, this is terrible. Please consider fixing this issue if the behavior is caused by this issue. |
@ciriousjoker that doesn't sound related unfortunately, the TLDR on this thread is -- there's not really anything to fix. Windows (the product)' window management features don't explicitly allow this to occur, and pretty much every way around it is garbage, so the best thing to do if you need to foreground windows explicitly, over the top of other applications, is to determine which workaround you can most live with the drawbacks of. |
that's so fanny, 2021 now, still has the problem, and your method still works |
I discovered that if you first minimize and then restore the
|
Take a look into BrowserWindow.moveTop(). Brings the window above all the other windows. Did the work for me. |
2022,
For some reason the |
@darklightblue, your example of |
The workaround of calling
Instead, using
It's worth noting that we observed this issue only happens when we construct the Not sure why this issue is unresolved after 8 years and this thread is closed. At a minimum, the Electron docs should be updated. |
After having to fight with this issue for most of the day yesterday none of the solutions here were acceptable. I ended up adopting a very hacky but functional workaround. I used RobotJS and tried several keyboard/mouse input combinations including "alt" and "shift", but the most stable, smooth and least harmful for the user ended up being a simple mouse click release.
Combined with electron-rebuild, this offered a smooth alternative to Windows' stubbornness and I hope this can help someone out. |
This commit updates the application startup behavior to prevent showing a blank window until it's fully loaded on all platforms. This enhancement improves the user experience by ensuring the UI only becomes visible when it is ready to interact with. This fix contributes to a smoother user experience by aligning the window display timing with content readiness, thus avoiding the brief display of an empty screen. Changes: - Set window to initially hide until fully loaded using the `ready-to-show` event. - Show the window, focus on it and bring it front once it is loaded. Windows requires additional logic to put Window to front, see electron/electron#2867. - Parametrize the behavior of opening developer tools for easier configuration during testing.
I am hitting an issue where a window (in Windows 10) is not being put to the front when I call focus() on it. The same code brings the window to the front on Windows 8 and below, so something must have changed in Windows 10 with regards to window management.
What I see happening is that the task bar is flashing in orange, so there is some indication. Just the window does not get focussed properly.
The text was updated successfully, but these errors were encountered: