electron 电子,如何从托盘打开应用程序,而不是开始一个新的?

ecr0jaav  于 2023-09-28  发布在  Electron
关注(0)|答案(2)|浏览(183)

如何创建Electron应用程序的单个示例?如果它已经在托盘中运行,用户再次启动它,如何从托盘中打开正在运行的应用程序,而不是启动一个新的?

gopyfrb3

gopyfrb31#

谢谢!我在docs中找到了这个,https://electronjs.org/docs/api/app#apprequestsingleinstancelock:

const { app } = require('electron')
let myWindow = null

const gotTheLock = app.requestSingleInstanceLock()

if (!gotTheLock) {
  app.quit()
} else {
  app.on('second-instance', (event, commandLine, workingDirectory) => {
    // Someone tried to start a second instance, focus our window.
    if (myWindow) {
      if (myWindow.isMinimized()) myWindow.restore()
      myWindow.focus()
    }
  })

  // Create myWindow, load the rest of the application, etc.
  app.on('ready', () => {
  })
}
bd1hkmkf

bd1hkmkf2#

使用app.makeSingleInstance(),确保用户不会打开多个electron示例。一旦你分享了你的代码,我会做一个编辑来告诉你如何正确地实现它。

var shouldQuit = app.makeSingleInstance(function(commandLine, workingDirectory) {
  // Someone tried to run a second instance, we should focus our window.
  if (myWindow) {
    if (myWindow.isMinimized()) myWindow.restore();
    myWindow.focus();
  }
});

相关问题