Reputation: 198
let firstWin = new BrowserWindow()
let secondWin = new BrowserWindow()
secondWin.hide()
Let's assume there are three active windows. Two of them belong to my application (firstWin
and secondWin
). The third one is Chrome (or any other application). firstWin
is at the bottom of the hierarchy, Chrome in the middle and secondWin
on top.
secondWin
Chrome
firstWin
When I call secondWin.hide()
, firstWin
automatically pops up and gets focused. The result is then:
firstWin
Chrome
Is there any way to prevent that? I would like firstWin
to not pop up but stay at the bottom of the window hierarchy when I hide secondWin
.
Upvotes: 1
Views: 1173
Reputation: 11
It's been a while since the last answer but here is how I solved this issue:
setting
firstWin.setFocusable(false)
just before calling
secondWin.hide()
will prevent the first window from getting focused.
After that, you can restore the focusable state again calling
firstWin.setFocusable(true)
Upvotes: 1
Reputation: 198
The solution that I went with for now is to set the opacity of the window to zero and deactivate mouse events. That would be
secondWin.setOpacity(0);
secondWin.setIgnoreMouseEvents(true);
instead of
secondWin.hide()
You then reverse the settings whenever you want the window to become visible again. It's a bit hacky but works for now. Let's see if anyone finds a better solution. Ideally there would be an event like window-about-to-be-shown
, which you can plug into and call event.preventDefault()
on.
Upvotes: 2
Reputation: 28913
I believe you should be able to listen to a window's focus
and/or show
events (see https://www.electronjs.org/docs/api/browser-window)
You could then make "a decision" to immediately call hide() on that window. (I suppose there is a chance of it flashing on screen briefly.)
(How to make that decision is a bit tricky; I suppose you could record the time you called secondWin.hide()
and use the number of seconds since then to decide... not ideal!)
As another approach, just before calling secondWin.hide()
you could look at the current state of firstWin
, and if that is minimized or hidden, you could instead call app.hide()
(see https://www.electronjs.org/docs/api/app#apphide-macos).
(Unless you always want all windows hidden when one window is hidden, this isn't going to scale very nicely beyond 2 windows, unfortunately.)
Upvotes: 2