'use strict'
|
|
import { app, protocol, BrowserWindow, ipcMain, ipcRenderer, dialog, shell, Menu, webContents } from 'electron'
|
import { autoUpdater } from 'electron-updater';
|
import { createProtocol } from 'vue-cli-plugin-electron-builder/lib'
|
// import installExtension, { VUEJS_DEVTOOLS } from 'electron-devtools-installer'
|
import path from 'path';
|
import child_process from 'child_process';
|
import log from 'electron-log';
|
import update from './update';
|
|
autoUpdater.logger = log;
|
autoUpdater.logger.transports.file.level = 'info';
|
|
let win = null;
|
|
|
|
// 调试自动升级
|
/**
|
* checkForUpdatesAndNotify() just won't work in development mode.
|
*
|
* If you insist on test it in dev mode, you can do some hack with isPackaged:
|
*
|
* Be careful, do not use this hack for production
|
*
|
* Object.defineProperty(app, 'isPackaged', {
|
get() {
|
return true;
|
}
|
});
|
*
|
*
|
*/
|
|
// 单例锁
|
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()
|
if (commandLine[2]) {
|
win.webContents.send('selected-file', { filePaths: [commandLine[2]] }, 'MenuList');
|
}
|
}
|
})
|
|
}
|
|
|
const remote = require('@electron/remote/main');
|
remote.initialize();
|
// remote.enable()
|
|
const isDevelopment = process.env.NODE_ENV !== 'production'
|
// const path = require('path');
|
|
// Scheme must be registered before the app is ready
|
protocol.registerSchemesAsPrivileged([
|
{ scheme: 'app', privileges: { secure: true, standard: true, stream: true } }
|
])
|
|
if (!isDevelopment) {
|
Menu.setApplicationMenu(null);
|
}
|
async function createWindow(cb) {
|
// Create the browser window.
|
win = new BrowserWindow({
|
width: 1000,
|
height: 600,
|
webPreferences: {
|
// 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
|
nodeIntegration: false,
|
webSecurity: true,
|
allowEval: false,
|
allowRunningInsecureContent: false,
|
contextIsolation: true,
|
enableRemoteModule: false,
|
preload: path.join(__dirname, 'preload.js')
|
}
|
})
|
|
// 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')
|
}
|
if (cb && 'function' == typeof cb) {
|
win.once('ready-to-show', () => {
|
cb(win.webContents);
|
})
|
}
|
|
win.webContents.session.on('will-download', (e, item) => {
|
// const filePath = path.join(saveUrl, item.getFilename());
|
// item.setSavePath(filePath); // 'C:\Users\kim\Downloads\第12次.zip'
|
// let value;
|
// //监听下载过程,计算并设置进度条进度
|
// item.on('updated', (evt, state) => {
|
// if ('progressing' === state) {
|
// //此处 用接收到的字节数和总字节数求一个比例 就是进度百分比
|
// if (item.getReceivedBytes() && item.getTotalBytes()) {
|
// value = parseInt(
|
// 100 * (
|
// item.getReceivedBytes() / item.getTotalBytes()
|
// )
|
// )
|
// }
|
// // 把百分比发给渲染进程进行展示
|
// // win.webContents.send('updateProgressing', value);
|
// // mac 程序坞、windows 任务栏显示进度
|
// win.setProgressBar(value);
|
// }
|
// });
|
//监听下载结束事件
|
item.on('done', (e, state) => {
|
//如果窗口还在的话,去掉进度条
|
// if (!win.isDestroyed()) {
|
// win.setProgressBar(-1);
|
// }
|
//下载被取消或中断了
|
if (state === 'interrupted') {
|
dialog.showErrorBox('下载失败', `文件 ${item.getFilename()} 因为某些原因被中断下载`);
|
}
|
// 下载成功后打开文件所在文件夹
|
if (state === 'completed') {
|
setTimeout(() => {
|
shell.showItemInFolder(item.getSavePath())
|
}, 1000);
|
}
|
});
|
});
|
}
|
|
// Quit when all windows are closed.
|
app.on('window-all-closed', () => {
|
// On macOS it is common for applications and their menu bar
|
// to stay active until the user quits explicitly with Cmd + Q
|
if (process.platform !== 'darwin') {
|
app.quit()
|
}
|
})
|
|
app.on('activate', () => {
|
// On macOS 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 (BrowserWindow.getAllWindows().length === 0) createWindow()
|
})
|
|
// This method will be called when Electron has finished
|
// initialization and is ready to create browser windows.
|
// Some APIs can only be used after this event occurs.
|
app.on('ready', async () => {
|
let cb = undefined;
|
if (process.argv[1]) {
|
cb = (sender) => {
|
sender.send('selected-file', { filePaths: [process.argv[1]] }, 'MenuList');
|
}
|
}
|
createWindow(cb);
|
})
|
|
// 所有的新开窗口事件都调用系统默认浏览器来打开页面
|
app.on('web-contents-created', (e, webContents) => {
|
webContents.on('new-window', (event, url) => {
|
event.preventDefault();
|
shell.openExternal(url);
|
})
|
});
|
|
ipcMain.on('open-file-dialog', (event, data) => {
|
dialog.showOpenDialog({
|
filters: [{ name: 'xml', extensions: ['xml'] }],
|
properties: ['openFile']
|
}).then((files) => {
|
// console.log(files, '0000000')
|
if (files) {
|
event.sender.send('selected-file', files, data);
|
}
|
})
|
});
|
ipcMain.on('open-directory-dialog', (event, data) => {
|
dialog.showOpenDialog({
|
properties: ['openDirectory']
|
}).then((files) => {
|
if (files) {
|
event.sender.send('selected-directory', files, data);
|
}
|
})
|
});
|
|
ipcMain.on('check-update', (event) => {
|
// checkForUpdates();
|
update(event.sender);
|
});
|
|
ipcMain.on('quitAndInstall', () => {
|
autoUpdater.quitAndInstall();
|
});
|
|
|
// Exit cleanly on request from parent process in development mode.
|
if (isDevelopment) { false;
|
if (process.platform === 'win32') {
|
process.on('message', (data) => {
|
if (data === 'graceful-exit') {
|
app.quit()
|
}
|
})
|
} else {
|
process.on('SIGTERM', () => {
|
app.quit()
|
})
|
}
|
}
|