electron app should run as a background process in windows after startup

huangapple go评论55阅读模式
英文:

electron app should run as a background process in windows after startup

问题

以下是您要翻译的内容:

"openAsHidden attribute of app.setLoginItemSettings work only with MAC. Then how does electron built apps like Slack startup as hidden and only shows up in the system tray on windows? Which method do they use?

Is there a way with which I can determine if the app is opened by system startup or by the user, so I can use the window.hide() and window.show() accordingly with dom-ready event.

I have tried following way to achieve it:

app.on("ready", async () => {
  app.setLoginItemSettings({
    openAtLogin: true,
    openAsHidden: true,
    path: exeName,
    args: [
      "--processStart",
      `"${exeName}"`,
      "--process-start-args",
      `"--hidden"`
    ]
  });
})
```"

<details>
<summary>英文:</summary>

openAsHidden attribute of app.setLoginItemSettings work only with MAC. Then how does electron built apps like Slack startup as hidden and only shows up in the system tray on windows? Which method do they use?

Is there a way with which I can determine if the app is opened by system startup or by the user, so I can use the window.hide() and window.show() accordingly with dom-ready event.

I have tried following way to achieve it:

```javascript
app.on(&quot;ready&quot;, async () =&gt; {
  app.setLoginItemSettings({
    openAtLogin: true,
    openAsHidden: true,
    path: exeName,
    args: [
      &quot;--processStart&quot;,
      `&quot;${exeName}&quot;`,
      &quot;--process-start-args&quot;,
      `&quot;--hidden&quot;`
    ]
  });
})

答案1

得分: 0

这个检查有效:

var isLaunchedOnStartup = process.argv.indexOf("--hidden") !== -1;

app.ready内部:

if (isLaunchedOnStartup === true) {
  createWindow();
  mainWindow?.hide();
} else {
  createWindow();
  mainWindow?.show();
}

默认情况下,BrowserWindow应该是隐藏的:

mainWindow = new BrowserWindow({show: false});
英文:

This check works:

var isLaunchedOnStartup = process.argv.indexOf(&quot;--hidden&quot;) !== -1;

and inside app.ready

if (isLaunchedOnStartup === true) {
  createWindow();
  mainWindow?.hide();
} else {
  createWindow();
  mainWindow?.show();
}

and by default BrowserWindow should be hidden:

mainWindow = new BrowserWindow({show: false});

huangapple
  • 本文由 发表于 2023年5月17日 15:18:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/76269454.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定