user17267053
user17267053

Reputation:

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?

Upvotes: 4

Views: 2723

Answers (2)

Predator13
Predator13

Reputation: 271

Electron has property of app.focus(). But make sure you use it with options like:-

app.focus({
   steal: true
});

Upvotes: 0

pushkin
pushkin

Reputation: 10199

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();

Upvotes: 12

Related Questions