electron-vue配置文件mian=>index.js

环境:windowsx64,node 14x版本electron 12x版本。

import { 
  app,
  BrowserWindow,
  Tray,
  Menu
} from 'electron'

/**
 * Set `__static` path to static files in production
 * https://simulatedgreg.gitbooks.io/electron-vue/content/en/using-static-assets.html
 */
//解决10.X版本跨域不成功问题(上线删除)
app.commandLine.appendSwitch('disable-features', 'OutOfBlinkCors');

if (process.env.NODE_ENV !== 'development') {
  global.__static = require('path').join(__dirname, '/static').replace(/\\/g, '\\\\')
}
//托盘对象
var appTray = null;

let mainWindow
const winURL = process.env.NODE_ENV === 'development'
  ? `http://localhost:9080`
  : `file://${__dirname}/index.html`

function createWindow () {
  /**
   * Initial window options
   */
  mainWindow = new BrowserWindow({
    height: 750,
    width: 1200,
    center: true, // 是否出现在屏幕居中的位置
    useContentSize: true,
    frame:true,//设置为 false 时可以创建一个无边框窗口
    resizable:true,//窗口是否可以改变尺寸
    autoHideMenuBar:true,//是否隐藏菜单栏
    backgroundColor:'#fff',// 窗口的背景颜色为十六进制值
    titleBarStyle:'hidden',//窗口标题栏的样式
    webPreferences: {
      nodeIntegration: true,//是否启用节点集成
      nodeIntegrationInWorker:true,//是否在Web工作器中启用了Node集成
      contextIsolation: false,//electron为12x版本新增此行
      devTools:true,//是否开启 DevTools
      // webSecurity: true//是否禁用同源策略(上线删除)
    }
  })

  mainWindow.loadURL(winURL)
  // mainWindow.webContents.openDevTools();//打包后可打开调试窗口

  mainWindow.on('closed', () => {
    mainWindow = null
  })

  //windows系统托盘图标
  if(process.platform === 'win32'){
    //设置托盘图标和菜单
    var trayMenuTemplate = [
      {
        label: '打开',
        click: () => {
          mainWindow.show();
          mainWindow.webContents.send('changeWin',1);
        }
      },
      {
        label: '退出',
        click: () => {
          app.quit();
          app.quit();//因为程序设定关闭为最小化,所以调用两次关闭,防止最大化时一次不能关闭的情况
        }
      }
    ];
    appTray = process.env.NODE_ENV === 'development'?new Tray('build/icons/icon.ico'):new Tray(`${__dirname}/static/images/icon.ico`);
    //图标的上下文菜单
    const contextMenu = Menu.buildFromTemplate(trayMenuTemplate);
    //设置此托盘图标的悬停提示内容
    appTray.setToolTip('客户端文件');
    //设置此图标的上下文菜单
    appTray.setContextMenu(contextMenu);
    //单击右下角小图标显示应用左键
    appTray.on('click',function(){
      mainWindow.show();
      mainWindow.webContents.send('changeWin',1);
    });
    //右键
    appTray.on('right-click', () => {
      appTray.popUpContextMenu(trayMenuTemplate);
    });
  };
};

app.on('ready', createWindow)

app.on('window-all-closed', () => {
  if (process.platform !== 'darwin') {
    app.quit()
  }
})

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

//限制只能开启一个应用
const gotTheLock = app.requestSingleInstanceLock();
if (!gotTheLock) {
 app.quit()
} else {
 app.on('second-instance', (event, commandLine, workingDirectory) => {
  // 当运行第二个实例时,将会聚焦到mainWindow这个窗口
  if (mainWindow) {
    if (mainWindow.isMinimized()){
      mainWindow.restore()
      mainWindow.focus()
      mainWindow.show()
    }
  }
 })
};

/**
 * Auto Updater
 *
 * Uncomment the following code below and install `electron-updater` to
 * support auto updating. Code Signing with a valid certificate is required.
 * https://simulatedgreg.gitbooks.io/electron-vue/content/en/using-electron-builder.html#auto-updating
 */

/*
import { autoUpdater } from 'electron-updater'

autoUpdater.on('update-downloaded', () => {
  autoUpdater.quitAndInstall()
})

app.on('ready', () => {
  if (process.env.NODE_ENV === 'production') autoUpdater.checkForUpdates()
})
 */
复制代码
© 版权声明
THE END
喜欢就支持一下吧
点赞0 分享