Home > other >  why win.focus() doesn't work? (electron js)
why win.focus() doesn't work? (electron js)

Time:02-01

In code:

function focusOnMainWindow(): void {
  win.focus();
}

In practice: electron

I want my electron application to appear above other programs. But only the yellow glow from him works! Why?

CodePudding user response:

win.focus() isn't necessarily designed to bring the window to the front (see this github issue).

If you want the window to be brought to the front, you'll have to get more creative. The function I have in my app is fairly complex to handle all sorts of edge cases, but maybe something like this will get you started:

// maybe you want to handle this case, maybe not
if (win.isMinimized())
    win.restore();

win.setAlwaysOnTop(true);
app.focus();
win.setAlwaysOnTop(false);

The idea is adapted from here. Note in their case, they're doing:

win.setAlwaysOnTop(true);
win.show();
win.setAlwaysOnTop(false);
app.focus();
  •  Tags:  
  • Related