主进程功能

组件引入

import { BrowserWindow, ipcMain, Menu, Tray, shell, Notification } from 'electron'

窗口

new BrowserWindow({
    width: 800,
    height: 600,
    // autoHideMenuBar: true,
    webPreferences: {
      nodeIntegration: true
    },
    frame: false
  })

frame 为是否开启浏览器边框、

进程通信

ipcRenderer.send('mainWindow:minimize')
ipcMain.on('mainWindow:minimize', () => {
  mainWindow.minimize()
})

托盘

  appTray = new Tray(iconPath)
  // 图标的上上下文
  contextMenu = Menu.buildFromTemplate(trayMenuTemplate)
  // 设置此托盘图标的悬停提示内容
  appTray.setToolTip('加菲猫 - for freedom and fun')
  // 设置此图标的上下文菜单
  appTray.setContextMenu(contextMenu)

其他

控制应用单例

应用多次打开不重新创建应用实例,只切换窗口焦点

// 单一实例
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()
    }
  })

安装包更新方案

const { autoUpdater } =require("electron-updater");
//执行自动更新检查
const feedUrl = `http://localhost:2060/zip`; // 更新包位置
  autoUpdater.setFeedURL(feedUrl);
  let message = {
    error: '检查更新出错',
    checking: '正在检查更新……',
    updateAva: '检测到新版本,正在下载……',
    updateNotAva: '现在使用的就是最新版本,不用更新',
  };
  autoUpdater.on('error', function (error) {
    sendUpdateMessage(message.error)
  });
  autoUpdater.on('checking-for-update', function () {
    sendUpdateMessage(message.checking)
  });
  autoUpdater.on('update-available', function (info) {
    sendUpdateMessage(message.updateAva)

  });
  autoUpdater.on('update-not-available', function (info) {
    sendUpdateMessage(message.updateNotAva)
  });

// 更新下载进度事件
autoUpdater.on('download-progress', function (progressObj) {
  console.log(progressObj)
 //与渲染进程通信
  mainWindow.webContents.send('downloadProgress', progressObj)
  mainWindow.setProgressBar(progressObj.percent / 100);
})
autoUpdater.on('update-downloaded', function (event, releaseNotes, releaseName, releaseDate, updateUrl, quitAndUpdate) {
  console.log('更新完成')
  ipcMain.on('isUpdateNow', (e, arg) => {
    console.log("开始更新");
    //some code here to handle event
    autoUpdater.quitAndInstall();
  });

  mainWindow.webContents.send('isUpdateNow')
});

ipcMain.on("checkForUpdate",()=>{
  //放外面的话启动客户端执行自动更新检查
  autoUpdater.checkForUpdates();
})
function sendUpdateMessage(text) {
  mainWindow.webContents.send('message', text)
}

// Renderer
//监听自动更新事件
const { ipcRenderer } = require("electron");

var update = document.getElementById('update');
update.addEventListener('click',function(){
    //发送请求执行自动更新
    ipcRenderer.send("checkForUpdate");
})

 ipcRenderer.on("message", (event, text) => {
        console.log(event)
        console.log(text)
        document.getElementById('mode').innerHTML=text;
});
ipcRenderer.on("downloadProgress", (event, progressObj)=> {
    console.log(progressObj); //下载进度
});
ipcRenderer.on("isUpdateNow", () => {
    ipcRenderer.send("isUpdateNow");
});

window.onunload=function(){
    ipcRenderer.removeAll(["message", "downloadProgress", "isUpdateNow"])
}
上次更新: 12/13/2019, 11:49:51 AM