美文网首页electron
electron 4.0打包成.exe后限制只启动一个应用

electron 4.0打包成.exe后限制只启动一个应用

作者: 扶得一人醉如苏沐晨 | 来源:发表于2023-02-06 09:49 被阅读0次

但是现在已经是electron4.0时代了,原来的老方法app.makeSingleInstance已经不存在了(一般来说应该做到向下兼容的,但是这个方法真的没有了),现在是使用app.requestSingleInstanceLock()方法来实现单实例的:
这里的win就是我们用new BrowserWindow()构造函数创建的全局win对象
app就是从electron模块引进来的

// electron 4.0打包成.exe后限制只启动一个应用
const gotTheLock = app.requestSingleInstanceLock()
if (!gotTheLock) {
  app.quit()
} else {
  app.on('second-instance', (event, commandLine, workingDirectory) => {
    // 当运行第二个实例时,将会聚焦到myWindow这个窗口
    if (win) {
      if (win.isMinimized()) win.restore()
      win.focus()
    }
  })
}

如果你真的是老项目,哈哈,那就用这个方法吧

const shouldQuit = app.makeSingleInstance((commandLine, workingDirectory) => {
  if (win) {
    if (win.isMinimized()) win.restore()
    win.focus()
    win.show()
  }
})
if (shouldQuit) {
  app.quit()
}

完整代码

"use strict";
import { app, protocol, BrowserWindow } from "electron";
import { createProtocol } from "vue-cli-plugin-electron-builder/lib";
// ************* 系统托盘***********************
import { Menu, Tray } from 'electron'
// 注意这里是全局定义这个隐藏到系统托盘变量,不然下面 new Tray 会报错
let tray = null
// 运行根路径
const rootSrc = process.cwd();
// 1.引入remote模块
const remote = require("@electron/remote/main");
remote.initialize();
// Scheme must be registered before the app is ready
protocol.registerSchemesAsPrivileged([
  { scheme: "app", privileges: { secure: true, standard: true } },
]);

let win

async function createWindow () {
  const filePath = rootSrc + "\\resources\\bin\\favicon.png";
  // Create the browser window.
  // 使用我们全局定义的的win,因为后面方法外用会用到
  win = new BrowserWindow({
    // 初始窗口
    width: 1200,
    height: 900,
    // 全屏
    // fullscreen: true,
    autoHideMenuBar: true, //隐藏菜单
    icon: filePath,
    webPreferences: {
      nodeIntegration: true, // 使渲染进程拥有node环境
      //关闭web权限检查,允许跨域
      webSecurity: false,
      // Use pluginOptions.nodeIntegration, leave this alone
      // See nklayman.github.io/vue-cli-plugin-electron-builder/guide/security.html#node-integration for more info
      nodeIntegration: process.env.ELECTRON_NODE_INTEGRATION,
      contextIsolation: !process.env.ELECTRON_NODE_INTEGRATION,
    },
  });
  // 2.启用remote模块,渲染进程就可以使用主程序模块
  remote.enable(win.webContents);
  if (process.env.WEBPACK_DEV_SERVER_URL) {
    // Load the url of the dev server if in development mode
    await win.loadURL(process.env.WEBPACK_DEV_SERVER_URL);
    if (!process.env.IS_TEST) win.webContents.openDevTools();
  } else {
    createProtocol("app");
    // Load the index.html when not in development
    win.loadURL("app://./index.html");
  }
  // ************* 系统托盘*********************** 
  // 当我们点击关闭时触发close事件,我们按照之前的思路在关闭时,隐藏窗口,隐藏任务栏窗口
  // event.preventDefault(); 禁止关闭行为(非常必要,因为我们并不是想要关闭窗口,所以需要禁止默认行为)
  win.on('close', (event) => {
    win.hide();
    win.setSkipTaskbar(true);
    event.preventDefault();
  });
  //创建系统通知区菜单
  // 这里filePath是系统托盘的图标路径
  // filePath生产环境和开发环境的路径并不一样
  // 所以很多小伙伴发现本地跑的的时候系统托盘创建没问题,但是打包exe安装之后,托盘就创建失败了
  tray = new Tray(filePath)
  const contextMenu = Menu.buildFromTemplate([
    { label: '退出', click: () => { win.destroy() } },//我们需要在这里有一个真正的退出(这里直接强制退出)
  ])
  tray.setToolTip('设备互联终端')
  tray.setContextMenu(contextMenu)
  tray.on('click', () => { //我们这里模拟桌面程序点击通知区图标实现打开关闭应用的功能
    win.isVisible() ? win.hide() : win.show()
    win.isVisible() ? win.setSkipTaskbar(false) : win.setSkipTaskbar(true);
  })
}
// electron 4.0打包成.exe后限制只启动一个应用 77~88
const gotTheLock = app.requestSingleInstanceLock()
if (!gotTheLock) {
  app.quit()
} else {
  app.on('second-instance', (event, commandLine, workingDirectory) => {
    // 当运行第二个实例时,将会聚焦到myWindow这个窗口
    if (win) {
      if (win.isMinimized()) win.restore()
      win.focus()
    }
  })
}
app.on("ready", async () => {
  createWindow();
});
app.on('window-all-closed', () => {
  if (process.platform !== 'darwin') {
    app.quit();
  }
});

app.on('activate', () => {
  if (win === null) {
    createWindow();
  }
});

相关文章

网友评论

    本文标题:electron 4.0打包成.exe后限制只启动一个应用

    本文链接:https://www.haomeiwen.com/subject/zuqtkdtx.html