为什么 win.focus() 不把 window 放在前面?

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

在代码中:

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

实践中:

我希望我的电子应用程序显示在其他程序之上。但只有他身上的黄色光芒起作用!为什么?

win.focus() isn't necessarily designed to bring the window to the front (see thisgithub问题)。

如果你想让 window 出现在前面,你就必须发挥更多的创造力。我的应用程序中的功能相当复杂,无法处理各种边缘情况,但也许像这样的东西会让你入门:

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

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

这个想法改编自here。请注意,在他们的案例中,他们正在做:

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