nwjs/electron

electron创建单实例应用

2018-08-31  本文已影响0人  你的名字_85b0

const {app, BrowserWindow} = require('electron')

const electron = require('electron')

const path = require('path');

// Keep a global reference of the window object, if you don't, the window will

// be closed automatically when the JavaScript object is garbage collected.

let mainWindow = null

const Menu = electron.Menu

const Tray = electron.Tray

var appTray = null;

function createWindow () {

  Menu.setApplicationMenu(null)

  // Create the browser window.

  mainWindow = new BrowserWindow({width: 936, height: 526 ,

        resizable: false,})//resizable,设置窗口是否可伸缩

  // and load the index.html of the app.

  mainWindow.loadFile('zhushou/index.html')//加载HTML页面

  mainWindow.setClosable(false)//设置窗口关闭按钮是否可用

  mainWindow.flashFrame(true)//设置窗口闪烁

  mainWindow.setSkipTaskbar(true)//设置任务栏是否显示

  // Open the DevTools.

  mainWindow.webContents.openDevTools()//增加开发者工具

  //系统托盘图标目录

  trayIcon = path.join(__dirname, 'res');//res是选取的目录

  appTray = new Tray(path.join(trayIcon, 'favicon.ico'));//res.ico是res目录下的ico文件

  //设置此托盘图标的悬停提示内容

  appTray.setToolTip('这里是提示信息');

  //单击右下角小图标显示应用

  appTray.on('click',function(){

    mainWindow.show();

  })

}

app.on('ready', createWindow)

// Quit when all windows are closed.

app.on('window-all-closed', function () {

  if (process.platform !== 'darwin') {

    app.quit()

  }

})

app.on('activate', function () {

  // On OS X it's common to re-create a window in the app when the

  // dock icon is clicked and there are no other windows open.

  if (mainWindow === null) {

    createWindow()

  }

  //Electron单实例

const shouldQuit = app.makeSingleInstance(

  (commandLine, workingDirectory) => {

      if (mainWindow) {

          if (mainWindow.isMinimized()){

              mainWindow.restore();

          };

      mainWindow.focus();

  };

  });

  if (shouldQuit) {

    app.quit();

    return;//没有这句话,会报错!

  };

})

上一篇 下一篇

猜你喜欢

热点阅读