优化electron通信

This commit is contained in:
kuaifan 2022-02-23 14:52:39 +08:00
parent dbd59cd958
commit f72114c223
5 changed files with 56 additions and 36 deletions

View File

@ -1,5 +1,4 @@
const {
shell,
contextBridge,
ipcRenderer
} = require("electron");

View File

@ -14,7 +14,6 @@ const config = require('./package.json');
let mainWindow = null,
subWindow = [],
willQuitApp = false,
inheritClose = false,
devloadUrl = "",
devloadCachePath = path.resolve(__dirname, ".devload"),
downloadList = [],
@ -70,7 +69,7 @@ function createMainWindow() {
center: true,
autoHideMenuBar: true,
webPreferences: {
preload: path.join(__dirname, 'preload.js'),
preload: path.join(__dirname, 'electron-preload.js'),
webSecurity: true,
nodeIntegration: true,
nodeIntegrationInSubFrames: true,
@ -96,18 +95,9 @@ function createMainWindow() {
}
})
mainWindow.on('close', (e) => {
mainWindow.on('close', event => {
if (!willQuitApp) {
e.preventDefault();
if (inheritClose) {
mainWindow.webContents.send("windowClose", {})
} else {
if (process.platform === 'darwin') {
app.hide();
} else {
app.quit();
}
}
utils.onBeforeUnload(event, app)
}
})
@ -154,7 +144,7 @@ function createSubWindow(args) {
parent: mainWindow,
autoHideMenuBar: true,
webPreferences: {
preload: path.join(__dirname, 'preload.js'),
preload: path.join(__dirname, 'electron-preload.js'),
devTools: args.devTools !== false,
webSecurity: true,
nodeIntegration: true,
@ -168,12 +158,18 @@ function createSubWindow(args) {
event.preventDefault()
}
})
browser.on('close', () => {
browser.on('close', event => {
utils.onBeforeUnload(event)
})
browser.on('closed', () => {
let index = subWindow.findIndex(item => item.name == name);
if (index > -1) {
subWindow.splice(index, 1)
}
})
subWindow.push({ name, browser })
}
browser.webContents.setUserAgent(browser.webContents.getUserAgent() + " SubTaskWindow/" + process.platform + "/" + os.arch() + "/1.0" + (args.userAgent ? (" " + args.userAgent) : ""));
@ -209,14 +205,6 @@ app.on('before-quit', () => {
willQuitApp = true
})
/**
* 继承关闭窗口事件
*/
ipcMain.on('inheritClose', (event) => {
inheritClose = true
event.returnValue = "ok"
})
/**
* 下载文件
* @param args {url}
@ -555,7 +543,7 @@ function exportVsdx(event, args, directFinalize) {
height: 800,
show: false,
webPreferences: {
preload: path.join(__dirname, 'preload.js'),
preload: path.join(__dirname, 'electron-preload.js'),
webSecurity: true,
nodeIntegration: true,
contextIsolation: true,
@ -648,7 +636,7 @@ function exportDiagram(event, args, directFinalize) {
try {
browser = new BrowserWindow({
webPreferences: {
preload: path.join(__dirname, 'preload.js'),
preload: path.join(__dirname, 'electron-preload.js'),
backgroundThrottling: false,
contextIsolation: true,
nativeWindowOpen: true

View File

@ -2,7 +2,7 @@
"name": "DooTask",
"version": "0.9.16",
"description": "DooTask is task management system.",
"main": "main.js",
"main": "electron.js",
"license": "MIT",
"scripts": {
"start": "electron-forge start",
@ -51,8 +51,8 @@
"artifactName": "${productName}-v${version}-${os}-${arch}.${ext}",
"files": [
"public/**/*",
"main.js",
"preload.js",
"electron-preload.js",
"electron.js",
"utils.js"
],
"mac": {

37
electron/utils.js vendored
View File

@ -1,5 +1,5 @@
const fs = require("fs");
const {shell} = require("electron");
const {shell, dialog} = require("electron");
module.exports = {
/**
@ -269,4 +269,39 @@ module.exports = {
}
return Math.round(time / 1000)
},
/**
* 窗口关闭事件
* @param event
* @param app
*/
onBeforeUnload(event, app) {
const sender = event.sender
const contents = sender.webContents
if (contents != null) {
const destroy = () => {
if (typeof app === "undefined") {
sender.destroy()
} else {
if (process.platform === 'darwin') {
app.hide()
} else {
app.quit()
}
}
}
contents.executeJavaScript('if(typeof window.__onBeforeUnload === \'function\'){window.__onBeforeUnload()}', true).then(options => {
if (this.isJson(options)) {
let choice = dialog.showMessageBoxSync(sender, options)
if (choice === 1) {
contents.executeJavaScript('if(typeof window.__removeBeforeUnload === \'function\'){window.__removeBeforeUnload()}', true).catch(() => {});
destroy()
}
} else if (options !== true) {
destroy()
}
})
event.preventDefault()
}
},
}

View File

@ -179,17 +179,15 @@ export default {
if (!this.$Electron) {
return;
}
this.$Electron.sendMessage('inheritClose');
this.$Electron.registerMsgListener('windowClose', () => {
window.__onBeforeUnload = () => {
if (this.$Modal.removeLast()) {
return;
return true;
}
if (this.cacheDrawerOverlay.length > 0) {
this.cacheDrawerOverlay[this.cacheDrawerOverlay.length - 1].close();
return;
return true;
}
this.$Electron.sendMessage('windowHidden');
})
}
this.$Electron.registerMsgListener('dispatch', (event, args) => {
if (!$A.isJson(args)) {
return;