Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why does win.focus() not bring the window to the front?

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?


1 Answers

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();
like image 66
pushkin Avatar answered Sep 03 '25 14:09

pushkin